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.text;
24 
25 import com.sun.star.beans.XPropertySet;
26 import com.sun.star.container.XIndexAccess;
27 import com.sun.star.container.XNameAccess;
28 import com.sun.star.container.XNamed;
29 import com.sun.star.lang.IllegalArgumentException;
30 import com.sun.star.lang.XMultiServiceFactory;
31 import com.sun.star.text.ControlCharacter;
32 import com.sun.star.text.SectionFileLink;
33 import com.sun.star.text.XText;
34 import com.sun.star.text.XTextContent;
35 import com.sun.star.text.XTextCursor;
36 import com.sun.star.text.XTextDocument;
37 import com.sun.star.text.XTextSectionsSupplier;
38 import com.sun.star.uno.AnyConverter;
39 import com.sun.star.uno.Exception;
40 import com.sun.star.uno.UnoRuntime;
41 import com.sun.star.wizards.common.Helper;
42 import com.sun.star.wizards.common.PropertyNames;
43 
44 public class TextSectionHandler
45 {
46 
47     public XTextSectionsSupplier xTextSectionsSupplier;
48     private XMultiServiceFactory xMSFDoc;
49     private XTextDocument xTextDocument;
50     private XText xText;
51 
52     /** Creates a new instance of TextSectionHandler */
TextSectionHandler(XMultiServiceFactory xMSF, XTextDocument xTextDocument)53     public TextSectionHandler(XMultiServiceFactory xMSF, XTextDocument xTextDocument)
54     {
55         this.xMSFDoc = xMSF;
56         this.xTextDocument = xTextDocument;
57         xText = xTextDocument.getText();
58         xTextSectionsSupplier = UnoRuntime.queryInterface(XTextSectionsSupplier.class, xTextDocument);
59     }
60 
removeTextSectionbyName(String SectionName)61     public void removeTextSectionbyName(String SectionName)
62     {
63         try
64         {
65             XNameAccess xAllTextSections = xTextSectionsSupplier.getTextSections();
66             if (xAllTextSections.hasByName(SectionName))
67             {
68                 Object oTextSection = xTextSectionsSupplier.getTextSections().getByName(SectionName);
69                 removeTextSection(oTextSection);
70             }
71         }
72         catch (Exception exception)
73         {
74             exception.printStackTrace(System.out);
75         }
76     }
77 
hasTextSectionByName(String SectionName)78     public boolean hasTextSectionByName(String SectionName)
79     {
80         com.sun.star.container.XNameAccess xAllTextSections = xTextSectionsSupplier.getTextSections();
81         return xAllTextSections.hasByName(SectionName);
82     }
83 
removeLastTextSection()84     public void removeLastTextSection()
85     {
86         try
87         {
88             XIndexAccess xAllTextSections = UnoRuntime.queryInterface(XIndexAccess.class, xTextSectionsSupplier.getTextSections());
89             Object oTextSection = xAllTextSections.getByIndex(xAllTextSections.getCount() - 1);
90             removeTextSection(oTextSection);
91         }
92         catch (Exception exception)
93         {
94             exception.printStackTrace(System.out);
95         }
96     }
97 
removeTextSection(Object _oTextSection)98     public void removeTextSection(Object _oTextSection)
99     {
100         try
101         {
102             XTextContent xTextContentTextSection = UnoRuntime.queryInterface(XTextContent.class, _oTextSection);
103             xText.removeTextContent(xTextContentTextSection);
104         }
105         catch (Exception exception)
106         {
107             exception.printStackTrace(System.out);
108         }
109     }
110 
removeInvisibleTextSections()111     public void removeInvisibleTextSections()
112     {
113         try
114         {
115             XIndexAccess xAllTextSections = UnoRuntime.queryInterface(XIndexAccess.class, xTextSectionsSupplier.getTextSections());
116             int TextSectionCount = xAllTextSections.getCount();
117             for (int i = TextSectionCount - 1; i >= 0; i--)
118             {
119                 XTextContent xTextContentTextSection = UnoRuntime.queryInterface(XTextContent.class, xAllTextSections.getByIndex(i));
120                 XPropertySet xTextSectionPropertySet = UnoRuntime.queryInterface(XPropertySet.class, xTextContentTextSection);
121                 boolean bRemoveTextSection = (!AnyConverter.toBoolean(xTextSectionPropertySet.getPropertyValue("IsVisible")));
122                 if (bRemoveTextSection)
123                 {
124                     xText.removeTextContent(xTextContentTextSection);
125                 }
126             }
127         }
128         catch (Exception exception)
129         {
130             exception.printStackTrace(System.out);
131         }
132     }
133 
removeAllTextSections()134     public void removeAllTextSections()
135     {
136         try
137         {
138             XIndexAccess xAllTextSections = UnoRuntime.queryInterface(XIndexAccess.class, xTextSectionsSupplier.getTextSections());
139             int TextSectionCount = xAllTextSections.getCount();
140             for (int i = TextSectionCount - 1; i >= 0; i--)
141             {
142                 XTextContent xTextContentTextSection = UnoRuntime.queryInterface(XTextContent.class, xAllTextSections.getByIndex(i));
143                 XPropertySet xTextSectionPropertySet = UnoRuntime.queryInterface(XPropertySet.class, xTextContentTextSection);
144                 xText.removeTextContent(xTextContentTextSection);
145             }
146         }
147         catch (Exception exception)
148         {
149             exception.printStackTrace(System.out);
150         }
151     }
152 
breakLinkofTextSections()153     public void breakLinkofTextSections()
154     {
155         try
156         {
157             Object oTextSection;
158             XIndexAccess xAllTextSections = UnoRuntime.queryInterface(XIndexAccess.class, xTextSectionsSupplier.getTextSections());
159             int iSectionCount = xAllTextSections.getCount();
160             SectionFileLink oSectionLink = new SectionFileLink();
161             oSectionLink.FileURL = PropertyNames.EMPTY_STRING;
162             for (int i = 0; i < iSectionCount; i++)
163             {
164                 oTextSection = xAllTextSections.getByIndex(i);
165                 Helper.setUnoPropertyValues(oTextSection, new String[]
166                         {
167                             "FileLink", "LinkRegion"
168                         }, new Object[]
169                         {
170                             oSectionLink, PropertyNames.EMPTY_STRING
171                         });
172             }
173         }
174         catch (Exception exception)
175         {
176             exception.printStackTrace(System.out);
177         }
178     }
179 
breakLinkOfTextSection(Object oTextSection)180     public void breakLinkOfTextSection(Object oTextSection)
181     {
182         SectionFileLink oSectionLink = new SectionFileLink();
183         oSectionLink.FileURL = PropertyNames.EMPTY_STRING;
184         Helper.setUnoPropertyValues(oTextSection, new String[]
185                 {
186                     "FileLink", "LinkRegion"
187                 }, new Object[]
188                 {
189                     oSectionLink, PropertyNames.EMPTY_STRING
190                 });
191     }
192 
linkSectiontoTemplate(String TemplateName, String SectionName)193     public void linkSectiontoTemplate(String TemplateName, String SectionName)
194     {
195         try
196         {
197             Object oTextSection = xTextSectionsSupplier.getTextSections().getByName(SectionName);
198             linkSectiontoTemplate(oTextSection, TemplateName, SectionName);
199         }
200         catch (Exception e)
201         {
202             e.printStackTrace(System.out);
203         }
204     }
205 
linkSectiontoTemplate(Object oTextSection, String TemplateName, String SectionName)206     public void linkSectiontoTemplate(Object oTextSection, String TemplateName, String SectionName)
207     {
208         SectionFileLink oSectionLink = new SectionFileLink();
209         oSectionLink.FileURL = TemplateName;
210         Helper.setUnoPropertyValues(oTextSection, new String[]
211                 {
212                     "FileLink", "LinkRegion"
213                 }, new Object[]
214                 {
215                     oSectionLink, SectionName
216                 });
217         XNamed xSectionName = UnoRuntime.queryInterface(XNamed.class, oTextSection);
218         String NewSectionName = xSectionName.getName();
219         if (NewSectionName.compareTo(SectionName) != 0)
220         {
221             xSectionName.setName(SectionName);
222         }
223     }
224 
insertTextSection(String GroupName, String TemplateName, boolean _bAddParagraph)225     public void insertTextSection(String GroupName, String TemplateName, boolean _bAddParagraph)
226     {
227         try
228         {
229             if (_bAddParagraph)
230             {
231                 XTextCursor xTextCursor = xText.createTextCursor();
232                 xText.insertControlCharacter(xTextCursor, ControlCharacter.PARAGRAPH_BREAK, false);
233                 //		Helper.setUnoPropertyValue(xTextCursor, "PageDescName", "First Page");
234                 xTextCursor.collapseToEnd();
235             }
236             XTextCursor xSecondTextCursor = xText.createTextCursor();
237             xSecondTextCursor.gotoEnd(false);
238             insertTextSection(GroupName, TemplateName, xSecondTextCursor);
239         }
240         catch (IllegalArgumentException e)
241         {
242             e.printStackTrace(System.out);
243         }
244     }
245 
insertTextSection(String sectionName, String templateName, XTextCursor position)246     public void insertTextSection(String sectionName, String templateName, XTextCursor position)
247     {
248         try
249         {
250             Object xTextSection;
251             if (xTextSectionsSupplier.getTextSections().hasByName(sectionName))
252             {
253                 xTextSection = xTextSectionsSupplier.getTextSections().getByName(sectionName);
254             }
255             else
256             {
257                 xTextSection = xMSFDoc.createInstance("com.sun.star.text.TextSection");
258                 XTextContent xTextContentSection = UnoRuntime.queryInterface(XTextContent.class, xTextSection);
259                 position.getText().insertTextContent(position, xTextContentSection, false);
260             }
261             linkSectiontoTemplate(xTextSection, templateName, sectionName);
262         }
263         catch (Exception exception)
264         {
265             exception.printStackTrace(System.out);
266         }
267     }
268 }
269