1 /* ColorChooser.java - a Java class for a color choosing dialog for OpenOffice.org
3 * Copyright (C) 2008 Dietmar Hiller, dhiller
5 This program is free software; you can redistribute it and/or modify it under the Terms of the GNU Lesser General Public License
6 as published by the Free Software Foundation; version 3 of the License.
8 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
11 You should have received a copy of the GNU Lesser General Public License along with this program; if not,
12 see <http://www.gnu.org/licenses/>.
14 * Created on 5. Mai 2008, 21:34
18 package name
.dhiller
.ooo
.ColorChooser
;
20 import com
.sun
.star
.awt
.*;
21 import com
.sun
.star
.beans
.PropertyVetoException
;
22 import com
.sun
.star
.beans
.UnknownPropertyException
;
23 import com
.sun
.star
.beans
.XPropertySet
;
24 import com
.sun
.star
.lang
.WrappedTargetException
;
25 import com
.sun
.star
.lib
.uno
.helper
.WeakBase
;
26 import com
.sun
.star
.lib
.uno
.helper
.Factory
;
27 import com
.sun
.star
.lang
.XMultiComponentFactory
;
28 import com
.sun
.star
.lang
.XSingleComponentFactory
;
29 import com
.sun
.star
.lang
.XServiceInfo
;
30 import com
.sun
.star
.registry
.XRegistryKey
;
31 import com
.sun
.star
.report
.XImageControl
;
32 import com
.sun
.star
.uno
.XComponentContext
;
33 import com
.sun
.star
.uno
.UnoRuntime
;
35 import java
.awt
.Color
;
37 /** Colordialog for OpenOffice.org
40 * @since OOo 2.0.4 (dialog handling, see http://api.openoffice.org/docs/DevelopersGuide/Components/Components.xhtml ch. 4.11)
42 public final class ColorChooser
extends WeakBase
43 implements XDialogEventHandler
45 private final XComponentContext m_xContext
;
46 private XDialog m_xDialog
;
47 private XControlContainer m_xCC
;
48 private XScrollBar m_xDlgHue
;
49 private XNumericField m_xDlgSat
;
50 private XNumericField m_xDlgVal
;
51 private XPropertySet m_xrColorPS
;
52 private Color m_initialColor
;
53 private Color m_returnColor
;
55 /** Creates a new instance of ColorChooser */
56 public ColorChooser(XComponentContext xContext
) {
57 m_xContext
= xContext
;
60 /** create ColorDialog and execute
61 * @param sDlgDir - URL of directory with dialog
62 * @param sDlgName - Name of dialog
63 * @param sDlgHue - URL to image of hue
65 * @since OpenOffice.org 2.0.4
66 * @return Color final color
67 * @exception com.sun.star.lang.IllegalArgumentException
68 * @exception com.sun.star.uno.Exception
70 // http://api.openoffice.org/docs/common/ref/com/sun/star/awt/XDialogProvider2.html
71 public Color
executeDialog(String sDlgDir
, String sDlgName
, String sDlgHue
,
74 final XMultiComponentFactory xFact
= m_xContext
.getServiceManager();
75 final Object xObject
= xFact
.createInstanceWithContext(
76 "com.sun.star.awt.DialogProvider2", m_xContext
);
77 final XDialogProvider2 aDialogProvider
= (XDialogProvider2
)
78 UnoRuntime
.queryInterface(XDialogProvider2
.class, xObject
);
79 m_xDialog
= aDialogProvider
.createDialogWithHandler
80 (sDlgDir
+"/"+sDlgName
, this);
82 Init(m_xDialog
, sDlgDir
+"/"+sDlgHue
,initialColor
);
84 short nReturnValue
= m_xDialog
.execute();
86 if (nReturnValue
== 1) { // okay
87 m_returnColor
= getColor(); }
88 } catch (com
.sun
.star
.lang
.IllegalArgumentException ex
) {
90 } catch (com
.sun
.star
.uno
.Exception ex
) {
97 * Initialize the Dialog (used internally only)
99 * @param sURLHue URL to hue file
100 * @param initialColor
102 private void Init(XDialog theDialog
, String sURLHue
, Color initialColor
) {
103 m_initialColor
= initialColor
;
104 m_xCC
= (XControlContainer
)UnoRuntime
.queryInterface(XControlContainer
.class,
106 // always useful controls etc
107 final XControl xDlgHueCtrl
= m_xCC
.getControl("dlgHue");
108 m_xDlgHue
= (XScrollBar
) UnoRuntime
.queryInterface(XScrollBar
.class, xDlgHueCtrl
);
110 final XControl xDlgSatCtrl
= m_xCC
.getControl("dlgSat");
111 m_xDlgSat
= (XNumericField
) UnoRuntime
.queryInterface(XNumericField
.class, xDlgSatCtrl
);
113 final XControl xDlgValCtrl
= m_xCC
.getControl("dlgVal");
114 m_xDlgVal
= (XNumericField
) UnoRuntime
.queryInterface(XNumericField
.class, xDlgValCtrl
);
116 final XControl xrColor
= m_xCC
.getControl("rColor");
117 final XControlModel xrColorModel
= xrColor
.getModel();
118 m_xrColorPS
= (XPropertySet
)UnoRuntime
.queryInterface
119 (XPropertySet
.class, xrColorModel
);
121 final XControl xImgHue
= m_xCC
.getControl("imgHue");
122 final XControlModel xImgHueModel
= xImgHue
.getModel();
123 final XPropertySet xPS
= (XPropertySet
)UnoRuntime
.queryInterface
124 (XPropertySet
.class, xImgHueModel
);
126 xPS
.setPropertyValue( "ImageURL", sURLHue
);
127 } catch (UnknownPropertyException ex
) {
128 ex
.printStackTrace();
129 } catch (PropertyVetoException ex
) {
130 ex
.printStackTrace();
131 } catch (WrappedTargetException ex
) {
132 ex
.printStackTrace();
133 } catch (com
.sun
.star
.lang
.IllegalArgumentException ex
) {
134 ex
.printStackTrace();
136 setColor(initialColor
);
139 /** called from the dialog to handle events
140 * there is no need to call it directly
141 * @param xDialog the dialog instance that generated the event
142 * @param aEvent object derived from ::com::sun::star::lang::EventObject
143 * @param aEventString the name of the function which is to be called
144 * @return true if the event was handled, otherwise false.
145 * interface XDialogEventHandler
147 public boolean callHandlerMethod(XDialog xDialog
, Object aEvent
, String aEventString
) {
148 boolean returnValue
= false;
149 if (aEventString
.equals("DlgColorHSV")) { // any color button
150 Color gotColor
= getColor();
151 setPreview(gotColor
);
154 else if (aEventString
.equals("DlgOkay")) { // BOK
155 m_returnColor
= getColor();
156 m_xDialog
.endExecute();
159 else if (aEventString
.equals("DlgCancel")) { // BCancel
160 m_returnColor
= m_initialColor
;
161 m_xDialog
.endExecute();
164 else if (aEventString
.equals("DlgReset")) { // BReset
165 setColor(m_initialColor
);
171 /** getSupportedMethodNames
172 * there is no need to call it directly
173 * interface XDialogEventHandler
175 public String
[] getSupportedMethodNames() {
176 String
[] retValue
= new String
[4];
177 retValue
[0]= "DlgOkay"; // BOK
178 retValue
[1]= "DlgCancel"; // BCancel
179 retValue
[2]= "DlgReset"; // BReset
180 retValue
[3]= "DlgColorHSV"; // any color button
184 /** gets the Color from the dialog
187 private Color
getColor() {
188 float hue
= (float) m_xDlgHue
.getValue() / 360.0f
;
189 float sat
= (float) m_xDlgSat
.getValue() / 100.0f
;
190 float val
= (float) m_xDlgVal
.getValue() / 100.0f
;
191 Color workColor
= new Color(0);
192 Color gotColor
= Color
.getHSBColor(hue
, sat
, val
);
196 /** display color values
197 * Hue: 0..360, Saturation: 0..100, Value/Brightness: 0..100
198 * @param initialColor
200 private void setColor(Color initialColor
) {
201 float[] colorHSB
= initialColor
.RGBtoHSB( initialColor
.getRed(),
202 initialColor
.getGreen(), initialColor
.getBlue(), null);
203 m_xDlgHue
.setValue(Math
.round(colorHSB
[0]*360));
204 m_xDlgSat
.setValue(Math
.round(colorHSB
[1]*100));
205 m_xDlgVal
.setValue(Math
.round(colorHSB
[2]*100));
206 setPreview(initialColor
);
209 /** set color of preview bar
210 * @param color color to be set
211 * @exception UnknownPropertyException Property "BackgroundColor" not available
212 * @exception PropertyVetoException Property "BackgroundColor" not available
213 * @exception WrappedTargetException at setPropertyValue "BackgroundColor"
214 * @exception com.sun.star.lang.IllegalArgumentException at setPropertyValue "BackgroundColor"
216 private void setPreview(Color color
) {
218 m_xrColorPS
.setPropertyValue( "BackgroundColor", Math
.round(color
.getRed()*256*256
219 +color
.getGreen()*256 + color
.getBlue()));
220 } catch (UnknownPropertyException ex
) {
221 ex
.printStackTrace();
222 } catch (PropertyVetoException ex
) {
223 ex
.printStackTrace();
224 } catch (WrappedTargetException ex
) {
225 ex
.printStackTrace();
226 } catch (com
.sun
.star
.lang
.IllegalArgumentException ex
) {
227 ex
.printStackTrace();
231 /** returns the version of this class
232 * this function MUST be maintained for every modification
233 * @return Version in the format YYMMDD-<name of developer>
235 public String
getVersion() {
236 return ("080507-DHiller");