cid#1607171 Data race condition
[LibreOffice.git] / odk / examples / java / Text / GraphicsInserter.java
blobb4b5b49eaa627c750807d9fa159b7bc63340d427
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
5 * the BSD license.
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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.uno.UnoRuntime;
38 import java.io.PrintWriter;
39 import java.io.BufferedWriter;
40 import java.io.FileWriter;
43 public class GraphicsInserter {
44 public static void main(String args[]) {
45 if ( args.length < 1 )
47 System.out.println(
48 "usage: java -jar GraphicsInserter.jar \"<Graphic URL|path>\"" );
49 System.out.println( "\ne.g.:" );
50 System.out.println(
51 "java -jar GraphicsInserter.jar \"file:///f:/TestGraphics.gif\"" );
52 System.exit( 1 );
55 com.sun.star.uno.XComponentContext xContext = null;
57 try {
59 // bootstrap UNO and get the remote component context. The context can
60 // be used to get the service manager
61 xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
62 System.out.println("Connected to a running office ...");
64 // get the remote office service manager
65 com.sun.star.lang.XMultiComponentFactory xMCF =
66 xContext.getServiceManager();
68 /* A desktop environment contains tasks with one or more
69 frames in which components can be loaded. Desktop is the
70 environment for components which can instantiate within
71 frames. */
72 com.sun.star.frame.XDesktop xDesktop = UnoRuntime.queryInterface(com.sun.star.frame.XDesktop.class,
73 xMCF.createInstanceWithContext("com.sun.star.frame.Desktop",
74 xContext ) );
76 com.sun.star.frame.XComponentLoader xCompLoader =
77 UnoRuntime.queryInterface(
78 com.sun.star.frame.XComponentLoader.class, xDesktop);
80 // Load a Writer document, which will be automatically displayed
81 com.sun.star.lang.XComponent xComp = xCompLoader.loadComponentFromURL(
82 "private:factory/swriter", "_blank", 0,
83 new com.sun.star.beans.PropertyValue[0]);
85 // Querying for the interface XTextDocument on the xcomponent
86 com.sun.star.text.XTextDocument xTextDoc =
87 UnoRuntime.queryInterface(
88 com.sun.star.text.XTextDocument.class, xComp);
90 // Querying for the interface XMultiServiceFactory on the xtextdocument
91 com.sun.star.lang.XMultiServiceFactory xMSFDoc =
92 UnoRuntime.queryInterface(
93 com.sun.star.lang.XMultiServiceFactory.class, xTextDoc);
95 // Providing a log file for output
96 PrintWriter printwriterLog = new PrintWriter(
97 new BufferedWriter( new FileWriter("log.txt") ) );
99 Object oGraphic = null;
100 try {
101 // Creating the service GraphicObject
102 oGraphic =xMSFDoc
103 .createInstance("com.sun.star.text.TextGraphicObject");
105 catch ( Exception exception ) {
106 System.out.println( "Could not create instance" );
107 exception.printStackTrace( printwriterLog );
110 // Getting the text
111 com.sun.star.text.XText xText = xTextDoc.getText();
113 // Getting the cursor on the document
114 com.sun.star.text.XTextCursor xTextCursor = xText.createTextCursor();
116 // Querying for the interface XTextContent on the GraphicObject
117 com.sun.star.text.XTextContent xTextContent =
118 UnoRuntime.queryInterface(
119 com.sun.star.text.XTextContent.class, oGraphic );
121 // Printing information to the log file
122 printwriterLog.println( "inserting graphic" );
123 try {
124 // Inserting the content
125 xText.insertTextContent(xTextCursor, xTextContent, true);
126 } catch ( Exception exception ) {
127 System.out.println( "Could not insert Content" );
128 exception.printStackTrace(System.err);
131 // Printing information to the log file
132 printwriterLog.println( "adding graphic" );
134 // Querying for the interface XPropertySet on GraphicObject
135 com.sun.star.beans.XPropertySet xPropSet =
136 UnoRuntime.queryInterface(
137 com.sun.star.beans.XPropertySet.class, oGraphic);
138 try {
139 // Creating a string for the graphic url
140 java.io.File sourceFile = new java.io.File(args[0]);
141 StringBuffer sUrl = new StringBuffer("file:///");
142 sUrl.append(sourceFile.getCanonicalPath().replace('\\', '/'));
143 System.out.println( "insert graphic \"" + sUrl + "\"");
145 com.sun.star.graphic.XGraphicProvider xGraphicProvider =
146 UnoRuntime.queryInterface(com.sun.star.graphic.XGraphicProvider.class,
147 xMCF.createInstanceWithContext("com.sun.star.graphic.GraphicProvider",
148 xContext));
151 com.sun.star.beans.PropertyValue[] aMediaProps = new com.sun.star.beans.PropertyValue[] { new com.sun.star.beans.PropertyValue() };
152 aMediaProps[0].Name = "URL";
153 aMediaProps[0].Value = sUrl;
155 com.sun.star.graphic.XGraphic xGraphic =
156 UnoRuntime.queryInterface(com.sun.star.graphic.XGraphic.class,
157 xGraphicProvider.queryGraphic(aMediaProps));
159 // Setting the anchor type
160 xPropSet.setPropertyValue("AnchorType",
161 com.sun.star.text.TextContentAnchorType.AT_PARAGRAPH );
163 // Setting the graphic url
164 xPropSet.setPropertyValue( "Graphic", xGraphic );
166 // Setting the horizontal position
167 xPropSet.setPropertyValue( "HoriOrientPosition",
168 Integer.valueOf( 5500 ) );
170 // Setting the vertical position
171 xPropSet.setPropertyValue( "VertOrientPosition",
172 Integer.valueOf( 4200 ) );
174 // Setting the width
175 xPropSet.setPropertyValue( "Width", Integer.valueOf( 4400 ) );
177 // Setting the height
178 xPropSet.setPropertyValue( "Height", Integer.valueOf( 4000 ) );
179 } catch ( Exception exception ) {
180 System.out.println( "Couldn't set property 'GraphicURL'" );
181 exception.printStackTrace( printwriterLog );
184 xContext = null;
186 System.exit(0);
188 catch( Exception e ) {
189 e.printStackTrace(System.err);
190 System.exit(1);
195 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */