xref: /AOO41X/main/odk/examples/DevelopersGuide/GUI/UnoDialogSample2.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  *  The Contents of this file are made available subject to the terms of
4*cdf0e10cSrcweir  *  the BSD license.
5*cdf0e10cSrcweir  *
6*cdf0e10cSrcweir  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7*cdf0e10cSrcweir  *  All rights reserved.
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  *  Redistribution and use in source and binary forms, with or without
10*cdf0e10cSrcweir  *  modification, are permitted provided that the following conditions
11*cdf0e10cSrcweir  *  are met:
12*cdf0e10cSrcweir  *  1. Redistributions of source code must retain the above copyright
13*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer.
14*cdf0e10cSrcweir  *  2. Redistributions in binary form must reproduce the above copyright
15*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer in the
16*cdf0e10cSrcweir  *     documentation and/or other materials provided with the distribution.
17*cdf0e10cSrcweir  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18*cdf0e10cSrcweir  *     contributors may be used to endorse or promote products derived
19*cdf0e10cSrcweir  *     from this software without specific prior written permission.
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22*cdf0e10cSrcweir  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23*cdf0e10cSrcweir  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24*cdf0e10cSrcweir  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25*cdf0e10cSrcweir  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26*cdf0e10cSrcweir  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27*cdf0e10cSrcweir  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28*cdf0e10cSrcweir  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29*cdf0e10cSrcweir  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30*cdf0e10cSrcweir  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31*cdf0e10cSrcweir  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*cdf0e10cSrcweir  *
33*cdf0e10cSrcweir  *************************************************************************/
34*cdf0e10cSrcweir import com.sun.star.awt.PushButtonType;
35*cdf0e10cSrcweir import com.sun.star.awt.XControl;
36*cdf0e10cSrcweir import com.sun.star.awt.XDialog;
37*cdf0e10cSrcweir import com.sun.star.awt.XFixedText;
38*cdf0e10cSrcweir import com.sun.star.awt.XListBox;
39*cdf0e10cSrcweir import com.sun.star.awt.XWindow;
40*cdf0e10cSrcweir import com.sun.star.beans.MethodConcept;
41*cdf0e10cSrcweir import com.sun.star.beans.Property;
42*cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
43*cdf0e10cSrcweir import com.sun.star.beans.XIntrospection;
44*cdf0e10cSrcweir import com.sun.star.beans.XIntrospectionAccess;
45*cdf0e10cSrcweir import com.sun.star.beans.XMultiPropertySet;
46*cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
47*cdf0e10cSrcweir import com.sun.star.frame.XComponentLoader;
48*cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory;
49*cdf0e10cSrcweir import com.sun.star.lang.XServiceInfo;
50*cdf0e10cSrcweir import com.sun.star.lang.XTypeProvider;
51*cdf0e10cSrcweir import com.sun.star.reflection.XIdlMethod;
52*cdf0e10cSrcweir import com.sun.star.uno.Type;
53*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
54*cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir public class UnoDialogSample2 extends UnoDialogSample {
57*cdf0e10cSrcweir     XIntrospectionAccess m_xIntrospectionAccess = null;
58*cdf0e10cSrcweir     Object m_oUnoObject = null;
59*cdf0e10cSrcweir     // define some constants used to set positions and sizes
60*cdf0e10cSrcweir     // of controls. For further information see
61*cdf0e10cSrcweir     // http://ui.openoffice.org/knowledge/DialogSpecificationandGuidelines.odt
62*cdf0e10cSrcweir     final static int nFixedTextHeight = 8;
63*cdf0e10cSrcweir     final static int nControlMargin = 6;
64*cdf0e10cSrcweir     final static int nDialogWidth = 250;
65*cdf0e10cSrcweir     final static int nDialogHeight = 140;
66*cdf0e10cSrcweir     // the default roadmap width == 80 MAPs
67*cdf0e10cSrcweir     final static int nRoadmapWidth = 80;
68*cdf0e10cSrcweir     final static int nButtonHeight = 14;
69*cdf0e10cSrcweir     final static int nButtonWidth = 50;
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir     public UnoDialogSample2(XComponentContext _xContext, XMultiComponentFactory _xMCF, Object _oUnoObject) {
73*cdf0e10cSrcweir         super(_xContext, _xMCF);
74*cdf0e10cSrcweir         try {
75*cdf0e10cSrcweir             m_oUnoObject = _oUnoObject;
76*cdf0e10cSrcweir             Object o = m_xMCF.createInstanceWithContext("com.sun.star.beans.Introspection", m_xContext);
77*cdf0e10cSrcweir             XIntrospection m_xIntrospection = ( XIntrospection ) UnoRuntime.queryInterface(XIntrospection.class, o );
78*cdf0e10cSrcweir             // the variable m_xIntrospectionAccess offers functionality to access all methods and properties
79*cdf0e10cSrcweir             // of a variable
80*cdf0e10cSrcweir             m_xIntrospectionAccess = m_xIntrospection.inspect(_oUnoObject);
81*cdf0e10cSrcweir         } catch (com.sun.star.uno.Exception ex) {
82*cdf0e10cSrcweir             ex.printStackTrace();
83*cdf0e10cSrcweir         }
84*cdf0e10cSrcweir     }
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir     public static void main(String args[]) {
87*cdf0e10cSrcweir         UnoDialogSample2 oUnoDialogSample2 = null;
88*cdf0e10cSrcweir         try {
89*cdf0e10cSrcweir             XComponentContext xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
90*cdf0e10cSrcweir             if(xContext != null )
91*cdf0e10cSrcweir                 System.out.println("Connected to a running office ...");
92*cdf0e10cSrcweir             XMultiComponentFactory xMCF = xContext.getServiceManager();
93*cdf0e10cSrcweir             PropertyValue[] aPropertyValues = new PropertyValue[]{};
94*cdf0e10cSrcweir             // create an arbitrary Uno-Object - in this case an empty writer document..
95*cdf0e10cSrcweir             Object oDesktop =xMCF.createInstanceWithContext("com.sun.star.frame.Desktop", xContext);
96*cdf0e10cSrcweir             XComponentLoader xComponentLoader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, oDesktop);
97*cdf0e10cSrcweir             Object oUnoObject = xComponentLoader.loadComponentFromURL("private:factory/swriter", "_default", 0, aPropertyValues);
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir             // define some coordinates where to position the controls
100*cdf0e10cSrcweir             final int nButtonPosX = (int)((nDialogWidth/2) - (nButtonWidth/2));
101*cdf0e10cSrcweir             final int nButtonPosY = nDialogHeight - nButtonHeight - nControlMargin;
102*cdf0e10cSrcweir             final int nControlPosX = nRoadmapWidth + 2*nControlMargin;
103*cdf0e10cSrcweir             final int nControlWidth = nDialogWidth - 3*nControlMargin - nRoadmapWidth;
104*cdf0e10cSrcweir             final int nListBoxHeight = nDialogHeight - 4*nControlMargin - nButtonHeight;
105*cdf0e10cSrcweir             oUnoDialogSample2 = new UnoDialogSample2(xContext, xMCF, oUnoObject);
106*cdf0e10cSrcweir             oUnoDialogSample2.initialize( new String[] {"Height", "Moveable", "Name","PositionX","PositionY", "Step", "TabIndex","Title","Width"},
107*cdf0e10cSrcweir                                           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*cdf0e10cSrcweir             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*cdf0e10cSrcweir             oUnoDialogSample2.insertMultiLineFixedText(nControlPosX, 27, nControlWidth, 4, 1, sIntroLabel);
110*cdf0e10cSrcweir             // get the data from the UNO object...
111*cdf0e10cSrcweir             String[] sSupportedServiceNames = oUnoDialogSample2.getSupportedServiceNames();
112*cdf0e10cSrcweir             String[] sInterfaceNames = oUnoDialogSample2.getExportedInterfaceNames();
113*cdf0e10cSrcweir             String[] sMethodNames = oUnoDialogSample2.getMethodNames();
114*cdf0e10cSrcweir             String[] sPropertyNames = oUnoDialogSample2.getPropertyNames();
115*cdf0e10cSrcweir             // add controls to the dialog...
116*cdf0e10cSrcweir             oUnoDialogSample2.insertListBox(nControlPosX, nControlMargin, nListBoxHeight, nControlWidth, 2, sSupportedServiceNames);
117*cdf0e10cSrcweir             oUnoDialogSample2.insertListBox(nControlPosX, nControlMargin, nListBoxHeight, nControlWidth, 3, sInterfaceNames);
118*cdf0e10cSrcweir             oUnoDialogSample2.insertListBox(nControlPosX, nControlMargin, nListBoxHeight, nControlWidth, 4, sMethodNames);
119*cdf0e10cSrcweir             oUnoDialogSample2.insertListBox(nControlPosX, nControlMargin, nListBoxHeight, nControlWidth, 5, sPropertyNames);
120*cdf0e10cSrcweir             oUnoDialogSample2.insertButton(oUnoDialogSample2, nButtonPosX, nButtonPosY, nButtonWidth, "~Close", (short) PushButtonType.OK_value);
121*cdf0e10cSrcweir             oUnoDialogSample2.insertHorizontalFixedLine(0, nButtonPosY - nControlMargin - 4, nDialogWidth, "");
122*cdf0e10cSrcweir             // create the windowpeer;
123*cdf0e10cSrcweir             // it must be kept in mind that it must be created after the insertion of the controls
124*cdf0e10cSrcweir             // (see http://qa.openoffice.org/issues/show_bug.cgi?id=75129)
125*cdf0e10cSrcweir             oUnoDialogSample2.createWindowPeer();
126*cdf0e10cSrcweir             // add the roadmap control. Note that the roadmap may not be created before the windowpeer of the dialog exists
127*cdf0e10cSrcweir             // (see http://qa.openoffice.org/issues/show_bug.cgi?id=67369)
128*cdf0e10cSrcweir             oUnoDialogSample2.addRoadmap(oUnoDialogSample2.getRoadmapItemStateChangeListener());
129*cdf0e10cSrcweir             oUnoDialogSample2.insertRoadmapItem(0, true, "Introduction", 1);
130*cdf0e10cSrcweir             oUnoDialogSample2.insertRoadmapItem(1, true, "Supported Services", 2);
131*cdf0e10cSrcweir             oUnoDialogSample2.insertRoadmapItem(2, true, "Interfaces", 3);
132*cdf0e10cSrcweir             oUnoDialogSample2.insertRoadmapItem(3, true, "Methods", 4);
133*cdf0e10cSrcweir             oUnoDialogSample2.insertRoadmapItem(4, true, "Properties", 5);
134*cdf0e10cSrcweir             oUnoDialogSample2.m_xRMPSet.setPropertyValue("CurrentItemID", new Short((short) 1));
135*cdf0e10cSrcweir             oUnoDialogSample2.m_xRMPSet.setPropertyValue("Complete", Boolean.TRUE);
136*cdf0e10cSrcweir             oUnoDialogSample2.xDialog = (XDialog) UnoRuntime.queryInterface(XDialog.class, oUnoDialogSample2.m_xDialogControl);
137*cdf0e10cSrcweir             oUnoDialogSample2.xDialog.execute();
138*cdf0e10cSrcweir         }catch( Exception ex ) {
139*cdf0e10cSrcweir             ex.printStackTrace(System.out);
140*cdf0e10cSrcweir         }
141*cdf0e10cSrcweir         finally{
142*cdf0e10cSrcweir             //make sure always to dispose the component and free the memory!
143*cdf0e10cSrcweir             if (oUnoDialogSample2 != null){
144*cdf0e10cSrcweir                 if (oUnoDialogSample2.m_xComponent != null){
145*cdf0e10cSrcweir                     oUnoDialogSample2.m_xComponent.dispose();
146*cdf0e10cSrcweir                 }
147*cdf0e10cSrcweir             }
148*cdf0e10cSrcweir         }
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir         System.exit( 0 );
151*cdf0e10cSrcweir     }
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir     public String[] getMethodNames() {
155*cdf0e10cSrcweir         String[] sMethodNames = new String[]{};
156*cdf0e10cSrcweir         try {
157*cdf0e10cSrcweir             XIdlMethod[] xIdlMethods = m_xIntrospectionAccess.getMethods(MethodConcept.ALL);
158*cdf0e10cSrcweir             sMethodNames = new String[xIdlMethods.length];
159*cdf0e10cSrcweir             for (int i = 0; i < xIdlMethods.length; i++){
160*cdf0e10cSrcweir                 sMethodNames[i] = xIdlMethods[i].getName();
161*cdf0e10cSrcweir             }
162*cdf0e10cSrcweir         }
163*cdf0e10cSrcweir         catch( Exception e ) {
164*cdf0e10cSrcweir             System.err.println( e );
165*cdf0e10cSrcweir         }
166*cdf0e10cSrcweir         return sMethodNames;
167*cdf0e10cSrcweir     }
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir     // returns the names of all supported servicenames of a UNO object
170*cdf0e10cSrcweir     public String[] getSupportedServiceNames() {
171*cdf0e10cSrcweir         String[] sSupportedServiceNames = new String[]{};
172*cdf0e10cSrcweir         // If the Uno-Object supports "com.sun.star.lang.XServiceInfo"
173*cdf0e10cSrcweir         // this will give access to all supported servicenames
174*cdf0e10cSrcweir         XServiceInfo xServiceInfo = ( XServiceInfo ) UnoRuntime.queryInterface( XServiceInfo.class, m_oUnoObject);
175*cdf0e10cSrcweir         if ( xServiceInfo != null ) {
176*cdf0e10cSrcweir             sSupportedServiceNames = xServiceInfo.getSupportedServiceNames();
177*cdf0e10cSrcweir         }
178*cdf0e10cSrcweir         return sSupportedServiceNames;
179*cdf0e10cSrcweir     }
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir     // returns the names of all properties of a UNO object
182*cdf0e10cSrcweir     protected String[] getPropertyNames() {
183*cdf0e10cSrcweir         String[] sPropertyNames = new String[]{};
184*cdf0e10cSrcweir         try {
185*cdf0e10cSrcweir             Property[] aProperties = m_xIntrospectionAccess.getProperties(com.sun.star.beans.PropertyConcept.ATTRIBUTES + com.sun.star.beans.PropertyConcept.PROPERTYSET);
186*cdf0e10cSrcweir             sPropertyNames = new String[aProperties.length];
187*cdf0e10cSrcweir             for (int i = 0; i < aProperties.length; i++){
188*cdf0e10cSrcweir                 sPropertyNames[i] = aProperties[i].Name;
189*cdf0e10cSrcweir             }
190*cdf0e10cSrcweir         }
191*cdf0e10cSrcweir         catch( Exception e ) {
192*cdf0e10cSrcweir             System.err.println( e );
193*cdf0e10cSrcweir         }
194*cdf0e10cSrcweir         return sPropertyNames;
195*cdf0e10cSrcweir     }
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir     // returns the names of all exported interfaces of a UNO object
199*cdf0e10cSrcweir     protected String[] getExportedInterfaceNames(){
200*cdf0e10cSrcweir         Type[] aTypes = new Type[]{};
201*cdf0e10cSrcweir         String[] sTypeNames = new String[]{};
202*cdf0e10cSrcweir         // The XTypeProvider interfaces offers access to all exported interfaces
203*cdf0e10cSrcweir         XTypeProvider xTypeProvider = ( XTypeProvider ) UnoRuntime.queryInterface( XTypeProvider.class, m_oUnoObject);
204*cdf0e10cSrcweir         if ( xTypeProvider != null ) {
205*cdf0e10cSrcweir             aTypes = xTypeProvider.getTypes();
206*cdf0e10cSrcweir             sTypeNames = new String[aTypes.length];
207*cdf0e10cSrcweir             for (int i = 0; i < aTypes.length - 1; i++){
208*cdf0e10cSrcweir                 sTypeNames[i] = aTypes[i].getTypeName();
209*cdf0e10cSrcweir             }
210*cdf0e10cSrcweir         }
211*cdf0e10cSrcweir         return sTypeNames;
212*cdf0e10cSrcweir     }
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir     public XListBox insertListBox(int _nPosX, int _nPosY, int _nHeight, int _nWidth, int _nStep, String[] _sStringItemList) {
217*cdf0e10cSrcweir         XListBox xListBox = null;
218*cdf0e10cSrcweir         try{
219*cdf0e10cSrcweir             // create a unique name by means of an own implementation...
220*cdf0e10cSrcweir             String sName = createUniqueName(m_xDlgModelNameContainer, "ListBox");
221*cdf0e10cSrcweir             // create a controlmodel at the multiservicefactory of the dialog model...
222*cdf0e10cSrcweir             Object oListBoxModel = m_xMSFDialogModel.createInstance("com.sun.star.awt.UnoControlListBoxModel");
223*cdf0e10cSrcweir             XMultiPropertySet xLBModelMPSet = (XMultiPropertySet) UnoRuntime.queryInterface(XMultiPropertySet.class, oListBoxModel);
224*cdf0e10cSrcweir             // Set the properties at the model - keep in mind to pass the property names in alphabetical order!
225*cdf0e10cSrcweir             xLBModelMPSet.setPropertyValues(
226*cdf0e10cSrcweir                 new String[]  {"Dropdown", "Height", "Name", "PositionX", "PositionY", "ReadOnly", "Step", "StringItemList", "Width" } ,
227*cdf0e10cSrcweir                 new Object[] {Boolean.FALSE, new Integer(_nHeight), sName, new Integer(_nPosX), new Integer(_nPosY), Boolean.TRUE, new Integer(_nStep), _sStringItemList, new Integer(_nWidth)});
228*cdf0e10cSrcweir             m_xDlgModelNameContainer.insertByName(sName, xLBModelMPSet);
229*cdf0e10cSrcweir         }catch (com.sun.star.uno.Exception ex) {
230*cdf0e10cSrcweir             throw new java.lang.RuntimeException("cannot happen...");
231*cdf0e10cSrcweir         }
232*cdf0e10cSrcweir         return xListBox;
233*cdf0e10cSrcweir     }
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir 
236*cdf0e10cSrcweir     public void insertMultiLineFixedText(int _nPosX, int _nPosY, int _nWidth, int _nLineCount, int _nStep, String _sLabel) {
237*cdf0e10cSrcweir         try {
238*cdf0e10cSrcweir             // create a unique name by means of an own implementation...
239*cdf0e10cSrcweir             String sName = createUniqueName(m_xDlgModelNameContainer, "Label");
240*cdf0e10cSrcweir             int nHeight = _nLineCount * nFixedTextHeight;
241*cdf0e10cSrcweir             // create a controlmodel at the multiservicefactory of the dialog model...
242*cdf0e10cSrcweir             Object oFTModel = m_xMSFDialogModel.createInstance("com.sun.star.awt.UnoControlFixedTextModel");
243*cdf0e10cSrcweir             XMultiPropertySet xFTModelMPSet = (XMultiPropertySet) UnoRuntime.queryInterface(XMultiPropertySet.class, oFTModel);
244*cdf0e10cSrcweir             // Set the properties at the model - keep in mind to pass the property names in alphabetical order!
245*cdf0e10cSrcweir             xFTModelMPSet.setPropertyValues(
246*cdf0e10cSrcweir                 new String[] {"Height", "Label", "MultiLine", "Name", "PositionX", "PositionY", "Step", "Width"},
247*cdf0e10cSrcweir                 new Object[] { new Integer(nHeight), _sLabel, Boolean.TRUE, sName, new Integer(_nPosX), new Integer(_nPosY), new Integer(_nStep), new Integer(_nWidth)});
248*cdf0e10cSrcweir             // add the model to the NameContainer of the dialog model
249*cdf0e10cSrcweir             m_xDlgModelNameContainer.insertByName(sName, oFTModel);
250*cdf0e10cSrcweir         }catch (com.sun.star.uno.Exception ex){
251*cdf0e10cSrcweir             /* perform individual exception handling here.
252*cdf0e10cSrcweir              * Possible exception types are:
253*cdf0e10cSrcweir              * com.sun.star.lang.IllegalArgumentException,
254*cdf0e10cSrcweir              * com.sun.star.lang.WrappedTargetException,
255*cdf0e10cSrcweir              * com.sun.star.container.ElementExistException,
256*cdf0e10cSrcweir              * com.sun.star.beans.PropertyVetoException,
257*cdf0e10cSrcweir              * com.sun.star.beans.UnknownPropertyException,
258*cdf0e10cSrcweir              * com.sun.star.uno.Exception
259*cdf0e10cSrcweir              */
260*cdf0e10cSrcweir             ex.printStackTrace(System.out);
261*cdf0e10cSrcweir         }
262*cdf0e10cSrcweir     }
263*cdf0e10cSrcweir 
264*cdf0e10cSrcweir }// end of class
265