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 import com.sun.star.awt.Rectangle; 25 import com.sun.star.awt.XMessageBox; 26 import com.sun.star.awt.XMessageBoxFactory; 27 import com.sun.star.awt.XWindowPeer; 28 import com.sun.star.beans.NamedValue; 29 import com.sun.star.beans.XPropertySet; 30 import com.sun.star.container.XNameAccess; 31 import com.sun.star.frame.XModel; 32 import com.sun.star.lang.XComponent; 33 import com.sun.star.lang.XMultiComponentFactory; 34 import com.sun.star.lang.XMultiServiceFactory; 35 import com.sun.star.lang.XServiceInfo; 36 import com.sun.star.lang.XSingleComponentFactory; 37 import com.sun.star.lib.uno.helper.Factory; 38 import com.sun.star.lib.uno.helper.WeakBase; 39 import com.sun.star.registry.XRegistryKey; 40 import com.sun.star.ui.dialogs.XExecutableDialog; 41 import com.sun.star.ui.dialogs.XFolderPicker; 42 import com.sun.star.uno.UnoRuntime; 43 import com.sun.star.uno.XComponentContext; 44 import com.sun.star.util.CloseVetoException; 45 import com.sun.star.util.XChangesBatch; 46 import com.sun.star.util.XCloseable; 47 import java.util.HashMap; 48 import java.util.Iterator; 49 import java.util.Set; 50 import java.util.Vector; 51 import org.openoffice.XInstanceInspector; 52 53 54 /** The purpose of this class is to provide a factory for creating the service 55 * (<CODE>__getServiceFactory</CODE>) and writes the information into the given 56 * registry key (<CODE>__writeRegistryServiceInfo</CODE>). 57 */ 58 public class Inspector{ 59 public static final String sIDLDOCUMENTSUBFOLDER = "docs/common/ref/"; 60 61 62 /** This class implements the method of the interface XInstanceInspector. 63 * Also the class implements the interfaces XServiceInfo, and XTypeProvider. 64 */ 65 static public class _Inspector extends WeakBase implements XInstanceInspector, XServiceInfo{ 66 67 static private final String __serviceName = "org.openoffice.InstanceInspector"; 68 private HashMap aApplicationHashMap = new HashMap(); 69 private String sTitle = "Object Inspector"; 70 private Vector aHiddenDocuments = new Vector(); 71 // private String[] sApplicationDocUrls = new String[]{"private:factory/swriter", "private:factory/scalc", "private:factory/simpress", "private:factory/sdraw", "private:factory/sbase"}; 72 // private String[] sApplicationDocNames = new String[]{"Text Document", "Spreadsheet", "Presentation", "Drawing", "Database"}; 73 private XComponentContext m_xComponentContext; 74 private HashMap aInspectorPanes = new HashMap(); 75 private XDialogProvider m_oSwingDialogProvider; 76 private TDocSupplier oTDocSupplier; 77 private Introspector m_oIntrospector = null; 78 // TODO: improve these strings: 79 private static final String sWRONGINSTALLATIONPATH = "Your selected path does not refer to an SDK-Installation!"; 80 /** Creates a new instance of Dialog */ 81 public _Inspector(XComponentContext _xComponentContext) { 82 m_xComponentContext = _xComponentContext; 83 m_oIntrospector = Introspector.getIntrospector(m_xComponentContext); 84 aApplicationHashMap.put("private:factory/swriter", "Text Document"); 85 aApplicationHashMap.put("private:factory/scalc", "Spreadsheet"); 86 aApplicationHashMap.put("private:factory/simpress", "Presentation"); 87 aApplicationHashMap.put("private:factory/sdraw", "Drawing"); 88 aApplicationHashMap.put("private:factory/smath", "Formula"); 89 m_oSwingDialogProvider = new SwingDialogProvider(this, sTitle); 90 // aApplicationHashMap.put("private:factory/sbase", "Database"); 91 } 92 93 94 public XComponentContext getXComponentContext(){ 95 return m_xComponentContext; 96 } 97 98 99 public HashMap getInspectorPages(){ 100 return aInspectorPanes; 101 } 102 103 104 protected String getSDKPath(){ 105 String sRetPath = ""; 106 try{ 107 XNameAccess xNameAccess = getConfigurationAccess("org.openoffice.inspector.ObjectInspector", true); 108 XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xNameAccess); 109 sRetPath = (String) xPropertySet.getPropertyValue("SDKPath"); 110 }catch( Exception exception ) { 111 exception.printStackTrace(System.out); 112 } 113 return sRetPath; 114 } 115 116 117 public String getIDLPath(){ 118 String sRetPath = getSDKPath(); 119 if (m_oIntrospector.isValidSDKInstallationPath(sRetPath)){ 120 sRetPath = m_oIntrospector.addToPath(sRetPath, sIDLDOCUMENTSUBFOLDER); 121 } 122 else{ 123 sRetPath = ""; 124 } 125 return sRetPath; 126 } 127 128 129 public void openIdlFileforSelectedNode(){ 130 InspectorPane oInspectorPane = m_oSwingDialogProvider.getSelectedInspectorPage(); 131 if (oInspectorPane != null){ 132 XUnoNode oUnoNode = oInspectorPane.getSelectedNode(); 133 if (oUnoNode != null){ 134 String sPath = getIDLPath(); 135 oUnoNode.openIdlDescription(sPath); 136 } 137 } 138 } 139 140 141 public void assignSDKPath() { 142 try { 143 String sInstallationFolder = ""; 144 Object oFolderPicker = m_xComponentContext.getServiceManager().createInstanceWithContext("com.sun.star.ui.dialogs.FolderPicker", m_xComponentContext); 145 XFolderPicker xFolderPicker = (XFolderPicker) UnoRuntime.queryInterface(XFolderPicker.class, oFolderPicker); 146 XExecutableDialog xExecutable = (XExecutableDialog) UnoRuntime.queryInterface(XExecutableDialog.class, oFolderPicker); 147 XComponent xComponent = (XComponent) UnoRuntime.queryInterface(XComponent.class, oFolderPicker); 148 String sPath = getSDKPath(); 149 if (!sPath.equals("")){ 150 xFolderPicker.setDisplayDirectory(sPath); 151 } 152 xFolderPicker.setTitle("Add the Path to your SDK installation"); 153 short nResult = xExecutable.execute(); 154 if (nResult == com.sun.star.ui.dialogs.ExecutableDialogResults.OK){ 155 sInstallationFolder = xFolderPicker.getDirectory(); 156 if (m_oIntrospector.isValidSDKInstallationPath(sInstallationFolder)){ 157 XNameAccess xNameAccess = getConfigurationAccess(true); 158 XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xNameAccess); 159 xPropertySet.setPropertyValue("SDKPath", sInstallationFolder); 160 XChangesBatch xBatch = (XChangesBatch) UnoRuntime.queryInterface(XChangesBatch.class, xNameAccess); 161 xBatch.commitChanges(); 162 } 163 else{ 164 XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xFolderPicker); 165 Object oWindow = xPropertySet.getPropertyValue("Window"); 166 XWindowPeer xWindowPeer = (XWindowPeer) UnoRuntime.queryInterface(XWindowPeer.class, oWindow); 167 showErrorMessageBox(xWindowPeer, sTitle, sWRONGINSTALLATIONPATH); 168 assignSDKPath(); 169 } 170 } 171 xComponent.dispose(); 172 }catch( Exception exception ) { 173 exception.printStackTrace(System.out); 174 }} 175 176 177 public void showErrorMessageBox(XWindowPeer _xWindowPeer, String _sTitle, String _sMessage){ 178 try { 179 Object oToolkit = m_xComponentContext.getServiceManager().createInstanceWithContext("com.sun.star.awt.Toolkit", m_xComponentContext); 180 XMessageBoxFactory xMessageBoxFactory = (XMessageBoxFactory) UnoRuntime.queryInterface(XMessageBoxFactory.class, oToolkit); 181 XMessageBox xMessageBox = xMessageBoxFactory.createMessageBox(_xWindowPeer, aRectangle, com.sun.star.awt.MessageBoxType.ERRORBOX, com.sun.star.awt.MessageBoxButtons.BUTTONS_OK, _sTitle, _sMessage); 182 XComponent xComponent = (XComponent) UnoRuntime.queryInterface(XComponent.class, xMessageBox); 183 if (xMessageBox != null){ 184 short nResult = xMessageBox.execute(); 185 xComponent.dispose(); 186 } 187 } catch (com.sun.star.uno.Exception ex) { 188 ex.printStackTrace(System.out); 189 }} 190 191 192 public void inspect(java.lang.Object _oUserDefinedObject, String _sTitle) throws com.sun.star.uno.RuntimeException { 193 try { 194 int nPageIndex = m_oSwingDialogProvider.getInspectorPageCount(); 195 SwingTreeControlProvider oSwingTreeControlProvider = new SwingTreeControlProvider(m_oSwingDialogProvider); 196 InspectorPane oInspectorPane = new InspectorPane(getXComponentContext(), m_oSwingDialogProvider, oSwingTreeControlProvider, getSourceCodeLanguage()); 197 oInspectorPane.inspect(_oUserDefinedObject, _sTitle); 198 getInspectorPages().put(_sTitle, oInspectorPane); 199 m_oSwingDialogProvider.show(nPageIndex); 200 }catch( Exception exception ) { 201 exception.printStackTrace(System.out); 202 }} 203 204 205 public void inspectOpenEmptyDocument(String _sApplicationDocUrl){ 206 XComponent xComponent = getTDocSupplier().openEmptyDocument(_sApplicationDocUrl); 207 String sRootTitle = (String) aApplicationHashMap.get(_sApplicationDocUrl); 208 inspect(xComponent, sRootTitle); 209 aHiddenDocuments.add(xComponent); 210 } 211 212 213 public void inspectOpenDocument(String _sTDocUrl){ 214 String sTreeNodeName = getTDocSupplier().getTitleByTDocUrl(_sTDocUrl); 215 XModel xTDocModel = getTDocSupplier().getXModelByTDocUrl(_sTDocUrl); 216 inspect(xTDocModel, sTreeNodeName); 217 } 218 219 220 public void inspectSelectedNode(){ 221 InspectorPane oInspectorPane = m_oSwingDialogProvider.getSelectedInspectorPage(); 222 if (oInspectorPane != null){ 223 XUnoNode oUnoNode = oInspectorPane.getSelectedNode(); 224 Object oUnoObject = oUnoNode.getUnoObject(); 225 if (oUnoObject != null){ 226 String sNodeDescription = UnoNode.getNodeDescription(oUnoObject); 227 inspect(oUnoObject, sNodeDescription); 228 } 229 } 230 } 231 232 233 public void addSourceCodeOfSelectedNode(){ 234 InspectorPane oInspectorPane = m_oSwingDialogProvider.getSelectedInspectorPage(); 235 if (oInspectorPane != null){ 236 oInspectorPane.addSourceCodeOfSelectedNode(); 237 } 238 } 239 240 241 public void invokeSelectedMethod(){ 242 InspectorPane oInspectorPane = m_oSwingDialogProvider.getSelectedInspectorPage(); 243 if (oInspectorPane != null){ 244 oInspectorPane.invokeSelectedMethodNode(); 245 } 246 } 247 248 249 public void setSourceCodeLanguage(final int _nLanguage){ 250 try{ 251 String sLanguage = "Java"; 252 XNameAccess xNameAccess = getConfigurationAccess("org.openoffice.inspector.ObjectInspector", true); 253 XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xNameAccess); 254 switch (_nLanguage){ 255 case XLanguageSourceCodeGenerator.nJAVA: 256 sLanguage = "Java"; 257 break; 258 case XLanguageSourceCodeGenerator.nCPLUSPLUS: 259 sLanguage = "CPlusPlus"; 260 break; 261 case XLanguageSourceCodeGenerator.nBASIC: 262 sLanguage = "Basic"; 263 break; 264 default: 265 System.out.println("Warning: Sourcecode language is not defined!"); 266 } 267 xPropertySet.setPropertyValue("Language", sLanguage); 268 XChangesBatch xBatch = (XChangesBatch) UnoRuntime.queryInterface(XChangesBatch.class, xNameAccess); 269 xBatch.commitChanges(); 270 for (int i = 0; i < m_oSwingDialogProvider.getInspectorPageCount(); i++){ 271 m_oSwingDialogProvider.getInspectorPage(i).convertCompleteSourceCode(_nLanguage); 272 } 273 }catch( Exception exception ) { 274 exception.printStackTrace(System.out); 275 }} 276 277 278 private TDocSupplier getTDocSupplier(){ 279 if (oTDocSupplier == null){ 280 oTDocSupplier = new TDocSupplier(m_xComponentContext); 281 } 282 return oTDocSupplier; 283 } 284 285 public String[] getTDocUrls(){ 286 return getTDocSupplier().getTDocUrls(); 287 } 288 289 290 public String[] getTDocTitles(String[] _sTDocUrls){ 291 return getTDocSupplier().getTDocTitles(_sTDocUrls); 292 } 293 294 295 public String[][] getApplicationUrls(){ 296 Set aSet = aApplicationHashMap.keySet(); 297 String[][] sReturnList = new String[aSet.size()][]; 298 int n= 0; 299 for ( Iterator i = aSet.iterator(); i.hasNext(); ){ 300 String[] sSingleApplication = new String[2]; 301 sSingleApplication[0] = (String) i.next(); 302 // assign the title in the second index 303 sSingleApplication[1] = (String) aApplicationHashMap.get(sSingleApplication[0]); 304 sReturnList[n++] = sSingleApplication; 305 } 306 return sReturnList; 307 } 308 309 310 public void disposeHiddenDocuments(){ 311 int nHiddenCount = aHiddenDocuments.size(); 312 if (nHiddenCount > 0){ 313 for (int i = nHiddenCount - 1; i >= 0; i--){ 314 XComponent xComponent = (XComponent) aHiddenDocuments.get(i); 315 if (xComponent != null){ 316 try { 317 XCloseable xCloseable = (XCloseable) UnoRuntime.queryInterface(XCloseable.class, xComponent); 318 xCloseable.close(true); 319 aHiddenDocuments.remove(i); 320 } catch (CloseVetoException ex) { 321 ex.printStackTrace(); 322 } 323 } 324 } 325 } 326 } 327 328 329 public static String[] getServiceNames() { 330 String[] sSupportedServiceNames = { __serviceName }; 331 return sSupportedServiceNames; 332 } 333 334 // Implement the interface XServiceInfo 335 /** Get all supported service names. 336 * @return Supported service names. 337 */ 338 public String[] getSupportedServiceNames() { 339 return getServiceNames(); 340 } 341 342 // Implement the interface XServiceInfo 343 /** Test, if the given service will be supported. 344 * @param sService Service name. 345 * @return Return true, if the service will be supported. 346 */ 347 public boolean supportsService( String sServiceName ) { 348 return sServiceName.equals( __serviceName ); 349 } 350 351 // Implement the interface XServiceInfo 352 /** Get the implementation name of the component. 353 * @return Implementation name of the component. 354 */ 355 public String getImplementationName() { 356 return _Inspector.class.getName(); 357 } 358 359 360 private int getSourceCodeLanguage(){ 361 int nLanguage = XLanguageSourceCodeGenerator.nJAVA; 362 try{ 363 XNameAccess xNameAccess = getConfigurationAccess("org.openoffice.inspector.ObjectInspector", false); 364 String sLanguage = (String) xNameAccess.getByName("Language"); 365 if (sLanguage.toUpperCase().equals("JAVA")){ 366 nLanguage = XLanguageSourceCodeGenerator.nJAVA; 367 } 368 else if (sLanguage.toUpperCase().equals("BASIC")){ 369 nLanguage = XLanguageSourceCodeGenerator.nBASIC; 370 } 371 else if (sLanguage.toUpperCase().equals("CPLUSPLUS")){ 372 nLanguage = XLanguageSourceCodeGenerator.nCPLUSPLUS; 373 } 374 else{ 375 System.out.println("Warning: Sourcecode language " + sLanguage + " is not defined!"); 376 } 377 m_oSwingDialogProvider.selectSourceCodeLanguage(nLanguage); 378 }catch( Exception exception ) { 379 exception.printStackTrace(System.out); 380 } 381 return nLanguage; 382 } 383 384 385 public XNameAccess getConfigurationAccess(boolean _bUpdate){ 386 return getConfigurationAccess("org.openoffice.inspector.ObjectInspector", _bUpdate); 387 } 388 389 390 public XNameAccess getConfigurationAccess(String _sNodePath, boolean update) { 391 XNameAccess xNameAccess = null; 392 try { 393 String sAccess = ""; 394 if (update) { 395 sAccess = "com.sun.star.configuration.ConfigurationUpdateAccess"; 396 } 397 else{ 398 sAccess = "com.sun.star.configuration.ConfigurationAccess"; 399 } 400 XMultiComponentFactory xMCF = m_xComponentContext.getServiceManager(); 401 Object oDefaultProvider = xMCF.createInstanceWithContext("com.sun.star.configuration.DefaultProvider", this.getXComponentContext()); 402 XMultiServiceFactory xMSFCfg = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, oDefaultProvider); 403 Object oAccess = xMSFCfg.createInstanceWithArguments(sAccess, new Object[]{new NamedValue("nodepath", _sNodePath)}); 404 xNameAccess = (XNameAccess) UnoRuntime.queryInterface(XNameAccess.class, oAccess); 405 } catch (com.sun.star.uno.Exception e) { 406 } 407 return xNameAccess; 408 } 409 410 411 } 412 413 // end of inner class 414 415 416 /** 417 * Gives a factory for creating the service. 418 * This method is called by the <code>JavaLoader</code> 419 * <p> 420 * @return returns a <code>XSingleComponentFactory</code> for creating 421 * the component 422 * @param sImplName the name of the implementation for which a 423 * service is desired 424 * @see com.sun.star.comp.loader.JavaLoader 425 */ 426 public static XSingleComponentFactory __getComponentFactory( String sImplName ) 427 { 428 XSingleComponentFactory xFactory = null; 429 if ( sImplName.equals( _Inspector.class.getName() ) ) 430 xFactory = Factory.createComponentFactory(_Inspector.class, _Inspector.getServiceNames()); 431 if ( xFactory == null ) 432 xFactory = InspectorAddon.__getComponentFactory(sImplName); 433 return xFactory; 434 } 435 436 /** 437 * Writes the service information into the given registry key. 438 * This method is called by the <code>JavaLoader</code> 439 * <p> 440 * @return returns true if the operation succeeded 441 * @param regKey the registryKey 442 * @see com.sun.star.comp.loader.JavaLoader 443 */ 444 public static boolean __writeRegistryServiceInfo(XRegistryKey regKey) { 445 return (Factory.writeRegistryServiceInfo(_Inspector.class.getName(), _Inspector.getServiceNames(), regKey) 446 && InspectorAddon.__writeRegistryServiceInfo(regKey)); 447 } 448 } 449 450