Update git submodules
[LibreOffice.git] / odk / examples / java / Inspector / Inspector.java
blob18d8d909a449f27c9b204cbdf43f26665298498e
1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
5 * the BSD license.
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
31 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *************************************************************************/
36 import java.util.ArrayList;
37 import java.util.HashMap;
38 import java.util.Iterator;
39 import java.util.Set;
41 import org.openoffice.XInstanceInspector;
43 import com.sun.star.awt.XMessageBox;
44 import com.sun.star.awt.XMessageBoxFactory;
45 import com.sun.star.awt.XWindowPeer;
46 import com.sun.star.beans.NamedValue;
47 import com.sun.star.beans.XPropertySet;
48 import com.sun.star.configuration.theDefaultProvider;
49 import com.sun.star.container.XNameAccess;
50 import com.sun.star.frame.XModel;
51 import com.sun.star.lang.XComponent;
52 import com.sun.star.lang.XMultiComponentFactory;
53 import com.sun.star.lang.XMultiServiceFactory;
54 import com.sun.star.lang.XServiceInfo;
55 import com.sun.star.lang.XSingleComponentFactory;
56 import com.sun.star.lib.uno.helper.Factory;
57 import com.sun.star.lib.uno.helper.WeakBase;
58 import com.sun.star.registry.XRegistryKey;
59 import com.sun.star.ui.dialogs.XExecutableDialog;
60 import com.sun.star.ui.dialogs.XFolderPicker2;
61 import com.sun.star.uno.UnoRuntime;
62 import com.sun.star.uno.XComponentContext;
63 import com.sun.star.util.CloseVetoException;
64 import com.sun.star.util.XChangesBatch;
65 import com.sun.star.util.XCloseable;
68 /** The purpose of this class is to provide a factory for creating the service
69 * (<CODE>__getServiceFactory</CODE>) and writes the information into the given
70 * registry key (<CODE>__writeRegistryServiceInfo</CODE>).
72 public class Inspector{
73 public static final String sIDLDOCUMENTSUBFOLDER = "docs/idl/ref/";
76 /** This class implements the method of the interface XInstanceInspector.
77 * Also the class implements the interfaces XServiceInfo, and XTypeProvider.
79 public static class _Inspector extends WeakBase implements XInstanceInspector, XServiceInfo{
81 private static final String __serviceName = "org.openoffice.InstanceInspector";
82 private HashMap<String, String> aApplicationHashMap = new HashMap<String, String>();
83 private String sTitle = "Object Inspector";
84 private ArrayList<XComponent> aHiddenDocuments = new ArrayList<XComponent>();
85 private XComponentContext m_xComponentContext;
86 private HashMap<String, InspectorPane> aInspectorPanes = new HashMap<String, InspectorPane>();
87 private XDialogProvider m_oSwingDialogProvider;
88 private TDocSupplier oTDocSupplier;
89 private Introspector m_oIntrospector = null;
90 // TODO: improve these strings:
91 private static final String sWRONGINSTALLATIONPATH = "Your selected path does not refer to an SDK-Installation!";
92 /** Creates a new instance of Dialog */
93 public _Inspector(XComponentContext _xComponentContext) {
94 m_xComponentContext = _xComponentContext;
95 m_oIntrospector = Introspector.getIntrospector(m_xComponentContext);
96 aApplicationHashMap.put("private:factory/swriter", "Text Document");
97 aApplicationHashMap.put("private:factory/scalc", "Spreadsheet");
98 aApplicationHashMap.put("private:factory/simpress", "Presentation");
99 aApplicationHashMap.put("private:factory/sdraw", "Drawing");
100 aApplicationHashMap.put("private:factory/smath", "Formula");
101 m_oSwingDialogProvider = new SwingDialogProvider(this, sTitle);
105 public XComponentContext getXComponentContext(){
106 return m_xComponentContext;
110 public HashMap<String, InspectorPane> getInspectorPages(){
111 return aInspectorPanes;
115 private String getSDKPath(){
116 String sRetPath = "";
117 try{
118 XNameAccess xNameAccess = getConfigurationAccess("org.openoffice.inspector.ObjectInspector", true);
119 XPropertySet xPropertySet = UnoRuntime.queryInterface(XPropertySet.class, xNameAccess);
120 sRetPath = (String) xPropertySet.getPropertyValue("SDKPath");
121 }catch( Exception exception ) {
122 exception.printStackTrace(System.err);
124 return sRetPath;
128 public String getIDLPath(){
129 String sRetPath = getSDKPath();
130 if (m_oIntrospector.isValidSDKInstallationPath(sRetPath)){
131 sRetPath = Introspector.addToPath(sRetPath, sIDLDOCUMENTSUBFOLDER);
133 else{
134 sRetPath = "";
136 return sRetPath;
140 public void openIdlFileforSelectedNode(){
141 InspectorPane oInspectorPane = m_oSwingDialogProvider.getSelectedInspectorPage();
142 if (oInspectorPane != null){
143 XUnoNode oUnoNode = oInspectorPane.getSelectedNode();
144 if (oUnoNode != null){
145 String sPath = getIDLPath();
146 oUnoNode.openIdlDescription(sPath);
152 public void assignSDKPath() {
153 try {
154 String sInstallationFolder = "";
155 Object oFolderPicker = m_xComponentContext.getServiceManager().createInstanceWithContext("com.sun.star.ui.dialogs.FolderPicker", m_xComponentContext);
156 XFolderPicker2 xFolderPicker = UnoRuntime.queryInterface(XFolderPicker2.class, oFolderPicker);
157 XExecutableDialog xExecutable = UnoRuntime.queryInterface(XExecutableDialog.class, oFolderPicker);
158 XComponent xComponent = UnoRuntime.queryInterface(XComponent.class, oFolderPicker);
159 String sPath = getSDKPath();
160 if (!sPath.equals("")){
161 xFolderPicker.setDisplayDirectory(sPath);
163 xFolderPicker.setTitle("Add the Path to your SDK installation");
164 short nResult = xExecutable.execute();
165 if (nResult == com.sun.star.ui.dialogs.ExecutableDialogResults.OK){
166 sInstallationFolder = xFolderPicker.getDirectory();
167 if (m_oIntrospector.isValidSDKInstallationPath(sInstallationFolder)){
168 XNameAccess xNameAccess = getConfigurationAccess(true);
169 XPropertySet xPropertySet = UnoRuntime.queryInterface(XPropertySet.class, xNameAccess);
170 xPropertySet.setPropertyValue("SDKPath", sInstallationFolder);
171 XChangesBatch xBatch = UnoRuntime.queryInterface(XChangesBatch.class, xNameAccess);
172 xBatch.commitChanges();
174 else{
175 XPropertySet xPropertySet = UnoRuntime.queryInterface(XPropertySet.class, xFolderPicker);
176 Object oWindow = xPropertySet.getPropertyValue("Window");
177 XWindowPeer xWindowPeer = UnoRuntime.queryInterface(XWindowPeer.class, oWindow);
178 showErrorMessageBox(xWindowPeer, sTitle, sWRONGINSTALLATIONPATH);
179 assignSDKPath();
182 xComponent.dispose();
183 }catch( Exception exception ) {
184 exception.printStackTrace(System.err);
188 private void showErrorMessageBox(XWindowPeer _xWindowPeer, String _sTitle, String _sMessage){
189 try {
190 Object oToolkit = m_xComponentContext.getServiceManager().createInstanceWithContext("com.sun.star.awt.Toolkit", m_xComponentContext);
191 XMessageBoxFactory xMessageBoxFactory = UnoRuntime.queryInterface(XMessageBoxFactory.class, oToolkit);
192 XMessageBox xMessageBox = xMessageBoxFactory.createMessageBox(_xWindowPeer, com.sun.star.awt.MessageBoxType.ERRORBOX, com.sun.star.awt.MessageBoxButtons.BUTTONS_OK, _sTitle, _sMessage);
193 XComponent xComponent = UnoRuntime.queryInterface(XComponent.class, xMessageBox);
194 if (xMessageBox != null){
195 xMessageBox.execute();
196 xComponent.dispose();
198 } catch (com.sun.star.uno.Exception ex) {
199 ex.printStackTrace(System.err);
203 public void inspect(java.lang.Object _oUserDefinedObject, String _sTitle) throws com.sun.star.uno.RuntimeException {
204 try {
205 int nPageIndex = m_oSwingDialogProvider.getInspectorPageCount();
206 SwingTreeControlProvider oSwingTreeControlProvider = new SwingTreeControlProvider(m_oSwingDialogProvider);
207 InspectorPane oInspectorPane = new InspectorPane(getXComponentContext(), m_oSwingDialogProvider, oSwingTreeControlProvider, getSourceCodeLanguage());
208 oInspectorPane.inspect(_oUserDefinedObject, _sTitle);
209 getInspectorPages().put(_sTitle, oInspectorPane);
210 m_oSwingDialogProvider.show(nPageIndex);
211 }catch( Exception exception ) {
212 exception.printStackTrace(System.err);
216 public void inspectOpenEmptyDocument(String _sApplicationDocUrl){
217 XComponent xComponent = getTDocSupplier().openEmptyDocument(_sApplicationDocUrl);
218 String sRootTitle = aApplicationHashMap.get(_sApplicationDocUrl);
219 inspect(xComponent, sRootTitle);
220 aHiddenDocuments.add(xComponent);
224 public void inspectOpenDocument(String _sTDocUrl){
225 String sTreeNodeName = getTDocSupplier().getTitleByTDocUrl(_sTDocUrl);
226 XModel xTDocModel = getTDocSupplier().getXModelByTDocUrl(_sTDocUrl);
227 inspect(xTDocModel, sTreeNodeName);
231 public void inspectSelectedNode(){
232 InspectorPane oInspectorPane = m_oSwingDialogProvider.getSelectedInspectorPage();
233 if (oInspectorPane != null){
234 XUnoNode oUnoNode = oInspectorPane.getSelectedNode();
235 Object oUnoObject = oUnoNode.getUnoObject();
236 if (oUnoObject != null){
237 String sNodeDescription = UnoNode.getNodeDescription(oUnoObject);
238 inspect(oUnoObject, sNodeDescription);
244 public void addSourceCodeOfSelectedNode(){
245 InspectorPane oInspectorPane = m_oSwingDialogProvider.getSelectedInspectorPage();
246 if (oInspectorPane != null){
247 oInspectorPane.addSourceCodeOfSelectedNode();
252 public void invokeSelectedMethod(){
253 InspectorPane oInspectorPane = m_oSwingDialogProvider.getSelectedInspectorPage();
254 if (oInspectorPane != null){
255 oInspectorPane.invokeSelectedMethodNode();
260 public void setSourceCodeLanguage(final int _nLanguage){
261 try{
262 String sLanguage = "Java";
263 XNameAccess xNameAccess = getConfigurationAccess("org.openoffice.inspector.ObjectInspector", true);
264 XPropertySet xPropertySet = UnoRuntime.queryInterface(XPropertySet.class, xNameAccess);
265 switch (_nLanguage){
266 case XLanguageSourceCodeGenerator.nJAVA:
267 sLanguage = "Java";
268 break;
269 case XLanguageSourceCodeGenerator.nCPLUSPLUS:
270 sLanguage = "CPlusPlus";
271 break;
272 case XLanguageSourceCodeGenerator.nBASIC:
273 sLanguage = "Basic";
274 break;
275 default:
276 System.out.println("Warning: Sourcecode language is not defined!");
278 xPropertySet.setPropertyValue("Language", sLanguage);
279 XChangesBatch xBatch = UnoRuntime.queryInterface(XChangesBatch.class, xNameAccess);
280 xBatch.commitChanges();
281 for (int i = 0; i < m_oSwingDialogProvider.getInspectorPageCount(); i++){
282 m_oSwingDialogProvider.getInspectorPage(i).convertCompleteSourceCode(_nLanguage);
284 }catch( Exception exception ) {
285 exception.printStackTrace(System.err);
289 private TDocSupplier getTDocSupplier(){
290 if (oTDocSupplier == null){
291 oTDocSupplier = new TDocSupplier(m_xComponentContext);
293 return oTDocSupplier;
296 public String[] getTDocUrls(){
297 return getTDocSupplier().getTDocUrls();
301 public String[] getTDocTitles(String[] _sTDocUrls){
302 return getTDocSupplier().getTDocTitles(_sTDocUrls);
306 public String[][] getApplicationUrls(){
307 Set<String> aSet = aApplicationHashMap.keySet();
308 String[][] sReturnList = new String[aSet.size()][];
309 int n= 0;
310 for ( Iterator<String> i = aSet.iterator(); i.hasNext(); ){
311 String[] sSingleApplication = new String[2];
312 sSingleApplication[0] = i.next();
313 // assign the title in the second index
314 sSingleApplication[1] = aApplicationHashMap.get(sSingleApplication[0]);
315 sReturnList[n++] = sSingleApplication;
317 return sReturnList;
321 public void disposeHiddenDocuments(){
322 int nHiddenCount = aHiddenDocuments.size();
323 if (nHiddenCount > 0){
324 for (int i = nHiddenCount - 1; i >= 0; i--){
325 XComponent xComponent = aHiddenDocuments.get(i);
326 if (xComponent != null){
327 try {
328 XCloseable xCloseable = UnoRuntime.queryInterface(XCloseable.class, xComponent);
329 xCloseable.close(true);
330 aHiddenDocuments.remove(i);
331 } catch (CloseVetoException ex) {
332 ex.printStackTrace();
340 private static String[] getServiceNames() {
341 String[] sSupportedServiceNames = { __serviceName };
342 return sSupportedServiceNames;
345 // Implement the interface XServiceInfo
346 /** Get all supported service names.
347 * @return Supported service names.
349 public String[] getSupportedServiceNames() {
350 return getServiceNames();
353 // Implement the interface XServiceInfo
354 /** Test, if the given service will be supported.
355 * @return Return true, if the service will be supported.
357 public boolean supportsService( String sServiceName ) {
358 return sServiceName.equals( __serviceName );
361 // Implement the interface XServiceInfo
362 /** Get the implementation name of the component.
363 * @return Implementation name of the component.
365 public String getImplementationName() {
366 return _Inspector.class.getName();
370 private int getSourceCodeLanguage(){
371 int nLanguage = XLanguageSourceCodeGenerator.nJAVA;
372 try{
373 XNameAccess xNameAccess = getConfigurationAccess("org.openoffice.inspector.ObjectInspector", false);
374 String sLanguage = (String) xNameAccess.getByName("Language");
375 if (sLanguage.toUpperCase().equals("JAVA")){
376 nLanguage = XLanguageSourceCodeGenerator.nJAVA;
378 else if (sLanguage.toUpperCase().equals("BASIC")){
379 nLanguage = XLanguageSourceCodeGenerator.nBASIC;
381 else if (sLanguage.toUpperCase().equals("CPLUSPLUS")){
382 nLanguage = XLanguageSourceCodeGenerator.nCPLUSPLUS;
384 else{
385 System.out.println("Warning: Sourcecode language " + sLanguage + " is not defined!");
387 m_oSwingDialogProvider.selectSourceCodeLanguage(nLanguage);
388 }catch( Exception exception ) {
389 exception.printStackTrace(System.err);
391 return nLanguage;
395 private XNameAccess getConfigurationAccess(boolean _bUpdate){
396 return getConfigurationAccess("org.openoffice.inspector.ObjectInspector", _bUpdate);
400 private XNameAccess getConfigurationAccess(String _sNodePath, boolean update) {
401 XNameAccess xNameAccess = null;
402 try {
403 String sAccess = "";
404 if (update) {
405 sAccess = "com.sun.star.configuration.ConfigurationUpdateAccess";
407 else{
408 sAccess = "com.sun.star.configuration.ConfigurationAccess";
410 XMultiComponentFactory xMCF = m_xComponentContext.getServiceManager();
411 XMultiServiceFactory xMSFCfg = theDefaultProvider.get(this.getXComponentContext());
412 Object oAccess = xMSFCfg.createInstanceWithArguments(sAccess, new Object[]{new NamedValue("nodepath", _sNodePath)});
413 xNameAccess = UnoRuntime.queryInterface(XNameAccess.class, oAccess);
414 } catch (com.sun.star.uno.Exception e) {
416 return xNameAccess;
422 // end of inner class
426 * Gives a factory for creating the service.
427 * This method is called by the <code>JavaLoader</code>
428 * <p>
429 * @return returns a <code>XSingleComponentFactory</code> for creating
430 * the component
431 * @param sImplName the name of the implementation for which a
432 * service is desired
433 * @see com.sun.star.comp.loader.JavaLoader
435 public static XSingleComponentFactory __getComponentFactory( String sImplName )
437 XSingleComponentFactory xFactory = null;
438 if ( sImplName.equals( _Inspector.class.getName() ) )
439 xFactory = Factory.createComponentFactory(_Inspector.class, _Inspector.getServiceNames());
440 if ( xFactory == null )
441 xFactory = InspectorAddon.__getComponentFactory(sImplName);
442 return xFactory;
446 * Writes the service information into the given registry key.
447 * This method is called by the <code>JavaLoader</code>
448 * <p>
449 * @return returns true if the operation succeeded
450 * @param regKey the registryKey
451 * @see com.sun.star.comp.loader.JavaLoader
453 public static boolean __writeRegistryServiceInfo(XRegistryKey regKey) {
454 return (Factory.writeRegistryServiceInfo(_Inspector.class.getName(), _Inspector.getServiceNames(), regKey)
455 && InspectorAddon.__writeRegistryServiceInfo(regKey));
459 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */