1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 package com.sun.star.wizards.letter;
24 
25 import com.sun.star.wizards.common.*;
26 import com.sun.star.wizards.text.*;
27 import com.sun.star.frame.XDesktop;
28 import com.sun.star.frame.XTerminateListener;
29 import com.sun.star.table.BorderLine;
30 import com.sun.star.text.*;
31 import com.sun.star.lang.XMultiServiceFactory;
32 import com.sun.star.awt.XWindowPeer;
33 import com.sun.star.uno.Exception;
34 import com.sun.star.uno.UnoRuntime;
35 import com.sun.star.drawing.XShape;
36 import com.sun.star.beans.XPropertySet;
37 import com.sun.star.style.NumberingType;
38 import com.sun.star.style.ParagraphAdjust;
39 import com.sun.star.style.XStyleFamiliesSupplier;
40 import com.sun.star.style.XStyle;
41 
42 public class LetterDocument extends TextDocument
43 {
44 
45     XDesktop xDesktop;
46     boolean keepLogoFrame = true;
47     boolean keepBendMarksFrame = true;
48     boolean keepLetterSignsFrame = true;
49     boolean keepSenderAddressRepeatedFrame = true;
50     boolean keepAddressFrame = true;
51 
LetterDocument(XMultiServiceFactory xMSF, XTerminateListener listener)52     public LetterDocument(XMultiServiceFactory xMSF, XTerminateListener listener)
53     {
54         super(xMSF, listener, "WIZARD_LIVE_PREVIEW");
55     }
56 
getWindowPeer()57     public XWindowPeer getWindowPeer()
58     {
59         return UnoRuntime.queryInterface(XWindowPeer.class, xTextDocument);
60 }
61 
switchElement(String sElement, boolean bState)62     public void switchElement(String sElement, boolean bState)
63     {
64         try
65         {
66             TextSectionHandler mySectionHandler = new TextSectionHandler(xMSF, xTextDocument);
67             Object oSection = mySectionHandler.xTextSectionsSupplier.getTextSections().getByName(sElement);
68             Helper.setUnoPropertyValue(oSection, "IsVisible", Boolean.valueOf(bState));
69 
70         }
71         catch (Exception exception)
72         {
73             exception.printStackTrace(System.out);
74         }
75     }
76 
updateDateFields()77     public void updateDateFields()
78     {
79         TextFieldHandler FH = new TextFieldHandler(xMSFDoc, xTextDocument);
80         FH.updateDateFields();
81     }
82 
switchFooter(String sPageStyle, boolean bState, boolean bPageNumber, String sText)83     public void switchFooter(String sPageStyle, boolean bState, boolean bPageNumber, String sText)
84     {
85         if (xTextDocument != null)
86         {
87             try
88             {
89                 xTextDocument.lockControllers();
90                 XStyleFamiliesSupplier xStyleFamiliesSupplier = UnoRuntime.queryInterface(XStyleFamiliesSupplier.class, xTextDocument);
91                 com.sun.star.container.XNameAccess xNameAccess = null;
92                 xNameAccess = xStyleFamiliesSupplier.getStyleFamilies();
93 
94                 com.sun.star.container.XNameContainer xPageStyleCollection = null;
95                 xPageStyleCollection = UnoRuntime.queryInterface(com.sun.star.container.XNameContainer.class, xNameAccess.getByName("PageStyles"));
96 
97                 XText xFooterText;
98                 XStyle xPageStyle = UnoRuntime.queryInterface(XStyle.class, xPageStyleCollection.getByName(sPageStyle));
99 
100                 if (bState)
101                 {
102                     Helper.setUnoPropertyValue(xPageStyle, "FooterIsOn", Boolean.TRUE);
103                     xFooterText = UnoRuntime.queryInterface(XText.class, Helper.getUnoPropertyValue(xPageStyle, "FooterText"));
104                     xFooterText.setString(sText);
105                     if (bPageNumber)
106                     {
107                         //Adding the Page Number
108                         XTextCursor myCursor = xFooterText.createTextCursor();
109                         myCursor.gotoEnd(false);
110                         xFooterText.insertControlCharacter(myCursor, ControlCharacter.PARAGRAPH_BREAK, false);
111                         XPropertySet xCursorPSet = UnoRuntime.queryInterface(XPropertySet.class, myCursor);
112                         xCursorPSet.setPropertyValue("ParaAdjust", ParagraphAdjust.CENTER);
113                         XTextField xPageNumberField = UnoRuntime.queryInterface(XTextField.class, xMSFDoc.createInstance("com.sun.star.text.TextField.PageNumber"));
114                         XPropertySet xPSet = UnoRuntime.queryInterface(XPropertySet.class, xPageNumberField);
115                         xPSet.setPropertyValue("SubType", PageNumberType.CURRENT);
116                         xPSet.setPropertyValue("NumberingType", new Short(NumberingType.ARABIC));
117                         xFooterText.insertTextContent(xFooterText.getEnd(), xPageNumberField, false);
118                     }
119                 }
120                 else
121                 {
122                     Helper.setUnoPropertyValue(xPageStyle, "FooterIsOn", Boolean.FALSE);
123                 }
124                 xTextDocument.unlockControllers();
125             }
126             catch (Exception exception)
127             {
128                 exception.printStackTrace(System.out);
129             }
130         }
131     }
132 
hasElement(String sElement)133     public boolean hasElement(String sElement)
134     {
135         if (xTextDocument != null)
136         {
137             TextSectionHandler SH = new TextSectionHandler(xMSF, xTextDocument);
138             return SH.hasTextSectionByName(sElement);
139         }
140         else
141         {
142             return false;
143         }
144     }
145 
switchUserField(String sFieldName, String sNewContent, boolean bState)146     public void switchUserField(String sFieldName, String sNewContent, boolean bState)
147     {
148         TextFieldHandler myFieldHandler = new TextFieldHandler(xMSF, xTextDocument);
149         if (bState)
150         {
151             myFieldHandler.changeUserFieldContent(sFieldName, sNewContent);
152         }
153         else
154         {
155             myFieldHandler.changeUserFieldContent(sFieldName, PropertyNames.EMPTY_STRING);
156         }
157     }
158 
fillSenderWithUserData()159     public void fillSenderWithUserData()
160     {
161         try
162         {
163             TextFieldHandler myFieldHandler = new TextFieldHandler(xMSFDoc, xTextDocument);
164             Object oUserDataAccess = Configuration.getConfigurationRoot(xMSF, "org.openoffice.UserProfile/Data", false);
165             myFieldHandler.changeUserFieldContent("Company", (String) Helper.getUnoObjectbyName(oUserDataAccess, "o"));
166             myFieldHandler.changeUserFieldContent("Street", (String) Helper.getUnoObjectbyName(oUserDataAccess, "street"));
167             myFieldHandler.changeUserFieldContent("PostCode", (String) Helper.getUnoObjectbyName(oUserDataAccess, "postalcode"));
168             myFieldHandler.changeUserFieldContent("City", (String) Helper.getUnoObjectbyName(oUserDataAccess, "l"));
169             myFieldHandler.changeUserFieldContent(PropertyNames.PROPERTY_STATE, (String) Helper.getUnoObjectbyName(oUserDataAccess, "st"));
170         }
171         catch (Exception exception)
172         {
173             exception.printStackTrace(System.out);
174         }
175     }
176 
killEmptyUserFields()177     public void killEmptyUserFields()
178     {
179         TextFieldHandler myFieldHandler = new TextFieldHandler(xMSF, xTextDocument);
180         myFieldHandler.removeUserFieldByContent(PropertyNames.EMPTY_STRING);
181     }
182 
killEmptyFrames()183     public void killEmptyFrames()
184     {
185         try
186         {
187             if (!keepLogoFrame)
188             {
189                 XTextFrame xTF = TextFrameHandler.getFrameByName("Company Logo", xTextDocument);
190                 if (xTF != null)
191                 {
192                     xTF.dispose();
193                 }
194             }
195             if (!keepBendMarksFrame)
196             {
197                 XTextFrame xTF = TextFrameHandler.getFrameByName("Bend Marks", xTextDocument);
198                 if (xTF != null)
199                 {
200                     xTF.dispose();
201                 }
202             }
203             if (!keepLetterSignsFrame)
204             {
205                 XTextFrame xTF = TextFrameHandler.getFrameByName("Letter Signs", xTextDocument);
206                 if (xTF != null)
207                 {
208                     xTF.dispose();
209                 }
210             }
211             if (!keepSenderAddressRepeatedFrame)
212             {
213                 XTextFrame xTF = TextFrameHandler.getFrameByName("Sender Address Repeated", xTextDocument);
214                 if (xTF != null)
215                 {
216                     xTF.dispose();
217                 }
218             }
219             if (!keepAddressFrame)
220             {
221                 XTextFrame xTF = TextFrameHandler.getFrameByName("Sender Address", xTextDocument);
222                 if (xTF != null)
223                 {
224                     xTF.dispose();
225                 }
226             }
227 
228         }
229         catch (Exception e)
230         {
231             e.printStackTrace();
232         }
233 
234     }
235 
236     public class BusinessPaperObject
237     {
238 
239         public int iWidth;
240         public int iHeight;
241         public int iXPos;
242         public int iYPos;
243         XTextFrame xFrame;
244         XShape xShape;
245 
BusinessPaperObject(String FrameText, int Width, int Height, int XPos, int YPos)246         public BusinessPaperObject(String FrameText, int Width, int Height, int XPos, int YPos)
247         {
248 
249             iWidth = Width;
250             iHeight = Height;
251             iXPos = XPos;
252             iYPos = YPos;
253 
254             try
255             {
256                 xFrame = UnoRuntime.queryInterface(XTextFrame.class, xMSFDoc.createInstance("com.sun.star.text.TextFrame"));
257                 xShape = UnoRuntime.queryInterface(XShape.class, xFrame);
258 
259                 setFramePosition();
260                 Helper.setUnoPropertyValue(xShape, "AnchorType", TextContentAnchorType.AT_PAGE);
261                 Helper.setUnoPropertyValue(xShape, "SizeType", new Short(SizeType.FIX));
262 
263                 Helper.setUnoPropertyValue(xFrame, "TextWrap", WrapTextMode.THROUGHT);
264                 Helper.setUnoPropertyValue(xFrame, "Opaque", Boolean.TRUE);
265                 Helper.setUnoPropertyValue(xFrame, "BackColor", 15790320);
266 
267                 BorderLine myBorder = new BorderLine();
268                 myBorder.OuterLineWidth = 0;
269                 Helper.setUnoPropertyValue(xFrame, "LeftBorder", myBorder);
270                 Helper.setUnoPropertyValue(xFrame, "RightBorder", myBorder);
271                 Helper.setUnoPropertyValue(xFrame, "TopBorder", myBorder);
272                 Helper.setUnoPropertyValue(xFrame, "BottomBorder", myBorder);
273                 Helper.setUnoPropertyValue(xFrame, "Print", Boolean.FALSE);
274 
275                 XTextCursor xTextCursor = xTextDocument.getText().createTextCursor();
276                 xTextCursor.gotoEnd(true);
277                 XText xText = xTextDocument.getText();
278                 xText.insertTextContent(xTextCursor, xFrame, false);
279 
280                 XText xFrameText = xFrame.getText();
281                 XTextCursor xFrameCursor = xFrameText.createTextCursor();
282                 XPropertySet xCursorProps = UnoRuntime.queryInterface(XPropertySet.class, xFrameCursor);
283                 xCursorProps.setPropertyValue("CharWeight", new Float(com.sun.star.awt.FontWeight.BOLD));
284                 xCursorProps.setPropertyValue("CharColor", 16777215);
285                 xCursorProps.setPropertyValue("CharFontName", "Albany");
286                 xCursorProps.setPropertyValue("CharHeight", new Float(18));
287 
288                 xFrameText.insertString(xFrameCursor, FrameText, false);
289 
290             }
291             catch (Exception e)
292             {
293                 e.printStackTrace(System.out);
294             }
295         }
296 
setFramePosition()297         public void setFramePosition()
298         {
299             Helper.setUnoPropertyValue(xFrame, "HoriOrient", new Short(HoriOrientation.NONE));
300             Helper.setUnoPropertyValue(xFrame, "VertOrient", new Short(VertOrientation.NONE));
301             Helper.setUnoPropertyValue(xFrame, PropertyNames.PROPERTY_HEIGHT, new Integer(iHeight));
302             Helper.setUnoPropertyValue(xFrame, PropertyNames.PROPERTY_WIDTH, new Integer(iWidth));
303             Helper.setUnoPropertyValue(xFrame, "HoriOrientPosition", new Integer(iXPos));
304             Helper.setUnoPropertyValue(xFrame, "VertOrientPosition", new Integer(iYPos));
305             Helper.setUnoPropertyValue(xFrame, "HoriOrientRelation", new Short(RelOrientation.PAGE_FRAME));
306             Helper.setUnoPropertyValue(xFrame, "VertOrientRelation", new Short(RelOrientation.PAGE_FRAME));
307         }
308 
removeFrame()309         public void removeFrame()
310         {
311             if (xFrame != null)
312             {
313                 try
314                 {
315                     xTextDocument.getText().removeTextContent(xFrame);
316                 }
317                 catch (Exception e)
318                 {
319                     e.printStackTrace(System.out);
320                 }
321             }
322         }
323     }
324 }
325