]> permondes.de Git - BorderLiner_Java.git/blob - ColorChooser/ColorChooser.java
Initial release to git of this project from 2012/2013
[BorderLiner_Java.git] / ColorChooser / ColorChooser.java
1 /* ColorChooser.java - a Java class for a color choosing dialog for OpenOffice.org
2 *
3 * Copyright (C) 2008 Dietmar Hiller, dhiller
4
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.
7
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.
10
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/>.
13 *
14 * Created on 5. Mai 2008, 21:34
15 *
16 */
17
18 package name.dhiller.ooo.ColorChooser;
19
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;
34
35 import java.awt.Color;
36
37 /** Colordialog for OpenOffice.org
38 * @author dhiller
39 * @version 080507
40 * @since OOo 2.0.4 (dialog handling, see http://api.openoffice.org/docs/DevelopersGuide/Components/Components.xhtml ch. 4.11)
41 */
42 public final class ColorChooser extends WeakBase
43 implements XDialogEventHandler
44 {
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;
54
55 /** Creates a new instance of ColorChooser */
56 public ColorChooser(XComponentContext xContext) {
57 m_xContext = xContext;
58 }
59
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
64 * @param initialColor
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
69 */
70 // http://api.openoffice.org/docs/common/ref/com/sun/star/awt/XDialogProvider2.html
71 public Color executeDialog(String sDlgDir, String sDlgName, String sDlgHue,
72 Color initialColor) {
73 try {
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);
81
82 Init(m_xDialog, sDlgDir+"/"+sDlgHue,initialColor);
83
84 short nReturnValue = m_xDialog.execute();
85
86 if (nReturnValue == 1) { // okay
87 m_returnColor = getColor(); }
88 } catch (com.sun.star.lang.IllegalArgumentException ex) {
89 ex.printStackTrace();
90 } catch (com.sun.star.uno.Exception ex) {
91 ex.printStackTrace();
92 }
93 return m_returnColor;
94 }
95
96 /**
97 * Initialize the Dialog (used internally only)
98 * @param theDialog
99 * @param sURLHue URL to hue file
100 * @param initialColor
101 */
102 private void Init(XDialog theDialog, String sURLHue, Color initialColor) {
103 m_initialColor = initialColor;
104 m_xCC = (XControlContainer)UnoRuntime.queryInterface(XControlContainer.class,
105 theDialog);
106 // always useful controls etc
107 final XControl xDlgHueCtrl = m_xCC.getControl("dlgHue");
108 m_xDlgHue = (XScrollBar) UnoRuntime.queryInterface(XScrollBar.class, xDlgHueCtrl);
109
110 final XControl xDlgSatCtrl = m_xCC.getControl("dlgSat");
111 m_xDlgSat = (XNumericField) UnoRuntime.queryInterface(XNumericField.class, xDlgSatCtrl);
112
113 final XControl xDlgValCtrl = m_xCC.getControl("dlgVal");
114 m_xDlgVal = (XNumericField) UnoRuntime.queryInterface(XNumericField.class, xDlgValCtrl);
115
116 final XControl xrColor = m_xCC.getControl("rColor");
117 final XControlModel xrColorModel = xrColor.getModel();
118 m_xrColorPS = (XPropertySet)UnoRuntime.queryInterface
119 (XPropertySet.class, xrColorModel );
120 // Colorbar
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 );
125 try {
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();
135 }
136 setColor(initialColor);
137 }
138
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
146 */
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);
152 returnValue = true;
153 }
154 else if (aEventString.equals("DlgOkay")) { // BOK
155 m_returnColor = getColor();
156 m_xDialog.endExecute();
157 returnValue = true;
158 }
159 else if (aEventString.equals("DlgCancel")) { // BCancel
160 m_returnColor = m_initialColor;
161 m_xDialog.endExecute();
162 returnValue = true;
163 }
164 else if (aEventString.equals("DlgReset")) { // BReset
165 setColor(m_initialColor);
166 returnValue = true;
167 }
168 return returnValue;
169 }
170
171 /** getSupportedMethodNames
172 * there is no need to call it directly
173 * interface XDialogEventHandler
174 */
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
181 return retValue;
182 }
183
184 /** gets the Color from the dialog
185 * @return Color
186 */
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);
193 return gotColor;
194 }
195
196 /** display color values
197 * Hue: 0..360, Saturation: 0..100, Value/Brightness: 0..100
198 * @param initialColor
199 */
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);
207 }
208
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"
215 */
216 private void setPreview(Color color) {
217 try {
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();
228 }
229 }
230
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>
234 */
235 public String getVersion() {
236 return ("080507-DHiller");
237 }
238
239 }