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 // __________ Imports __________
25 
26 import java.util.Random;
27 
28 // base classes
29 import com.sun.star.uno.XInterface;
30 import com.sun.star.uno.UnoRuntime;
31 import com.sun.star.uno.XComponentContext;
32 import com.sun.star.lang.*;
33 
34 // factory for creating components
35 import com.sun.star.comp.servicemanager.ServiceManager;
36 import com.sun.star.lang.XMultiServiceFactory;
37 import com.sun.star.bridge.XUnoUrlResolver;
38 import com.sun.star.uno.XNamingService;
39 import com.sun.star.frame.XDesktop;
40 import com.sun.star.frame.XComponentLoader;
41 
42 // property access
43 import com.sun.star.beans.*;
44 
45 // container access
46 import com.sun.star.container.*;
47 
48 // application specific classes
49 import com.sun.star.sheet.XSpreadsheetDocument;
50 import com.sun.star.text.XTextDocument;
51 
52 import com.sun.star.document.XEmbeddedObjectSupplier;
53 import com.sun.star.frame.XModel;
54 import com.sun.star.frame.XController;
55 
56 // Exceptions
57 import com.sun.star.uno.RuntimeException;
58 import com.sun.star.container.NoSuchElementException;
59 import com.sun.star.beans.UnknownPropertyException;
60 import com.sun.star.lang.IndexOutOfBoundsException;
61 
62 // __________ Implementation __________
63 
64 /** Helper for creating a calc document adding cell values and charts
65     @author Björn Milcke
66  */
67 public class Helper
68 {
Helper( String[] args )69     public Helper( String[] args )
70     {
71         // connect to a running office and get the ServiceManager
72         try {
73             // get the remote office component context
74             maContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
75             System.out.println("Connected to a running office ...");
76 
77             // get the remote office service manager
78             maMCFactory = maContext.getServiceManager();
79         }
80         catch( Exception e) {
81             System.out.println( "Couldn't get ServiceManager: " + e );
82             e.printStackTrace();
83             System.exit(1);
84         }
85     }
86 
87     // ____________________
88 
createSpreadsheetDocument()89     public XSpreadsheetDocument createSpreadsheetDocument()
90     {
91         return (XSpreadsheetDocument) UnoRuntime.queryInterface(
92             XSpreadsheetDocument.class, createDocument( "scalc" ));
93     }
94 
95     // ____________________
96 
createPresentationDocument()97     public XModel createPresentationDocument()
98     {
99         return createDocument( "simpress" );
100     }
101 
102     // ____________________
103 
createDrawingDocument()104     public XModel createDrawingDocument()
105     {
106         return createDocument( "sdraw" );
107     }
108 
109     // ____________________
110 
createTextDocument()111     public XModel createTextDocument()
112     {
113         return createDocument( "swriter" );
114     }
115 
116     // ____________________
117 
createDocument( String sDocType )118     public XModel createDocument( String sDocType )
119     {
120         XModel aResult = null;
121         try
122         {
123             XComponentLoader aLoader = (XComponentLoader)
124                 UnoRuntime.queryInterface(XComponentLoader.class,
125                 maMCFactory.createInstanceWithContext("com.sun.star.frame.Desktop",
126                                                       maContext) );
127 
128             aResult = (XModel) UnoRuntime.queryInterface(
129                 XModel.class,
130                 aLoader.loadComponentFromURL( "private:factory/" + sDocType,
131                                               "_blank",
132                                               0,
133                                               new PropertyValue[ 0 ] ) );
134         }
135         catch( Exception e )
136         {
137             System.err.println("Couldn't create Document of type "+ sDocType +": "+e);
138             e.printStackTrace();
139             System.exit( 0 );
140         }
141 
142         return aResult;
143     }
144 
getComponentContext()145     public XComponentContext getComponentContext(){
146         return maContext;
147 
148     }
149 
150     // __________ private members and methods __________
151 
152     private final String  msDataSheetName  = "Data";
153     private final String  msChartSheetName = "Chart";
154     private final String  msChartName      = "SampleChart";
155 
156     private XComponentContext      maContext;
157     private XMultiComponentFactory maMCFactory;
158     private XSpreadsheetDocument   maSpreadSheetDoc;
159 }
160