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 package integration.extensions; 25 26 import com.sun.star.uno.XComponentContext; 27 import com.sun.star.uno.UnoRuntime; 28 import com.sun.star.lang.XMultiServiceFactory; 29 30 import com.sun.star.frame.*; 31 import com.sun.star.inspection.*; 32 import com.sun.star.beans.*; 33 34 import integration.extensions.Frame; 35 36 import org.junit.After; 37 import org.junit.AfterClass; 38 import org.junit.Before; 39 import org.junit.BeforeClass; 40 import org.junit.Test; 41 import static org.junit.Assert.*; 42 import org.openoffice.test.OfficeConnection; 43 44 public class ObjectInspector 45 { 46 private static final OfficeConnection officeConnection = new OfficeConnection(); 47 private XComponentContext m_context; 48 private XMultiServiceFactory m_orb; 49 private Frame m_desktop; 50 51 final private String m_inspectorFrameName = new String( "ObjectInspector" ); 52 53 @BeforeClass beforeClass()54 public static void beforeClass() throws Exception 55 { 56 officeConnection.setUp(); 57 } 58 59 @AfterClass afterClass()60 public static void afterClass() throws Exception 61 { 62 officeConnection.tearDown(); 63 } 64 65 /** Creates a new instance of ValueBinding */ ObjectInspector()66 public ObjectInspector() 67 { 68 } 69 70 /* ------------------------------------------------------------------ */ 71 @Before before()72 public void before() throws com.sun.star.uno.Exception, java.lang.Exception 73 { 74 m_orb = UnoRuntime.queryInterface(XMultiServiceFactory.class, officeConnection.getComponentContext().getServiceManager()); 75 m_context = (XComponentContext)UnoRuntime.queryInterface( XComponentContext.class, 76 ((XPropertySet)UnoRuntime.queryInterface( XPropertySet.class, m_orb )).getPropertyValue( "DefaultContext" ) ); 77 m_desktop = new Frame( m_orb.createInstance( "com.sun.star.frame.Desktop" ) ); 78 } 79 80 /* ------------------------------------------------------------------ */ 81 @After after()82 public void after() throws com.sun.star.uno.Exception, java.lang.Exception 83 { 84 closeExistentInspector(); 85 } 86 87 /* ------------------------------------------------------------------ */ 88 @Test interactiveObjectInspector()89 public void interactiveObjectInspector() throws com.sun.star.uno.Exception, java.lang.Exception 90 { 91 closeExistentInspector(); 92 93 // the to-be-inspected object 94 XFrame inspectee = m_desktop.getActiveFrame(); 95 96 // the inspector 97 XObjectInspector inspector = createObjectInspector(); 98 99 // do inspect 100 inspector.inspect( new Object[] { inspectee } ); 101 102 ConsoleWait keyWaiter = new ConsoleWait( inspector ); 103 keyWaiter.waitForUserInput(); 104 } 105 106 /* ------------------------------------------------------------------ */ createObjectInspector()107 private XObjectInspector createObjectInspector() throws com.sun.star.uno.Exception 108 { 109 com.sun.star.awt.XWindow floatingWindow = createFloatingWindow(); 110 111 Frame inspectorFrame = new Frame( m_orb.createInstance( "com.sun.star.frame.Frame" ) ); 112 inspectorFrame.setName( m_inspectorFrameName ); 113 inspectorFrame.initialize( floatingWindow ); 114 m_desktop.getFrames().append( inspectorFrame.getXFrame() ); 115 116 // handler factories: 117 Object[] handlerFactories = new Object[] { 118 "com.sun.star.inspection.GenericPropertyHandler", 119 new ComponentFactory( ServicesHandler.class ), 120 new ComponentFactory( MethodHandler.class ) 121 }; 122 // a model 123 XObjectInspectorModel model = ObjectInspectorModel.createWithHandlerFactoriesAndHelpSection( 124 m_context, handlerFactories, 4, 4 ); 125 126 // create the ObjectInspector 127 XObjectInspector inspector = com.sun.star.inspection.ObjectInspector.createWithModel( 128 m_context, model ); 129 130 // add an observer which will emit help texts 131 new HelpTextProvider( inspector.getInspectorUI() ); 132 133 // plug it into the frame 134 inspector.attachFrame( inspectorFrame.getXFrame() ); 135 136 // make the window visible 137 floatingWindow.setVisible( true ); 138 139 // outta here 140 return inspector; 141 } 142 143 /* ------------------------------------------------------------------ */ closeExistentInspector()144 private void closeExistentInspector() 145 { 146 Frame existentInspectorFrame = new Frame( m_desktop.findFrame( m_inspectorFrameName, 255 ) ); 147 if ( existentInspectorFrame != null ) 148 { 149 try 150 { 151 existentInspectorFrame.close( true ); 152 } 153 catch( com.sun.star.util.CloseVetoException e ) 154 { 155 fail( "could not close the existent inspector frame" ); 156 } 157 } 158 } 159 160 /* ------------------------------------------------------------------ */ createFloatingWindow()161 private com.sun.star.awt.XWindow createFloatingWindow() throws com.sun.star.uno.Exception 162 { 163 com.sun.star.awt.XToolkit toolkit = (com.sun.star.awt.XToolkit)UnoRuntime.queryInterface( 164 com.sun.star.awt.XToolkit.class, m_orb.createInstance( "com.sun.star.awt.Toolkit" ) ); 165 166 com.sun.star.awt.WindowDescriptor windowDescriptor = new com.sun.star.awt.WindowDescriptor(); 167 windowDescriptor.Type = com.sun.star.awt.WindowClass.TOP; 168 windowDescriptor.WindowServiceName = "modelessdialog"; // "floatingwindow" would need a parent 169 windowDescriptor.ParentIndex = -1; 170 //windowDescriptor.Parent = null; 171 172 windowDescriptor.Bounds = new com.sun.star.awt.Rectangle( 500, 100, 400, 600 ); 173 windowDescriptor.WindowAttributes = com.sun.star.awt.WindowAttribute.BORDER 174 + com.sun.star.awt.WindowAttribute.MOVEABLE 175 + com.sun.star.awt.WindowAttribute.SIZEABLE 176 + com.sun.star.awt.WindowAttribute.CLOSEABLE 177 + com.sun.star.awt.VclWindowPeerAttribute.CLIPCHILDREN; 178 179 return (com.sun.star.awt.XWindow)UnoRuntime.queryInterface( com.sun.star.awt.XWindow.class, 180 toolkit.createWindow( windowDescriptor ) ); 181 } 182 } 183