1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 package com.sun.star.wizards.ui;
28 
29 import com.sun.star.awt.*;
30 import com.sun.star.awt.XReschedule;
31 import com.sun.star.beans.Property;
32 import com.sun.star.beans.XMultiPropertySet;
33 
34 import com.sun.star.beans.XPropertySet;
35 import com.sun.star.container.XNameAccess;
36 import com.sun.star.container.XNameContainer;
37 import com.sun.star.lang.*;
38 import com.sun.star.frame.XFrame;
39 import com.sun.star.lang.IllegalArgumentException;
40 import com.sun.star.lang.XComponent;
41 import com.sun.star.lang.XMultiServiceFactory;
42 import com.sun.star.uno.*;
43 import com.sun.star.wizards.common.*;
44 import com.sun.star.wizards.common.Desktop;
45 import com.sun.star.wizards.common.Helper;
46 import com.sun.star.wizards.ui.event.*;
47 
48 import java.util.Hashtable;
49 
50 public class UnoDialog implements EventNames
51 {
52 
53     public XMultiServiceFactory xMSF;
54     public XMultiServiceFactory MSFDialogModel;
55     public XNameContainer xDlgNames;
56     public XControlContainer xDlgContainer;
57     private XNameAccess m_xDlgNameAccess;
58     public XControl xControl;
59     public XDialog xDialog;
60     public XReschedule xReschedule;
61     public XWindow xWindow;
62     public XComponent xComponent;
63     public XInterface xDialogModel;
64     public XInterface xUnoDialog;
65     public XPropertySet xPSetDlg;
66     public XVclWindowPeer xVclWindowPeer;
67     public Hashtable ControlList;
68     public Resource m_oResource;
69     public XWindowPeer xWindowPeer = null;
70     private PeerConfig m_oPeerConfig;
71     protected AbstractListener guiEventListener;
72 
73     public AbstractListener getGuiEventListener()
74     {
75         return guiEventListener;
76     }
77 
78     public UnoDialog(XMultiServiceFactory xMSF, String[] PropertyNames, Object[] PropertyValues)
79     {
80         try
81         {
82             this.xMSF = xMSF;
83             ControlList = new Hashtable();
84             xDialogModel = (XInterface) xMSF.createInstance("com.sun.star.awt.UnoControlDialogModel");
85             XMultiPropertySet xMultiPSetDlg = UnoRuntime.queryInterface(XMultiPropertySet.class, xDialogModel);
86             xMultiPSetDlg.setPropertyValues(PropertyNames, PropertyValues);
87             MSFDialogModel = UnoRuntime.queryInterface(XMultiServiceFactory.class, xDialogModel);
88             xUnoDialog = (XInterface) xMSF.createInstance("com.sun.star.awt.UnoControlDialog");
89             xControl = UnoRuntime.queryInterface(XControl.class, xUnoDialog);
90             XControlModel xControlModel = UnoRuntime.queryInterface(XControlModel.class, xDialogModel);
91             xControl.setModel(xControlModel);
92             xPSetDlg = UnoRuntime.queryInterface(XPropertySet.class, xDialogModel);
93             xDlgContainer = UnoRuntime.queryInterface(XControlContainer.class, xUnoDialog);
94             xDlgNames = UnoRuntime.queryInterface(XNameContainer.class, xDialogModel);
95             // xDlgNameAccess = (XNameAccess) UnoRuntime.queryInterface(XNameAccess.class, xDialogModel);
96             xComponent = UnoRuntime.queryInterface(XComponent.class, xUnoDialog);
97             xWindow = UnoRuntime.queryInterface(XWindow.class, xUnoDialog);
98 
99         // setPeerConfiguration(); // LLA: will be done, if really used!
100         }
101         catch (com.sun.star.uno.Exception exception)
102         {
103             exception.printStackTrace(System.out);
104         }
105     }
106 
107     public int getControlKey(Object EventObject, Hashtable ControlList)
108     {
109         int iKey;
110         XControl xContrl = UnoRuntime.queryInterface(XControl.class, EventObject);
111         XControlModel xControlModel = xContrl.getModel();
112         XPropertySet xPSet = UnoRuntime.queryInterface(XPropertySet.class, xControlModel);
113         try
114         {
115             String sName = (String) xPSet.getPropertyValue(PropertyNames.PROPERTY_NAME);
116             Integer KeyObject = (Integer) ControlList.get(sName);
117             iKey = KeyObject.intValue();
118         }
119         catch (com.sun.star.uno.Exception exception)
120         {
121             exception.printStackTrace(System.out);
122             iKey = 2000;
123         }
124         return iKey;
125     }
126 
127     public void createPeerConfiguration()
128     {
129         m_oPeerConfig = new PeerConfig(this);
130     }
131 
132     public PeerConfig getPeerConfiguration()
133     {
134         if (m_oPeerConfig == null)
135         {
136             createPeerConfiguration();
137         }
138         return m_oPeerConfig;
139     }
140 
141     XNameAccess getDlgNameAccess()
142     {
143         if (m_xDlgNameAccess == null)
144         {
145             m_xDlgNameAccess = UnoRuntime.queryInterface(XNameAccess.class, xDialogModel);
146         }
147         return m_xDlgNameAccess;
148     }
149     public void setControlProperty(String ControlName, String PropertyName, Object PropertyValue)
150     {
151         try
152         {
153             if (PropertyValue != null)
154             {
155                 if (!getDlgNameAccess().hasByName(ControlName))
156                 {
157                     return;
158                 }
159                 Object xControlModel = getDlgNameAccess().getByName(ControlName);
160                 XPropertySet xPSet = UnoRuntime.queryInterface(XPropertySet.class, xControlModel);
161                 if (AnyConverter.isArray(PropertyValue))
162                 {
163                     com.sun.star.uno.Type seqType = new com.sun.star.uno.Type(PropertyValue.getClass());
164                     PropertyValue = new com.sun.star.uno.Any(seqType, PropertyValue);
165                 /*              PropertyValue = Helper.getArrayValue(PropertyValue);
166                 if (PropertyValue == null)
167                 PropertyValue = new short[]{};
168                  */
169                 }
170                 xPSet.setPropertyValue(PropertyName, PropertyValue);
171             }
172         }
173         catch (com.sun.star.uno.Exception exception)
174         {
175             exception.printStackTrace(System.out);
176         }
177     }
178 
179     public Resource getResource()
180     {
181         return m_oResource;
182     }
183 
184     public void setControlProperties(String ControlName, String[] PropertyNames, Object[] PropertyValues)
185     {
186         try
187         {
188             if (PropertyValues != null)
189             {
190                 if (!getDlgNameAccess().hasByName(ControlName))
191                 {
192                     return;
193                 }
194                 Object xControlModel = getDlgNameAccess().getByName(ControlName);
195                 XMultiPropertySet xMultiPSet = UnoRuntime.queryInterface(XMultiPropertySet.class, xControlModel);
196                 xMultiPSet.setPropertyValues(PropertyNames, PropertyValues);
197             }
198         }
199         catch (com.sun.star.uno.Exception exception)
200         {
201             exception.printStackTrace(System.out);
202         }
203     }
204 
205     public Object getControlProperty(String ControlName, String PropertyName)
206     {
207         try
208         {
209             Object xControlModel = getDlgNameAccess().getByName(ControlName);
210             XPropertySet xPSet = UnoRuntime.queryInterface(XPropertySet.class, xControlModel);
211             return xPSet.getPropertyValue(PropertyName);
212         }
213         catch (com.sun.star.uno.Exception exception)
214         { // com.sun.star.container.NoSuchElementException, com.sun.star.beans.UnknownPropertyException,
215             exception.printStackTrace(System.out); // com.sun.star.lang.WrappedTargetException, com.sun.star.beans.PropertyVetoException
216             return null; // com.sun.star.lang.IllegalArgumentException
217         }
218     }
219 
220     public void printControlProperties(String ControlName)
221     {
222         try
223         {
224             Object xControlModel = getDlgNameAccess().getByName(ControlName);
225             XPropertySet xPSet = UnoRuntime.queryInterface(XPropertySet.class, xControlModel);
226             Property[] allProps = xPSet.getPropertySetInfo().getProperties();
227             for (int i = 0; i < allProps.length; i++)
228             {
229                 String sName = allProps[i].Name;
230                 System.out.println(sName);
231             }
232         }
233         catch (com.sun.star.uno.Exception exception)
234         { // com.sun.star.container.NoSuchElementException, com.sun.star.beans.UnknownPropertyException,
235             exception.printStackTrace(System.out); // com.sun.star.lang.WrappedTargetException, com.sun.star.beans.PropertyVetoException
236         }
237     }
238 
239     public double getMAPConversionFactor(String ControlName)
240     {
241         XControl xControl2 = xDlgContainer.getControl(ControlName);
242         XView xView = UnoRuntime.queryInterface(XView.class, xControl2);
243         Size aSize = xView.getSize();
244         double dblMAPWidth = (double) (((Integer) Helper.getUnoPropertyValue(xControl2.getModel(), PropertyNames.PROPERTY_WIDTH)).intValue());
245         return (((double) (aSize.Width)) / dblMAPWidth);
246     }
247 
248     public Size getpreferredLabelSize(String LabelName, String sLabel)
249     {
250         XControl xControl2 = xDlgContainer.getControl(LabelName);
251         XFixedText xFixedText = UnoRuntime.queryInterface(XFixedText.class, xControl2);
252         String OldText = xFixedText.getText();
253         xFixedText.setText(sLabel);
254         XLayoutConstrains xLayoutConstrains = UnoRuntime.queryInterface(XLayoutConstrains.class, xControl2);
255         Size aSize = xLayoutConstrains.getPreferredSize();
256         xFixedText.setText(OldText);
257         return aSize;
258     }
259 
260     public void removeSelectedItems(XListBox xListBox)
261     {
262         short[] SelList = xListBox.getSelectedItemsPos();
263         int Sellen = SelList.length;
264         for (int i = Sellen - 1; i >= 0; i--)
265         {
266             xListBox.removeItems(SelList[i], (short) 1);
267         }
268     }
269 
270     public static int getListBoxItemCount(XListBox _xListBox)
271     {
272         // This function may look ugly, but this is the only way to check the count
273         // of values in the model,which is always right.
274         // the control is only a view and could be right or not.
275         final String[] fieldnames = (String[]) Helper.getUnoPropertyValue(getModel(_xListBox), PropertyNames.STRING_ITEM_LIST);
276         return fieldnames.length;
277     }
278 
279     public static short getSelectedItemPos(XListBox _xListBox)
280     {
281         short ipos[] = (short[]) Helper.getUnoPropertyValue(getModel(_xListBox), PropertyNames.SELECTED_ITEMS);
282         return ipos[0];
283     }
284 
285     public static boolean isListBoxSelected(XListBox _xListBox)
286     {
287         short ipos[] = (short[]) Helper.getUnoPropertyValue(getModel(_xListBox), PropertyNames.SELECTED_ITEMS);
288         return ipos.length > 0;
289     }
290 
291     public void addSingleItemtoListbox(XListBox xListBox, String ListItem, short iSelIndex)
292     {
293         xListBox.addItem(ListItem, xListBox.getItemCount());
294         if (iSelIndex != -1)
295         {
296             xListBox.selectItemPos(iSelIndex, true);
297         }
298     }
299 
300     public XFixedText insertLabel(String sName, String[] sPropNames, Object[] oPropValues)
301     {
302         try
303         {
304             Object oFixedText = insertControlModel("com.sun.star.awt.UnoControlFixedTextModel", sName, sPropNames, oPropValues);
305             XPropertySet xPSet = UnoRuntime.queryInterface(XPropertySet.class, oFixedText);
306             xPSet.setPropertyValue(PropertyNames.PROPERTY_NAME, sName);
307             Object oLabel = xDlgContainer.getControl(sName);
308             return UnoRuntime.queryInterface(XFixedText.class, oLabel);
309         }
310         catch (java.lang.Exception ex)
311         {
312             ex.printStackTrace();
313             return null;
314         }
315     }
316 
317     /*    public XButton insertButton(String sName, int iControlKey, XActionListener xActionListener, String[] sProperties, Object[] sValues) throws com.sun.star.uno.Exception{
318     Object oButtonModel = insertControlModel("com.sun.star.awt.UnoControlButtonModel", sName, sProperties, sValues);
319     XPropertySet xPSet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, oButtonModel);
320     xPSet.setPropertyValue(PropertyNames.PROPERTY_NAME, sName);
321     Object objectButton = xDlgContainer.getControl( new String(sName));
322     XButton xButton = ( XButton ) UnoRuntime.queryInterface( XButton.class, objectButton );
323     xButton.addActionListener(xActionListener);
324     Integer ControlKey = new Integer(iControlKey);
325     if (ControlList != null)
326     ControlList.put(sName, ControlKey);
327     return xButton;
328     } */
329     public XButton insertButton(String sName, int iControlKey, XActionListener xActionListener, String[] sProperties, Object[] sValues) throws com.sun.star.uno.Exception
330     {
331         Object oButtonModel = insertControlModel("com.sun.star.awt.UnoControlButtonModel", sName, sProperties, sValues);
332         XPropertySet xPSet = UnoRuntime.queryInterface(XPropertySet.class, oButtonModel);
333         xPSet.setPropertyValue(PropertyNames.PROPERTY_NAME, sName);
334         Object objectButton = xDlgContainer.getControl(sName);
335         XButton xButton = UnoRuntime.queryInterface(XButton.class, objectButton);
336         if (xActionListener != null)
337         {
338             xButton.addActionListener(xActionListener);
339         }
340         Integer ControlKey = new Integer(iControlKey);
341         if (ControlList != null)
342         {
343             ControlList.put(sName, ControlKey);
344         }
345         return xButton;
346     }
347 
348     public void insertCheckBox(String sName, int iControlKey, XItemListener xItemListener, String[] sProperties, Object[] sValues) throws com.sun.star.uno.Exception
349     {
350         Object oButtonModel = insertControlModel("com.sun.star.awt.UnoControlCheckBoxModel", sName, sProperties, sValues);
351         XPropertySet xPSet = UnoRuntime.queryInterface(XPropertySet.class, oButtonModel);
352         xPSet.setPropertyValue(PropertyNames.PROPERTY_NAME, sName);
353         Object objectCheckBox = xDlgContainer.getControl(sName);
354         XCheckBox xCheckBox = UnoRuntime.queryInterface(XCheckBox.class, objectCheckBox);
355         if (xItemListener != null)
356         {
357             xCheckBox.addItemListener(xItemListener);
358         }
359         Integer ControlKey = new Integer(iControlKey);
360         if (ControlList != null)
361         {
362             ControlList.put(sName, ControlKey);
363         }
364     }
365 
366     public void insertNumericField(String sName, int iControlKey, XTextListener xTextListener, String[] sProperties, Object[] sValues) throws com.sun.star.uno.Exception
367     {
368         Object oNumericFieldModel = insertControlModel("com.sun.star.awt.UnoControlNumericFieldModel", sName, sProperties, sValues);
369         XPropertySet xPSet = UnoRuntime.queryInterface(XPropertySet.class, oNumericFieldModel);
370         xPSet.setPropertyValue(PropertyNames.PROPERTY_NAME, sName);
371         Object objectNumericField = xDlgContainer.getControl(sName);
372         XTextComponent xNumericField = UnoRuntime.queryInterface(XTextComponent.class, objectNumericField);
373         if (xTextListener != null)
374         {
375             xNumericField.addTextListener(xTextListener);
376         }
377         Integer ControlKey = new Integer(iControlKey);
378         if (ControlList != null)
379         {
380             ControlList.put(sName, ControlKey);
381         }
382     }
383 
384     public XScrollBar insertScrollBar(String sName, int iControlKey, XAdjustmentListener xAdjustmentListener, String[] sProperties, Object[] sValues)
385     {
386         try
387         {
388             Object oScrollModel = insertControlModel("com.sun.star.awt.UnoControlScrollBarModel", sName, sProperties, sValues);
389             XPropertySet xPSet = UnoRuntime.queryInterface(XPropertySet.class, oScrollModel);
390             xPSet.setPropertyValue(PropertyNames.PROPERTY_NAME, sName);
391             Object oScrollBar = xDlgContainer.getControl(sName);
392             XScrollBar xScrollBar = UnoRuntime.queryInterface(XScrollBar.class, oScrollBar);
393             if (xAdjustmentListener != null)
394             {
395                 xScrollBar.addAdjustmentListener(xAdjustmentListener);
396             }
397             Integer ControlKey = new Integer(iControlKey);
398             if (ControlList != null)
399             {
400                 ControlList.put(sName, ControlKey);
401             }
402             return xScrollBar;
403         }
404         catch (com.sun.star.uno.Exception exception)
405         {
406             exception.printStackTrace(System.out);
407             return null;
408         }
409     }
410 
411     public XTextComponent insertTextField(String sName, int iControlKey, XTextListener xTextListener, String[] sProperties, Object[] sValues)
412     {
413         return insertEditField("com.sun.star.awt.UnoControlEditModel", sName, iControlKey, xTextListener, sProperties, sValues);
414     }
415 
416     public XTextComponent insertFormattedField(String sName, int iControlKey, XTextListener xTextListener, String[] sProperties, Object[] sValues)
417     {
418         return insertEditField("com.sun.star.awt.UnoControlFormattedFieldModel", sName, iControlKey, xTextListener, sProperties, sValues);
419     }
420 
421     public XTextComponent insertEditField(String ServiceName, String sName, int iControlKey, XTextListener xTextListener, String[] sProperties, Object[] sValues)
422     {
423         try
424         {
425             XInterface xTextModel = insertControlModel(ServiceName, sName, sProperties, sValues);
426             XPropertySet xPSet = UnoRuntime.queryInterface(XPropertySet.class, xTextModel);
427             xPSet.setPropertyValue(PropertyNames.PROPERTY_NAME, sName);
428             XControl xTextField = xDlgContainer.getControl(sName);
429             XTextComponent xTextBox = UnoRuntime.queryInterface(XTextComponent.class, xTextField);
430             if (xTextListener != null)
431             {
432                 xTextBox.addTextListener(xTextListener);
433             }
434             Integer ControlKey = new Integer(iControlKey);
435             ControlList.put(sName, ControlKey);
436             return xTextBox;
437         }
438         catch (com.sun.star.uno.Exception exception)
439         {
440             exception.printStackTrace(System.out);
441             return null;
442         }
443     }
444 
445     public XListBox insertListBox(String sName, int iControlKey, XActionListener xActionListener, XItemListener xItemListener, String[] sProperties, Object[] sValues) throws com.sun.star.uno.Exception
446     {
447         XInterface xListBoxModel = insertControlModel("com.sun.star.awt.UnoControlListBoxModel", sName, sProperties, sValues);
448         XPropertySet xPSet = UnoRuntime.queryInterface(XPropertySet.class, xListBoxModel);
449         xPSet.setPropertyValue(PropertyNames.PROPERTY_NAME, sName);
450         XControl xControlListBox = xDlgContainer.getControl(sName);
451         XListBox xListBox = UnoRuntime.queryInterface(XListBox.class, xControlListBox);
452         if (xItemListener != null)
453         {
454             xListBox.addItemListener(xItemListener);
455         }
456         if (xActionListener != null)
457         {
458             xListBox.addActionListener(xActionListener);
459         }
460         Integer ControlKey = new Integer(iControlKey);
461         ControlList.put(sName, ControlKey);
462         return xListBox;
463     }
464 
465     public XComboBox insertComboBox(String sName, int iControlKey, XActionListener xActionListener, XTextListener xTextListener, XItemListener xItemListener, String[] sProperties, Object[] sValues) throws com.sun.star.uno.Exception
466     {
467         XInterface xComboBoxModel = insertControlModel("com.sun.star.awt.UnoControlComboBoxModel", sName, sProperties, sValues);
468         XPropertySet xPSet = UnoRuntime.queryInterface(XPropertySet.class, xComboBoxModel);
469         xPSet.setPropertyValue(PropertyNames.PROPERTY_NAME, sName);
470         XControl xControlComboBox = xDlgContainer.getControl(sName);
471         XComboBox xComboBox = UnoRuntime.queryInterface(XComboBox.class, xControlComboBox);
472         if (xItemListener != null)
473         {
474             xComboBox.addItemListener(xItemListener);
475         }
476         if (xTextListener != null)
477         {
478             XTextComponent xTextComponent = UnoRuntime.queryInterface(XTextComponent.class, xComboBox);
479             xTextComponent.addTextListener(xTextListener);
480         }
481         if (xActionListener != null)
482         {
483             xComboBox.addActionListener(xActionListener);
484         }
485         Integer ControlKey = new Integer(iControlKey);
486         ControlList.put(sName, ControlKey);
487         return xComboBox;
488     }
489 
490     public XRadioButton insertRadioButton(String sName, int iControlKey, XItemListener xItemListener, String[] sProperties, Object[] sValues)
491     {
492         try
493         {
494             XRadioButton xRadioButton = insertRadioButton(sName, iControlKey, sProperties, sValues);
495             if (xItemListener != null)
496             {
497                 xRadioButton.addItemListener(xItemListener);
498             }
499             return xRadioButton;
500         }
501         catch (com.sun.star.uno.Exception exception)
502         {
503             exception.printStackTrace(System.out);
504             return null;
505         }
506     }
507 
508     public XButton insertRadioButton(String sName, int iControlKey, XActionListener xActionListener, String[] sProperties, Object[] sValues)
509     {
510         try
511         {
512             XRadioButton xRadioButton = insertRadioButton(sName, iControlKey, sProperties, sValues);
513             XButton xButton = UnoRuntime.queryInterface(XButton.class, xRadioButton);
514             if (xActionListener != null)
515             {
516                 xButton.addActionListener(xActionListener);
517             }
518             return xButton;
519         }
520         catch (com.sun.star.uno.Exception exception)
521         {
522             exception.printStackTrace(System.out);
523             return null;
524         }
525     }
526 
527     public XRadioButton insertRadioButton(String sName, int iControlKey, String[] sProperties, Object[] sValues) throws com.sun.star.uno.Exception
528     {
529         XRadioButton xRadioButton = insertRadioButton(sName, sProperties, sValues);
530         Integer ControlKey = new Integer(iControlKey);
531         ControlList.put(sName, ControlKey);
532         return xRadioButton;
533     }
534 
535     public XRadioButton insertRadioButton(String sName, String[] sProperties, Object[] sValues)
536     {
537         try
538         {
539             XInterface oRadioButtonModel = insertControlModel("com.sun.star.awt.UnoControlRadioButtonModel", sName, sProperties, sValues);
540             XPropertySet xPSet = UnoRuntime.queryInterface(XPropertySet.class, oRadioButtonModel);
541             xPSet.setPropertyValue(PropertyNames.PROPERTY_NAME, sName);
542             XControl xControlRadioButton = xDlgContainer.getControl(sName);
543             return UnoRuntime.queryInterface(XRadioButton.class, xControlRadioButton);
544         }
545         catch (com.sun.star.uno.Exception exception)
546         {
547             exception.printStackTrace(System.out);
548             return null;
549         }
550     }
551 
552     /**
553      * @author bc93774
554      * The problem with setting the visibility of controls is that changing the current step
555      * of a dialog will automatically make all controls visible. The PropertyNames.PROPERTY_STEP property always wins against
556      * the property "visible". Therfor a control meant to be invisible is placed on a step far far away.
557      * @param the name of the control
558      * @param iStep  change the step if you want to make the control invisible
559      */
560     private void setControlVisible(String controlname, int iStep)
561     {
562         try
563         {
564             int iCurStep = AnyConverter.toInt(getControlProperty(controlname, PropertyNames.PROPERTY_STEP));
565             setControlProperty(controlname, PropertyNames.PROPERTY_STEP, new Integer(iStep));
566         }
567         catch (com.sun.star.uno.Exception exception)
568         {
569             exception.printStackTrace(System.out);
570         }
571     }
572 
573     /**
574      * @author bc93774
575      * The problem with setting the visibility of controls is that changing the current step
576      * of a dialog will automatically make all controls visible. The PropertyNames.PROPERTY_STEP property always wins against
577      * the property "visible". Therfor a control meant to be invisible is placed on a step far far away.
578      * Afterwards the step property of the dialog has to be set with "repaintDialogStep". As the performance
579      * of that method is very bad it should be used only once for all controls
580      * @param controlname the name of the control
581      * @param bIsVisible sets the control visible or invisible
582      */
583     public void setControlVisible(String controlname, boolean bIsVisible)
584     {
585         try
586         {
587             int iCurControlStep = AnyConverter.toInt(getControlProperty(controlname, PropertyNames.PROPERTY_STEP));
588             int iCurDialogStep = AnyConverter.toInt(Helper.getUnoPropertyValue(this.xDialogModel, PropertyNames.PROPERTY_STEP));
589             int iNewStep;
590             if (bIsVisible)
591             {
592                 setControlProperty(controlname, PropertyNames.PROPERTY_STEP, new Integer(iCurDialogStep));
593             }
594             else
595             {
596                 setControlProperty(controlname, PropertyNames.PROPERTY_STEP, UIConsts.INVISIBLESTEP);
597             }
598         }
599         catch (com.sun.star.uno.Exception exception)
600         {
601             exception.printStackTrace(System.out);
602         }
603     }
604 
605     /**
606      * repaints the currentDialogStep
607      * @author bc93774
608      */
609     public void repaintDialogStep()
610     {
611         try
612         {
613             int ncurstep = AnyConverter.toInt(Helper.getUnoPropertyValue(this.xDialogModel, PropertyNames.PROPERTY_STEP));
614             Helper.setUnoPropertyValue(xDialogModel, PropertyNames.PROPERTY_STEP, 99);
615             Helper.setUnoPropertyValue(xDialogModel, PropertyNames.PROPERTY_STEP, new Integer(ncurstep));
616         }
617         catch (com.sun.star.uno.Exception exception)
618         {
619             exception.printStackTrace(System.out);
620         }
621     }
622 
623     public XInterface insertControlModel(String ServiceName, String sName, String[] sProperties, Object[] sValues)
624     {
625         try
626         {
627             XInterface xControlModel = (XInterface) MSFDialogModel.createInstance(ServiceName);
628             Helper.setUnoPropertyValues(xControlModel, sProperties, sValues);
629             xDlgNames.insertByName(sName, xControlModel);
630             return xControlModel;
631         }
632         catch (com.sun.star.uno.Exception exception)
633         {
634             exception.printStackTrace(System.out);
635             return null;
636         }
637     }
638 
639     public void setFocus(String ControlName)
640     {
641         Object oFocusControl = xDlgContainer.getControl(ControlName);
642         XWindow xWindow = UnoRuntime.queryInterface(XWindow.class, oFocusControl);
643         xWindow.setFocus();
644     }
645 
646     public static String[] combineListboxList(String sFirstEntry, String[] MainList)
647     {
648         try
649         {
650             String[] FirstList = new String[]
651             {
652                 sFirstEntry
653             };
654             String[] ResultList = new String[MainList.length + 1];
655             System.arraycopy(FirstList, 0, ResultList, 0, 1);
656             System.arraycopy(MainList, 0, ResultList, 1, MainList.length);
657             return ResultList;
658         }
659         catch (java.lang.Exception jexception)
660         {
661             jexception.printStackTrace(System.out);
662             return null;
663         }
664     }
665 
666     public void selectListBoxItem(XListBox xListBox, short iFieldsSelIndex)
667     {
668         if (iFieldsSelIndex > -1)
669         {
670             int FieldCount = xListBox.getItemCount();
671             if (FieldCount > 0)
672             {
673                 if (iFieldsSelIndex < FieldCount)
674                 {
675                     xListBox.selectItemPos(iFieldsSelIndex, true);
676                 }
677                 else
678                 {
679                     xListBox.selectItemPos((short) (iFieldsSelIndex - 1), true);
680                 }
681             }
682         }
683     }
684 
685     /** deselects a Listbox. MultipleMode is not supported
686      *
687      * @param _xBasisListBox
688      */
689     public static void deselectListBox(XInterface _xBasisListBox)
690     {
691         Object oListBoxModel = getModel(_xBasisListBox);
692         Object sList = Helper.getUnoPropertyValue(oListBoxModel, PropertyNames.STRING_ITEM_LIST);
693         Helper.setUnoPropertyValue(oListBoxModel, PropertyNames.STRING_ITEM_LIST, new String[]
694                 {
695                 });
696         Helper.setUnoPropertyValue(oListBoxModel, PropertyNames.STRING_ITEM_LIST, sList);
697     }
698 
699     public void calculateDialogPosition(Rectangle FramePosSize)
700     {
701         // Todo: check if it would be useful or possible to create a dialog peer, that can be used for the messageboxes to
702         // maintain modality when they pop up.
703         Rectangle CurPosSize = xWindow.getPosSize();
704         int WindowHeight = FramePosSize.Height;
705         int WindowWidth = FramePosSize.Width;
706         int DialogWidth = CurPosSize.Width;
707         int DialogHeight = CurPosSize.Height;
708         int iXPos = ((WindowWidth / 2) - (DialogWidth / 2));
709         int iYPos = ((WindowHeight / 2) - (DialogHeight / 2));
710         xWindow.setPosSize(iXPos, iYPos, DialogWidth, DialogHeight, PosSize.POS);
711     }
712 
713     /**
714      *
715      * @param FramePosSize
716      * @return 0 for cancel, 1 for ok
717      * @throws com.sun.star.uno.Exception
718      */
719     public short executeDialog(Rectangle FramePosSize) throws com.sun.star.uno.Exception
720     {
721         if (xControl.getPeer() == null)
722         {
723             throw new java.lang.IllegalArgumentException("Please create a peer, using your own frame");
724         }
725         calculateDialogPosition(FramePosSize);
726         if (xWindowPeer == null)
727         {
728             createWindowPeer();
729         }
730         xVclWindowPeer = UnoRuntime.queryInterface(XVclWindowPeer.class, xWindowPeer);
731 //      xVclWindowPeer.setProperty("AutoMnemonics", new Boolean(true));
732         this.BisHighContrastModeActivated = Boolean.valueOf(this.isHighContrastModeActivated());
733         xDialog = UnoRuntime.queryInterface(XDialog.class, xUnoDialog);
734         return xDialog.execute();
735     }
736 
737     public void setVisible(UnoDialog parent) throws com.sun.star.uno.Exception
738     {
739         calculateDialogPosition(parent.xWindow.getPosSize());
740         if (xWindowPeer == null)
741         {
742             createWindowPeer();
743         }
744         XVclWindowPeer xVclWindowPeer = UnoRuntime.queryInterface(XVclWindowPeer.class, xWindowPeer);
745         xDialog = UnoRuntime.queryInterface(XDialog.class, xUnoDialog);
746         this.xWindow.setVisible(true);
747     }
748 
749     /**
750      *
751      * @param parent
752      * @return 0 for cancel, 1 for ok.
753      * @throws com.sun.star.uno.Exception
754      */
755     public short executeDialog(UnoDialog parent)
756             throws com.sun.star.uno.Exception
757     {
758         return executeDialog(parent.xWindow.getPosSize());
759     }
760 
761     /**
762      *
763      * @param xComponent
764      * @return 0 for cancel, 1 for ok.
765      * @throws com.sun.star.uno.Exception
766      */
767     public short executeDialog(XInterface xComponent) throws com.sun.star.uno.Exception
768     {
769         XFrame frame = UnoRuntime.queryInterface(XFrame.class, xComponent);
770         if (frame != null)
771         {
772             XWindow w = frame.getComponentWindow();
773             if (w != null)
774             {
775                 return executeDialog(w.getPosSize());
776             }
777         }
778 
779         return executeDialog(new Rectangle(0, 0, 640, 400));
780     }
781 
782     /**
783      * When possible, use the other executeDialog methods, since
784      * there may be problems retrieving the actual active frame,
785      * for example under linux.
786      * @return 0 for cancel, 1 for ok
787      */
788     public short executeDialog() throws com.sun.star.uno.Exception
789     {
790         return executeDialog(Desktop.getActiveFrame(xMSF));
791     }
792 
793     public void setAutoMnemonic(String ControlName, boolean bValue)
794     {
795         Object oControl = xDlgContainer.getControl(ControlName);
796         xControl = UnoRuntime.queryInterface(XControl.class, oControl);
797         XWindowPeer xWindowPeer = xControl.getPeer();
798         XVclWindowPeer xVclWindowPeer = UnoRuntime.queryInterface(XVclWindowPeer.class, xControl.getPeer());
799         xVclWindowPeer.setProperty("AutoMnemonics", Boolean.valueOf(bValue));
800     }
801 
802     public void modifyFontWeight(String ControlName, float FontWeight)
803     {
804         FontDescriptor oFontDesc = new FontDescriptor();
805         oFontDesc.Weight = FontWeight;
806         setControlProperty(ControlName, PropertyNames.FONT_DESCRIPTOR, oFontDesc);
807     }
808 
809     /**
810      * create a peer for this
811      * dialog, using the given
812      * peer as a parent.
813      * @param parentPeer
814      * @return
815      * @throws java.lang.Exception
816      */
817     public XWindowPeer createWindowPeer(XWindowPeer parentPeer) throws com.sun.star.uno.Exception
818     {
819         xWindow.setVisible(false);
820         Object tk = xMSF.createInstance("com.sun.star.awt.Toolkit");
821         if (parentPeer == null)
822         {
823             parentPeer = UnoRuntime.queryInterface(XToolkit.class, tk).getDesktopWindow();
824         }
825         XToolkit xToolkit = UnoRuntime.queryInterface(XToolkit.class, tk);
826         xReschedule = UnoRuntime.queryInterface(XReschedule.class, xToolkit);
827         // TEUER!
828         xControl.createPeer(xToolkit, parentPeer);
829         xWindowPeer = xControl.getPeer();
830         return xControl.getPeer();
831     }
832 
833     /**
834      * Creates a peer for this
835      * dialog, using the active OO frame
836      * as the parent window.
837      * @return
838      * @throws java.lang.Exception
839      */
840     public XWindowPeer createWindowPeer() throws com.sun.star.uno.Exception
841     {
842         return createWindowPeer(null);
843     }
844 
845     // deletes the first entry when this is equal to "DelEntryName"
846     // returns true when a new item is selected
847     public void deletefirstListboxEntry(String ListBoxName, String DelEntryName)
848     {
849         XControl xListControl = xDlgContainer.getControl(ListBoxName);
850         XListBox xListBox = UnoRuntime.queryInterface(XListBox.class, xListControl);
851         String FirstItem = xListBox.getItem((short) 0);
852         if (FirstItem.equals(DelEntryName))
853         {
854             short SelPos = xListBox.getSelectedItemPos();
855             xListBox.removeItems((short) 0, (short) 1);
856             if (SelPos > 0)
857             {
858                 setControlProperty(ListBoxName, PropertyNames.SELECTED_ITEMS, new short[SelPos]);
859                 xListBox.selectItemPos((short) (SelPos - 1), true);
860             }
861         }
862     }
863 
864     public void setPeerProperty(String ControlName, String PropertyName, Object PropertyValue)
865     {
866         Object oControl = xDlgContainer.getControl(ControlName);
867         XControl xControl = UnoRuntime.queryInterface(XControl.class, oControl);
868         XWindowPeer xControlPeer = xControl.getPeer();
869         XVclWindowPeer xVclWindowPeer = UnoRuntime.queryInterface(XVclWindowPeer.class, xControlPeer);
870         xVclWindowPeer.setProperty(PropertyName, PropertyValue);
871     }
872 
873     public static Object getModel(Object control)
874     {
875         XControl xControl = UnoRuntime.queryInterface(XControl.class, control);
876         return xControl.getModel();
877     }
878 
879     public static void setEnabled(Object control, boolean enabled)
880     {
881         setEnabled(control, enabled ? Boolean.TRUE : Boolean.FALSE);
882     }
883 
884     public static void setEnabled(Object control, Boolean enabled)
885     {
886         Helper.setUnoPropertyValue(getModel(control), PropertyNames.PROPERTY_ENABLED, enabled);
887     }
888 
889     /**
890      * @author bc93774
891      * @param oControlModel the model of a control
892      * @return the LabelType according to UIConsts.CONTROLTYPE
893      */
894     public static int getControlModelType(Object oControlModel)
895     {
896         XServiceInfo xServiceInfo = UnoRuntime.queryInterface(XServiceInfo.class, oControlModel);
897         if (xServiceInfo.supportsService("com.sun.star.awt.UnoControlFixedTextModel"))
898         {
899             return UIConsts.CONTROLTYPE.FIXEDTEXT;
900         }
901         else if (xServiceInfo.supportsService("com.sun.star.awt.UnoControlButtonModel"))
902         {
903             return UIConsts.CONTROLTYPE.BUTTON;
904         }
905         else if (xServiceInfo.supportsService("com.sun.star.awt.UnoControlCurrencyFieldModel"))
906         {
907             return UIConsts.CONTROLTYPE.CURRENCYFIELD;
908         }
909         else if (xServiceInfo.supportsService("com.sun.star.awt.UnoControlDateFieldModel"))
910         {
911             return UIConsts.CONTROLTYPE.DATEFIELD;
912         }
913         else if (xServiceInfo.supportsService("com.sun.star.awt.UnoControlFixedLineModel"))
914         {
915             return UIConsts.CONTROLTYPE.FIXEDLINE;
916         }
917         else if (xServiceInfo.supportsService("com.sun.star.awt.UnoControlFormattedFieldModel"))
918         {
919             return UIConsts.CONTROLTYPE.FORMATTEDFIELD;
920         }
921         else if (xServiceInfo.supportsService("com.sun.star.awt.UnoControlRoadmapModel"))
922         {
923             return UIConsts.CONTROLTYPE.ROADMAP;
924         }
925         else if (xServiceInfo.supportsService("com.sun.star.awt.UnoControlNumericFieldModel"))
926         {
927             return UIConsts.CONTROLTYPE.NUMERICFIELD;
928         }
929         else if (xServiceInfo.supportsService("com.sun.star.awt.UnoControlPatternFieldModel"))
930         {
931             return UIConsts.CONTROLTYPE.PATTERNFIELD;
932         }
933         else if (xServiceInfo.supportsService("com.sun.star.awt.UnoControlHyperTextModel"))
934         {
935             return UIConsts.CONTROLTYPE.HYPERTEXT;
936         }
937         else if (xServiceInfo.supportsService("com.sun.star.awt.UnoControlProgressBarModel"))
938         {
939             return UIConsts.CONTROLTYPE.PROGRESSBAR;
940         }
941         else if (xServiceInfo.supportsService("com.sun.star.awt.UnoControlTimeFieldModel"))
942         {
943             return UIConsts.CONTROLTYPE.TIMEFIELD;
944         }
945         else if (xServiceInfo.supportsService("com.sun.star.awt.UnoControlImageControlModel"))
946         {
947             return UIConsts.CONTROLTYPE.IMAGECONTROL;
948         }
949         else if (xServiceInfo.supportsService("com.sun.star.awt.UnoControlRadioButtonModel"))
950         {
951             return UIConsts.CONTROLTYPE.RADIOBUTTON;
952         }
953         else if (xServiceInfo.supportsService("com.sun.star.awt.UnoControlCheckBoxModel"))
954         {
955             return UIConsts.CONTROLTYPE.CHECKBOX;
956         }
957         else if (xServiceInfo.supportsService("com.sun.star.awt.UnoControlEditModel"))
958         {
959             return UIConsts.CONTROLTYPE.EDITCONTROL;
960         }
961         else if (xServiceInfo.supportsService("com.sun.star.awt.UnoControlComboBoxModel"))
962         {
963             return UIConsts.CONTROLTYPE.COMBOBOX;
964         }
965         else if (xServiceInfo.supportsService("com.sun.star.awt.UnoControlListBoxModel")) // TODO Comboboxes??
966         {
967             return UIConsts.CONTROLTYPE.LISTBOX;
968         }
969         else
970         {
971             return UIConsts.CONTROLTYPE.UNKNOWN;
972         }
973     }
974 
975     /**
976      * @author bc93774
977      * @param oControlModel
978      * @return the name of the property that contains the value of a controlmodel
979      */
980     public static String getDisplayProperty(Object oControlModel)
981     {
982         XServiceInfo xServiceInfo = UnoRuntime.queryInterface(XServiceInfo.class, oControlModel);
983         int itype = getControlModelType(oControlModel);
984         return getDisplayProperty(itype);
985     }
986 
987     /**
988      * @author bc93774
989      * @param itype The type of the control conforming to UIConst.ControlType
990      * @return the name of the property that contains the value of a controlmodel
991      */
992     public static String getDisplayProperty(int itype)
993     {
994         // String propertyname = PropertyNames.EMPTY_STRING;
995         switch (itype)
996         {
997             case UIConsts.CONTROLTYPE.FIXEDTEXT:
998                 return PropertyNames.PROPERTY_LABEL;
999             case UIConsts.CONTROLTYPE.BUTTON:
1000                 return PropertyNames.PROPERTY_LABEL;
1001             case UIConsts.CONTROLTYPE.FIXEDLINE:
1002                 return PropertyNames.PROPERTY_LABEL;
1003             case UIConsts.CONTROLTYPE.NUMERICFIELD:
1004                 return "Value";
1005             case UIConsts.CONTROLTYPE.CURRENCYFIELD:
1006                 return "Value";
1007             case UIConsts.CONTROLTYPE.FORMATTEDFIELD:
1008                 return "EffectiveValue";
1009             case UIConsts.CONTROLTYPE.DATEFIELD:
1010                 return "Date";
1011             case UIConsts.CONTROLTYPE.TIMEFIELD:
1012                 return "Time";
1013             case UIConsts.CONTROLTYPE.SCROLLBAR:
1014                 return "ScrollValue";
1015             case UIConsts.CONTROLTYPE.PROGRESSBAR:
1016                 return "ProgressValue";
1017             case UIConsts.CONTROLTYPE.IMAGECONTROL:
1018                 return PropertyNames.PROPERTY_IMAGEURL;
1019             case UIConsts.CONTROLTYPE.RADIOBUTTON:
1020                 return PropertyNames.PROPERTY_STATE;
1021             case UIConsts.CONTROLTYPE.CHECKBOX:
1022                 return PropertyNames.PROPERTY_STATE;
1023             case UIConsts.CONTROLTYPE.EDITCONTROL:
1024                 return "Text";
1025             case UIConsts.CONTROLTYPE.COMBOBOX:
1026                 return "Text";
1027             case UIConsts.CONTROLTYPE.PATTERNFIELD:
1028                 return "Text";
1029             case UIConsts.CONTROLTYPE.LISTBOX:
1030                 return PropertyNames.SELECTED_ITEMS;
1031             default:
1032                 return PropertyNames.EMPTY_STRING;
1033         }
1034     }
1035 
1036     public void addResourceHandler(String _Unit, String _Module)
1037     {
1038         m_oResource = new Resource(xMSF, _Unit, _Module);
1039     }
1040 
1041     public static short setInitialTabindex(int _istep)
1042     {
1043         return (short) (_istep * 100);
1044     }
1045     private Boolean BisHighContrastModeActivated = null;
1046 
1047     public boolean isHighContrastModeActivated()
1048     {
1049         if (xVclWindowPeer != null)
1050         {
1051             if (BisHighContrastModeActivated == null)
1052             {
1053                 int nUIColor;
1054                 try
1055                 {
1056                     nUIColor = AnyConverter.toInt(this.xVclWindowPeer.getProperty("DisplayBackgroundColor"));
1057                 }
1058                 catch (IllegalArgumentException e)
1059                 {
1060                     e.printStackTrace(System.out);
1061                     return false;
1062                 }
1063                 //TODO: The following methods could be wrapped in an own class implementation
1064                 int nRed = getRedColorShare(nUIColor);
1065                 int nGreen = getGreenColorShare(nUIColor);
1066                 int nBlue = getBlueColorShare(nUIColor);
1067                 int nLuminance = ((nBlue * 28 + nGreen * 151 + nRed * 77) / 256);
1068                 boolean bisactivated = (nLuminance <= 25);
1069                 BisHighContrastModeActivated = Boolean.valueOf(bisactivated);
1070                 return bisactivated;
1071             }
1072             else
1073             {
1074                 return BisHighContrastModeActivated.booleanValue();
1075             }
1076         }
1077         else
1078         {
1079             return false;
1080         }
1081     }
1082 
1083     public static int getRedColorShare(int _nColor)
1084     {
1085         int nRed = _nColor / 65536;
1086         int nRedModulo = _nColor % 65536;
1087         int nGreen = nRedModulo / 256;
1088         int nGreenModulo = (nRedModulo % 256);
1089         int nBlue = nGreenModulo;
1090         return nRed;
1091     }
1092 
1093     public static int getGreenColorShare(int _nColor)
1094     {
1095         int nRed = _nColor / 65536;
1096         int nRedModulo = _nColor % 65536;
1097         int nGreen = nRedModulo / 256;
1098         return nGreen;
1099     }
1100 
1101     public static int getBlueColorShare(int _nColor)
1102     {
1103         int nRed = _nColor / 65536;
1104         int nRedModulo = _nColor % 65536;
1105         int nGreen = nRedModulo / 256;
1106         int nGreenModulo = (nRedModulo % 256);
1107         int nBlue = nGreenModulo;
1108         return nBlue;
1109     }
1110 
1111     public String getWizardImageUrl(int _nResId, int _nHCResId)
1112     {
1113         if (isHighContrastModeActivated())
1114         {
1115             return "private:resource/wzi/image/" + _nHCResId;
1116         }
1117         else
1118         {
1119             return "private:resource/wzi/image/" + _nResId;
1120         }
1121     }
1122 
1123     public String getImageUrl(String _surl, String _shcurl)
1124     {
1125         if (isHighContrastModeActivated())
1126         {
1127             return _shcurl;
1128         }
1129         else
1130         {
1131             return _surl;
1132         }
1133     }
1134 
1135     public static short getListBoxLineCount()
1136     {
1137         return (short)20;
1138     }
1139 }
1140