1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 package com.sun.star.wizards.text; 28 29 import com.sun.star.beans.XPropertySet; 30 import com.sun.star.container.XIndexAccess; 31 import com.sun.star.container.XNameAccess; 32 import com.sun.star.container.XNamed; 33 import com.sun.star.lang.IllegalArgumentException; 34 import com.sun.star.lang.XMultiServiceFactory; 35 import com.sun.star.text.ControlCharacter; 36 import com.sun.star.text.SectionFileLink; 37 import com.sun.star.text.XText; 38 import com.sun.star.text.XTextContent; 39 import com.sun.star.text.XTextCursor; 40 import com.sun.star.text.XTextDocument; 41 import com.sun.star.text.XTextSectionsSupplier; 42 import com.sun.star.uno.AnyConverter; 43 import com.sun.star.uno.Exception; 44 import com.sun.star.uno.UnoRuntime; 45 import com.sun.star.wizards.common.Helper; 46 import com.sun.star.wizards.common.PropertyNames; 47 48 public class TextSectionHandler 49 { 50 51 public XTextSectionsSupplier xTextSectionsSupplier; 52 private XMultiServiceFactory xMSFDoc; 53 private XTextDocument xTextDocument; 54 private XText xText; 55 56 /** Creates a new instance of TextSectionHandler */ 57 public TextSectionHandler(XMultiServiceFactory xMSF, XTextDocument xTextDocument) 58 { 59 this.xMSFDoc = xMSF; 60 this.xTextDocument = xTextDocument; 61 xText = xTextDocument.getText(); 62 xTextSectionsSupplier = UnoRuntime.queryInterface(XTextSectionsSupplier.class, xTextDocument); 63 } 64 65 public void removeTextSectionbyName(String SectionName) 66 { 67 try 68 { 69 XNameAccess xAllTextSections = xTextSectionsSupplier.getTextSections(); 70 if (xAllTextSections.hasByName(SectionName)) 71 { 72 Object oTextSection = xTextSectionsSupplier.getTextSections().getByName(SectionName); 73 removeTextSection(oTextSection); 74 } 75 } 76 catch (Exception exception) 77 { 78 exception.printStackTrace(System.out); 79 } 80 } 81 82 public boolean hasTextSectionByName(String SectionName) 83 { 84 com.sun.star.container.XNameAccess xAllTextSections = xTextSectionsSupplier.getTextSections(); 85 return xAllTextSections.hasByName(SectionName); 86 } 87 88 public void removeLastTextSection() 89 { 90 try 91 { 92 XIndexAccess xAllTextSections = UnoRuntime.queryInterface(XIndexAccess.class, xTextSectionsSupplier.getTextSections()); 93 Object oTextSection = xAllTextSections.getByIndex(xAllTextSections.getCount() - 1); 94 removeTextSection(oTextSection); 95 } 96 catch (Exception exception) 97 { 98 exception.printStackTrace(System.out); 99 } 100 } 101 102 public void removeTextSection(Object _oTextSection) 103 { 104 try 105 { 106 XTextContent xTextContentTextSection = UnoRuntime.queryInterface(XTextContent.class, _oTextSection); 107 xText.removeTextContent(xTextContentTextSection); 108 } 109 catch (Exception exception) 110 { 111 exception.printStackTrace(System.out); 112 } 113 } 114 115 public void removeInvisibleTextSections() 116 { 117 try 118 { 119 XIndexAccess xAllTextSections = UnoRuntime.queryInterface(XIndexAccess.class, xTextSectionsSupplier.getTextSections()); 120 int TextSectionCount = xAllTextSections.getCount(); 121 for (int i = TextSectionCount - 1; i >= 0; i--) 122 { 123 XTextContent xTextContentTextSection = UnoRuntime.queryInterface(XTextContent.class, xAllTextSections.getByIndex(i)); 124 XPropertySet xTextSectionPropertySet = UnoRuntime.queryInterface(XPropertySet.class, xTextContentTextSection); 125 boolean bRemoveTextSection = (!AnyConverter.toBoolean(xTextSectionPropertySet.getPropertyValue("IsVisible"))); 126 if (bRemoveTextSection) 127 { 128 xText.removeTextContent(xTextContentTextSection); 129 } 130 } 131 } 132 catch (Exception exception) 133 { 134 exception.printStackTrace(System.out); 135 } 136 } 137 138 public void removeAllTextSections() 139 { 140 try 141 { 142 XIndexAccess xAllTextSections = UnoRuntime.queryInterface(XIndexAccess.class, xTextSectionsSupplier.getTextSections()); 143 int TextSectionCount = xAllTextSections.getCount(); 144 for (int i = TextSectionCount - 1; i >= 0; i--) 145 { 146 XTextContent xTextContentTextSection = UnoRuntime.queryInterface(XTextContent.class, xAllTextSections.getByIndex(i)); 147 XPropertySet xTextSectionPropertySet = UnoRuntime.queryInterface(XPropertySet.class, xTextContentTextSection); 148 xText.removeTextContent(xTextContentTextSection); 149 } 150 } 151 catch (Exception exception) 152 { 153 exception.printStackTrace(System.out); 154 } 155 } 156 157 public void breakLinkofTextSections() 158 { 159 try 160 { 161 Object oTextSection; 162 XIndexAccess xAllTextSections = UnoRuntime.queryInterface(XIndexAccess.class, xTextSectionsSupplier.getTextSections()); 163 int iSectionCount = xAllTextSections.getCount(); 164 SectionFileLink oSectionLink = new SectionFileLink(); 165 oSectionLink.FileURL = PropertyNames.EMPTY_STRING; 166 for (int i = 0; i < iSectionCount; i++) 167 { 168 oTextSection = xAllTextSections.getByIndex(i); 169 Helper.setUnoPropertyValues(oTextSection, new String[] 170 { 171 "FileLink", "LinkRegion" 172 }, new Object[] 173 { 174 oSectionLink, PropertyNames.EMPTY_STRING 175 }); 176 } 177 } 178 catch (Exception exception) 179 { 180 exception.printStackTrace(System.out); 181 } 182 } 183 184 public void breakLinkOfTextSection(Object oTextSection) 185 { 186 SectionFileLink oSectionLink = new SectionFileLink(); 187 oSectionLink.FileURL = PropertyNames.EMPTY_STRING; 188 Helper.setUnoPropertyValues(oTextSection, new String[] 189 { 190 "FileLink", "LinkRegion" 191 }, new Object[] 192 { 193 oSectionLink, PropertyNames.EMPTY_STRING 194 }); 195 } 196 197 public void linkSectiontoTemplate(String TemplateName, String SectionName) 198 { 199 try 200 { 201 Object oTextSection = xTextSectionsSupplier.getTextSections().getByName(SectionName); 202 linkSectiontoTemplate(oTextSection, TemplateName, SectionName); 203 } 204 catch (Exception e) 205 { 206 e.printStackTrace(System.out); 207 } 208 } 209 210 public void linkSectiontoTemplate(Object oTextSection, String TemplateName, String SectionName) 211 { 212 SectionFileLink oSectionLink = new SectionFileLink(); 213 oSectionLink.FileURL = TemplateName; 214 Helper.setUnoPropertyValues(oTextSection, new String[] 215 { 216 "FileLink", "LinkRegion" 217 }, new Object[] 218 { 219 oSectionLink, SectionName 220 }); 221 XNamed xSectionName = UnoRuntime.queryInterface(XNamed.class, oTextSection); 222 String NewSectionName = xSectionName.getName(); 223 if (NewSectionName.compareTo(SectionName) != 0) 224 { 225 xSectionName.setName(SectionName); 226 } 227 } 228 229 public void insertTextSection(String GroupName, String TemplateName, boolean _bAddParagraph) 230 { 231 try 232 { 233 if (_bAddParagraph) 234 { 235 XTextCursor xTextCursor = xText.createTextCursor(); 236 xText.insertControlCharacter(xTextCursor, ControlCharacter.PARAGRAPH_BREAK, false); 237 // Helper.setUnoPropertyValue(xTextCursor, "PageDescName", "First Page"); 238 xTextCursor.collapseToEnd(); 239 } 240 XTextCursor xSecondTextCursor = xText.createTextCursor(); 241 xSecondTextCursor.gotoEnd(false); 242 insertTextSection(GroupName, TemplateName, xSecondTextCursor); 243 } 244 catch (IllegalArgumentException e) 245 { 246 e.printStackTrace(System.out); 247 } 248 } 249 250 public void insertTextSection(String sectionName, String templateName, XTextCursor position) 251 { 252 try 253 { 254 Object xTextSection; 255 if (xTextSectionsSupplier.getTextSections().hasByName(sectionName)) 256 { 257 xTextSection = xTextSectionsSupplier.getTextSections().getByName(sectionName); 258 } 259 else 260 { 261 xTextSection = xMSFDoc.createInstance("com.sun.star.text.TextSection"); 262 XTextContent xTextContentSection = UnoRuntime.queryInterface(XTextContent.class, xTextSection); 263 position.getText().insertTextContent(position, xTextContentSection, false); 264 } 265 linkSectiontoTemplate(xTextSection, templateName, sectionName); 266 } 267 catch (Exception exception) 268 { 269 exception.printStackTrace(System.out); 270 } 271 } 272 } 273