update dev300-m58
[ooovba.git] / odk / examples / java / Inspector / Inspector.java
blob54edeb1ee75ff0545bb601ffbd5afcc175e79f24
1 /*************************************************************************
3 * $RCSfile: Inspector.java,v $
5 * $Revision: 1.5 $
7 * last change: $Author: rt $ $Date: 2007-04-04 09:18:05 $
9 * The Contents of this file are made available subject to the terms of
10 * the BSD license.
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
41 import com.sun.star.awt.Rectangle;
42 import com.sun.star.awt.XMessageBox;
43 import com.sun.star.awt.XMessageBoxFactory;
44 import com.sun.star.awt.XWindowPeer;
45 import com.sun.star.beans.NamedValue;
46 import com.sun.star.beans.XPropertySet;
47 import com.sun.star.container.XNameAccess;
48 import com.sun.star.frame.XModel;
49 import com.sun.star.lang.XComponent;
50 import com.sun.star.lang.XMultiComponentFactory;
51 import com.sun.star.lang.XMultiServiceFactory;
52 import com.sun.star.lang.XServiceInfo;
53 import com.sun.star.lang.XSingleComponentFactory;
54 import com.sun.star.lib.uno.helper.Factory;
55 import com.sun.star.lib.uno.helper.WeakBase;
56 import com.sun.star.registry.XRegistryKey;
57 import com.sun.star.ui.dialogs.XExecutableDialog;
58 import com.sun.star.ui.dialogs.XFolderPicker;
59 import com.sun.star.uno.UnoRuntime;
60 import com.sun.star.uno.XComponentContext;
61 import com.sun.star.util.CloseVetoException;
62 import com.sun.star.util.XChangesBatch;
63 import com.sun.star.util.XCloseable;
64 import java.util.HashMap;
65 import java.util.Iterator;
66 import java.util.Set;
67 import java.util.Vector;
68 import org.openoffice.XInstanceInspector;
71 /** The purpose of this class is to provide a factory for creating the service
72 * (<CODE>__getServiceFactory</CODE>) and writes the information into the given
73 * registry key (<CODE>__writeRegistryServiceInfo</CODE>).
75 public class Inspector{
76 public static final String sIDLDOCUMENTSUBFOLDER = "docs/common/ref/";
79 /** This class implements the method of the interface XInstanceInspector.
80 * Also the class implements the interfaces XServiceInfo, and XTypeProvider.
82 static public class _Inspector extends WeakBase implements XInstanceInspector, XServiceInfo{
84 static private final String __serviceName = "org.openoffice.InstanceInspector";
85 private HashMap aApplicationHashMap = new HashMap();
86 private String sTitle = "Object Inspector";
87 private Vector aHiddenDocuments = new Vector();
88 // private String[] sApplicationDocUrls = new String[]{"private:factory/swriter", "private:factory/scalc", "private:factory/simpress", "private:factory/sdraw", "private:factory/sbase"};
89 // private String[] sApplicationDocNames = new String[]{"Text Document", "Spreadsheet", "Presentation", "Drawing", "Database"};
90 private XComponentContext m_xComponentContext;
91 private HashMap aInspectorPanes = new HashMap();
92 private XDialogProvider m_oSwingDialogProvider;
93 private TDocSupplier oTDocSupplier;
94 private Introspector m_oIntrospector = null;
95 // TODO: improve these strings:
96 private static final String sWRONGINSTALLATIONPATH = "Your selected path does not refer to an SDK-Installation!";
97 /** Creates a new instance of Dialog */
98 public _Inspector(XComponentContext _xComponentContext) {
99 m_xComponentContext = _xComponentContext;
100 m_oIntrospector = Introspector.getIntrospector(m_xComponentContext);
101 aApplicationHashMap.put("private:factory/swriter", "Text Document");
102 aApplicationHashMap.put("private:factory/scalc", "Spreadsheet");
103 aApplicationHashMap.put("private:factory/simpress", "Presentation");
104 aApplicationHashMap.put("private:factory/sdraw", "Drawing");
105 aApplicationHashMap.put("private:factory/smath", "Formula");
106 m_oSwingDialogProvider = new SwingDialogProvider(this, sTitle);
107 // aApplicationHashMap.put("private:factory/sbase", "Database");
111 public XComponentContext getXComponentContext(){
112 return m_xComponentContext;
116 public HashMap getInspectorPages(){
117 return aInspectorPanes;
121 protected String getSDKPath(){
122 String sRetPath = "";
123 try{
124 XNameAccess xNameAccess = getConfigurationAccess("org.openoffice.inspector.ObjectInspector", true);
125 XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xNameAccess);
126 sRetPath = (String) xPropertySet.getPropertyValue("SDKPath");
127 }catch( Exception exception ) {
128 exception.printStackTrace(System.out);
130 return sRetPath;
134 public String getIDLPath(){
135 String sRetPath = getSDKPath();
136 if (m_oIntrospector.isValidSDKInstallationPath(sRetPath)){
137 sRetPath = m_oIntrospector.addToPath(sRetPath, sIDLDOCUMENTSUBFOLDER);
139 else{
140 sRetPath = "";
142 return sRetPath;
146 public void openIdlFileforSelectedNode(){
147 InspectorPane oInspectorPane = m_oSwingDialogProvider.getSelectedInspectorPage();
148 if (oInspectorPane != null){
149 XUnoNode oUnoNode = oInspectorPane.getSelectedNode();
150 if (oUnoNode != null){
151 String sPath = getIDLPath();
152 oUnoNode.openIdlDescription(sPath);
158 public void assignSDKPath() {
159 try {
160 String sInstallationFolder = "";
161 Object oFolderPicker = m_xComponentContext.getServiceManager().createInstanceWithContext("com.sun.star.ui.dialogs.FolderPicker", m_xComponentContext);
162 XFolderPicker xFolderPicker = (XFolderPicker) UnoRuntime.queryInterface(XFolderPicker.class, oFolderPicker);
163 XExecutableDialog xExecutable = (XExecutableDialog) UnoRuntime.queryInterface(XExecutableDialog.class, oFolderPicker);
164 XComponent xComponent = (XComponent) UnoRuntime.queryInterface(XComponent.class, oFolderPicker);
165 String sPath = getSDKPath();
166 if (!sPath.equals("")){
167 xFolderPicker.setDisplayDirectory(sPath);
169 xFolderPicker.setTitle("Add the Path to your SDK installation");
170 short nResult = xExecutable.execute();
171 if (nResult == com.sun.star.ui.dialogs.ExecutableDialogResults.OK){
172 sInstallationFolder = xFolderPicker.getDirectory();
173 if (m_oIntrospector.isValidSDKInstallationPath(sInstallationFolder)){
174 XNameAccess xNameAccess = getConfigurationAccess(true);
175 XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xNameAccess);
176 xPropertySet.setPropertyValue("SDKPath", sInstallationFolder);
177 XChangesBatch xBatch = (XChangesBatch) UnoRuntime.queryInterface(XChangesBatch.class, xNameAccess);
178 xBatch.commitChanges();
180 else{
181 XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xFolderPicker);
182 Object oWindow = xPropertySet.getPropertyValue("Window");
183 XWindowPeer xWindowPeer = (XWindowPeer) UnoRuntime.queryInterface(XWindowPeer.class, oWindow);
184 showErrorMessageBox(xWindowPeer, sTitle, sWRONGINSTALLATIONPATH);
185 assignSDKPath();
188 xComponent.dispose();
189 }catch( Exception exception ) {
190 exception.printStackTrace(System.out);
194 public void showErrorMessageBox(XWindowPeer _xWindowPeer, String _sTitle, String _sMessage){
195 try {
196 Object oToolkit = m_xComponentContext.getServiceManager().createInstanceWithContext("com.sun.star.awt.Toolkit", m_xComponentContext);
197 XMessageBoxFactory xMessageBoxFactory = (XMessageBoxFactory) UnoRuntime.queryInterface(XMessageBoxFactory.class, oToolkit);
198 Rectangle aRectangle = new Rectangle();
199 XMessageBox xMessageBox = xMessageBoxFactory.createMessageBox(_xWindowPeer, aRectangle, "errorbox", com.sun.star.awt.MessageBoxButtons.BUTTONS_OK, _sTitle, _sMessage);
200 XComponent xComponent = (XComponent) UnoRuntime.queryInterface(XComponent.class, xMessageBox);
201 if (xMessageBox != null){
202 short nResult = xMessageBox.execute();
203 xComponent.dispose();
205 } catch (com.sun.star.uno.Exception ex) {
206 ex.printStackTrace(System.out);
210 public void inspect(java.lang.Object _oUserDefinedObject, String _sTitle) throws com.sun.star.uno.RuntimeException {
211 try {
212 int nPageIndex = m_oSwingDialogProvider.getInspectorPageCount();
213 SwingTreeControlProvider oSwingTreeControlProvider = new SwingTreeControlProvider(m_oSwingDialogProvider);
214 InspectorPane oInspectorPane = new InspectorPane(getXComponentContext(), m_oSwingDialogProvider, oSwingTreeControlProvider, getSourceCodeLanguage());
215 oInspectorPane.inspect(_oUserDefinedObject, _sTitle);
216 getInspectorPages().put(_sTitle, oInspectorPane);
217 m_oSwingDialogProvider.show(nPageIndex);
218 }catch( Exception exception ) {
219 exception.printStackTrace(System.out);
223 public void inspectOpenEmptyDocument(String _sApplicationDocUrl){
224 XComponent xComponent = getTDocSupplier().openEmptyDocument(_sApplicationDocUrl);
225 String sRootTitle = (String) aApplicationHashMap.get(_sApplicationDocUrl);
226 inspect(xComponent, sRootTitle);
227 aHiddenDocuments.add(xComponent);
231 public void inspectOpenDocument(String _sTDocUrl){
232 String sTreeNodeName = getTDocSupplier().getTitleByTDocUrl(_sTDocUrl);
233 XModel xTDocModel = getTDocSupplier().getXModelByTDocUrl(_sTDocUrl);
234 inspect(xTDocModel, sTreeNodeName);
238 public void inspectSelectedNode(){
239 InspectorPane oInspectorPane = m_oSwingDialogProvider.getSelectedInspectorPage();
240 if (oInspectorPane != null){
241 XUnoNode oUnoNode = oInspectorPane.getSelectedNode();
242 Object oUnoObject = oUnoNode.getUnoObject();
243 if (oUnoObject != null){
244 String sNodeDescription = UnoNode.getNodeDescription(oUnoObject);
245 inspect(oUnoObject, sNodeDescription);
251 public void addSourceCodeOfSelectedNode(){
252 InspectorPane oInspectorPane = m_oSwingDialogProvider.getSelectedInspectorPage();
253 if (oInspectorPane != null){
254 oInspectorPane.addSourceCodeOfSelectedNode();
259 public void invokeSelectedMethod(){
260 InspectorPane oInspectorPane = m_oSwingDialogProvider.getSelectedInspectorPage();
261 if (oInspectorPane != null){
262 oInspectorPane.invokeSelectedMethodNode();
267 public void setSourceCodeLanguage(final int _nLanguage){
268 try{
269 String sLanguage = "Java";
270 XNameAccess xNameAccess = getConfigurationAccess("org.openoffice.inspector.ObjectInspector", true);
271 XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xNameAccess);
272 switch (_nLanguage){
273 case XLanguageSourceCodeGenerator.nJAVA:
274 sLanguage = "Java";
275 break;
276 case XLanguageSourceCodeGenerator.nCPLUSPLUS:
277 sLanguage = "CPlusPlus";
278 break;
279 case XLanguageSourceCodeGenerator.nBASIC:
280 sLanguage = "Basic";
281 break;
282 default:
283 System.out.println("Warning: Sourcecode language is not defined!");
285 xPropertySet.setPropertyValue("Language", sLanguage);
286 XChangesBatch xBatch = (XChangesBatch) UnoRuntime.queryInterface(XChangesBatch.class, xNameAccess);
287 xBatch.commitChanges();
288 for (int i = 0; i < m_oSwingDialogProvider.getInspectorPageCount(); i++){
289 m_oSwingDialogProvider.getInspectorPage(i).convertCompleteSourceCode(_nLanguage);
291 }catch( Exception exception ) {
292 exception.printStackTrace(System.out);
296 private TDocSupplier getTDocSupplier(){
297 if (oTDocSupplier == null){
298 oTDocSupplier = new TDocSupplier(m_xComponentContext);
300 return oTDocSupplier;
303 public String[] getTDocUrls(){
304 return getTDocSupplier().getTDocUrls();
308 public String[] getTDocTitles(String[] _sTDocUrls){
309 return getTDocSupplier().getTDocTitles(_sTDocUrls);
313 public String[][] getApplicationUrls(){
314 Set aSet = aApplicationHashMap.keySet();
315 String[][] sReturnList = new String[aSet.size()][];
316 int n= 0;
317 for ( Iterator i = aSet.iterator(); i.hasNext(); ){
318 String[] sSingleApplication = new String[2];
319 sSingleApplication[0] = (String) i.next();
320 // assign the title in the second index
321 sSingleApplication[1] = (String) aApplicationHashMap.get(sSingleApplication[0]);
322 sReturnList[n++] = sSingleApplication;
324 return sReturnList;
328 public void disposeHiddenDocuments(){
329 int nHiddenCount = aHiddenDocuments.size();
330 if (nHiddenCount > 0){
331 for (int i = nHiddenCount - 1; i >= 0; i--){
332 XComponent xComponent = (XComponent) aHiddenDocuments.get(i);
333 if (xComponent != null){
334 try {
335 XCloseable xCloseable = (XCloseable) UnoRuntime.queryInterface(XCloseable.class, xComponent);
336 xCloseable.close(true);
337 aHiddenDocuments.remove(i);
338 } catch (CloseVetoException ex) {
339 ex.printStackTrace();
347 public static String[] getServiceNames() {
348 String[] sSupportedServiceNames = { __serviceName };
349 return sSupportedServiceNames;
352 // Implement the interface XServiceInfo
353 /** Get all supported service names.
354 * @return Supported service names.
356 public String[] getSupportedServiceNames() {
357 return getServiceNames();
360 // Implement the interface XServiceInfo
361 /** Test, if the given service will be supported.
362 * @param sService Service name.
363 * @return Return true, if the service will be supported.
365 public boolean supportsService( String sServiceName ) {
366 return sServiceName.equals( __serviceName );
369 // Implement the interface XServiceInfo
370 /** Get the implementation name of the component.
371 * @return Implementation name of the component.
373 public String getImplementationName() {
374 return _Inspector.class.getName();
378 private int getSourceCodeLanguage(){
379 int nLanguage = XLanguageSourceCodeGenerator.nJAVA;
380 try{
381 XNameAccess xNameAccess = getConfigurationAccess("org.openoffice.inspector.ObjectInspector", false);
382 String sLanguage = (String) xNameAccess.getByName("Language");
383 if (sLanguage.toUpperCase().equals("JAVA")){
384 nLanguage = XLanguageSourceCodeGenerator.nJAVA;
386 else if (sLanguage.toUpperCase().equals("BASIC")){
387 nLanguage = XLanguageSourceCodeGenerator.nBASIC;
389 else if (sLanguage.toUpperCase().equals("CPLUSPLUS")){
390 nLanguage = XLanguageSourceCodeGenerator.nCPLUSPLUS;
392 else{
393 System.out.println("Warning: Sourcecode language " + sLanguage + " is not defined!");
395 m_oSwingDialogProvider.selectSourceCodeLanguage(nLanguage);
396 }catch( Exception exception ) {
397 exception.printStackTrace(System.out);
399 return nLanguage;
403 public XNameAccess getConfigurationAccess(boolean _bUpdate){
404 return getConfigurationAccess("org.openoffice.inspector.ObjectInspector", _bUpdate);
408 public XNameAccess getConfigurationAccess(String _sNodePath, boolean update) {
409 XNameAccess xNameAccess = null;
410 try {
411 String sAccess = "";
412 if (update) {
413 sAccess = "com.sun.star.configuration.ConfigurationUpdateAccess";
415 else{
416 sAccess = "com.sun.star.configuration.ConfigurationAccess";
418 XMultiComponentFactory xMCF = m_xComponentContext.getServiceManager();
419 Object oDefaultProvider = xMCF.createInstanceWithContext("com.sun.star.configuration.DefaultProvider", this.getXComponentContext());
420 XMultiServiceFactory xMSFCfg = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, oDefaultProvider);
421 Object oAccess = xMSFCfg.createInstanceWithArguments(sAccess, new Object[]{new NamedValue("nodepath", _sNodePath)});
422 xNameAccess = (XNameAccess) UnoRuntime.queryInterface(XNameAccess.class, oAccess);
423 } catch (com.sun.star.uno.Exception e) {
425 return xNameAccess;
431 // end of inner class
435 * Gives a factory for creating the service.
436 * This method is called by the <code>JavaLoader</code>
437 * <p>
438 * @return returns a <code>XSingleComponentFactory</code> for creating
439 * the component
440 * @param sImplName the name of the implementation for which a
441 * service is desired
442 * @see com.sun.star.comp.loader.JavaLoader
444 public static XSingleComponentFactory __getComponentFactory( String sImplName )
446 XSingleComponentFactory xFactory = null;
447 if ( sImplName.equals( _Inspector.class.getName() ) )
448 xFactory = Factory.createComponentFactory(_Inspector.class, _Inspector.getServiceNames());
449 if ( xFactory == null )
450 xFactory = InspectorAddon.__getComponentFactory(sImplName);
451 return xFactory;
455 * Writes the service information into the given registry key.
456 * This method is called by the <code>JavaLoader</code>
457 * <p>
458 * @return returns true if the operation succeeded
459 * @param regKey the registryKey
460 * @see com.sun.star.comp.loader.JavaLoader
462 public static boolean __writeRegistryServiceInfo(XRegistryKey regKey) {
463 return (Factory.writeRegistryServiceInfo(_Inspector.class.getName(), _Inspector.getServiceNames(), regKey)
464 && InspectorAddon.__writeRegistryServiceInfo(regKey));