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