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 java.util.Vector;
30 
31 import com.sun.star.awt.*;
32 import com.sun.star.lang.EventObject;
33 import com.sun.star.uno.AnyConverter;
34 import com.sun.star.uno.UnoRuntime;
35 import com.sun.star.wizards.common.Helper;
36 import com.sun.star.wizards.common.PropertyNames;
37 
38 /**
39  * @author rpiterman
40  *
41  * To change the template for this generated type comment go to
42  * Window>Preferences>Java>Code Generation>Code and Comments
43  */
44 public class PeerConfig implements XWindowListener
45 {
46 
47     private Vector m_aPeerTasks = new Vector();
48     Vector aControlTasks = new Vector();
49     Vector aImageUrlTasks = new Vector();
50     UnoDialog oUnoDialog = null;
51 
52     public PeerConfig(UnoDialog _oUnoDialog)
53     {
54         oUnoDialog = _oUnoDialog;
55         oUnoDialog.xWindow.addWindowListener(this);
56     }
57 
58     class PeerTask
59     {
60 
61         XControl xControl;
62         String[] propnames;
63         Object[] propvalues;
64 
65         public PeerTask(XControl _xControl, String[] propNames_, Object[] propValues_)
66         {
67             propnames = propNames_;
68             propvalues = propValues_;
69             xControl = _xControl;
70         }
71     }
72 
73     class ControlTask
74     {
75 
76         Object oModel;
77         String propname;
78         Object propvalue;
79 
80         public ControlTask(Object _oModel, String _propName, Object _propValue)
81         {
82             propname = _propName;
83             propvalue = _propValue;
84             oModel = _oModel;
85         }
86     }
87 
88     class ImageUrlTask
89     {
90 
91         Object oModel;
92         Object oResource;
93         Object oHCResource;
94 
95         public ImageUrlTask(Object _oModel, Object _oResource, Object _oHCResource)
96         {
97             oResource = _oResource;
98             oHCResource = _oHCResource;
99             oModel = _oModel;
100         }
101     }
102 
103     public void windowResized(WindowEvent arg0)
104     {
105     }
106 
107     public void windowMoved(WindowEvent arg0)
108     {
109     }
110 
111     public void windowShown(EventObject arg0)
112     {
113         try
114         {
115             for (int i = 0; i < this.m_aPeerTasks.size(); i++)
116             {
117                 PeerTask aPeerTask = (PeerTask) m_aPeerTasks.elementAt(i);
118                 XVclWindowPeer xVclWindowPeer = UnoRuntime.queryInterface(XVclWindowPeer.class, aPeerTask.xControl.getPeer());
119                 for (int n = 0; n < aPeerTask.propnames.length; n++)
120                 {
121                     xVclWindowPeer.setProperty(aPeerTask.propnames[n], aPeerTask.propvalues[n]);
122                 }
123             }
124             for (int i = 0; i < this.aControlTasks.size(); i++)
125             {
126                 ControlTask aControlTask = (ControlTask) aControlTasks.elementAt(i);
127                 Helper.setUnoPropertyValue(aControlTask.oModel, aControlTask.propname, aControlTask.propvalue);
128             }
129             for (int i = 0; i < this.aImageUrlTasks.size(); i++)
130             {
131                 ImageUrlTask aImageUrlTask = (ImageUrlTask) aImageUrlTasks.elementAt(i);
132                 String sImageUrl = PropertyNames.EMPTY_STRING;
133                 if (AnyConverter.isInt(aImageUrlTask.oResource))
134                 {
135                     sImageUrl = oUnoDialog.getWizardImageUrl(((Integer) aImageUrlTask.oResource).intValue(), ((Integer) aImageUrlTask.oHCResource).intValue());
136                 }
137                 else if (AnyConverter.isString(aImageUrlTask.oResource))
138                 {
139                     sImageUrl = oUnoDialog.getImageUrl(((String) aImageUrlTask.oResource), ((String) aImageUrlTask.oHCResource));
140                 }
141                 if (!sImageUrl.equals(PropertyNames.EMPTY_STRING))
142                 {
143                     Helper.setUnoPropertyValue(aImageUrlTask.oModel, PropertyNames.PROPERTY_IMAGEURL, sImageUrl);
144                 }
145             }
146 
147         }
148         catch (RuntimeException re)
149         {
150             re.printStackTrace(System.out);
151             throw re;
152         }
153     }
154 
155     public void windowHidden(EventObject arg0)
156     {
157     }
158 
159     public void disposing(EventObject arg0)
160     {
161     }
162 
163     /**
164      *
165      * @param oAPIControl an API control that the interface XControl can be derived from
166      * @param _saccessname
167      */
168     public void setAccessibleName(Object oAPIControl, String _saccessname)
169     {
170         XControl xControl = UnoRuntime.queryInterface(XControl.class, oAPIControl);
171         setPeerProperties(xControl, new String[]
172                 {
173                     "AccessibleName"
174                 }, new String[]
175                 {
176                     _saccessname
177                 });
178     }
179 
180     public void setAccessibleName(XControl _xControl, String _saccessname)
181     {
182         setPeerProperties(_xControl, new String[]
183                 {
184                     "AccessibleName"
185                 }, new String[]
186                 {
187                     _saccessname
188                 });
189     }
190 
191     /**
192      *
193      * @param oAPIControl an API control that the interface XControl can be derived from
194      * @param _propnames
195      * @param _propvalues
196      */
197     public void setPeerProperties(Object oAPIControl, String[] _propnames, Object[] _propvalues)
198     {
199         XControl xControl = UnoRuntime.queryInterface(XControl.class, oAPIControl);
200         setPeerProperties(xControl, _propnames, _propvalues);
201     }
202 
203     public void setPeerProperties(XControl _xControl, String[] propnames, Object[] propvalues)
204     {
205         PeerTask oPeerTask = new PeerTask(_xControl, propnames, propvalues);
206         this.m_aPeerTasks.add(oPeerTask);
207     }
208 
209     /**
210      * assigns an arbitrary property to a control as soon as the peer is created
211      * Note: The property 'ImageUrl' should better be assigned with 'setImageurl(...)', to consider the High Contrast Mode
212      * @param _ocontrolmodel
213      * @param _spropname
214      * @param _propvalue
215      */
216     public void setControlProperty(Object _ocontrolmodel, String _spropname, Object _propvalue)
217     {
218         ControlTask oControlTask = new ControlTask(_ocontrolmodel, _spropname, _propvalue);
219         this.aControlTasks.add(oControlTask);
220     }
221 
222     /**
223      * Assigns an image to the property 'ImageUrl' of a dialog control. The image id must be assigned in a resource file
224      * within the wizards project
225      * wizards project
226      * @param _ocontrolmodel
227      * @param _nResId
228      * @param _nhcResId
229      */
230     public void setImageUrl(Object _ocontrolmodel, int _nResId, int _nhcResId)
231     {
232         ImageUrlTask oImageUrlTask = new ImageUrlTask(_ocontrolmodel, new Integer(_nResId), new Integer(_nhcResId));
233         this.aImageUrlTasks.add(oImageUrlTask);
234     }
235 
236     /**
237      * Assigns an image to the property 'ImageUrl' of a dialog control. The image ids that the Resource urls point to
238      * may be assigned in a Resource file outside the wizards project
239      * @param _ocontrolmodel
240      * @param _sResourceUrl
241      * @param _sHCResourceUrl
242      */
243     public void setImageUrl(Object _ocontrolmodel, String _sResourceUrl, String _sHCResourceUrl)
244     {
245         ImageUrlTask oImageUrlTask = new ImageUrlTask(_ocontrolmodel, _sResourceUrl, _sHCResourceUrl);
246         this.aImageUrlTasks.add(oImageUrlTask);
247     }
248 
249     /**
250      * Assigns an image to the property 'ImageUrl' of a dialog control. The image id must be assigned in a resource file
251      * within the wizards project
252      * wizards project
253      * @param _ocontrolmodel
254      * @param _oResource
255      * @param _oHCResource
256      */
257     public void setImageUrl(Object _ocontrolmodel, Object _oResource, Object _oHCResource)
258     {
259         ImageUrlTask oImageUrlTask = new ImageUrlTask(_ocontrolmodel, _oResource, _oHCResource);
260         this.aImageUrlTasks.add(oImageUrlTask);
261     }
262 }
263