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