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 
24 import com.sun.star.lang.XComponent;
25 import com.sun.star.uno.XComponentContext;
26 import com.sun.star.uno.UnoRuntime;
27 import com.sun.star.uno.AnyConverter;
28 import com.sun.star.bridge.XUnoUrlResolver;
29 import com.sun.star.frame.XComponentLoader;
30 import com.sun.star.lang.XMultiComponentFactory;
31 import com.sun.star.lang.XMultiServiceFactory;
32 import com.sun.star.lang.XServiceInfo;
33 import com.sun.star.beans.XPropertySet;
34 import com.sun.star.beans.XPropertySetInfo;
35 import com.sun.star.beans.PropertyValue;
36 import com.sun.star.beans.UnknownPropertyException;
37 import com.sun.star.beans.PropertyVetoException;
38 import com.sun.star.text.XTextDocument;
39 import com.sun.star.text.XText;
40 import com.sun.star.text.XTextCursor;
41 import com.sun.star.text.XWordCursor;
42 import com.sun.star.text.XTextContent;
43 import com.sun.star.text.XTextTable;
44 import com.sun.star.text.XTextTableCursor;
45 import com.sun.star.table.XTableRows;
46 import com.sun.star.table.XCellRange;
47 import com.sun.star.table.XCell;
48 import com.sun.star.table.XCellCursor;
49 import com.sun.star.table.TableBorder;
50 import com.sun.star.table.BorderLine;
51 import com.sun.star.drawing.XShape;
52 import com.sun.star.awt.Size;
53 import com.sun.star.awt.Point;
54 import com.sun.star.sheet.XSpreadsheetDocument;
55 import com.sun.star.sheet.XSpreadsheet;
56 import com.sun.star.sheet.XSheetCellCursor;
57 import com.sun.star.container.XIndexAccess;
58 import com.sun.star.drawing.XDrawPagesSupplier;
59 import com.sun.star.drawing.XDrawPageSupplier;
60 import com.sun.star.drawing.XDrawPage;
61 
62 import com.sun.star.text.XTextTablesSupplier;
63 import com.sun.star.container.XNameAccess;
64 import com.sun.star.container.XNamed;
65 import com.sun.star.text.XBookmarksSupplier;
66 import com.sun.star.text.XTextRange;
67 
68 /**
69  *
70  * @author  dschulten
71  */
72 public class HelloTextTableShape {
73 
74     private XComponentContext xRemoteContext = null;
75     private XMultiComponentFactory xRemoteServiceManager = null;
76 
77     /** Creates a new instance of HelloTextTableShape */
HelloTextTableShape()78     public HelloTextTableShape() {
79     }
80 
81     /**
82      * @param args the command line arguments
83      */
main(String[] args)84     public static void main(String[] args) {
85         HelloTextTableShape helloTextTableShape1 = new HelloTextTableShape();
86         try {
87             helloTextTableShape1.useDocuments();
88         }
89         catch (java.lang.Exception e){
90             System.err.println(e.getMessage());
91             e.printStackTrace();
92         }
93         finally {
94             System.exit(0);
95         }
96 
97     }
98 
useDocuments()99     protected void useDocuments() throws java.lang.Exception  {
100         useWriter();
101         useCalc();
102         useDraw();
103     }
104 
useWriter()105     protected void useWriter() throws java.lang.Exception {
106         try {
107             // create new writer document and get text, then manipulate text
108             XComponent xWriterComponent = newDocComponent("swriter");
109             XTextDocument xTextDocument = (XTextDocument)UnoRuntime.queryInterface(
110                 XTextDocument.class, xWriterComponent);
111             XText xText = xTextDocument.getText();
112 
113             manipulateText(xText);
114 
115             // get internal service factory of the document
116             XMultiServiceFactory xWriterFactory = (XMultiServiceFactory)UnoRuntime.queryInterface(
117                 XMultiServiceFactory.class, xWriterComponent);
118 
119             // insert TextTable and get cell text, then manipulate text in cell
120             Object table = xWriterFactory.createInstance("com.sun.star.text.TextTable");
121             XTextContent xTextContentTable = (XTextContent)UnoRuntime.queryInterface(
122                 XTextContent.class, table);
123 
124             xText.insertTextContent(xText.getEnd(), xTextContentTable, false);
125 
126             XCellRange xCellRange = (XCellRange)UnoRuntime.queryInterface(
127                 XCellRange.class, table);
128             XCell xCell = xCellRange.getCellByPosition(0, 1);
129             XText xCellText = (XText)UnoRuntime.queryInterface(XText.class, xCell);
130 
131             manipulateText(xCellText);
132             manipulateTable(xCellRange);
133 
134             // insert RectangleShape and get shape text, then manipulate text
135             Object writerShape = xWriterFactory.createInstance(
136                 "com.sun.star.drawing.RectangleShape");
137             XShape xWriterShape = (XShape)UnoRuntime.queryInterface(
138                 XShape.class, writerShape);
139             xWriterShape.setSize(new Size(10000, 10000));
140             XTextContent xTextContentShape = (XTextContent)UnoRuntime.queryInterface(
141                 XTextContent.class, writerShape);
142 
143             xText.insertTextContent(xText.getEnd(), xTextContentShape, false);
144 
145             XPropertySet xShapeProps = (XPropertySet)UnoRuntime.queryInterface(
146                 XPropertySet.class, writerShape);
147             // wrap text inside shape
148             xShapeProps.setPropertyValue("TextContourFrame", new Boolean(true));
149 
150 
151             XText xShapeText = (XText)UnoRuntime.queryInterface(XText.class, writerShape);
152 
153             manipulateText(xShapeText);
154             manipulateShape(xWriterShape);
155 
156 /* more code snippets used in the manual:
157 
158             Object bookmark = xWriterFactory.createInstance ( "com.sun.star.text.Bookmark" );
159             // name the bookmark
160             XNamed xNamed = (XNamed) UnoRuntime.queryInterface (
161                     XNamed.class, bookmark );
162             xNamed.setName("MyUniqueBookmarkName");
163 
164             // get XTextContent interface and insert it at the end of the document
165             XTextContent xTextContent = (XTextContent) UnoRuntime.queryInterface (
166                     XTextContent.class, bookmark );
167             //mxDocText.insertTextContent ( mxDocText.getEnd(), xTextContent, false );
168             xText.insertTextContent ( xText.getEnd(), xTextContent, false );
169 
170                         //query BookmarksSupplier
171                         XBookmarksSupplier xBookmarksSupplier = (XBookmarksSupplier)UnoRuntime.queryInterface(
172                             XBookmarksSupplier.class, xWriterComponent);
173                         XNameAccess xNamedBookmarks = xBookmarksSupplier.getBookmarks();
174                         Object foundBookmark = xNamedBookmarks.getByName("MyUniqueBookmarkName");
175                         XTextContent xFoundBookmark = (XTextContent)UnoRuntime.queryInterface(XTextContent.class, foundBookmark);
176                         XTextRange xFound = xFoundBookmark.getAnchor();
177                         xFound.setString(" The throat mike, glued to her neck, "
178                             + "looked as much as possible like an analgesic dermadisk.");
179 
180 
181 
182 
183             // first query the XTextTablesSupplier interface from our document
184             XTextTablesSupplier xTablesSupplier = (XTextTablesSupplier) UnoRuntime.queryInterface(
185                     XTextTablesSupplier.class, xWriterComponent);
186             // get the tables collection
187             XNameAccess xNamedTables = xTablesSupplier.getTextTables();
188 
189             // now query the XIndexAccess from the tables collection
190             XIndexAccess xIndexedTables = (XIndexAccess) UnoRuntime.queryInterface(
191                     XIndexAccess.class, xNamedTables);
192 
193             // we need properties
194             XPropertySet xTableProps = null;
195 
196             // get the tables
197             for (int i = 0; i < xIndexedTables.getCount(); i++) {
198                     //Object table = xIndexedTables.getByIndex(i);
199                     table = xIndexedTables.getByIndex(i);
200                     xTableProps = (XPropertySet) UnoRuntime.queryInterface(
201                             XPropertySet.class, table);
202                     xTableProps.setPropertyValue("BackColor", new Integer(0xC8FFB9));
203             }
204  */
205         }
206         catch( com.sun.star.lang.DisposedException e ) { //works from Patch 1
207             xRemoteContext = null;
208             throw e;
209         }
210 
211     }
212 
useCalc()213     protected void useCalc() throws java.lang.Exception {
214         try {
215             // create new calc document and manipulate cell text
216             XComponent xCalcComponent = newDocComponent("scalc");
217             XSpreadsheetDocument  xSpreadsheetDocument  =
218                 (XSpreadsheetDocument)UnoRuntime.queryInterface(
219                     XSpreadsheetDocument .class, xCalcComponent);
220             Object sheets = xSpreadsheetDocument.getSheets();
221             XIndexAccess xIndexedSheets = (XIndexAccess)UnoRuntime.queryInterface(
222                 XIndexAccess.class, sheets);
223             Object sheet =  xIndexedSheets.getByIndex(0);
224 
225             //get cell A2 in first sheet
226             XCellRange xSpreadsheetCells = (XCellRange)UnoRuntime.queryInterface(
227                 XCellRange.class, sheet);
228             XCell xCell = xSpreadsheetCells.getCellByPosition(0,1);
229             XPropertySet xCellProps = (XPropertySet)UnoRuntime.queryInterface(
230                 XPropertySet.class, xCell);
231             xCellProps.setPropertyValue("IsTextWrapped", new Boolean(true));
232 
233             XText xCellText = (XText)UnoRuntime.queryInterface(XText.class, xCell);
234 
235             manipulateText(xCellText);
236             manipulateTable(xSpreadsheetCells);
237 
238             // get internal service factory of the document
239             XMultiServiceFactory xCalcFactory = (XMultiServiceFactory)UnoRuntime.queryInterface(
240                 XMultiServiceFactory.class, xCalcComponent);
241             // get Drawpage
242             XDrawPageSupplier xDrawPageSupplier = (XDrawPageSupplier)UnoRuntime.queryInterface(XDrawPageSupplier.class, sheet);
243             XDrawPage xDrawPage = xDrawPageSupplier.getDrawPage();
244 
245             // create and insert RectangleShape and get shape text, then manipulate text
246             Object calcShape = xCalcFactory.createInstance(
247                 "com.sun.star.drawing.RectangleShape");
248             XShape xCalcShape = (XShape)UnoRuntime.queryInterface(
249                 XShape.class, calcShape);
250             xCalcShape.setSize(new Size(10000, 10000));
251             xCalcShape.setPosition(new Point(7000, 3000));
252 
253             xDrawPage.add(xCalcShape);
254 
255             XPropertySet xShapeProps = (XPropertySet)UnoRuntime.queryInterface(
256                 XPropertySet.class, calcShape);
257             // wrap text inside shape
258             xShapeProps.setPropertyValue("TextContourFrame", new Boolean(true));
259 
260 
261             XText xShapeText = (XText)UnoRuntime.queryInterface(XText.class, calcShape);
262 
263             manipulateText(xShapeText);
264             manipulateShape(xCalcShape);
265 
266         }
267         catch( com.sun.star.lang.DisposedException e ) { //works from Patch 1
268             xRemoteContext = null;
269             throw e;
270         }
271 
272     }
273 
useDraw()274     protected void useDraw() throws java.lang.Exception {
275         try {
276             //create new draw document and insert ractangle shape
277             XComponent xDrawComponent = newDocComponent("sdraw");
278             XDrawPagesSupplier xDrawPagesSupplier =
279                 (XDrawPagesSupplier)UnoRuntime.queryInterface(
280                     XDrawPagesSupplier.class, xDrawComponent);
281 
282             Object drawPages = xDrawPagesSupplier.getDrawPages();
283             XIndexAccess xIndexedDrawPages = (XIndexAccess)UnoRuntime.queryInterface(
284                 XIndexAccess.class, drawPages);
285             Object drawPage = xIndexedDrawPages.getByIndex(0);
286             XDrawPage xDrawPage = (XDrawPage)UnoRuntime.queryInterface(XDrawPage.class, drawPage);
287 
288             // get internal service factory of the document
289             XMultiServiceFactory xDrawFactory =
290                 (XMultiServiceFactory)UnoRuntime.queryInterface(
291                     XMultiServiceFactory.class, xDrawComponent);
292 
293             Object drawShape = xDrawFactory.createInstance(
294                 "com.sun.star.drawing.RectangleShape");
295             XShape xDrawShape = (XShape)UnoRuntime.queryInterface(XShape.class, drawShape);
296             xDrawShape.setSize(new Size(10000, 20000));
297             xDrawShape.setPosition(new Point(5000, 5000));
298             xDrawPage.add(xDrawShape);
299 
300             XText xShapeText = (XText)UnoRuntime.queryInterface(XText.class, drawShape);
301             XPropertySet xShapeProps = (XPropertySet)UnoRuntime.queryInterface(
302                 XPropertySet.class, drawShape);
303 
304             // wrap text inside shape
305             xShapeProps.setPropertyValue("TextContourFrame", new Boolean(true));
306 
307             manipulateText(xShapeText);
308             manipulateShape(xDrawShape);
309         }
310         catch( com.sun.star.lang.DisposedException e ) { //works from Patch 1
311             xRemoteContext = null;
312             throw e;
313         }
314 
315 
316     }
317 
manipulateText(XText xText)318     protected void manipulateText(XText xText) throws com.sun.star.uno.Exception {
319         // simply set whole text as one string
320         xText.setString("He lay flat on the brown, pine-needled floor of the forest, "
321             + "his chin on his folded arms, and high overhead the wind blew in the tops "
322             + "of the pine trees.");
323 
324         // create text cursor for selecting and formatting
325         XTextCursor xTextCursor = xText.createTextCursor();
326         XPropertySet xCursorProps = (XPropertySet)UnoRuntime.queryInterface(
327             XPropertySet.class, xTextCursor);
328 
329         // use cursor to select "He lay" and apply bold italic
330         xTextCursor.gotoStart(false);
331         xTextCursor.goRight((short)6, true);
332         // from CharacterProperties
333         xCursorProps.setPropertyValue("CharPosture",
334             com.sun.star.awt.FontSlant.ITALIC);
335         xCursorProps.setPropertyValue("CharWeight",
336             new Float(com.sun.star.awt.FontWeight.BOLD));
337 
338         // add more text at the end of the text using insertString
339         xTextCursor.gotoEnd(false);
340         xText.insertString(xTextCursor, " The mountainside sloped gently where he lay; "
341             + "but below it was steep and he could see the dark of the oiled road "
342             + "winding through the pass. There was a stream alongside the road "
343             + "and far down the pass he saw a mill beside the stream and the falling water "
344             + "of the dam, white in the summer sunlight.", false);
345         // after insertString the cursor is behind the inserted text, insert more text
346         xText.insertString(xTextCursor, "\n  \"Is that the mill?\" he asked.", false);
347     }
348 
manipulateTable(XCellRange xCellRange)349     protected void manipulateTable(XCellRange xCellRange) throws com.sun.star.uno.Exception {
350 
351         String backColorPropertyName = "";
352         XPropertySet xTableProps = null;
353 
354         // enter column titles and a cell value
355         XCell xCell = xCellRange.getCellByPosition(0,0);
356         XText xCellText = (XText)UnoRuntime.queryInterface(XText.class, xCell);
357         xCellText.setString("Quotation");
358         xCell = xCellRange.getCellByPosition(1,0);
359         xCellText = (XText)UnoRuntime.queryInterface(XText.class, xCell);
360         xCellText.setString("Year");
361         xCell = xCellRange.getCellByPosition(1,1);
362         xCell.setValue(1940);
363         XCellRange xSelectedCells = xCellRange.getCellRangeByName("A1:B1");
364         XPropertySet xCellProps = (XPropertySet)UnoRuntime.queryInterface(
365             XPropertySet.class, xSelectedCells);
366 
367         // format table headers and table borders
368         // we need to distinguish text and sheet tables:
369         // property name for cell colors is different in text and sheet cells
370         // we want to apply TableBorder to whole text table, but only to sheet cells with content
371         XServiceInfo xServiceInfo = (XServiceInfo)UnoRuntime.queryInterface(
372             XServiceInfo.class, xCellRange);
373         if (xServiceInfo.supportsService("com.sun.star.sheet.Spreadsheet")) {
374             backColorPropertyName = "CellBackColor";
375             xSelectedCells = xCellRange.getCellRangeByName("A1:B2");
376             xTableProps = (XPropertySet)UnoRuntime.queryInterface(
377                 XPropertySet.class, xSelectedCells);
378         }
379         else if (xServiceInfo.supportsService("com.sun.star.text.TextTable")) {
380             backColorPropertyName = "BackColor";
381             xTableProps = (XPropertySet)UnoRuntime.queryInterface(
382                 XPropertySet.class, xCellRange);
383         }
384         // set cell background color
385         xCellProps.setPropertyValue(backColorPropertyName, new Integer(0x99CCFF));
386 
387         // set table borders
388         // create description for blue line, width 10
389         BorderLine theLine = new BorderLine();
390         theLine.Color = 0x000099;
391         theLine.OuterLineWidth = 10;
392         // apply line description to all border lines and make them valid
393         TableBorder bord = new TableBorder();
394         bord.VerticalLine = bord.HorizontalLine =
395             bord.LeftLine = bord.RightLine =
396             bord.TopLine = bord.BottomLine =
397                 theLine;
398         bord.IsVerticalLineValid = bord.IsHorizontalLineValid =
399             bord.IsLeftLineValid = bord.IsRightLineValid =
400             bord.IsTopLineValid = bord.IsBottomLineValid =
401                 true;
402 
403         xTableProps.setPropertyValue("TableBorder", bord);
404 
405         bord = (TableBorder)xTableProps.getPropertyValue("TableBorder");
406         theLine = bord.TopLine;
407         int col = theLine.Color;
408         System.out.println(col);
409     }
410 
manipulateShape(XShape xShape)411     protected void manipulateShape(XShape xShape) throws com.sun.star.uno.Exception {
412         XPropertySet xShapeProps = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xShape);
413         xShapeProps.setPropertyValue("FillColor", new Integer(0x99CCFF));
414         xShapeProps.setPropertyValue("LineColor", new Integer(0x000099));
415         xShapeProps.setPropertyValue("RotateAngle", new Integer(3000));
416 
417         xShapeProps.setPropertyValue("TextLeftDistance", new Integer(0));
418         xShapeProps.setPropertyValue("TextRightDistance", new Integer(0));
419         xShapeProps.setPropertyValue("TextUpperDistance", new Integer(0));
420         xShapeProps.setPropertyValue("TextLowerDistance", new Integer(0));
421     }
422 
423 
newDocComponent(String docType)424     protected XComponent newDocComponent(String docType) throws java.lang.Exception {
425         String loadUrl = "private:factory/" + docType;
426         xRemoteServiceManager = this.getRemoteServiceManager();
427         Object desktop = xRemoteServiceManager.createInstanceWithContext(
428             "com.sun.star.frame.Desktop", xRemoteContext);
429         XComponentLoader xComponentLoader = (XComponentLoader)UnoRuntime.queryInterface(
430             XComponentLoader.class, desktop);
431         PropertyValue[] loadProps = new PropertyValue[0];
432         return xComponentLoader.loadComponentFromURL(loadUrl, "_blank", 0, loadProps);
433     }
434 
getRemoteServiceManager()435     protected XMultiComponentFactory getRemoteServiceManager() throws java.lang.Exception {
436         if (xRemoteContext == null && xRemoteServiceManager == null) {
437             try {
438                 // First step: get the remote office component context
439                 xRemoteContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
440                 System.out.println("Connected to a running office ...");
441 
442                 xRemoteServiceManager = xRemoteContext.getServiceManager();
443             }
444             catch( Exception e) {
445                 e.printStackTrace();
446                 System.exit(1);
447             }
448         }
449         return xRemoteServiceManager;
450     }
451 }
452