Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / java / Inspector / Inspector.java
bloba537d9c77941a402ff672959d43621b21fd88d1c
1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
4 * the BSD license.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * All rights reserved.
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.
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.
33 *************************************************************************/
35 import java.util.ArrayList;
36 import java.util.HashMap;
37 import java.util.Iterator;
38 import java.util.Set;
40 import org.openoffice.XInstanceInspector;
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.configuration.theDefaultProvider;
48 import com.sun.star.container.XNameAccess;
49 import com.sun.star.frame.XModel;
50 import com.sun.star.lang.XComponent;
51 import com.sun.star.lang.XMultiComponentFactory;
52 import com.sun.star.lang.XMultiServiceFactory;
53 import com.sun.star.lang.XServiceInfo;
54 import com.sun.star.lang.XSingleComponentFactory;
55 import com.sun.star.lib.uno.helper.Factory;
56 import com.sun.star.lib.uno.helper.WeakBase;
57 import com.sun.star.registry.XRegistryKey;
58 import com.sun.star.ui.dialogs.XExecutableDialog;
59 import com.sun.star.ui.dialogs.XFolderPicker2;
60 import com.sun.star.uno.UnoRuntime;
61 import com.sun.star.uno.XComponentContext;
62 import com.sun.star.util.CloseVetoException;
63 import com.sun.star.util.XChangesBatch;
64 import com.sun.star.util.XCloseable;
67 /** The purpose of this class is to provide a factory for creating the service
68 * (<CODE>__getServiceFactory</CODE>) and writes the information into the given
69 * registry key (<CODE>__writeRegistryServiceInfo</CODE>).
71 public class Inspector{
72 public static final String sIDLDOCUMENTSUBFOLDER = "docs/idl/ref/";
75 /** This class implements the method of the interface XInstanceInspector.
76 * Also the class implements the interfaces XServiceInfo, and XTypeProvider.
78 static public class _Inspector extends WeakBase implements XInstanceInspector, XServiceInfo{
80 static private final String __serviceName = "org.openoffice.InstanceInspector";
81 private HashMap<String, String> aApplicationHashMap = new HashMap<String, String>();
82 private String sTitle = "Object Inspector";
83 private ArrayList<XComponent> aHiddenDocuments = new ArrayList<XComponent>();
84 private XComponentContext m_xComponentContext;
85 private HashMap<String, InspectorPane> aInspectorPanes = new HashMap<String, InspectorPane>();
86 private XDialogProvider m_oSwingDialogProvider;
87 private TDocSupplier oTDocSupplier;
88 private Introspector m_oIntrospector = null;
89 // TODO: improve these strings:
90 private static final String sWRONGINSTALLATIONPATH = "Your selected path does not refer to an SDK-Installation!";
91 /** Creates a new instance of Dialog */
92 public _Inspector(XComponentContext _xComponentContext) {
93 m_xComponentContext = _xComponentContext;
94 m_oIntrospector = Introspector.getIntrospector(m_xComponentContext);
95 aApplicationHashMap.put("private:factory/swriter", "Text Document");
96 aApplicationHashMap.put("private:factory/scalc", "Spreadsheet");
97 aApplicationHashMap.put("private:factory/simpress", "Presentation");
98 aApplicationHashMap.put("private:factory/sdraw", "Drawing");
99 aApplicationHashMap.put("private:factory/smath", "Formula");
100 m_oSwingDialogProvider = new SwingDialogProvider(this, sTitle);
104 public XComponentContext getXComponentContext(){
105 return m_xComponentContext;
109 public HashMap<String, InspectorPane> getInspectorPages(){
110 return aInspectorPanes;
114 private String getSDKPath(){
115 String sRetPath = "";
116 try{
117 XNameAccess xNameAccess = getConfigurationAccess("org.openoffice.inspector.ObjectInspector", true);
118 XPropertySet xPropertySet = UnoRuntime.queryInterface(XPropertySet.class, xNameAccess);
119 sRetPath = (String) xPropertySet.getPropertyValue("SDKPath");
120 }catch( Exception exception ) {
121 exception.printStackTrace(System.err);
123 return sRetPath;
127 public String getIDLPath(){
128 String sRetPath = getSDKPath();
129 if (m_oIntrospector.isValidSDKInstallationPath(sRetPath)){
130 sRetPath = Introspector.addToPath(sRetPath, sIDLDOCUMENTSUBFOLDER);
132 else{
133 sRetPath = "";
135 return sRetPath;
139 public void openIdlFileforSelectedNode(){
140 InspectorPane oInspectorPane = m_oSwingDialogProvider.getSelectedInspectorPage();
141 if (oInspectorPane != null){
142 XUnoNode oUnoNode = oInspectorPane.getSelectedNode();
143 if (oUnoNode != null){
144 String sPath = getIDLPath();
145 oUnoNode.openIdlDescription(sPath);
151 public void assignSDKPath() {
152 try {
153 String sInstallationFolder = "";
154 Object oFolderPicker = m_xComponentContext.getServiceManager().createInstanceWithContext("com.sun.star.ui.dialogs.FolderPicker", m_xComponentContext);
155 XFolderPicker2 xFolderPicker = UnoRuntime.queryInterface(XFolderPicker2.class, oFolderPicker);
156 XExecutableDialog xExecutable = UnoRuntime.queryInterface(XExecutableDialog.class, oFolderPicker);
157 XComponent xComponent = UnoRuntime.queryInterface(XComponent.class, oFolderPicker);
158 String sPath = getSDKPath();
159 if (!sPath.equals("")){
160 xFolderPicker.setDisplayDirectory(sPath);
162 xFolderPicker.setTitle("Add the Path to your SDK installation");
163 short nResult = xExecutable.execute();
164 if (nResult == com.sun.star.ui.dialogs.ExecutableDialogResults.OK){
165 sInstallationFolder = xFolderPicker.getDirectory();
166 if (m_oIntrospector.isValidSDKInstallationPath(sInstallationFolder)){
167 XNameAccess xNameAccess = getConfigurationAccess(true);
168 XPropertySet xPropertySet = UnoRuntime.queryInterface(XPropertySet.class, xNameAccess);
169 xPropertySet.setPropertyValue("SDKPath", sInstallationFolder);
170 XChangesBatch xBatch = UnoRuntime.queryInterface(XChangesBatch.class, xNameAccess);
171 xBatch.commitChanges();
173 else{
174 XPropertySet xPropertySet = UnoRuntime.queryInterface(XPropertySet.class, xFolderPicker);
175 Object oWindow = xPropertySet.getPropertyValue("Window");
176 XWindowPeer xWindowPeer = UnoRuntime.queryInterface(XWindowPeer.class, oWindow);
177 showErrorMessageBox(xWindowPeer, sTitle, sWRONGINSTALLATIONPATH);
178 assignSDKPath();
181 xComponent.dispose();
182 }catch( Exception exception ) {
183 exception.printStackTrace(System.err);
187 private void showErrorMessageBox(XWindowPeer _xWindowPeer, String _sTitle, String _sMessage){
188 try {
189 Object oToolkit = m_xComponentContext.getServiceManager().createInstanceWithContext("com.sun.star.awt.Toolkit", m_xComponentContext);
190 XMessageBoxFactory xMessageBoxFactory = UnoRuntime.queryInterface(XMessageBoxFactory.class, oToolkit);
191 XMessageBox xMessageBox = xMessageBoxFactory.createMessageBox(_xWindowPeer, com.sun.star.awt.MessageBoxType.ERRORBOX, com.sun.star.awt.MessageBoxButtons.BUTTONS_OK, _sTitle, _sMessage);
192 XComponent xComponent = UnoRuntime.queryInterface(XComponent.class, xMessageBox);
193 if (xMessageBox != null){
194 xMessageBox.execute();
195 xComponent.dispose();
197 } catch (com.sun.star.uno.Exception ex) {
198 ex.printStackTrace(System.err);
202 public void inspect(java.lang.Object _oUserDefinedObject, String _sTitle) throws com.sun.star.uno.RuntimeException {
203 try {
204 int nPageIndex = m_oSwingDialogProvider.getInspectorPageCount();
205 SwingTreeControlProvider oSwingTreeControlProvider = new SwingTreeControlProvider(m_oSwingDialogProvider);
206 InspectorPane oInspectorPane = new InspectorPane(getXComponentContext(), m_oSwingDialogProvider, oSwingTreeControlProvider, getSourceCodeLanguage());
207 oInspectorPane.inspect(_oUserDefinedObject, _sTitle);
208 getInspectorPages().put(_sTitle, oInspectorPane);
209 m_oSwingDialogProvider.show(nPageIndex);
210 }catch( Exception exception ) {
211 exception.printStackTrace(System.err);
215 public void inspectOpenEmptyDocument(String _sApplicationDocUrl){
216 XComponent xComponent = getTDocSupplier().openEmptyDocument(_sApplicationDocUrl);
217 String sRootTitle = aApplicationHashMap.get(_sApplicationDocUrl);
218 inspect(xComponent, sRootTitle);
219 aHiddenDocuments.add(xComponent);
223 public void inspectOpenDocument(String _sTDocUrl){
224 String sTreeNodeName = getTDocSupplier().getTitleByTDocUrl(_sTDocUrl);
225 XModel xTDocModel = getTDocSupplier().getXModelByTDocUrl(_sTDocUrl);
226 inspect(xTDocModel, sTreeNodeName);
230 public void inspectSelectedNode(){
231 InspectorPane oInspectorPane = m_oSwingDialogProvider.getSelectedInspectorPage();
232 if (oInspectorPane != null){
233 XUnoNode oUnoNode = oInspectorPane.getSelectedNode();
234 Object oUnoObject = oUnoNode.getUnoObject();
235 if (oUnoObject != null){
236 String sNodeDescription = UnoNode.getNodeDescription(oUnoObject);
237 inspect(oUnoObject, sNodeDescription);
243 public void addSourceCodeOfSelectedNode(){
244 InspectorPane oInspectorPane = m_oSwingDialogProvider.getSelectedInspectorPage();
245 if (oInspectorPane != null){
246 oInspectorPane.addSourceCodeOfSelectedNode();
251 public void invokeSelectedMethod(){
252 InspectorPane oInspectorPane = m_oSwingDialogProvider.getSelectedInspectorPage();
253 if (oInspectorPane != null){
254 oInspectorPane.invokeSelectedMethodNode();
259 public void setSourceCodeLanguage(final int _nLanguage){
260 try{
261 String sLanguage = "Java";
262 XNameAccess xNameAccess = getConfigurationAccess("org.openoffice.inspector.ObjectInspector", true);
263 XPropertySet xPropertySet = UnoRuntime.queryInterface(XPropertySet.class, xNameAccess);
264 switch (_nLanguage){
265 case XLanguageSourceCodeGenerator.nJAVA:
266 sLanguage = "Java";
267 break;
268 case XLanguageSourceCodeGenerator.nCPLUSPLUS:
269 sLanguage = "CPlusPlus";
270 break;
271 case XLanguageSourceCodeGenerator.nBASIC:
272 sLanguage = "Basic";
273 break;
274 default:
275 System.out.println("Warning: Sourcecode language is not defined!");
277 xPropertySet.setPropertyValue("Language", sLanguage);
278 XChangesBatch xBatch = UnoRuntime.queryInterface(XChangesBatch.class, xNameAccess);
279 xBatch.commitChanges();
280 for (int i = 0; i < m_oSwingDialogProvider.getInspectorPageCount(); i++){
281 m_oSwingDialogProvider.getInspectorPage(i).convertCompleteSourceCode(_nLanguage);
283 }catch( Exception exception ) {
284 exception.printStackTrace(System.err);
288 private TDocSupplier getTDocSupplier(){
289 if (oTDocSupplier == null){
290 oTDocSupplier = new TDocSupplier(m_xComponentContext);
292 return oTDocSupplier;
295 public String[] getTDocUrls(){
296 return getTDocSupplier().getTDocUrls();
300 public String[] getTDocTitles(String[] _sTDocUrls){
301 return getTDocSupplier().getTDocTitles(_sTDocUrls);
305 public String[][] getApplicationUrls(){
306 Set<String> aSet = aApplicationHashMap.keySet();
307 String[][] sReturnList = new String[aSet.size()][];
308 int n= 0;
309 for ( Iterator<String> i = aSet.iterator(); i.hasNext(); ){
310 String[] sSingleApplication = new String[2];
311 sSingleApplication[0] = i.next();
312 // assign the title in the second index
313 sSingleApplication[1] = aApplicationHashMap.get(sSingleApplication[0]);
314 sReturnList[n++] = sSingleApplication;
316 return sReturnList;
320 public void disposeHiddenDocuments(){
321 int nHiddenCount = aHiddenDocuments.size();
322 if (nHiddenCount > 0){
323 for (int i = nHiddenCount - 1; i >= 0; i--){
324 XComponent xComponent = aHiddenDocuments.get(i);
325 if (xComponent != null){
326 try {
327 XCloseable xCloseable = UnoRuntime.queryInterface(XCloseable.class, xComponent);
328 xCloseable.close(true);
329 aHiddenDocuments.remove(i);
330 } catch (CloseVetoException ex) {
331 ex.printStackTrace();
339 private static String[] getServiceNames() {
340 String[] sSupportedServiceNames = { __serviceName };
341 return sSupportedServiceNames;
344 // Implement the interface XServiceInfo
345 /** Get all supported service names.
346 * @return Supported service names.
348 public String[] getSupportedServiceNames() {
349 return getServiceNames();
352 // Implement the interface XServiceInfo
353 /** Test, if the given service will be supported.
354 * @return Return true, if the service will be supported.
356 public boolean supportsService( String sServiceName ) {
357 return sServiceName.equals( __serviceName );
360 // Implement the interface XServiceInfo
361 /** Get the implementation name of the component.
362 * @return Implementation name of the component.
364 public String getImplementationName() {
365 return _Inspector.class.getName();
369 private int getSourceCodeLanguage(){
370 int nLanguage = XLanguageSourceCodeGenerator.nJAVA;
371 try{
372 XNameAccess xNameAccess = getConfigurationAccess("org.openoffice.inspector.ObjectInspector", false);
373 String sLanguage = (String) xNameAccess.getByName("Language");
374 if (sLanguage.toUpperCase().equals("JAVA")){
375 nLanguage = XLanguageSourceCodeGenerator.nJAVA;
377 else if (sLanguage.toUpperCase().equals("BASIC")){
378 nLanguage = XLanguageSourceCodeGenerator.nBASIC;
380 else if (sLanguage.toUpperCase().equals("CPLUSPLUS")){
381 nLanguage = XLanguageSourceCodeGenerator.nCPLUSPLUS;
383 else{
384 System.out.println("Warning: Sourcecode language " + sLanguage + " is not defined!");
386 m_oSwingDialogProvider.selectSourceCodeLanguage(nLanguage);
387 }catch( Exception exception ) {
388 exception.printStackTrace(System.err);
390 return nLanguage;
394 private XNameAccess getConfigurationAccess(boolean _bUpdate){
395 return getConfigurationAccess("org.openoffice.inspector.ObjectInspector", _bUpdate);
399 private XNameAccess getConfigurationAccess(String _sNodePath, boolean update) {
400 XNameAccess xNameAccess = null;
401 try {
402 String sAccess = "";
403 if (update) {
404 sAccess = "com.sun.star.configuration.ConfigurationUpdateAccess";
406 else{
407 sAccess = "com.sun.star.configuration.ConfigurationAccess";
409 XMultiComponentFactory xMCF = m_xComponentContext.getServiceManager();
410 XMultiServiceFactory xMSFCfg = theDefaultProvider.get(this.getXComponentContext());
411 Object oAccess = xMSFCfg.createInstanceWithArguments(sAccess, new Object[]{new NamedValue("nodepath", _sNodePath)});
412 xNameAccess = UnoRuntime.queryInterface(XNameAccess.class, oAccess);
413 } catch (com.sun.star.uno.Exception e) {
415 return xNameAccess;
421 // end of inner class
425 * Gives a factory for creating the service.
426 * This method is called by the <code>JavaLoader</code>
427 * <p>
428 * @return returns a <code>XSingleComponentFactory</code> for creating
429 * the component
430 * @param sImplName the name of the implementation for which a
431 * service is desired
432 * @see com.sun.star.comp.loader.JavaLoader
434 public static XSingleComponentFactory __getComponentFactory( String sImplName )
436 XSingleComponentFactory xFactory = null;
437 if ( sImplName.equals( _Inspector.class.getName() ) )
438 xFactory = Factory.createComponentFactory(_Inspector.class, _Inspector.getServiceNames());
439 if ( xFactory == null )
440 xFactory = InspectorAddon.__getComponentFactory(sImplName);
441 return xFactory;
445 * Writes the service information into the given registry key.
446 * This method is called by the <code>JavaLoader</code>
447 * <p>
448 * @return returns true if the operation succeeded
449 * @param regKey the registryKey
450 * @see com.sun.star.comp.loader.JavaLoader
452 public static boolean __writeRegistryServiceInfo(XRegistryKey regKey) {
453 return (Factory.writeRegistryServiceInfo(_Inspector.class.getName(), _Inspector.getServiceNames(), regKey)
454 && InspectorAddon.__writeRegistryServiceInfo(regKey));