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.fax;
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.text.*;
30 import com.sun.star.lang.XMultiServiceFactory;
31 import com.sun.star.awt.XWindowPeer;
32 import com.sun.star.uno.Exception;
33 import com.sun.star.uno.UnoRuntime;
34 import com.sun.star.beans.XPropertySet;
35 import com.sun.star.style.NumberingType;
36 import com.sun.star.style.ParagraphAdjust;
37 import com.sun.star.style.XStyleFamiliesSupplier;
38 import com.sun.star.style.XStyle;
39 
40 public class FaxDocument extends TextDocument
41 {
42 
43     XDesktop xDesktop;
44     boolean keepLogoFrame = true;
45     boolean keepTypeFrame = true;
46 
FaxDocument(XMultiServiceFactory xMSF, XTerminateListener listener)47     public FaxDocument(XMultiServiceFactory xMSF, XTerminateListener listener)
48     {
49         super(xMSF, listener, "WIZARD_LIVE_PREVIEW");
50     }
51 
getWindowPeer()52     public XWindowPeer getWindowPeer()
53     {
54         return UnoRuntime.queryInterface(XWindowPeer.class, xTextDocument);
55     }
56 
switchElement(String sElement, boolean bState)57     public void switchElement(String sElement, boolean bState)
58     {
59         try
60         {
61             TextSectionHandler mySectionHandler = new TextSectionHandler(xMSF, xTextDocument);
62             Object oSection = mySectionHandler.xTextSectionsSupplier.getTextSections().getByName(sElement);
63             Helper.setUnoPropertyValue(oSection, "IsVisible", Boolean.valueOf(bState));
64 
65         }
66         catch (Exception exception)
67         {
68             exception.printStackTrace(System.out);
69         }
70     }
71 
updateDateFields()72     public void updateDateFields()
73     {
74         TextFieldHandler FH = new TextFieldHandler(xMSFDoc, xTextDocument);
75         FH.updateDateFields();
76     }
77 
switchFooter(String sPageStyle, boolean bState, boolean bPageNumber, String sText)78     public void switchFooter(String sPageStyle, boolean bState, boolean bPageNumber, String sText)
79     {
80         if (xTextDocument != null)
81         {
82             xTextDocument.lockControllers();
83             try
84             {
85                 XStyleFamiliesSupplier xStyleFamiliesSupplier = UnoRuntime.queryInterface(XStyleFamiliesSupplier.class, xTextDocument);
86                 com.sun.star.container.XNameAccess xNameAccess = null;
87                 xNameAccess = xStyleFamiliesSupplier.getStyleFamilies();
88 
89                 com.sun.star.container.XNameContainer xPageStyleCollection = null;
90                 xPageStyleCollection = UnoRuntime.queryInterface(com.sun.star.container.XNameContainer.class, xNameAccess.getByName("PageStyles"));
91 
92                 XText xFooterText;
93                 XStyle xPageStyle = UnoRuntime.queryInterface(XStyle.class, xPageStyleCollection.getByName(sPageStyle));
94 
95                 if (bState)
96                 {
97                     Helper.setUnoPropertyValue(xPageStyle, "FooterIsOn", Boolean.TRUE);
98                     xFooterText = UnoRuntime.queryInterface(XText.class, Helper.getUnoPropertyValue(xPageStyle, "FooterText"));
99                     xFooterText.setString(sText);
100                     if (bPageNumber)
101                     {
102                         //Adding the Page Number
103                         XTextCursor myCursor = xFooterText.createTextCursor();
104                         myCursor.gotoEnd(false);
105                         xFooterText.insertControlCharacter(myCursor, ControlCharacter.PARAGRAPH_BREAK, false);
106                         XPropertySet xCursorPSet = UnoRuntime.queryInterface(XPropertySet.class, myCursor);
107                         xCursorPSet.setPropertyValue("ParaAdjust", ParagraphAdjust.CENTER);
108                         XTextField xPageNumberField = UnoRuntime.queryInterface(XTextField.class, xMSFDoc.createInstance("com.sun.star.text.TextField.PageNumber"));
109                         XPropertySet xPSet = UnoRuntime.queryInterface(XPropertySet.class, xPageNumberField);
110                         xPSet.setPropertyValue("SubType", PageNumberType.CURRENT);
111                         xPSet.setPropertyValue("NumberingType", new Short(NumberingType.ARABIC));
112                         xFooterText.insertTextContent(xFooterText.getEnd(), xPageNumberField, false);
113                     }
114                 }
115                 else
116                 {
117                     Helper.setUnoPropertyValue(xPageStyle, "FooterIsOn", Boolean.FALSE);
118                 }
119                 xTextDocument.unlockControllers();
120             }
121             catch (Exception exception)
122             {
123                 exception.printStackTrace(System.out);
124             }
125         }
126     }
127 
hasElement(String sElement)128     public boolean hasElement(String sElement)
129     {
130         if (xTextDocument != null)
131         {
132             TextSectionHandler mySectionHandler = new TextSectionHandler(xMSF, xTextDocument);
133             return mySectionHandler.hasTextSectionByName(sElement);
134         }
135         else
136         {
137             return false;
138         }
139     }
140 
switchUserField(String sFieldName, String sNewContent, boolean bState)141     public void switchUserField(String sFieldName, String sNewContent, boolean bState)
142     {
143         TextFieldHandler myFieldHandler = new TextFieldHandler(xMSF, xTextDocument);
144         if (bState)
145         {
146             myFieldHandler.changeUserFieldContent(sFieldName, sNewContent);
147         }
148         else
149         {
150             myFieldHandler.changeUserFieldContent(sFieldName, PropertyNames.EMPTY_STRING);
151         }
152     }
153 
fillSenderWithUserData()154     public void fillSenderWithUserData()
155     {
156         try
157         {
158             TextFieldHandler myFieldHandler = new TextFieldHandler(xMSFDoc, xTextDocument);
159             Object oUserDataAccess = Configuration.getConfigurationRoot(xMSF, "org.openoffice.UserProfile/Data", false);
160             myFieldHandler.changeUserFieldContent("Company", (String) Helper.getUnoObjectbyName(oUserDataAccess, "o"));
161             myFieldHandler.changeUserFieldContent("Street", (String) Helper.getUnoObjectbyName(oUserDataAccess, "street"));
162             myFieldHandler.changeUserFieldContent("PostCode", (String) Helper.getUnoObjectbyName(oUserDataAccess, "postalcode"));
163             myFieldHandler.changeUserFieldContent(PropertyNames.PROPERTY_STATE, (String) Helper.getUnoObjectbyName(oUserDataAccess, "st"));
164             myFieldHandler.changeUserFieldContent("City", (String) Helper.getUnoObjectbyName(oUserDataAccess, "l"));
165             myFieldHandler.changeUserFieldContent("Fax", (String) Helper.getUnoObjectbyName(oUserDataAccess, "facsimiletelephonenumber"));
166         }
167         catch (Exception exception)
168         {
169             exception.printStackTrace(System.out);
170         }
171     }
172 
killEmptyUserFields()173     public void killEmptyUserFields()
174     {
175         TextFieldHandler myFieldHandler = new TextFieldHandler(xMSF, xTextDocument);
176         myFieldHandler.removeUserFieldByContent(PropertyNames.EMPTY_STRING);
177     }
178 
killEmptyFrames()179     public void killEmptyFrames()
180     {
181         try
182         {
183             if (!keepLogoFrame)
184             {
185                 XTextFrame xTF = TextFrameHandler.getFrameByName("Company Logo", xTextDocument);
186                 if (xTF != null)
187                 {
188                     xTF.dispose();
189                 }
190             }
191             if (!keepTypeFrame)
192             {
193                 XTextFrame xTF = TextFrameHandler.getFrameByName("Communication Type", xTextDocument);
194                 if (xTF != null)
195                 {
196                     xTF.dispose();
197                 }
198             }
199         }
200         catch (Exception e)
201         {
202             e.printStackTrace();
203         }
204 
205     }
206 }
207