1*a1b4a26bSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*a1b4a26bSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*a1b4a26bSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*a1b4a26bSAndrew Rist  * distributed with this work for additional information
6*a1b4a26bSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*a1b4a26bSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*a1b4a26bSAndrew Rist  * "License"); you may not use this file except in compliance
9*a1b4a26bSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*a1b4a26bSAndrew Rist  *
11*a1b4a26bSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*a1b4a26bSAndrew Rist  *
13*a1b4a26bSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*a1b4a26bSAndrew Rist  * software distributed under the License is distributed on an
15*a1b4a26bSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*a1b4a26bSAndrew Rist  * KIND, either express or implied.  See the License for the
17*a1b4a26bSAndrew Rist  * specific language governing permissions and limitations
18*a1b4a26bSAndrew Rist  * under the License.
19*a1b4a26bSAndrew Rist  *
20*a1b4a26bSAndrew Rist  *************************************************************/
21*a1b4a26bSAndrew Rist 
22*a1b4a26bSAndrew Rist 
23cdf0e10cSrcweir package com.sun.star.wizards.text;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
26cdf0e10cSrcweir import com.sun.star.container.XIndexAccess;
27cdf0e10cSrcweir import com.sun.star.container.XNameAccess;
28cdf0e10cSrcweir import com.sun.star.container.XNamed;
29cdf0e10cSrcweir import com.sun.star.lang.IllegalArgumentException;
30cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
31cdf0e10cSrcweir import com.sun.star.text.ControlCharacter;
32cdf0e10cSrcweir import com.sun.star.text.SectionFileLink;
33cdf0e10cSrcweir import com.sun.star.text.XText;
34cdf0e10cSrcweir import com.sun.star.text.XTextContent;
35cdf0e10cSrcweir import com.sun.star.text.XTextCursor;
36cdf0e10cSrcweir import com.sun.star.text.XTextDocument;
37cdf0e10cSrcweir import com.sun.star.text.XTextSectionsSupplier;
38cdf0e10cSrcweir import com.sun.star.uno.AnyConverter;
39cdf0e10cSrcweir import com.sun.star.uno.Exception;
40cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
41cdf0e10cSrcweir import com.sun.star.wizards.common.Helper;
42cdf0e10cSrcweir import com.sun.star.wizards.common.PropertyNames;
43cdf0e10cSrcweir 
44cdf0e10cSrcweir public class TextSectionHandler
45cdf0e10cSrcweir {
46cdf0e10cSrcweir 
47cdf0e10cSrcweir     public XTextSectionsSupplier xTextSectionsSupplier;
48cdf0e10cSrcweir     private XMultiServiceFactory xMSFDoc;
49cdf0e10cSrcweir     private XTextDocument xTextDocument;
50cdf0e10cSrcweir     private XText xText;
51cdf0e10cSrcweir 
52cdf0e10cSrcweir     /** Creates a new instance of TextSectionHandler */
TextSectionHandler(XMultiServiceFactory xMSF, XTextDocument xTextDocument)53cdf0e10cSrcweir     public TextSectionHandler(XMultiServiceFactory xMSF, XTextDocument xTextDocument)
54cdf0e10cSrcweir     {
55cdf0e10cSrcweir         this.xMSFDoc = xMSF;
56cdf0e10cSrcweir         this.xTextDocument = xTextDocument;
57cdf0e10cSrcweir         xText = xTextDocument.getText();
58cdf0e10cSrcweir         xTextSectionsSupplier = UnoRuntime.queryInterface(XTextSectionsSupplier.class, xTextDocument);
59cdf0e10cSrcweir     }
60cdf0e10cSrcweir 
removeTextSectionbyName(String SectionName)61cdf0e10cSrcweir     public void removeTextSectionbyName(String SectionName)
62cdf0e10cSrcweir     {
63cdf0e10cSrcweir         try
64cdf0e10cSrcweir         {
65cdf0e10cSrcweir             XNameAccess xAllTextSections = xTextSectionsSupplier.getTextSections();
66cdf0e10cSrcweir             if (xAllTextSections.hasByName(SectionName))
67cdf0e10cSrcweir             {
68cdf0e10cSrcweir                 Object oTextSection = xTextSectionsSupplier.getTextSections().getByName(SectionName);
69cdf0e10cSrcweir                 removeTextSection(oTextSection);
70cdf0e10cSrcweir             }
71cdf0e10cSrcweir         }
72cdf0e10cSrcweir         catch (Exception exception)
73cdf0e10cSrcweir         {
74cdf0e10cSrcweir             exception.printStackTrace(System.out);
75cdf0e10cSrcweir         }
76cdf0e10cSrcweir     }
77cdf0e10cSrcweir 
hasTextSectionByName(String SectionName)78cdf0e10cSrcweir     public boolean hasTextSectionByName(String SectionName)
79cdf0e10cSrcweir     {
80cdf0e10cSrcweir         com.sun.star.container.XNameAccess xAllTextSections = xTextSectionsSupplier.getTextSections();
81cdf0e10cSrcweir         return xAllTextSections.hasByName(SectionName);
82cdf0e10cSrcweir     }
83cdf0e10cSrcweir 
removeLastTextSection()84cdf0e10cSrcweir     public void removeLastTextSection()
85cdf0e10cSrcweir     {
86cdf0e10cSrcweir         try
87cdf0e10cSrcweir         {
88cdf0e10cSrcweir             XIndexAccess xAllTextSections = UnoRuntime.queryInterface(XIndexAccess.class, xTextSectionsSupplier.getTextSections());
89cdf0e10cSrcweir             Object oTextSection = xAllTextSections.getByIndex(xAllTextSections.getCount() - 1);
90cdf0e10cSrcweir             removeTextSection(oTextSection);
91cdf0e10cSrcweir         }
92cdf0e10cSrcweir         catch (Exception exception)
93cdf0e10cSrcweir         {
94cdf0e10cSrcweir             exception.printStackTrace(System.out);
95cdf0e10cSrcweir         }
96cdf0e10cSrcweir     }
97cdf0e10cSrcweir 
removeTextSection(Object _oTextSection)98cdf0e10cSrcweir     public void removeTextSection(Object _oTextSection)
99cdf0e10cSrcweir     {
100cdf0e10cSrcweir         try
101cdf0e10cSrcweir         {
102cdf0e10cSrcweir             XTextContent xTextContentTextSection = UnoRuntime.queryInterface(XTextContent.class, _oTextSection);
103cdf0e10cSrcweir             xText.removeTextContent(xTextContentTextSection);
104cdf0e10cSrcweir         }
105cdf0e10cSrcweir         catch (Exception exception)
106cdf0e10cSrcweir         {
107cdf0e10cSrcweir             exception.printStackTrace(System.out);
108cdf0e10cSrcweir         }
109cdf0e10cSrcweir     }
110cdf0e10cSrcweir 
removeInvisibleTextSections()111cdf0e10cSrcweir     public void removeInvisibleTextSections()
112cdf0e10cSrcweir     {
113cdf0e10cSrcweir         try
114cdf0e10cSrcweir         {
115cdf0e10cSrcweir             XIndexAccess xAllTextSections = UnoRuntime.queryInterface(XIndexAccess.class, xTextSectionsSupplier.getTextSections());
116cdf0e10cSrcweir             int TextSectionCount = xAllTextSections.getCount();
117cdf0e10cSrcweir             for (int i = TextSectionCount - 1; i >= 0; i--)
118cdf0e10cSrcweir             {
119cdf0e10cSrcweir                 XTextContent xTextContentTextSection = UnoRuntime.queryInterface(XTextContent.class, xAllTextSections.getByIndex(i));
120cdf0e10cSrcweir                 XPropertySet xTextSectionPropertySet = UnoRuntime.queryInterface(XPropertySet.class, xTextContentTextSection);
121cdf0e10cSrcweir                 boolean bRemoveTextSection = (!AnyConverter.toBoolean(xTextSectionPropertySet.getPropertyValue("IsVisible")));
122cdf0e10cSrcweir                 if (bRemoveTextSection)
123cdf0e10cSrcweir                 {
124cdf0e10cSrcweir                     xText.removeTextContent(xTextContentTextSection);
125cdf0e10cSrcweir                 }
126cdf0e10cSrcweir             }
127cdf0e10cSrcweir         }
128cdf0e10cSrcweir         catch (Exception exception)
129cdf0e10cSrcweir         {
130cdf0e10cSrcweir             exception.printStackTrace(System.out);
131cdf0e10cSrcweir         }
132cdf0e10cSrcweir     }
133cdf0e10cSrcweir 
removeAllTextSections()134cdf0e10cSrcweir     public void removeAllTextSections()
135cdf0e10cSrcweir     {
136cdf0e10cSrcweir         try
137cdf0e10cSrcweir         {
138cdf0e10cSrcweir             XIndexAccess xAllTextSections = UnoRuntime.queryInterface(XIndexAccess.class, xTextSectionsSupplier.getTextSections());
139cdf0e10cSrcweir             int TextSectionCount = xAllTextSections.getCount();
140cdf0e10cSrcweir             for (int i = TextSectionCount - 1; i >= 0; i--)
141cdf0e10cSrcweir             {
142cdf0e10cSrcweir                 XTextContent xTextContentTextSection = UnoRuntime.queryInterface(XTextContent.class, xAllTextSections.getByIndex(i));
143cdf0e10cSrcweir                 XPropertySet xTextSectionPropertySet = UnoRuntime.queryInterface(XPropertySet.class, xTextContentTextSection);
144cdf0e10cSrcweir                 xText.removeTextContent(xTextContentTextSection);
145cdf0e10cSrcweir             }
146cdf0e10cSrcweir         }
147cdf0e10cSrcweir         catch (Exception exception)
148cdf0e10cSrcweir         {
149cdf0e10cSrcweir             exception.printStackTrace(System.out);
150cdf0e10cSrcweir         }
151cdf0e10cSrcweir     }
152cdf0e10cSrcweir 
breakLinkofTextSections()153cdf0e10cSrcweir     public void breakLinkofTextSections()
154cdf0e10cSrcweir     {
155cdf0e10cSrcweir         try
156cdf0e10cSrcweir         {
157cdf0e10cSrcweir             Object oTextSection;
158cdf0e10cSrcweir             XIndexAccess xAllTextSections = UnoRuntime.queryInterface(XIndexAccess.class, xTextSectionsSupplier.getTextSections());
159cdf0e10cSrcweir             int iSectionCount = xAllTextSections.getCount();
160cdf0e10cSrcweir             SectionFileLink oSectionLink = new SectionFileLink();
161cdf0e10cSrcweir             oSectionLink.FileURL = PropertyNames.EMPTY_STRING;
162cdf0e10cSrcweir             for (int i = 0; i < iSectionCount; i++)
163cdf0e10cSrcweir             {
164cdf0e10cSrcweir                 oTextSection = xAllTextSections.getByIndex(i);
165cdf0e10cSrcweir                 Helper.setUnoPropertyValues(oTextSection, new String[]
166cdf0e10cSrcweir                         {
167cdf0e10cSrcweir                             "FileLink", "LinkRegion"
168cdf0e10cSrcweir                         }, new Object[]
169cdf0e10cSrcweir                         {
170cdf0e10cSrcweir                             oSectionLink, PropertyNames.EMPTY_STRING
171cdf0e10cSrcweir                         });
172cdf0e10cSrcweir             }
173cdf0e10cSrcweir         }
174cdf0e10cSrcweir         catch (Exception exception)
175cdf0e10cSrcweir         {
176cdf0e10cSrcweir             exception.printStackTrace(System.out);
177cdf0e10cSrcweir         }
178cdf0e10cSrcweir     }
179cdf0e10cSrcweir 
breakLinkOfTextSection(Object oTextSection)180cdf0e10cSrcweir     public void breakLinkOfTextSection(Object oTextSection)
181cdf0e10cSrcweir     {
182cdf0e10cSrcweir         SectionFileLink oSectionLink = new SectionFileLink();
183cdf0e10cSrcweir         oSectionLink.FileURL = PropertyNames.EMPTY_STRING;
184cdf0e10cSrcweir         Helper.setUnoPropertyValues(oTextSection, new String[]
185cdf0e10cSrcweir                 {
186cdf0e10cSrcweir                     "FileLink", "LinkRegion"
187cdf0e10cSrcweir                 }, new Object[]
188cdf0e10cSrcweir                 {
189cdf0e10cSrcweir                     oSectionLink, PropertyNames.EMPTY_STRING
190cdf0e10cSrcweir                 });
191cdf0e10cSrcweir     }
192cdf0e10cSrcweir 
linkSectiontoTemplate(String TemplateName, String SectionName)193cdf0e10cSrcweir     public void linkSectiontoTemplate(String TemplateName, String SectionName)
194cdf0e10cSrcweir     {
195cdf0e10cSrcweir         try
196cdf0e10cSrcweir         {
197cdf0e10cSrcweir             Object oTextSection = xTextSectionsSupplier.getTextSections().getByName(SectionName);
198cdf0e10cSrcweir             linkSectiontoTemplate(oTextSection, TemplateName, SectionName);
199cdf0e10cSrcweir         }
200cdf0e10cSrcweir         catch (Exception e)
201cdf0e10cSrcweir         {
202cdf0e10cSrcweir             e.printStackTrace(System.out);
203cdf0e10cSrcweir         }
204cdf0e10cSrcweir     }
205cdf0e10cSrcweir 
linkSectiontoTemplate(Object oTextSection, String TemplateName, String SectionName)206cdf0e10cSrcweir     public void linkSectiontoTemplate(Object oTextSection, String TemplateName, String SectionName)
207cdf0e10cSrcweir     {
208cdf0e10cSrcweir         SectionFileLink oSectionLink = new SectionFileLink();
209cdf0e10cSrcweir         oSectionLink.FileURL = TemplateName;
210cdf0e10cSrcweir         Helper.setUnoPropertyValues(oTextSection, new String[]
211cdf0e10cSrcweir                 {
212cdf0e10cSrcweir                     "FileLink", "LinkRegion"
213cdf0e10cSrcweir                 }, new Object[]
214cdf0e10cSrcweir                 {
215cdf0e10cSrcweir                     oSectionLink, SectionName
216cdf0e10cSrcweir                 });
217cdf0e10cSrcweir         XNamed xSectionName = UnoRuntime.queryInterface(XNamed.class, oTextSection);
218cdf0e10cSrcweir         String NewSectionName = xSectionName.getName();
219cdf0e10cSrcweir         if (NewSectionName.compareTo(SectionName) != 0)
220cdf0e10cSrcweir         {
221cdf0e10cSrcweir             xSectionName.setName(SectionName);
222cdf0e10cSrcweir         }
223cdf0e10cSrcweir     }
224cdf0e10cSrcweir 
insertTextSection(String GroupName, String TemplateName, boolean _bAddParagraph)225cdf0e10cSrcweir     public void insertTextSection(String GroupName, String TemplateName, boolean _bAddParagraph)
226cdf0e10cSrcweir     {
227cdf0e10cSrcweir         try
228cdf0e10cSrcweir         {
229cdf0e10cSrcweir             if (_bAddParagraph)
230cdf0e10cSrcweir             {
231cdf0e10cSrcweir                 XTextCursor xTextCursor = xText.createTextCursor();
232cdf0e10cSrcweir                 xText.insertControlCharacter(xTextCursor, ControlCharacter.PARAGRAPH_BREAK, false);
233cdf0e10cSrcweir                 //		Helper.setUnoPropertyValue(xTextCursor, "PageDescName", "First Page");
234cdf0e10cSrcweir                 xTextCursor.collapseToEnd();
235cdf0e10cSrcweir             }
236cdf0e10cSrcweir             XTextCursor xSecondTextCursor = xText.createTextCursor();
237cdf0e10cSrcweir             xSecondTextCursor.gotoEnd(false);
238cdf0e10cSrcweir             insertTextSection(GroupName, TemplateName, xSecondTextCursor);
239cdf0e10cSrcweir         }
240cdf0e10cSrcweir         catch (IllegalArgumentException e)
241cdf0e10cSrcweir         {
242cdf0e10cSrcweir             e.printStackTrace(System.out);
243cdf0e10cSrcweir         }
244cdf0e10cSrcweir     }
245cdf0e10cSrcweir 
insertTextSection(String sectionName, String templateName, XTextCursor position)246cdf0e10cSrcweir     public void insertTextSection(String sectionName, String templateName, XTextCursor position)
247cdf0e10cSrcweir     {
248cdf0e10cSrcweir         try
249cdf0e10cSrcweir         {
250cdf0e10cSrcweir             Object xTextSection;
251cdf0e10cSrcweir             if (xTextSectionsSupplier.getTextSections().hasByName(sectionName))
252cdf0e10cSrcweir             {
253cdf0e10cSrcweir                 xTextSection = xTextSectionsSupplier.getTextSections().getByName(sectionName);
254cdf0e10cSrcweir             }
255cdf0e10cSrcweir             else
256cdf0e10cSrcweir             {
257cdf0e10cSrcweir                 xTextSection = xMSFDoc.createInstance("com.sun.star.text.TextSection");
258cdf0e10cSrcweir                 XTextContent xTextContentSection = UnoRuntime.queryInterface(XTextContent.class, xTextSection);
259cdf0e10cSrcweir                 position.getText().insertTextContent(position, xTextContentSection, false);
260cdf0e10cSrcweir             }
261cdf0e10cSrcweir             linkSectiontoTemplate(xTextSection, templateName, sectionName);
262cdf0e10cSrcweir         }
263cdf0e10cSrcweir         catch (Exception exception)
264cdf0e10cSrcweir         {
265cdf0e10cSrcweir             exception.printStackTrace(System.out);
266cdf0e10cSrcweir         }
267cdf0e10cSrcweir     }
268cdf0e10cSrcweir }
269