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 package com.sun.star.wizards.ui;
24 
25 import com.sun.star.awt.*;
26 import com.sun.star.lang.XMultiServiceFactory;
27 import com.sun.star.uno.UnoRuntime;
28 import com.sun.star.uno.XInterface;
29 import com.sun.star.wizards.common.Desktop;
30 import com.sun.star.wizards.common.Helper;
31 import com.sun.star.wizards.common.PropertyNames;
32 import com.sun.star.wizards.common.SystemDialog;
33 import com.sun.star.wizards.ui.event.*;
34 
35 /**
36  * This class contains convenience methods for inserting components to a dialog.
37  * It was created for use with the automatic conversion of Basic XML Dialog
38  * description files to a Java class which builds the same dialog through the UNO API.<br/>
39  * It uses an Event-Listener method, which calls a method through reflection
40  * wenn an event on a component is trigered.
41  * see the classes AbstractListener, CommonListener, MethodInvocation for details.
42  */
43 public class UnoDialog2 extends UnoDialog implements EventNames
44 {
45 
46     /**
47      * Override this method to return another listener.
48      * @return
49      */
createListener()50     protected AbstractListener createListener()
51     {
52         return new CommonListener();
53     }
54 
UnoDialog2(XMultiServiceFactory xmsf)55     public UnoDialog2(XMultiServiceFactory xmsf)
56     {
57         super(xmsf, new String[]
58                 {
59                 }, new Object[]
60                 {
61                 });
62         guiEventListener = createListener();
63     }
64 
insertButton(String sName, String actionPerformed, Object eventTarget, String[] sPropNames, Object[] oPropValues)65     public XButton insertButton(String sName, String actionPerformed, Object eventTarget, String[] sPropNames, Object[] oPropValues)
66     {
67 
68         XButton xButton = (XButton) insertControlModel2("com.sun.star.awt.UnoControlButtonModel", sName, sPropNames, oPropValues, XButton.class);
69 
70         if (actionPerformed != null)
71         {
72             xButton.addActionListener((XActionListener) guiEventListener);
73             guiEventListener.add(sName, EVENT_ACTION_PERFORMED, actionPerformed, eventTarget);
74         }
75         return xButton;
76     }
77 
insertButton(String sName, String actionPerformed, String[] sPropNames, Object[] oPropValues)78     public XButton insertButton(String sName, String actionPerformed, String[] sPropNames, Object[] oPropValues)
79     {
80         return insertButton(sName, actionPerformed, this, sPropNames, oPropValues);
81     }
82 
insertImageButton(String sName, com.sun.star.awt.XActionListener actionPerformed, Object eventTarget, String[] sPropNames, Object[] oPropValues)83     public XButton insertImageButton(String sName, com.sun.star.awt.XActionListener actionPerformed, Object eventTarget, String[] sPropNames, Object[] oPropValues)
84     {
85 
86         XButton xButton = (XButton) insertControlModel2("com.sun.star.awt.UnoControlButtonModel", sName, sPropNames, oPropValues, XButton.class);
87 
88         if (actionPerformed != null)
89         {
90             xButton.addActionListener(actionPerformed);
91         }
92         return xButton;
93     }
94 
insertImageButton(String sName, com.sun.star.awt.XActionListener actionPerformed, String[] sPropNames, Object[] oPropValues)95     public XButton insertImageButton(String sName, com.sun.star.awt.XActionListener actionPerformed, String[] sPropNames, Object[] oPropValues)
96     {
97         return insertImageButton(sName, actionPerformed, this, sPropNames, oPropValues);
98     }
99 
insertCheckBox(String sName, String itemChanged, Object eventTarget, String[] sPropNames, Object[] oPropValues)100     public XCheckBox insertCheckBox(String sName, String itemChanged, Object eventTarget, String[] sPropNames, Object[] oPropValues)
101     {
102 
103         XCheckBox xCheckBox = (XCheckBox) insertControlModel2("com.sun.star.awt.UnoControlCheckBoxModel", sName, sPropNames, oPropValues, XCheckBox.class);
104 
105         if (itemChanged != null)
106         {
107             xCheckBox.addItemListener((XItemListener) guiEventListener);
108             guiEventListener.add(sName, EVENT_ITEM_CHANGED, itemChanged, eventTarget);
109         }
110         return xCheckBox;
111     }
112 
insertCheckBox(String sName, String itemChanged, String[] sPropNames, Object[] oPropValues)113     public XCheckBox insertCheckBox(String sName, String itemChanged, String[] sPropNames, Object[] oPropValues)
114     {
115         return insertCheckBox(sName, itemChanged, this, sPropNames, oPropValues);
116     }
117 
insertComboBox(String sName, String actionPerformed, String itemChanged, String textChanged, Object eventTarget, String[] sPropNames, Object[] oPropValues)118     public XComboBox insertComboBox(String sName, String actionPerformed, String itemChanged, String textChanged, Object eventTarget, String[] sPropNames, Object[] oPropValues)
119     {
120         XComboBox xComboBox = (XComboBox) insertControlModel2("com.sun.star.awt.UnoControlComboBoxModel", sName, sPropNames, oPropValues, XComboBox.class);
121         if (actionPerformed != null)
122         {
123             xComboBox.addActionListener((XActionListener) guiEventListener);
124             guiEventListener.add(sName, EVENT_ACTION_PERFORMED, actionPerformed, eventTarget);
125         }
126         if (itemChanged != null)
127         {
128             xComboBox.addItemListener((XItemListener) guiEventListener);
129             guiEventListener.add(sName, EVENT_ITEM_CHANGED, itemChanged, eventTarget);
130         }
131         if (textChanged != null)
132         {
133             XTextComponent xTextComponent = UnoRuntime.queryInterface(XTextComponent.class, xComboBox);
134             xTextComponent.addTextListener((XTextListener) guiEventListener);
135             guiEventListener.add(sName, EVENT_TEXT_CHANGED, textChanged, eventTarget);
136         }
137         return xComboBox;
138     }
139 
insertComboBox(String sName, String actionPerformed, String itemChanged, String textChanged, String[] sPropNames, Object[] oPropValues)140     public XComboBox insertComboBox(String sName, String actionPerformed, String itemChanged, String textChanged, String[] sPropNames, Object[] oPropValues)
141     {
142         return insertComboBox(sName, actionPerformed, textChanged, itemChanged, this, sPropNames, oPropValues);
143     }
144 
insertListBox(String sName, String actionPerformed, String itemChanged, Object eventTarget, String[] sPropNames, Object[] oPropValues)145     public XListBox insertListBox(String sName, String actionPerformed, String itemChanged, Object eventTarget, String[] sPropNames, Object[] oPropValues)
146     {
147         XListBox xListBox = (XListBox) insertControlModel2("com.sun.star.awt.UnoControlListBoxModel", sName, sPropNames, oPropValues, XListBox.class);
148         if (actionPerformed != null)
149         {
150             xListBox.addActionListener((XActionListener) guiEventListener);
151             guiEventListener.add(sName, EVENT_ACTION_PERFORMED, actionPerformed, eventTarget);
152         }
153         if (itemChanged != null)
154         {
155             xListBox.addItemListener((XItemListener) guiEventListener);
156             guiEventListener.add(sName, EVENT_ITEM_CHANGED, itemChanged, eventTarget);
157         }
158         return xListBox;
159     }
160 
insertListBox(String sName, String actionPerformed, String itemChanged, String[] sPropNames, Object[] oPropValues)161     public XListBox insertListBox(String sName, String actionPerformed, String itemChanged, String[] sPropNames, Object[] oPropValues)
162     {
163         return insertListBox(sName, actionPerformed, itemChanged, this, sPropNames, oPropValues);
164     }
165 
insertRadioButton(String sName, String itemChanged, Object eventTarget, String[] sPropNames, Object[] oPropValues)166     public XRadioButton insertRadioButton(String sName, String itemChanged, Object eventTarget, String[] sPropNames, Object[] oPropValues)
167     {
168         XRadioButton xRadioButton = (XRadioButton) insertControlModel2("com.sun.star.awt.UnoControlRadioButtonModel", sName, sPropNames, oPropValues, XRadioButton.class);
169         if (itemChanged != null)
170         {
171             xRadioButton.addItemListener((XItemListener) guiEventListener);
172             guiEventListener.add(sName, EVENT_ITEM_CHANGED, itemChanged, eventTarget);
173         }
174         return xRadioButton;
175     }
176 
insertRadioButton(String sName, String itemChanged, String[] sPropNames, Object[] oPropValues)177     public XRadioButton insertRadioButton(String sName, String itemChanged, String[] sPropNames, Object[] oPropValues)
178     {
179         return insertRadioButton(sName, itemChanged, this, sPropNames, oPropValues);
180     }
181 
insertTitledBox(String sName, String[] sPropNames, Object[] oPropValues)182     public XControl insertTitledBox(String sName, String[] sPropNames, Object[] oPropValues)
183     {
184         Object oTitledBox = insertControlModel2("com.sun.star.awt.UnoControlGroupBoxModel", sName, sPropNames, oPropValues);
185         return UnoRuntime.queryInterface(XControl.class, oTitledBox);
186     }
187 
insertTextField(String sName, String sTextChanged, Object eventTarget, String[] sPropNames, Object[] oPropValues)188     public XTextComponent insertTextField(String sName, String sTextChanged, Object eventTarget, String[] sPropNames, Object[] oPropValues)
189     {
190         return (XTextComponent) insertEditField(sName, sTextChanged, eventTarget, "com.sun.star.awt.UnoControlEditModel", sPropNames, oPropValues, XTextComponent.class);
191     }
192 
insertTextField(String sName, String sTextChanged, String[] sPropNames, Object[] oPropValues)193     public XTextComponent insertTextField(String sName, String sTextChanged, String[] sPropNames, Object[] oPropValues)
194     {
195         return insertTextField(sName, sTextChanged, this, sPropNames, oPropValues);
196     }
197 
insertImage(String sName, String[] sPropNames, Object[] oPropValues)198     public XControl insertImage(String sName, String[] sPropNames, Object[] oPropValues)
199     {
200         return (XControl) insertControlModel2("com.sun.star.awt.UnoControlImageControlModel", sName, sPropNames, oPropValues, XControl.class);
201     }
202 
insertInfoImage(int _posx, int _posy, int _iStep)203     public XControl insertInfoImage(int _posx, int _posy, int _iStep)
204     {
205         XControl xImgControl = insertImage(Desktop.getUniqueName(getDlgNameAccess(), "imgHint"),
206                 new String[]
207                 {
208                     PropertyNames.PROPERTY_BORDER, PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_IMAGEURL, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, "ScaleImage", PropertyNames.PROPERTY_STEP, PropertyNames.PROPERTY_WIDTH
209                 },
210                 new Object[]
211                 {
212                     new Short((short) 0), 10, UIConsts.INFOIMAGEURL, new Integer(_posx), new Integer(_posy), Boolean.FALSE, new Integer(_iStep), 10
213                 });
214         super.getPeerConfiguration().setImageUrl(getModel(xImgControl), UIConsts.INFOIMAGEURL, UIConsts.INFOIMAGEURL_HC);
215         return xImgControl;
216     }
217 
218     /**
219      * This method is used for creating Edit, Currency, Date, Formatted, Pattern, File
220      * and Time edit components.
221      */
insertEditField(String sName, String sTextChanged, Object eventTarget, String sModelClass, String[] sPropNames, Object[] oPropValues, Class type)222     private Object insertEditField(String sName, String sTextChanged, Object eventTarget, String sModelClass, String[] sPropNames, Object[] oPropValues, Class type)
223     {
224         XTextComponent xField = (XTextComponent) insertControlModel2(sModelClass, sName, sPropNames, oPropValues, XTextComponent.class);
225         if (sTextChanged != null)
226         {
227             xField.addTextListener((XTextListener) guiEventListener);
228             guiEventListener.add(sName, EVENT_TEXT_CHANGED, sTextChanged, eventTarget);
229         }
230         return UnoRuntime.queryInterface(type, xField);
231     }
232 
insertFileControl(String sName, String sTextChanged, Object eventTarget, String[] sPropNames, Object[] oPropValues)233     public XControl insertFileControl(String sName, String sTextChanged, Object eventTarget, String[] sPropNames, Object[] oPropValues)
234     {
235         return (XControl) insertEditField(sName, sTextChanged, eventTarget, "com.sun.star.awt.UnoControlFileControlModel", sPropNames, oPropValues, XControl.class);
236     }
237 
insertFileControl(String sName, String sTextChanged, String[] sPropNames, Object[] oPropValues)238     public XControl insertFileControl(String sName, String sTextChanged, String[] sPropNames, Object[] oPropValues)
239     {
240         return insertFileControl(sName, sTextChanged, this, sPropNames, oPropValues);
241     }
242 
insertCurrencyField(String sName, String sTextChanged, Object eventTarget, String[] sPropNames, Object[] oPropValues)243     public XCurrencyField insertCurrencyField(String sName, String sTextChanged, Object eventTarget, String[] sPropNames, Object[] oPropValues)
244     {
245         return (XCurrencyField) insertEditField(sName, sTextChanged, eventTarget, "com.sun.star.awt.UnoControlCurrencyFieldModel", sPropNames, oPropValues, XCurrencyField.class);
246     }
247 
insertCurrencyField(String sName, String sTextChanged, String[] sPropNames, Object[] oPropValues)248     public XCurrencyField insertCurrencyField(String sName, String sTextChanged, String[] sPropNames, Object[] oPropValues)
249     {
250         return insertCurrencyField(sName, sTextChanged, this, sPropNames, oPropValues);
251     }
252 
insertDateField(String sName, String sTextChanged, Object eventTarget, String[] sPropNames, Object[] oPropValues)253     public XDateField insertDateField(String sName, String sTextChanged, Object eventTarget, String[] sPropNames, Object[] oPropValues)
254     {
255         return (XDateField) insertEditField(sName, sTextChanged, eventTarget, "com.sun.star.awt.UnoControlDateFieldModel", sPropNames, oPropValues, XDateField.class);
256     }
257 
insertDateField(String sName, String sTextChanged, String[] sPropNames, Object[] oPropValues)258     public XDateField insertDateField(String sName, String sTextChanged, String[] sPropNames, Object[] oPropValues)
259     {
260         return insertDateField(sName, sTextChanged, this, sPropNames, oPropValues);
261     }
262 
insertNumericField(String sName, String sTextChanged, Object eventTarget, String[] sPropNames, Object[] oPropValues)263     public XNumericField insertNumericField(String sName, String sTextChanged, Object eventTarget, String[] sPropNames, Object[] oPropValues)
264     {
265         return (XNumericField) insertEditField(sName, sTextChanged, eventTarget, "com.sun.star.awt.UnoControlNumericFieldModel", sPropNames, oPropValues, XNumericField.class);
266     }
267 
insertNumericField(String sName, String sTextChanged, String[] sPropNames, Object[] oPropValues)268     public XNumericField insertNumericField(String sName, String sTextChanged, String[] sPropNames, Object[] oPropValues)
269     {
270         return insertNumericField(sName, sTextChanged, this, sPropNames, oPropValues);
271     }
272 
insertTimeField(String sName, String sTextChanged, Object eventTarget, String[] sPropNames, Object[] oPropValues)273     public XTimeField insertTimeField(String sName, String sTextChanged, Object eventTarget, String[] sPropNames, Object[] oPropValues)
274     {
275         return (XTimeField) insertEditField(sName, sTextChanged, eventTarget, "com.sun.star.awt.UnoControlTimeFieldModel", sPropNames, oPropValues, XTimeField.class);
276     }
277 
insertTimeField(String sName, String sTextChanged, String[] sPropNames, Object[] oPropValues)278     public XTimeField insertTimeField(String sName, String sTextChanged, String[] sPropNames, Object[] oPropValues)
279     {
280         return insertTimeField(sName, sTextChanged, this, sPropNames, oPropValues);
281     }
282 
insertPatternField(String sName, String sTextChanged, Object eventTarget, String[] sPropNames, Object[] oPropValues)283     public XPatternField insertPatternField(String sName, String sTextChanged, Object eventTarget, String[] sPropNames, Object[] oPropValues)
284     {
285         return (XPatternField) insertEditField(sName, sTextChanged, eventTarget, "com.sun.star.awt.UnoControlPatternFieldModel", sPropNames, oPropValues, XPatternField.class);
286     }
287 
insertPatternField(String sName, String sTextChanged, String[] sPropNames, Object[] oPropValues)288     public XPatternField insertPatternField(String sName, String sTextChanged, String[] sPropNames, Object[] oPropValues)
289     {
290         return insertPatternField(sName, sTextChanged, this, sPropNames, oPropValues);
291     }
292 
insertFormattedField(String sName, String sTextChanged, Object eventTarget, String[] sPropNames, Object[] oPropValues)293     public XTextComponent insertFormattedField(String sName, String sTextChanged, Object eventTarget, String[] sPropNames, Object[] oPropValues)
294     {
295         return (XTextComponent) insertEditField(sName, sTextChanged, eventTarget, "com.sun.star.awt.UnoControlFormattedFieldModel", sPropNames, oPropValues, XTextComponent.class);
296     }
297 
insertFormattedField(String sName, String sTextChanged, String[] sPropNames, Object[] oPropValues)298     public XTextComponent insertFormattedField(String sName, String sTextChanged, String[] sPropNames, Object[] oPropValues)
299     {
300         return insertFormattedField(sName, sTextChanged, this, sPropNames, oPropValues);
301     }
302 
insertFixedLine(String sName, String[] sPropNames, Object[] oPropValues)303     public XControl insertFixedLine(String sName, String[] sPropNames, Object[] oPropValues)
304     {
305         Object oLine = insertControlModel2("com.sun.star.awt.UnoControlFixedLineModel", sName, sPropNames, oPropValues);
306         return UnoRuntime.queryInterface(XControl.class, oLine);
307     }
308 
insertScrollBar(String sName, String[] sPropNames, Object[] oPropValues)309     public XScrollBar insertScrollBar(String sName, String[] sPropNames, Object[] oPropValues)
310     {
311         Object oScrollBar = insertControlModel2("com.sun.star.awt.UnoControlScrollBarModel", sName, sPropNames, oPropValues);
312         return UnoRuntime.queryInterface(XScrollBar.class, oScrollBar);
313     }
314 
insertProgressBar(String sName, String[] sPropNames, Object[] oPropValues)315     public XProgressBar insertProgressBar(String sName, String[] sPropNames, Object[] oPropValues)
316     {
317         Object oProgressBar = insertControlModel2("com.sun.star.awt.UnoControlProgressBarModel", sName, sPropNames, oPropValues);
318         return UnoRuntime.queryInterface(XProgressBar.class, oProgressBar);
319     }
320 
insertGroupBox(String sName, String[] sPropNames, Object[] oPropValues)321     public XControl insertGroupBox(String sName, String[] sPropNames, Object[] oPropValues)
322     {
323         Object oGroupBox = insertControlModel2("com.sun.star.awt.UnoControlGroupBoxModel", sName, sPropNames, oPropValues);
324         return UnoRuntime.queryInterface(XControl.class, oGroupBox);
325     }
326 
insertControlModel2(String serviceName, String componentName, String[] sPropNames, Object[] oPropValues)327     public Object insertControlModel2(String serviceName, String componentName, String[] sPropNames, Object[] oPropValues)
328     {
329         try
330         {
331             //System.out.println("Inserting : " + componentName);
332             XInterface xControlModel = insertControlModel(serviceName, componentName, new String[]
333                     {
334                     }, new Object[]
335                     {
336                     });
337             Helper.setUnoPropertyValues(xControlModel, sPropNames, oPropValues);
338             //setControlPropertiesDebug(xControlModel, sPropNames, oPropValues);
339             //System.out.println("  Setting props successful !");
340             Helper.setUnoPropertyValue(xControlModel, PropertyNames.PROPERTY_NAME, componentName);
341         }
342         catch (Exception ex)
343         {
344             ex.printStackTrace();
345         }
346         return xDlgContainer.getControl(componentName);
347     }
348 
setControlPropertiesDebug(Object model, String[] names, Object[] values)349     private void setControlPropertiesDebug(Object model, String[] names, Object[] values)
350     {
351         for (int i = 0; i < names.length; i++)
352         {
353             System.out.println("   Settings: " + names[i]);
354             Helper.setUnoPropertyValue(model, names[i], values[i]);
355         }
356 
357 
358     }
359 
insertControlModel2(String serviceName, String componentName, String[] sPropNames, Object[] oPropValues, Class type)360     public Object insertControlModel2(String serviceName, String componentName, String[] sPropNames, Object[] oPropValues, Class type)
361     {
362         return UnoRuntime.queryInterface(type, insertControlModel2(serviceName, componentName, sPropNames, oPropValues));
363     }
364 
translateURL(String relativeURL)365     public String translateURL(String relativeURL)
366     {
367         return PropertyNames.EMPTY_STRING;
368     }
369 
getControlModel(Object unoControl)370     public static Object getControlModel(Object unoControl)
371     {
372         return UnoRuntime.queryInterface(XControl.class, unoControl).getModel();
373     }
374 
showMessageBox(String windowServiceName, int windowAttribute, String MessageText)375     public int showMessageBox(String windowServiceName, int windowAttribute, String MessageText)
376     {
377         return SystemDialog.showMessageBox(xMSF, this.xControl.getPeer(), windowServiceName, windowAttribute, MessageText);
378     }
379 }
380