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