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             Rectangle aRectangle = new Rectangle();
182             XMessageBox xMessageBox = xMessageBoxFactory.createMessageBox(_xWindowPeer, aRectangle, "errorbox", com.sun.star.awt.MessageBoxButtons.BUTTONS_OK, _sTitle, _sMessage);
183             XComponent xComponent = (XComponent) UnoRuntime.queryInterface(XComponent.class, xMessageBox);
184             if (xMessageBox != null){
185                 short nResult = xMessageBox.execute();
186                 xComponent.dispose();
187             }
188         } catch (com.sun.star.uno.Exception ex) {
189             ex.printStackTrace(System.out);
190         }}
191 
192 
193         public void inspect(java.lang.Object _oUserDefinedObject, String _sTitle) throws com.sun.star.uno.RuntimeException {
194         try {
195             int nPageIndex = m_oSwingDialogProvider.getInspectorPageCount();
196             SwingTreeControlProvider oSwingTreeControlProvider = new SwingTreeControlProvider(m_oSwingDialogProvider);
197             InspectorPane oInspectorPane = new InspectorPane(getXComponentContext(), m_oSwingDialogProvider, oSwingTreeControlProvider, getSourceCodeLanguage());
198             oInspectorPane.inspect(_oUserDefinedObject, _sTitle);
199             getInspectorPages().put(_sTitle, oInspectorPane);
200             m_oSwingDialogProvider.show(nPageIndex);
201         }catch( Exception exception ) {
202             exception.printStackTrace(System.out);
203         }}
204 
205 
206         public void inspectOpenEmptyDocument(String _sApplicationDocUrl){
207             XComponent xComponent = getTDocSupplier().openEmptyDocument(_sApplicationDocUrl);
208             String sRootTitle = (String) aApplicationHashMap.get(_sApplicationDocUrl);
209             inspect(xComponent, sRootTitle);
210             aHiddenDocuments.add(xComponent);
211         }
212 
213 
214         public void inspectOpenDocument(String _sTDocUrl){
215             String sTreeNodeName = getTDocSupplier().getTitleByTDocUrl(_sTDocUrl);
216             XModel xTDocModel = getTDocSupplier().getXModelByTDocUrl(_sTDocUrl);
217             inspect(xTDocModel, sTreeNodeName);
218         }
219 
220 
221         public void inspectSelectedNode(){
222             InspectorPane oInspectorPane = m_oSwingDialogProvider.getSelectedInspectorPage();
223             if (oInspectorPane != null){
224                 XUnoNode oUnoNode = oInspectorPane.getSelectedNode();
225                 Object oUnoObject = oUnoNode.getUnoObject();
226                 if (oUnoObject != null){
227                     String sNodeDescription = UnoNode.getNodeDescription(oUnoObject);
228                     inspect(oUnoObject, sNodeDescription);
229                 }
230             }
231         }
232 
233 
234         public void addSourceCodeOfSelectedNode(){
235             InspectorPane oInspectorPane = m_oSwingDialogProvider.getSelectedInspectorPage();
236             if (oInspectorPane != null){
237                 oInspectorPane.addSourceCodeOfSelectedNode();
238             }
239         }
240 
241 
242         public void invokeSelectedMethod(){
243             InspectorPane oInspectorPane = m_oSwingDialogProvider.getSelectedInspectorPage();
244             if (oInspectorPane != null){
245                 oInspectorPane.invokeSelectedMethodNode();
246             }
247         }
248 
249 
250         public void setSourceCodeLanguage(final int _nLanguage){
251         try{
252             String sLanguage = "Java";
253             XNameAccess xNameAccess  = getConfigurationAccess("org.openoffice.inspector.ObjectInspector", true);
254             XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xNameAccess);
255             switch (_nLanguage){
256                 case XLanguageSourceCodeGenerator.nJAVA:
257                     sLanguage = "Java";
258                     break;
259                 case XLanguageSourceCodeGenerator.nCPLUSPLUS:
260                     sLanguage = "CPlusPlus";
261                     break;
262                 case XLanguageSourceCodeGenerator.nBASIC:
263                     sLanguage = "Basic";
264                     break;
265                 default:
266                     System.out.println("Warning: Sourcecode language is not defined!");
267             }
268             xPropertySet.setPropertyValue("Language", sLanguage);
269             XChangesBatch xBatch = (XChangesBatch) UnoRuntime.queryInterface(XChangesBatch.class, xNameAccess);
270             xBatch.commitChanges();
271             for (int i = 0; i < m_oSwingDialogProvider.getInspectorPageCount(); i++){
272                 m_oSwingDialogProvider.getInspectorPage(i).convertCompleteSourceCode(_nLanguage);
273             }
274         }catch( Exception exception ) {
275             exception.printStackTrace(System.out);
276         }}
277 
278 
279         private TDocSupplier getTDocSupplier(){
280             if (oTDocSupplier == null){
281                 oTDocSupplier = new TDocSupplier(m_xComponentContext);
282             }
283             return oTDocSupplier;
284         }
285 
286         public String[] getTDocUrls(){
287             return getTDocSupplier().getTDocUrls();
288         }
289 
290 
291         public String[] getTDocTitles(String[] _sTDocUrls){
292             return getTDocSupplier().getTDocTitles(_sTDocUrls);
293         }
294 
295 
296         public String[][] getApplicationUrls(){
297             Set aSet = aApplicationHashMap.keySet();
298             String[][] sReturnList = new String[aSet.size()][];
299             int n= 0;
300             for ( Iterator i = aSet.iterator(); i.hasNext(); ){
301                 String[] sSingleApplication = new String[2];
302                 sSingleApplication[0] = (String) i.next();
303                 // assign the title in the second index
304                 sSingleApplication[1] = (String) aApplicationHashMap.get(sSingleApplication[0]);
305                 sReturnList[n++] = sSingleApplication;
306             }
307             return sReturnList;
308         }
309 
310 
311         public void disposeHiddenDocuments(){
312             int nHiddenCount = aHiddenDocuments.size();
313             if (nHiddenCount > 0){
314                 for (int i = nHiddenCount - 1; i >= 0; i--){
315                     XComponent xComponent = (XComponent) aHiddenDocuments.get(i);
316                     if (xComponent != null){
317                         try {
318                             XCloseable xCloseable = (XCloseable) UnoRuntime.queryInterface(XCloseable.class, xComponent);
319                             xCloseable.close(true);
320                             aHiddenDocuments.remove(i);
321                         } catch (CloseVetoException ex) {
322                             ex.printStackTrace();
323                         }
324                     }
325                 }
326             }
327         }
328 
329 
330         public static String[] getServiceNames() {
331             String[] sSupportedServiceNames = { __serviceName };
332             return sSupportedServiceNames;
333         }
334 
335         // Implement the interface XServiceInfo
336         /** Get all supported service names.
337          * @return Supported service names.
338          */
339         public String[] getSupportedServiceNames() {
340             return getServiceNames();
341         }
342 
343         // Implement the interface XServiceInfo
344         /** Test, if the given service will be supported.
345          * @param sService Service name.
346          * @return Return true, if the service will be supported.
347          */
348         public boolean supportsService( String sServiceName ) {
349             return sServiceName.equals( __serviceName );
350         }
351 
352         // Implement the interface XServiceInfo
353         /** Get the implementation name of the component.
354          * @return Implementation name of the component.
355          */
356         public String getImplementationName() {
357             return _Inspector.class.getName();
358         }
359 
360 
361         private int getSourceCodeLanguage(){
362         int nLanguage = XLanguageSourceCodeGenerator.nJAVA;
363         try{
364             XNameAccess xNameAccess  = getConfigurationAccess("org.openoffice.inspector.ObjectInspector", false);
365             String sLanguage = (String) xNameAccess.getByName("Language");
366             if (sLanguage.toUpperCase().equals("JAVA")){
367                 nLanguage = XLanguageSourceCodeGenerator.nJAVA;
368             }
369             else if (sLanguage.toUpperCase().equals("BASIC")){
370                 nLanguage = XLanguageSourceCodeGenerator.nBASIC;
371             }
372             else if (sLanguage.toUpperCase().equals("CPLUSPLUS")){
373                 nLanguage = XLanguageSourceCodeGenerator.nCPLUSPLUS;
374             }
375             else{
376                 System.out.println("Warning: Sourcecode language " + sLanguage + " is not defined!");
377             }
378             m_oSwingDialogProvider.selectSourceCodeLanguage(nLanguage);
379         }catch( Exception exception ) {
380             exception.printStackTrace(System.out);
381         }
382             return nLanguage;
383         }
384 
385 
386         public XNameAccess getConfigurationAccess(boolean _bUpdate){
387             return getConfigurationAccess("org.openoffice.inspector.ObjectInspector", _bUpdate);
388         }
389 
390 
391         public XNameAccess getConfigurationAccess(String _sNodePath, boolean update) {
392         XNameAccess xNameAccess = null;
393         try {
394             String sAccess = "";
395             if (update) {
396                 sAccess = "com.sun.star.configuration.ConfigurationUpdateAccess";
397             }
398             else{
399                 sAccess = "com.sun.star.configuration.ConfigurationAccess";
400             }
401             XMultiComponentFactory xMCF = m_xComponentContext.getServiceManager();
402             Object oDefaultProvider = xMCF.createInstanceWithContext("com.sun.star.configuration.DefaultProvider", this.getXComponentContext());
403             XMultiServiceFactory xMSFCfg = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, oDefaultProvider);
404             Object oAccess = xMSFCfg.createInstanceWithArguments(sAccess, new Object[]{new NamedValue("nodepath", _sNodePath)});
405             xNameAccess = (XNameAccess) UnoRuntime.queryInterface(XNameAccess.class, oAccess);
406         } catch (com.sun.star.uno.Exception e) {
407         }
408         return xNameAccess;
409         }
410 
411 
412     }
413 
414 // end of inner class
415 
416 
417     /**
418      * Gives a factory for creating the service.
419      * This method is called by the <code>JavaLoader</code>
420      * <p>
421      * @return  returns a <code>XSingleComponentFactory</code> for creating
422      *          the component
423      * @param   sImplName the name of the implementation for which a
424      *          service is desired
425      * @see     com.sun.star.comp.loader.JavaLoader
426      */
427     public static XSingleComponentFactory __getComponentFactory( String sImplName )
428     {
429         XSingleComponentFactory xFactory = null;
430         if ( sImplName.equals( _Inspector.class.getName() ) )
431             xFactory = Factory.createComponentFactory(_Inspector.class, _Inspector.getServiceNames());
432         if ( xFactory == null )
433             xFactory = InspectorAddon.__getComponentFactory(sImplName);
434         return xFactory;
435     }
436 
437     /**
438      * Writes the service information into the given registry key.
439      * This method is called by the <code>JavaLoader</code>
440      * <p>
441      * @return  returns true if the operation succeeded
442      * @param   regKey the registryKey
443      * @see     com.sun.star.comp.loader.JavaLoader
444      */
445     public static boolean __writeRegistryServiceInfo(XRegistryKey regKey) {
446         return (Factory.writeRegistryServiceInfo(_Inspector.class.getName(), _Inspector.getServiceNames(), regKey)
447                 && InspectorAddon.__writeRegistryServiceInfo(regKey));
448     }
449 }
450 
451