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