xref: /trunk/main/wizards/com/sun/star/wizards/text/TextDocument.java (revision 3309286857f19787ae62bd793a98b5af4edd2ad3)
1a1b4a26bSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3a1b4a26bSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4a1b4a26bSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5a1b4a26bSAndrew Rist  * distributed with this work for additional information
6a1b4a26bSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7a1b4a26bSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8a1b4a26bSAndrew Rist  * "License"); you may not use this file except in compliance
9a1b4a26bSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11a1b4a26bSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13a1b4a26bSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14a1b4a26bSAndrew Rist  * software distributed under the License is distributed on an
15a1b4a26bSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16a1b4a26bSAndrew Rist  * KIND, either express or implied.  See the License for the
17a1b4a26bSAndrew Rist  * specific language governing permissions and limitations
18a1b4a26bSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20a1b4a26bSAndrew Rist  *************************************************************/
21a1b4a26bSAndrew Rist 
22a1b4a26bSAndrew Rist 
23cdf0e10cSrcweir package com.sun.star.wizards.text;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir import java.util.Calendar;
26cdf0e10cSrcweir import java.util.GregorianCalendar;
27cdf0e10cSrcweir 
28cdf0e10cSrcweir import com.sun.star.container.NoSuchElementException;
29cdf0e10cSrcweir import com.sun.star.container.XNameAccess;
30cdf0e10cSrcweir import com.sun.star.document.XDocumentProperties;
31cdf0e10cSrcweir import com.sun.star.document.XDocumentPropertiesSupplier;
32cdf0e10cSrcweir import com.sun.star.frame.XController;
33cdf0e10cSrcweir import com.sun.star.frame.XComponentLoader;
34cdf0e10cSrcweir import com.sun.star.frame.XDesktop;
35cdf0e10cSrcweir import com.sun.star.frame.XFramesSupplier;
36cdf0e10cSrcweir import com.sun.star.frame.XLoadable;
37cdf0e10cSrcweir import com.sun.star.frame.XModel;
38cdf0e10cSrcweir import com.sun.star.frame.XModule;
39cdf0e10cSrcweir import com.sun.star.frame.XTerminateListener;
40cdf0e10cSrcweir import com.sun.star.frame.XStorable;
41cdf0e10cSrcweir import com.sun.star.i18n.NumberFormatIndex;
42cdf0e10cSrcweir import com.sun.star.awt.Size;
43cdf0e10cSrcweir import com.sun.star.awt.XWindow;
44cdf0e10cSrcweir import com.sun.star.awt.XWindowPeer;
45cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
46cdf0e10cSrcweir import com.sun.star.beans.PropertyVetoException;
47cdf0e10cSrcweir import com.sun.star.lang.Locale;
48cdf0e10cSrcweir import com.sun.star.lang.WrappedTargetException;
49cdf0e10cSrcweir import com.sun.star.lang.XComponent;
50cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
51cdf0e10cSrcweir 
52cdf0e10cSrcweir import com.sun.star.style.XStyle;
53cdf0e10cSrcweir import com.sun.star.style.XStyleFamiliesSupplier;
54cdf0e10cSrcweir import com.sun.star.task.XStatusIndicatorFactory;
55cdf0e10cSrcweir import com.sun.star.text.XPageCursor;
56cdf0e10cSrcweir import com.sun.star.text.XSimpleText;
57cdf0e10cSrcweir import com.sun.star.text.XText;
58cdf0e10cSrcweir import com.sun.star.text.XTextContent;
59cdf0e10cSrcweir import com.sun.star.text.XTextCursor;
60cdf0e10cSrcweir import com.sun.star.text.XTextDocument;
61cdf0e10cSrcweir import com.sun.star.text.XTextViewCursor;
62cdf0e10cSrcweir import com.sun.star.text.XTextViewCursorSupplier;
63cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
64cdf0e10cSrcweir import com.sun.star.util.DateTime;
65cdf0e10cSrcweir import com.sun.star.util.XModifiable;
66cdf0e10cSrcweir import com.sun.star.util.XNumberFormatsSupplier;
67cdf0e10cSrcweir import com.sun.star.util.XRefreshable;
68cdf0e10cSrcweir import com.sun.star.wizards.common.Configuration;
69cdf0e10cSrcweir import com.sun.star.wizards.common.Desktop;
70cdf0e10cSrcweir import com.sun.star.wizards.common.Helper;
71cdf0e10cSrcweir import com.sun.star.wizards.common.JavaTools;
72cdf0e10cSrcweir import com.sun.star.wizards.common.Helper.DateUtils;
73cdf0e10cSrcweir import com.sun.star.wizards.common.PropertyNames;
74cdf0e10cSrcweir import com.sun.star.wizards.document.OfficeDocument;
75cdf0e10cSrcweir 
76cdf0e10cSrcweir public class TextDocument
77cdf0e10cSrcweir {
78cdf0e10cSrcweir 
79cdf0e10cSrcweir     public XComponent xComponent;
80cdf0e10cSrcweir     public com.sun.star.text.XTextDocument xTextDocument;
81cdf0e10cSrcweir     public com.sun.star.util.XNumberFormats NumberFormats;
82cdf0e10cSrcweir     public com.sun.star.document.XDocumentProperties m_xDocProps;
83cdf0e10cSrcweir     public com.sun.star.task.XStatusIndicator xProgressBar;
84cdf0e10cSrcweir     public com.sun.star.frame.XFrame xFrame;
85cdf0e10cSrcweir     public XText xText;
86cdf0e10cSrcweir     public XMultiServiceFactory xMSFDoc;
87cdf0e10cSrcweir     public XMultiServiceFactory xMSF;
88cdf0e10cSrcweir     public com.sun.star.util.XNumberFormatsSupplier xNumberFormatsSupplier;
89cdf0e10cSrcweir     public com.sun.star.awt.XWindowPeer xWindowPeer;
90cdf0e10cSrcweir     public int PageWidth;
91cdf0e10cSrcweir     public int ScaleWidth;
92cdf0e10cSrcweir     public Size DocSize;
93cdf0e10cSrcweir     public com.sun.star.awt.Rectangle PosSize;
94cdf0e10cSrcweir     public com.sun.star.lang.Locale CharLocale;
95cdf0e10cSrcweir     public XStorable xStorable;
96cdf0e10cSrcweir 
97cdf0e10cSrcweir     // creates an instance of TextDocument and creates a named frame. No document is actually loaded into this frame.
TextDocument(XMultiServiceFactory xMSF, XTerminateListener listener, String FrameName)98cdf0e10cSrcweir     public TextDocument(XMultiServiceFactory xMSF, XTerminateListener listener, String FrameName)
99cdf0e10cSrcweir     {
100cdf0e10cSrcweir         this.xMSF = xMSF;
101cdf0e10cSrcweir         xFrame = OfficeDocument.createNewFrame(xMSF, listener, FrameName);
102cdf0e10cSrcweir     }
103cdf0e10cSrcweir 
104cdf0e10cSrcweir     // creates an instance of TextDocument by loading a given URL as preview
TextDocument(XMultiServiceFactory xMSF, String _sPreviewURL, boolean bShowStatusIndicator, XTerminateListener listener)105cdf0e10cSrcweir     public TextDocument(XMultiServiceFactory xMSF, String _sPreviewURL, boolean bShowStatusIndicator, XTerminateListener listener)
106cdf0e10cSrcweir     {
107cdf0e10cSrcweir         this.xMSF = xMSF;
108cdf0e10cSrcweir 
109cdf0e10cSrcweir         xFrame = OfficeDocument.createNewFrame(xMSF, listener);
110cdf0e10cSrcweir         xTextDocument = loadAsPreview(_sPreviewURL, true);
111cdf0e10cSrcweir         xComponent = UnoRuntime.queryInterface(XComponent.class, xTextDocument);
112cdf0e10cSrcweir 
113cdf0e10cSrcweir         if (bShowStatusIndicator)
114cdf0e10cSrcweir         {
115cdf0e10cSrcweir             showStatusIndicator();
116cdf0e10cSrcweir         }
117cdf0e10cSrcweir         init();
118cdf0e10cSrcweir     }
119cdf0e10cSrcweir 
120cdf0e10cSrcweir     // creates an instance of TextDocument from the desktop's current frame
TextDocument(XMultiServiceFactory xMSF, boolean bShowStatusIndicator, XTerminateListener listener)121cdf0e10cSrcweir     public TextDocument(XMultiServiceFactory xMSF, boolean bShowStatusIndicator, XTerminateListener listener)
122cdf0e10cSrcweir     {
123cdf0e10cSrcweir         this.xMSF = xMSF;
124cdf0e10cSrcweir 
125cdf0e10cSrcweir         XDesktop xDesktop = Desktop.getDesktop(xMSF);
126cdf0e10cSrcweir         XFramesSupplier xFrameSupplier = UnoRuntime.queryInterface(XFramesSupplier.class, xDesktop);
127cdf0e10cSrcweir         xFrame = xFrameSupplier.getActiveFrame();
128cdf0e10cSrcweir         xComponent = UnoRuntime.queryInterface(XComponent.class, xFrame.getController().getModel());
129cdf0e10cSrcweir         xTextDocument = UnoRuntime.queryInterface(XTextDocument.class, xComponent);
130cdf0e10cSrcweir 
131cdf0e10cSrcweir         if (bShowStatusIndicator)
132cdf0e10cSrcweir         {
133cdf0e10cSrcweir             showStatusIndicator();
134cdf0e10cSrcweir         }
135cdf0e10cSrcweir         init();
136cdf0e10cSrcweir     }
137cdf0e10cSrcweir 
138cdf0e10cSrcweir     public static class ModuleIdentifier
139cdf0e10cSrcweir     {
140cdf0e10cSrcweir 
141cdf0e10cSrcweir         private String m_identifier;
142cdf0e10cSrcweir 
getIdentifier()143cdf0e10cSrcweir         protected final String getIdentifier()
144cdf0e10cSrcweir         {
145cdf0e10cSrcweir             return m_identifier;
146cdf0e10cSrcweir         }
147cdf0e10cSrcweir 
ModuleIdentifier(String _identifier)148cdf0e10cSrcweir         public ModuleIdentifier(String _identifier)
149cdf0e10cSrcweir         {
150cdf0e10cSrcweir             m_identifier = _identifier;
151cdf0e10cSrcweir         }
152cdf0e10cSrcweir     }
153cdf0e10cSrcweir 
154cdf0e10cSrcweir     // creates an instance of TextDocument containing a blank text document
TextDocument(XMultiServiceFactory xMSF, ModuleIdentifier _moduleIdentifier, boolean bShowStatusIndicator)155cdf0e10cSrcweir     public TextDocument(XMultiServiceFactory xMSF, ModuleIdentifier _moduleIdentifier, boolean bShowStatusIndicator)
156cdf0e10cSrcweir     {
157cdf0e10cSrcweir         this.xMSF = xMSF;
158cdf0e10cSrcweir 
159cdf0e10cSrcweir         try
160cdf0e10cSrcweir         {
161cdf0e10cSrcweir             // create the empty document, and set its module identifier
162cdf0e10cSrcweir             xTextDocument = UnoRuntime.queryInterface(XTextDocument.class,
163cdf0e10cSrcweir                     xMSF.createInstance("com.sun.star.text.TextDocument"));
164cdf0e10cSrcweir 
165cdf0e10cSrcweir             XLoadable xLoadable = UnoRuntime.queryInterface(XLoadable.class, xTextDocument);
166cdf0e10cSrcweir             xLoadable.initNew();
167cdf0e10cSrcweir 
168cdf0e10cSrcweir             XModule xModule = UnoRuntime.queryInterface(XModule.class,
169cdf0e10cSrcweir                     xTextDocument);
170cdf0e10cSrcweir             xModule.setIdentifier(_moduleIdentifier.getIdentifier());
171cdf0e10cSrcweir 
172cdf0e10cSrcweir             // load the document into a blank frame
173cdf0e10cSrcweir             XDesktop xDesktop = Desktop.getDesktop(xMSF);
174cdf0e10cSrcweir             XComponentLoader xLoader = UnoRuntime.queryInterface(XComponentLoader.class, xDesktop);
175cdf0e10cSrcweir             PropertyValue[] loadArgs = new PropertyValue[]
176cdf0e10cSrcweir             {
177cdf0e10cSrcweir                 new PropertyValue("Model", -1, xTextDocument, com.sun.star.beans.PropertyState.DIRECT_VALUE)
178cdf0e10cSrcweir             };
179cdf0e10cSrcweir             xLoader.loadComponentFromURL("private:object", "_blank", 0, loadArgs);
180cdf0e10cSrcweir 
181cdf0e10cSrcweir             // remember some things for later usage
182cdf0e10cSrcweir             xFrame = xTextDocument.getCurrentController().getFrame();
183cdf0e10cSrcweir             xComponent = UnoRuntime.queryInterface(XComponent.class, xTextDocument);
184cdf0e10cSrcweir         }
185cdf0e10cSrcweir         catch (Exception e)
186cdf0e10cSrcweir         {
187*0cb2ec91SJohn Bampton             // TODO: it seems the whole project does not really have an error handling. Other methods
188cdf0e10cSrcweir             // seem to generally silence errors, so we can't do anything else here ...
189cdf0e10cSrcweir         }
190cdf0e10cSrcweir 
191cdf0e10cSrcweir         if (bShowStatusIndicator)
192cdf0e10cSrcweir         {
193cdf0e10cSrcweir             showStatusIndicator();
194cdf0e10cSrcweir         }
195cdf0e10cSrcweir         init();
196cdf0e10cSrcweir     }
197cdf0e10cSrcweir 
198cdf0e10cSrcweir     //creates an instance of TextDocument from a given XTextDocument
TextDocument(XMultiServiceFactory xMSF, XTextDocument _textDocument, boolean bshowStatusIndicator)199cdf0e10cSrcweir     public TextDocument(XMultiServiceFactory xMSF, XTextDocument _textDocument, boolean bshowStatusIndicator)
200cdf0e10cSrcweir     {
201cdf0e10cSrcweir         this.xMSF = xMSF;
202cdf0e10cSrcweir         xFrame = _textDocument.getCurrentController().getFrame();
203cdf0e10cSrcweir         xComponent = UnoRuntime.queryInterface(XComponent.class, _textDocument);
204cdf0e10cSrcweir         xTextDocument = UnoRuntime.queryInterface(XTextDocument.class, xComponent);
205cdf0e10cSrcweir         //PosSize = xFrame.getComponentWindow().getPosSize();
206cdf0e10cSrcweir         if (bshowStatusIndicator)
207cdf0e10cSrcweir         {
208cdf0e10cSrcweir             XStatusIndicatorFactory xStatusIndicatorFactory = UnoRuntime.queryInterface(XStatusIndicatorFactory.class, xFrame);
209cdf0e10cSrcweir             xProgressBar = xStatusIndicatorFactory.createStatusIndicator();
210cdf0e10cSrcweir             xProgressBar.start(PropertyNames.EMPTY_STRING, 100);
211cdf0e10cSrcweir             xProgressBar.setValue(5);
212cdf0e10cSrcweir         }
213cdf0e10cSrcweir         xWindowPeer = UnoRuntime.queryInterface(XWindowPeer.class, xFrame.getComponentWindow());
214cdf0e10cSrcweir         xMSFDoc = UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDocument);
215cdf0e10cSrcweir         xNumberFormatsSupplier = UnoRuntime.queryInterface(XNumberFormatsSupplier.class, xTextDocument);
216cdf0e10cSrcweir 
217cdf0e10cSrcweir         XDocumentPropertiesSupplier xDocPropsSuppl = UnoRuntime.queryInterface(XDocumentPropertiesSupplier.class, xTextDocument);
218cdf0e10cSrcweir         m_xDocProps = xDocPropsSuppl.getDocumentProperties();
219cdf0e10cSrcweir         CharLocale = (Locale) Helper.getUnoStructValue((Object) xComponent, "CharLocale");
220cdf0e10cSrcweir         xText = xTextDocument.getText();
221cdf0e10cSrcweir     }
222cdf0e10cSrcweir 
init()223cdf0e10cSrcweir     private void init()
224cdf0e10cSrcweir     {
225cdf0e10cSrcweir         xWindowPeer = UnoRuntime.queryInterface(XWindowPeer.class, xFrame.getComponentWindow());
226cdf0e10cSrcweir         xMSFDoc = UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDocument);
227cdf0e10cSrcweir         xNumberFormatsSupplier = UnoRuntime.queryInterface(XNumberFormatsSupplier.class, xTextDocument);
228cdf0e10cSrcweir         XDocumentPropertiesSupplier xDocPropsSuppl = UnoRuntime.queryInterface(XDocumentPropertiesSupplier.class, xTextDocument);
229cdf0e10cSrcweir         m_xDocProps = xDocPropsSuppl.getDocumentProperties();
230cdf0e10cSrcweir         CharLocale = (Locale) Helper.getUnoStructValue((Object) xComponent, "CharLocale");
231cdf0e10cSrcweir         xStorable = UnoRuntime.queryInterface(XStorable.class, xTextDocument);
232cdf0e10cSrcweir         xText = xTextDocument.getText();
233cdf0e10cSrcweir     }
234cdf0e10cSrcweir 
showStatusIndicator()235cdf0e10cSrcweir     private void showStatusIndicator()
236cdf0e10cSrcweir     {
237cdf0e10cSrcweir         XStatusIndicatorFactory xStatusIndicatorFactory = UnoRuntime.queryInterface(XStatusIndicatorFactory.class, xFrame);
238cdf0e10cSrcweir         xProgressBar = xStatusIndicatorFactory.createStatusIndicator();
239cdf0e10cSrcweir         xProgressBar.start(PropertyNames.EMPTY_STRING, 100);
240cdf0e10cSrcweir         xProgressBar.setValue(5);
241cdf0e10cSrcweir     }
242cdf0e10cSrcweir 
loadAsPreview(String sDefaultTemplate, boolean asTemplate)243cdf0e10cSrcweir     public XTextDocument loadAsPreview(String sDefaultTemplate, boolean asTemplate)
244cdf0e10cSrcweir     {
245cdf0e10cSrcweir         PropertyValue loadValues[] = new PropertyValue[3];
246cdf0e10cSrcweir         //      open document in the Preview mode
247cdf0e10cSrcweir         loadValues[0] = new PropertyValue();
248cdf0e10cSrcweir         loadValues[0].Name = PropertyNames.READ_ONLY;
249cdf0e10cSrcweir         loadValues[0].Value = Boolean.TRUE;
250cdf0e10cSrcweir         loadValues[1] = new PropertyValue();
251cdf0e10cSrcweir         loadValues[1].Name = "AsTemplate";
252cdf0e10cSrcweir         loadValues[1].Value = asTemplate ? Boolean.TRUE : Boolean.FALSE;
253cdf0e10cSrcweir         loadValues[2] = new PropertyValue();
254cdf0e10cSrcweir         loadValues[2].Name = "Preview";
255cdf0e10cSrcweir         loadValues[2].Value = Boolean.TRUE;
256cdf0e10cSrcweir 
257cdf0e10cSrcweir         //set the preview document to non-modified mode in order to avoid the 'do u want to save' box
258cdf0e10cSrcweir         if (xTextDocument != null)
259cdf0e10cSrcweir         {
260cdf0e10cSrcweir             try
261cdf0e10cSrcweir             {
262cdf0e10cSrcweir                 XModifiable xModi = UnoRuntime.queryInterface(XModifiable.class, xTextDocument);
263cdf0e10cSrcweir                 xModi.setModified(false);
264cdf0e10cSrcweir             }
265cdf0e10cSrcweir             catch (PropertyVetoException e1)
266cdf0e10cSrcweir             {
267cdf0e10cSrcweir                 e1.printStackTrace(System.out);
268cdf0e10cSrcweir             }
269cdf0e10cSrcweir         }
270cdf0e10cSrcweir         Object oDoc = OfficeDocument.load(xFrame, sDefaultTemplate, "_self", loadValues);
271cdf0e10cSrcweir         xTextDocument = (com.sun.star.text.XTextDocument) oDoc;
272cdf0e10cSrcweir         DocSize = getPageSize();
273cdf0e10cSrcweir         xMSFDoc = UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDocument);
274cdf0e10cSrcweir 
275cdf0e10cSrcweir         ViewHandler myViewHandler = new ViewHandler(xMSFDoc, xTextDocument);
276cdf0e10cSrcweir         try
277cdf0e10cSrcweir         {
278cdf0e10cSrcweir             myViewHandler.setViewSetting("ZoomType", new Short(com.sun.star.view.DocumentZoomType.ENTIRE_PAGE));
279cdf0e10cSrcweir         }
280cdf0e10cSrcweir         catch (Exception e)
281cdf0e10cSrcweir         {
282cdf0e10cSrcweir             e.printStackTrace();
283cdf0e10cSrcweir         }
284cdf0e10cSrcweir 
285cdf0e10cSrcweir         TextFieldHandler myFieldHandler = new TextFieldHandler(xMSF, xTextDocument);
286cdf0e10cSrcweir         myFieldHandler.updateDocInfoFields();
287cdf0e10cSrcweir 
288cdf0e10cSrcweir         return xTextDocument;
289cdf0e10cSrcweir 
290cdf0e10cSrcweir     }
291cdf0e10cSrcweir 
getPageSize()292cdf0e10cSrcweir     public Size getPageSize()
293cdf0e10cSrcweir     {
294cdf0e10cSrcweir         try
295cdf0e10cSrcweir         {
296cdf0e10cSrcweir             XStyleFamiliesSupplier xStyleFamiliesSupplier = UnoRuntime.queryInterface(XStyleFamiliesSupplier.class, xTextDocument);
297cdf0e10cSrcweir             com.sun.star.container.XNameAccess xNameAccess = null;
298cdf0e10cSrcweir             xNameAccess = xStyleFamiliesSupplier.getStyleFamilies();
299cdf0e10cSrcweir             com.sun.star.container.XNameContainer xPageStyleCollection = null;
300cdf0e10cSrcweir             xPageStyleCollection = UnoRuntime.queryInterface(com.sun.star.container.XNameContainer.class, xNameAccess.getByName("PageStyles"));
301cdf0e10cSrcweir             XStyle xPageStyle = UnoRuntime.queryInterface(XStyle.class, xPageStyleCollection.getByName("First Page"));
302cdf0e10cSrcweir             return (Size) Helper.getUnoPropertyValue(xPageStyle, "Size");
303cdf0e10cSrcweir 
304cdf0e10cSrcweir         }
305cdf0e10cSrcweir         catch (Exception exception)
306cdf0e10cSrcweir         {
307cdf0e10cSrcweir             exception.printStackTrace(System.out);
308cdf0e10cSrcweir             return null;
309cdf0e10cSrcweir         }
310cdf0e10cSrcweir     }
311cdf0e10cSrcweir 
312cdf0e10cSrcweir     //creates an instance of TextDocument and creates a frame and loads a document
TextDocument(XMultiServiceFactory xMSF, String URL, PropertyValue[] xArgs, XTerminateListener listener)313cdf0e10cSrcweir     public TextDocument(XMultiServiceFactory xMSF, String URL, PropertyValue[] xArgs, XTerminateListener listener)
314cdf0e10cSrcweir     {
315cdf0e10cSrcweir         this.xMSF = xMSF;
316cdf0e10cSrcweir         XDesktop xDesktop = Desktop.getDesktop(xMSF);
317cdf0e10cSrcweir 
318cdf0e10cSrcweir         xFrame = OfficeDocument.createNewFrame(xMSF, listener);
319cdf0e10cSrcweir         Object oDoc = OfficeDocument.load(xFrame, URL, "_self", xArgs);
320cdf0e10cSrcweir         xTextDocument = (XTextDocument) oDoc;
321cdf0e10cSrcweir         xComponent = UnoRuntime.queryInterface(XComponent.class, xTextDocument);
322cdf0e10cSrcweir         XWindow xWindow = xFrame.getComponentWindow();
323cdf0e10cSrcweir 
324cdf0e10cSrcweir         xWindowPeer = UnoRuntime.queryInterface(XWindowPeer.class, xFrame.getComponentWindow());
325cdf0e10cSrcweir         xMSFDoc = UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDocument);
326cdf0e10cSrcweir         xNumberFormatsSupplier = UnoRuntime.queryInterface(XNumberFormatsSupplier.class, xTextDocument);
327cdf0e10cSrcweir 
328cdf0e10cSrcweir         XDocumentPropertiesSupplier xDocPropsSuppl = UnoRuntime.queryInterface(XDocumentPropertiesSupplier.class, xTextDocument);
329cdf0e10cSrcweir         m_xDocProps = xDocPropsSuppl.getDocumentProperties();
330cdf0e10cSrcweir         CharLocale = (Locale) Helper.getUnoStructValue((Object) xComponent, "CharLocale");
331cdf0e10cSrcweir     }
332cdf0e10cSrcweir 
createTextCursor(Object oCursorContainer)333cdf0e10cSrcweir     public static XTextCursor createTextCursor(Object oCursorContainer)
334cdf0e10cSrcweir     {
335cdf0e10cSrcweir         XSimpleText xText = UnoRuntime.queryInterface(XSimpleText.class, oCursorContainer);
336cdf0e10cSrcweir         return xText.createTextCursor();
337cdf0e10cSrcweir     }
338cdf0e10cSrcweir 
339cdf0e10cSrcweir     // Todo: This method is  unsecure because the last index is not necessarily the last section
340cdf0e10cSrcweir 
341cdf0e10cSrcweir     // Todo: This Routine should be  modified, because I cannot rely on the last Table in the document to be the last in the TextTables sequence
342cdf0e10cSrcweir     // to make it really safe you must acquire the Tablenames before the insertion and after the insertion of the new Table. By comparing the
343cdf0e10cSrcweir     // two sequences of tablenames you can find out the tablename of the last inserted Table
344cdf0e10cSrcweir 
345cdf0e10cSrcweir     // Todo: This method is  unsecure because the last index is not necessarily the last section
getCharWidth(String ScaleString)346cdf0e10cSrcweir     public int getCharWidth(String ScaleString)
347cdf0e10cSrcweir     {
348cdf0e10cSrcweir         int iScale = 200;
349cdf0e10cSrcweir         xTextDocument.lockControllers();
350cdf0e10cSrcweir         int iScaleLen = ScaleString.length();
351cdf0e10cSrcweir         com.sun.star.text.XTextCursor xTextCursor = createTextCursor(xTextDocument.getText());
352cdf0e10cSrcweir         xTextCursor.gotoStart(false);
353cdf0e10cSrcweir         com.sun.star.wizards.common.Helper.setUnoPropertyValue(xTextCursor, "PageDescName", "First Page");
354cdf0e10cSrcweir         xTextCursor.setString(ScaleString);
355cdf0e10cSrcweir         XTextViewCursorSupplier xViewCursor = UnoRuntime.queryInterface(XTextViewCursorSupplier.class, xTextDocument.getCurrentController());
356cdf0e10cSrcweir         XTextViewCursor xTextViewCursor = xViewCursor.getViewCursor();
357cdf0e10cSrcweir         xTextViewCursor.gotoStart(false);
358cdf0e10cSrcweir         int iFirstPos = xTextViewCursor.getPosition().X;
359cdf0e10cSrcweir         xTextViewCursor.gotoEnd(false);
360cdf0e10cSrcweir         int iLastPos = xTextViewCursor.getPosition().X;
361cdf0e10cSrcweir         iScale = (iLastPos - iFirstPos) / iScaleLen;
362cdf0e10cSrcweir         xTextCursor.gotoStart(false);
363cdf0e10cSrcweir         xTextCursor.gotoEnd(true);
364cdf0e10cSrcweir         xTextCursor.setString(PropertyNames.EMPTY_STRING);
365cdf0e10cSrcweir         unlockallControllers();
366cdf0e10cSrcweir         return iScale;
367cdf0e10cSrcweir     }
368cdf0e10cSrcweir 
unlockallControllers()369cdf0e10cSrcweir     public void unlockallControllers()
370cdf0e10cSrcweir     {
371cdf0e10cSrcweir         while (xTextDocument.hasControllersLocked())
372cdf0e10cSrcweir         {
373cdf0e10cSrcweir             xTextDocument.unlockControllers();
374cdf0e10cSrcweir         }
375cdf0e10cSrcweir     }
376cdf0e10cSrcweir 
refresh()377cdf0e10cSrcweir     public void refresh()
378cdf0e10cSrcweir     {
379cdf0e10cSrcweir         XRefreshable xRefreshable = UnoRuntime.queryInterface(XRefreshable.class, xTextDocument);
380cdf0e10cSrcweir         xRefreshable.refresh();
381cdf0e10cSrcweir     }
382cdf0e10cSrcweir 
383cdf0e10cSrcweir     /**
384cdf0e10cSrcweir      * This method sets the Author of a Wizard-generated template correctly
385cdf0e10cSrcweir      * and adds a explanatory sentence to the template description.
386cdf0e10cSrcweir      * @param WizardName The name of the Wizard.
387cdf0e10cSrcweir      * @param TemplateDescription The old Description which is being appended with another sentence.
388cdf0e10cSrcweir      * @return void.
389cdf0e10cSrcweir      */
setWizardTemplateDocInfo(String WizardName, String TemplateDescription)390cdf0e10cSrcweir     public void setWizardTemplateDocInfo(String WizardName, String TemplateDescription)
391cdf0e10cSrcweir     {
392cdf0e10cSrcweir         try
393cdf0e10cSrcweir         {
394cdf0e10cSrcweir             Object uD = Configuration.getConfigurationRoot(xMSF, "/org.openoffice.UserProfile/Data", false);
395cdf0e10cSrcweir             XNameAccess xNA = UnoRuntime.queryInterface(XNameAccess.class, uD);
396cdf0e10cSrcweir             Object gn = xNA.getByName("givenname");
397cdf0e10cSrcweir             Object sn = xNA.getByName("sn");
398cdf0e10cSrcweir             String fullname = gn + PropertyNames.SPACE + sn;
399cdf0e10cSrcweir 
400cdf0e10cSrcweir             Calendar cal = new GregorianCalendar();
401cdf0e10cSrcweir             int year = cal.get(Calendar.YEAR);
402cdf0e10cSrcweir             int month = cal.get(Calendar.MONTH);
403cdf0e10cSrcweir             int day = cal.get(Calendar.DAY_OF_MONTH);
404cdf0e10cSrcweir             DateTime currentDate = new DateTime();
405cdf0e10cSrcweir             currentDate.Day = (short) day;
406cdf0e10cSrcweir             currentDate.Month = (short) month;
407cdf0e10cSrcweir             currentDate.Year = (short) year;
408cdf0e10cSrcweir             DateUtils du = new DateUtils(xMSF, this.xTextDocument);
409cdf0e10cSrcweir             int ff = du.getFormat(NumberFormatIndex.DATE_SYS_DDMMYY);
410cdf0e10cSrcweir             String myDate = du.format(ff, currentDate);
411cdf0e10cSrcweir 
412cdf0e10cSrcweir             XDocumentPropertiesSupplier xDocPropsSuppl = UnoRuntime.queryInterface(XDocumentPropertiesSupplier.class, xTextDocument);
413cdf0e10cSrcweir             XDocumentProperties xDocProps2 = xDocPropsSuppl.getDocumentProperties();
414cdf0e10cSrcweir             xDocProps2.setAuthor(fullname);
415cdf0e10cSrcweir             xDocProps2.setModifiedBy(fullname);
416cdf0e10cSrcweir             String description = xDocProps2.getDescription();
417cdf0e10cSrcweir             description = description + PropertyNames.SPACE + TemplateDescription;
418cdf0e10cSrcweir             description = JavaTools.replaceSubString(description, WizardName, "<wizard_name>");
419cdf0e10cSrcweir             description = JavaTools.replaceSubString(description, myDate, "<current_date>");
420cdf0e10cSrcweir             xDocProps2.setDescription(description);
421cdf0e10cSrcweir         }
422cdf0e10cSrcweir         catch (NoSuchElementException e)
423cdf0e10cSrcweir         {
424cdf0e10cSrcweir             // TODO Auto-generated catch block
425cdf0e10cSrcweir             e.printStackTrace();
426cdf0e10cSrcweir         }
427cdf0e10cSrcweir         catch (WrappedTargetException e)
428cdf0e10cSrcweir         {
429cdf0e10cSrcweir             // TODO Auto-generated catch block
430cdf0e10cSrcweir             e.printStackTrace();
431cdf0e10cSrcweir         }
432cdf0e10cSrcweir         catch (Exception e)
433cdf0e10cSrcweir         {
434cdf0e10cSrcweir             // TODO Auto-generated catch block
435cdf0e10cSrcweir             e.printStackTrace();
436cdf0e10cSrcweir         }
437cdf0e10cSrcweir     }
438cdf0e10cSrcweir 
439cdf0e10cSrcweir     /**
440cdf0e10cSrcweir      * removes an arbitrary Object which supports the  'XTextContent' interface
441cdf0e10cSrcweir      * @param oTextContent
442cdf0e10cSrcweir      * @return
443cdf0e10cSrcweir      */
removeTextContent(Object oTextContent)444cdf0e10cSrcweir     public boolean removeTextContent(Object oTextContent)
445cdf0e10cSrcweir     {
446cdf0e10cSrcweir         try
447cdf0e10cSrcweir         {
448cdf0e10cSrcweir             XTextContent xTextContent = UnoRuntime.queryInterface(XTextContent.class, oTextContent);
449cdf0e10cSrcweir             xText.removeTextContent(xTextContent);
450cdf0e10cSrcweir             return true;
451cdf0e10cSrcweir         }
452cdf0e10cSrcweir         catch (NoSuchElementException e)
453cdf0e10cSrcweir         {
454cdf0e10cSrcweir             e.printStackTrace(System.out);
455cdf0e10cSrcweir             return false;
456cdf0e10cSrcweir         }
457cdf0e10cSrcweir     }
458cdf0e10cSrcweir 
459cdf0e10cSrcweir     /**
460cdf0e10cSrcweir      * Apparently there is no other way to get the
461cdf0e10cSrcweir      * page count of a text document other than using a cursor and
462cdf0e10cSrcweir      * making it jump to the last page...
463cdf0e10cSrcweir      * @param model the document model.
464cdf0e10cSrcweir      * @return the page count of the document.
465cdf0e10cSrcweir      */
getPageCount(Object model)466cdf0e10cSrcweir     public static int getPageCount(Object model)
467cdf0e10cSrcweir     {
468cdf0e10cSrcweir         XModel xModel = UnoRuntime.queryInterface(XModel.class, model);
469cdf0e10cSrcweir         XController xController = xModel.getCurrentController();
470cdf0e10cSrcweir         XTextViewCursorSupplier xTextVCS = UnoRuntime.queryInterface(XTextViewCursorSupplier.class, xController);
471cdf0e10cSrcweir         XTextViewCursor xTextVC = xTextVCS.getViewCursor();
472cdf0e10cSrcweir         XPageCursor xPC = UnoRuntime.queryInterface(XPageCursor.class, xTextVC);
473cdf0e10cSrcweir         xPC.jumpToLastPage();
474cdf0e10cSrcweir         return xPC.getPage();
475cdf0e10cSrcweir     }
476cdf0e10cSrcweir 
477cdf0e10cSrcweir     /* Possible Values for "OptionString" are: "LoadCellStyles", "LoadTextStyles", "LoadFrameStyles",
478cdf0e10cSrcweir     "LoadPageStyles", "LoadNumberingStyles", "OverwriteStyles" */
479cdf0e10cSrcweir }
480