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