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