1 /************************************************************************* 2 * 3 * The Contents of this file are made available subject to the terms of 4 * the BSD license. 5 * 6 * Copyright 2000, 2010 Oracle and/or its affiliates. 7 * All rights reserved. 8 * 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. 20 * 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. 32 * 33 *************************************************************************/ 34 import com.sun.star.awt.PushButtonType; 35 import com.sun.star.awt.XControl; 36 import com.sun.star.awt.XDialog; 37 import com.sun.star.awt.XFixedText; 38 import com.sun.star.awt.XListBox; 39 import com.sun.star.awt.XWindow; 40 import com.sun.star.beans.MethodConcept; 41 import com.sun.star.beans.Property; 42 import com.sun.star.beans.PropertyValue; 43 import com.sun.star.beans.XIntrospection; 44 import com.sun.star.beans.XIntrospectionAccess; 45 import com.sun.star.beans.XMultiPropertySet; 46 import com.sun.star.beans.XPropertySet; 47 import com.sun.star.frame.XComponentLoader; 48 import com.sun.star.lang.XMultiComponentFactory; 49 import com.sun.star.lang.XServiceInfo; 50 import com.sun.star.lang.XTypeProvider; 51 import com.sun.star.reflection.XIdlMethod; 52 import com.sun.star.uno.Type; 53 import com.sun.star.uno.UnoRuntime; 54 import com.sun.star.uno.XComponentContext; 55 56 public class UnoDialogSample2 extends UnoDialogSample { 57 XIntrospectionAccess m_xIntrospectionAccess = null; 58 Object m_oUnoObject = null; 59 // define some constants used to set positions and sizes 60 // of controls. For further information see 61 // http://ui.openoffice.org/knowledge/DialogSpecificationandGuidelines.odt 62 final static int nFixedTextHeight = 8; 63 final static int nControlMargin = 6; 64 final static int nDialogWidth = 250; 65 final static int nDialogHeight = 140; 66 // the default roadmap width == 80 MAPs 67 final static int nRoadmapWidth = 80; 68 final static int nButtonHeight = 14; 69 final static int nButtonWidth = 50; 70 71 72 public UnoDialogSample2(XComponentContext _xContext, XMultiComponentFactory _xMCF, Object _oUnoObject) { 73 super(_xContext, _xMCF); 74 try { 75 m_oUnoObject = _oUnoObject; 76 Object o = m_xMCF.createInstanceWithContext("com.sun.star.beans.Introspection", m_xContext); 77 XIntrospection m_xIntrospection = ( XIntrospection ) UnoRuntime.queryInterface(XIntrospection.class, o ); 78 // the variable m_xIntrospectionAccess offers functionality to access all methods and properties 79 // of a variable 80 m_xIntrospectionAccess = m_xIntrospection.inspect(_oUnoObject); 81 } catch (com.sun.star.uno.Exception ex) { 82 ex.printStackTrace(); 83 } 84 } 85 86 public static void main(String args[]) { 87 UnoDialogSample2 oUnoDialogSample2 = null; 88 try { 89 XComponentContext xContext = com.sun.star.comp.helper.Bootstrap.bootstrap(); 90 if(xContext != null ) 91 System.out.println("Connected to a running office ..."); 92 XMultiComponentFactory xMCF = xContext.getServiceManager(); 93 PropertyValue[] aPropertyValues = new PropertyValue[]{}; 94 // create an arbitrary Uno-Object - in this case an empty writer document.. 95 Object oDesktop =xMCF.createInstanceWithContext("com.sun.star.frame.Desktop", xContext); 96 XComponentLoader xComponentLoader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, oDesktop); 97 Object oUnoObject = xComponentLoader.loadComponentFromURL("private:factory/swriter", "_default", 0, aPropertyValues); 98 99 // define some coordinates where to position the controls 100 final int nButtonPosX = (int)((nDialogWidth/2) - (nButtonWidth/2)); 101 final int nButtonPosY = nDialogHeight - nButtonHeight - nControlMargin; 102 final int nControlPosX = nRoadmapWidth + 2*nControlMargin; 103 final int nControlWidth = nDialogWidth - 3*nControlMargin - nRoadmapWidth; 104 final int nListBoxHeight = nDialogHeight - 4*nControlMargin - nButtonHeight; 105 oUnoDialogSample2 = new UnoDialogSample2(xContext, xMCF, oUnoObject); 106 oUnoDialogSample2.initialize( new String[] {"Height", "Moveable", "Name","PositionX","PositionY", "Step", "TabIndex","Title","Width"}, 107 new Object[] { new Integer(nDialogHeight), Boolean.TRUE, "Dialog1", new Integer(102),new Integer(41), new Integer(1), new Short((short) 0), "Inspect a Uno-Object", new Integer(nDialogWidth)}); 108 String sIntroLabel = "This Dialog lists information about a given Uno-Object.\nIt offers a view to inspect all suppported servicenames, exported interfaces, methods and properties."; 109 oUnoDialogSample2.insertMultiLineFixedText(nControlPosX, 27, nControlWidth, 4, 1, sIntroLabel); 110 // get the data from the UNO object... 111 String[] sSupportedServiceNames = oUnoDialogSample2.getSupportedServiceNames(); 112 String[] sInterfaceNames = oUnoDialogSample2.getExportedInterfaceNames(); 113 String[] sMethodNames = oUnoDialogSample2.getMethodNames(); 114 String[] sPropertyNames = oUnoDialogSample2.getPropertyNames(); 115 // add controls to the dialog... 116 oUnoDialogSample2.insertListBox(nControlPosX, nControlMargin, nListBoxHeight, nControlWidth, 2, sSupportedServiceNames); 117 oUnoDialogSample2.insertListBox(nControlPosX, nControlMargin, nListBoxHeight, nControlWidth, 3, sInterfaceNames); 118 oUnoDialogSample2.insertListBox(nControlPosX, nControlMargin, nListBoxHeight, nControlWidth, 4, sMethodNames); 119 oUnoDialogSample2.insertListBox(nControlPosX, nControlMargin, nListBoxHeight, nControlWidth, 5, sPropertyNames); 120 oUnoDialogSample2.insertButton(oUnoDialogSample2, nButtonPosX, nButtonPosY, nButtonWidth, "~Close", (short) PushButtonType.OK_value); 121 oUnoDialogSample2.insertHorizontalFixedLine(0, nButtonPosY - nControlMargin - 4, nDialogWidth, ""); 122 // create the windowpeer; 123 // it must be kept in mind that it must be created after the insertion of the controls 124 // (see http://qa.openoffice.org/issues/show_bug.cgi?id=75129) 125 oUnoDialogSample2.createWindowPeer(); 126 // add the roadmap control. Note that the roadmap may not be created before the windowpeer of the dialog exists 127 // (see http://qa.openoffice.org/issues/show_bug.cgi?id=67369) 128 oUnoDialogSample2.addRoadmap(oUnoDialogSample2.getRoadmapItemStateChangeListener()); 129 oUnoDialogSample2.insertRoadmapItem(0, true, "Introduction", 1); 130 oUnoDialogSample2.insertRoadmapItem(1, true, "Supported Services", 2); 131 oUnoDialogSample2.insertRoadmapItem(2, true, "Interfaces", 3); 132 oUnoDialogSample2.insertRoadmapItem(3, true, "Methods", 4); 133 oUnoDialogSample2.insertRoadmapItem(4, true, "Properties", 5); 134 oUnoDialogSample2.m_xRMPSet.setPropertyValue("CurrentItemID", new Short((short) 1)); 135 oUnoDialogSample2.m_xRMPSet.setPropertyValue("Complete", Boolean.TRUE); 136 oUnoDialogSample2.xDialog = (XDialog) UnoRuntime.queryInterface(XDialog.class, oUnoDialogSample2.m_xDialogControl); 137 oUnoDialogSample2.xDialog.execute(); 138 }catch( Exception ex ) { 139 ex.printStackTrace(System.out); 140 } 141 finally{ 142 //make sure always to dispose the component and free the memory! 143 if (oUnoDialogSample2 != null){ 144 if (oUnoDialogSample2.m_xComponent != null){ 145 oUnoDialogSample2.m_xComponent.dispose(); 146 } 147 } 148 } 149 150 System.exit( 0 ); 151 } 152 153 154 public String[] getMethodNames() { 155 String[] sMethodNames = new String[]{}; 156 try { 157 XIdlMethod[] xIdlMethods = m_xIntrospectionAccess.getMethods(MethodConcept.ALL); 158 sMethodNames = new String[xIdlMethods.length]; 159 for (int i = 0; i < xIdlMethods.length; i++){ 160 sMethodNames[i] = xIdlMethods[i].getName(); 161 } 162 } 163 catch( Exception e ) { 164 System.err.println( e ); 165 } 166 return sMethodNames; 167 } 168 169 // returns the names of all supported servicenames of a UNO object 170 public String[] getSupportedServiceNames() { 171 String[] sSupportedServiceNames = new String[]{}; 172 // If the Uno-Object supports "com.sun.star.lang.XServiceInfo" 173 // this will give access to all supported servicenames 174 XServiceInfo xServiceInfo = ( XServiceInfo ) UnoRuntime.queryInterface( XServiceInfo.class, m_oUnoObject); 175 if ( xServiceInfo != null ) { 176 sSupportedServiceNames = xServiceInfo.getSupportedServiceNames(); 177 } 178 return sSupportedServiceNames; 179 } 180 181 // returns the names of all properties of a UNO object 182 protected String[] getPropertyNames() { 183 String[] sPropertyNames = new String[]{}; 184 try { 185 Property[] aProperties = m_xIntrospectionAccess.getProperties(com.sun.star.beans.PropertyConcept.ATTRIBUTES + com.sun.star.beans.PropertyConcept.PROPERTYSET); 186 sPropertyNames = new String[aProperties.length]; 187 for (int i = 0; i < aProperties.length; i++){ 188 sPropertyNames[i] = aProperties[i].Name; 189 } 190 } 191 catch( Exception e ) { 192 System.err.println( e ); 193 } 194 return sPropertyNames; 195 } 196 197 198 // returns the names of all exported interfaces of a UNO object 199 protected String[] getExportedInterfaceNames(){ 200 Type[] aTypes = new Type[]{}; 201 String[] sTypeNames = new String[]{}; 202 // The XTypeProvider interfaces offers access to all exported interfaces 203 XTypeProvider xTypeProvider = ( XTypeProvider ) UnoRuntime.queryInterface( XTypeProvider.class, m_oUnoObject); 204 if ( xTypeProvider != null ) { 205 aTypes = xTypeProvider.getTypes(); 206 sTypeNames = new String[aTypes.length]; 207 for (int i = 0; i < aTypes.length - 1; i++){ 208 sTypeNames[i] = aTypes[i].getTypeName(); 209 } 210 } 211 return sTypeNames; 212 } 213 214 215 216 public XListBox insertListBox(int _nPosX, int _nPosY, int _nHeight, int _nWidth, int _nStep, String[] _sStringItemList) { 217 XListBox xListBox = null; 218 try{ 219 // create a unique name by means of an own implementation... 220 String sName = createUniqueName(m_xDlgModelNameContainer, "ListBox"); 221 // create a controlmodel at the multiservicefactory of the dialog model... 222 Object oListBoxModel = m_xMSFDialogModel.createInstance("com.sun.star.awt.UnoControlListBoxModel"); 223 XMultiPropertySet xLBModelMPSet = (XMultiPropertySet) UnoRuntime.queryInterface(XMultiPropertySet.class, oListBoxModel); 224 // Set the properties at the model - keep in mind to pass the property names in alphabetical order! 225 xLBModelMPSet.setPropertyValues( 226 new String[] {"Dropdown", "Height", "Name", "PositionX", "PositionY", "ReadOnly", "Step", "StringItemList", "Width" } , 227 new Object[] {Boolean.FALSE, new Integer(_nHeight), sName, new Integer(_nPosX), new Integer(_nPosY), Boolean.TRUE, new Integer(_nStep), _sStringItemList, new Integer(_nWidth)}); 228 m_xDlgModelNameContainer.insertByName(sName, xLBModelMPSet); 229 }catch (com.sun.star.uno.Exception ex) { 230 throw new java.lang.RuntimeException("cannot happen..."); 231 } 232 return xListBox; 233 } 234 235 236 public void insertMultiLineFixedText(int _nPosX, int _nPosY, int _nWidth, int _nLineCount, int _nStep, String _sLabel) { 237 try { 238 // create a unique name by means of an own implementation... 239 String sName = createUniqueName(m_xDlgModelNameContainer, "Label"); 240 int nHeight = _nLineCount * nFixedTextHeight; 241 // create a controlmodel at the multiservicefactory of the dialog model... 242 Object oFTModel = m_xMSFDialogModel.createInstance("com.sun.star.awt.UnoControlFixedTextModel"); 243 XMultiPropertySet xFTModelMPSet = (XMultiPropertySet) UnoRuntime.queryInterface(XMultiPropertySet.class, oFTModel); 244 // Set the properties at the model - keep in mind to pass the property names in alphabetical order! 245 xFTModelMPSet.setPropertyValues( 246 new String[] {"Height", "Label", "MultiLine", "Name", "PositionX", "PositionY", "Step", "Width"}, 247 new Object[] { new Integer(nHeight), _sLabel, Boolean.TRUE, sName, new Integer(_nPosX), new Integer(_nPosY), new Integer(_nStep), new Integer(_nWidth)}); 248 // add the model to the NameContainer of the dialog model 249 m_xDlgModelNameContainer.insertByName(sName, oFTModel); 250 }catch (com.sun.star.uno.Exception ex){ 251 /* perform individual exception handling here. 252 * Possible exception types are: 253 * com.sun.star.lang.IllegalArgumentException, 254 * com.sun.star.lang.WrappedTargetException, 255 * com.sun.star.container.ElementExistException, 256 * com.sun.star.beans.PropertyVetoException, 257 * com.sun.star.beans.UnknownPropertyException, 258 * com.sun.star.uno.Exception 259 */ 260 ex.printStackTrace(System.out); 261 } 262 } 263 264 }// end of class 265