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.container.XIndexAccess;
26 import com.sun.star.container.XNameAccess;
27 import com.sun.star.container.XNamed;
28 import com.sun.star.frame.XFrame;
29 import com.sun.star.lang.Locale;
30 import com.sun.star.lang.XMultiServiceFactory;
31 import com.sun.star.style.BreakType;
32 import com.sun.star.table.XCellRange;
33 import com.sun.star.text.XSimpleText;
34 import com.sun.star.text.XText;
35 import com.sun.star.text.XTextContent;
36 import com.sun.star.text.XTextDocument;
37 import com.sun.star.text.XTextTable;
38 import com.sun.star.text.XTextTablesSupplier;
39 import com.sun.star.uno.AnyConverter;
40 import com.sun.star.uno.Exception;
41 import com.sun.star.uno.UnoRuntime;
42 import com.sun.star.uno.XInterface;
43 import com.sun.star.util.XNumberFormatsSupplier;
44 import com.sun.star.view.XSelectionSupplier;
45 import com.sun.star.wizards.common.Desktop;
46 import com.sun.star.wizards.common.Helper;
47 import com.sun.star.wizards.common.NumberFormatter;
48 
49 public class TextTableHandler
50 {
51 
52     public XTextTablesSupplier xTextTablesSupplier;
53     public XMultiServiceFactory xMSFDoc;
54     public XTextDocument xTextDocument;
55     public XSimpleText xSimpleText;
56     private XText xText;
57     private NumberFormatter oNumberFormatter;
58     private Locale aCharLocale;
59 
60     /** Creates a new instance of TextTableHandler */
TextTableHandler(XMultiServiceFactory xMSF, XTextDocument xTextDocument)61     public TextTableHandler(XMultiServiceFactory xMSF, XTextDocument xTextDocument)
62     {
63         try
64         {
65             this.xMSFDoc = xMSF;
66             this.xTextDocument = xTextDocument;
67             xText = xTextDocument.getText();
68             xTextTablesSupplier = UnoRuntime.queryInterface(XTextTablesSupplier.class, xTextDocument);
69             xSimpleText = UnoRuntime.queryInterface(XSimpleText.class, xTextDocument.getText());
70             XNumberFormatsSupplier xNumberFormatsSupplier = UnoRuntime.queryInterface(XNumberFormatsSupplier.class, xTextDocument);
71             aCharLocale = (Locale) Helper.getUnoStructValue((Object) xTextDocument, "CharLocale");
72             oNumberFormatter = new NumberFormatter(xNumberFormatsSupplier, aCharLocale);
73         }
74         catch (java.lang.Exception e)
75         {
76             e.printStackTrace(System.out);
77         }
78     }
79 
getNumberFormatter()80     public NumberFormatter getNumberFormatter()
81     {
82         return oNumberFormatter;
83     }
84 
getByName(String _sTableName)85     public XTextTable getByName(String _sTableName)
86     {
87         XTextTable xTextTable = null;
88         try
89         {
90             XNameAccess xAllTextTables = xTextTablesSupplier.getTextTables();
91             if (xAllTextTables.hasByName(_sTableName))
92             {
93                 Object oTable = xAllTextTables.getByName(_sTableName);
94                 xTextTable = UnoRuntime.queryInterface(XTextTable.class, oTable);
95             }
96         }
97         catch (Exception exception)
98         {
99             exception.printStackTrace(System.out);
100         }
101         return xTextTable;
102     }
103 
getlastTextTable()104     public com.sun.star.text.XTextTable getlastTextTable()
105     {
106         try
107         {
108             XIndexAccess xAllTextTables = UnoRuntime.queryInterface(XIndexAccess.class, xTextTablesSupplier.getTextTables());
109             int MaxIndex = xAllTextTables.getCount() - 1;
110             Object oTable = xAllTextTables.getByIndex(MaxIndex);
111             return UnoRuntime.queryInterface(XTextTable.class, oTable);
112         }
113         catch (Exception exception)
114         {
115             exception.printStackTrace(System.out);
116             return null;
117         }
118     }
119 
insertTextTable(com.sun.star.text.XTextCursor xTextCursor)120     public void insertTextTable(com.sun.star.text.XTextCursor xTextCursor)
121     {
122         try
123         {
124             com.sun.star.uno.XInterface xTextTable = (XInterface) xMSFDoc.createInstance("com.sun.star.text.TextTable");
125             XTextContent xTextContentTable = UnoRuntime.queryInterface(XTextContent.class, xTextTable);
126             if (xTextCursor == null)
127             {
128                 xTextCursor = xTextDocument.getText().createTextCursor();
129                 xTextCursor.gotoEnd(false);
130             }
131             xTextCursor.getText().insertTextContent(xTextCursor, xTextContentTable, false);
132         }
133         catch (Exception exception)
134         {
135             exception.printStackTrace(System.out);
136         }
137     }
138 
removeAllTextTables()139     public void removeAllTextTables()
140     {
141         try
142         {
143             XIndexAccess xAllTextTables = UnoRuntime.queryInterface(XIndexAccess.class, xTextTablesSupplier.getTextTables());
144             int TextTableCount = xAllTextTables.getCount();
145             for (int i = TextTableCount - 1; i >= 0; i--)
146             {
147                 removeTextTable(xAllTextTables.getByIndex(i));
148             }
149         }
150         catch (Exception exception)
151         {
152             exception.printStackTrace(System.out);
153         }
154     }
155 
removeLastTextTable()156     public void removeLastTextTable()
157     {
158         try
159         {
160             XIndexAccess xAllTextTables = UnoRuntime.queryInterface(XIndexAccess.class, xTextTablesSupplier.getTextTables());
161             Object oTextTable = xAllTextTables.getByIndex(xAllTextTables.getCount() - 1);
162             removeTextTable(oTextTable);
163         }
164         catch (Exception exception)
165         {
166             exception.printStackTrace(System.out);
167         }
168     }
169 
removeTextTable(Object oTextTable)170     public void removeTextTable(Object oTextTable)
171     {
172         try
173         {
174             XTextContent xTextContentTable = UnoRuntime.queryInterface(XTextContent.class, oTextTable);
175             xTextDocument.getText().removeTextContent(xTextContentTable);
176         }
177         catch (Exception exception)
178         {
179             exception.printStackTrace(System.out);
180         }
181     }
182 
removeTextTablebyName(String TableName)183     public void removeTextTablebyName(String TableName)
184     {
185         try
186         {
187             XNameAccess xAllTextTables = xTextTablesSupplier.getTextTables();
188             if (xAllTextTables.hasByName(TableName))
189             {
190                 removeTextTable(xAllTextTables.getByName(TableName));
191             }
192         }
193         catch (Exception exception)
194         {
195             exception.printStackTrace(System.out);
196         }
197     }
198 
renameTextTable(String OldTableName, String NewTableName)199     public void renameTextTable(String OldTableName, String NewTableName)
200     {
201         try
202         {
203             XNameAccess xTextTableNames = xTextTablesSupplier.getTextTables();
204             if (xTextTableNames.hasByName(OldTableName))
205             {
206                 Object oTextTable = xTextTableNames.getByName(OldTableName);
207                 XNamed xTextTableName = UnoRuntime.queryInterface(XNamed.class, oTextTable);
208                 xTextTableName.setName(NewTableName);
209             }
210         }
211         catch (Exception exception)
212         {
213             exception.printStackTrace(System.out);
214         }
215     }
216 
resetBreakTypeofTextTable(Object oTextTable)217     public static BreakType resetBreakTypeofTextTable(Object oTextTable)
218     {
219         BreakType CorrBreakValue = null;
220         BreakType BreakValue = (BreakType) com.sun.star.wizards.common.Helper.getUnoStructValue(oTextTable, "BreakType");
221         //  if (BreakValue.equals(BreakType.NONE) == false){
222         //      CorrBreakValue = BreakValue;
223         Helper.setUnoPropertyValue(oTextTable, "BreakType", BreakType.NONE);
224         //  }
225         return BreakType.NONE;
226     }
227 
adjustOptimalTableWidths(XMultiServiceFactory _xMSF, XTextTable xTextTable)228     public void adjustOptimalTableWidths(XMultiServiceFactory _xMSF, XTextTable xTextTable)
229     {        // setTableColumnSeparators(){
230         try
231         {
232             XFrame xFrame = this.xTextDocument.getCurrentController().getFrame();
233             int ColCount = xTextTable.getColumns().getCount();
234             XCellRange xCellRange = UnoRuntime.queryInterface(XCellRange.class, xTextTable);
235             XCellRange xLocCellRange = xCellRange.getCellRangeByPosition(0, 0, ColCount - 1, 1);
236             short iHoriOrient = AnyConverter.toShort(Helper.getUnoPropertyValue(xTextTable, "HoriOrient"));
237             XSelectionSupplier xSelection = UnoRuntime.queryInterface(XSelectionSupplier.class, xTextDocument.getCurrentController());
238             xSelection.select(xLocCellRange);
239             Desktop.dispatchURL(_xMSF, ".Uno:DistributeColumns", xFrame);
240             Desktop.dispatchURL(_xMSF, ".Uno:SetOptimalColumnWidth", xFrame);
241             Helper.setUnoPropertyValue(xTextTable, "HoriOrient", new Short(iHoriOrient));
242         }
243         catch (Exception exception)
244         {
245             exception.printStackTrace(System.out);
246         }
247     }
248 }
249