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