KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > widgets > CmsColorpickerWidget


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/widgets/CmsColorpickerWidget.java,v $
3  * Date : $Date: 2006/03/27 14:52:19 $
4  * Version: $Revision: 1.9 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.widgets;
33
34 import org.opencms.file.CmsObject;
35 import org.opencms.util.CmsStringUtil;
36 import org.opencms.workplace.CmsWorkplace;
37 import org.opencms.xml.types.CmsXmlColorValue;
38
39 import java.util.Map JavaDoc;
40
41 /**
42  * Provides a HTML "color picker" widget, for use on a widget dialog.<p>
43  *
44  * @author Andreas Zahner
45  *
46  * @version $Revision: 1.9 $
47  *
48  * @since 6.0.0
49  */

50 public class CmsColorpickerWidget extends A_CmsWidget {
51
52     /**
53      * Creates a new color picker widget.<p>
54      */

55     public CmsColorpickerWidget() {
56
57         // empty constructor is required for class registration
58
this("");
59     }
60
61     /**
62      * Creates a new color picker widget with the given configuration.<p>
63      *
64      * @param configuration the configuration to use
65      */

66     public CmsColorpickerWidget(String JavaDoc configuration) {
67
68         super(configuration);
69     }
70
71     /**
72      * @see org.opencms.widgets.I_CmsWidget#getDialogIncludes(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog)
73      */

74     public String JavaDoc getDialogIncludes(CmsObject cms, I_CmsWidgetDialog widgetDialog) {
75
76         return getJSIncludeFile(CmsWorkplace.getSkinUri() + "components/widgets/colorpicker.js");
77     }
78
79     /**
80      * @see org.opencms.widgets.I_CmsWidget#getDialogInitCall(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog)
81      */

82     public String JavaDoc getDialogInitCall(CmsObject cms, I_CmsWidgetDialog widgetDialog) {
83
84         return "\tinitColorPicker();\n";
85     }
86
87     /**
88      * @see org.opencms.widgets.I_CmsWidget#getDialogInitMethod(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog)
89      */

90     public String JavaDoc getDialogInitMethod(CmsObject cms, I_CmsWidgetDialog widgetDialog) {
91
92         StringBuffer JavaDoc result = new StringBuffer JavaDoc(128);
93         result.append("function initColorPicker() {\n");
94         result.append("\tcolorPicker.title = \"");
95         result.append(Messages.get().getBundle(widgetDialog.getLocale()).key(Messages.GUI_DIALOG_COLOR_TITLE_0));
96         result.append("\";\n");
97         result.append("\tcolorPicker.url=\"");
98         result.append(CmsWorkplace.getSkinUri());
99         result.append("components/js_colorpicker/index.html\";\n");
100         result.append("}\n");
101         return result.toString();
102     }
103
104     /**
105      * @see org.opencms.widgets.I_CmsWidget#getDialogWidget(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter)
106      */

107     public String JavaDoc getDialogWidget(CmsObject cms, I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param) {
108
109         StringBuffer JavaDoc result = new StringBuffer JavaDoc(16);
110         result.append("<td class=\"xmlTd\">");
111         String JavaDoc colorValue = param.getStringValue(cms);
112         String JavaDoc id = param.getId();
113
114         result.append("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr><td>");
115         result.append("<input type=\"text\"");
116         result.append(" class=\"xmlInputSmall\" name=\"");
117         result.append(id);
118         result.append("\" value=\"");
119         result.append(colorValue);
120         result.append("\" maxlength=\"7\" onkeyup=\"previewColor('");
121         result.append(id);
122         result.append("');\"");
123         result.append(" style=\"background-color: ");
124         result.append(checkColor(colorValue));
125         result.append("; color: ");
126         result.append(getInputFontColor(colorValue));
127         result.append(";\"></td>");
128
129         result.append(widgetDialog.dialogHorizontalSpacer(10));
130         result.append("<td><table class=\"editorbuttonbackground\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>");
131         result.append(widgetDialog.button(
132             "javascript:showColorPicker('" + id + "');",
133             null,
134             "color_fill",
135             Messages.GUI_BUTTON_COLOR_0,
136             widgetDialog.getButtonStyle()));
137         result.append("</tr></table>");
138
139         result.append("</td></tr></table>");
140
141         result.append("</td>");
142         return result.toString();
143     }
144
145     /**
146      * @see org.opencms.widgets.I_CmsWidget#newInstance()
147      */

148     public I_CmsWidget newInstance() {
149
150         return new CmsColorpickerWidget(getConfiguration());
151     }
152
153     /**
154      * @see org.opencms.widgets.I_CmsWidget#setEditorValue(org.opencms.file.CmsObject, java.util.Map, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter)
155      */

156     public void setEditorValue(
157         CmsObject cms,
158         Map JavaDoc formParameters,
159         I_CmsWidgetDialog widgetDialog,
160         I_CmsWidgetParameter param) {
161
162         String JavaDoc[] values = (String JavaDoc[])formParameters.get(param.getId());
163         if ((values != null) && (values.length > 0)) {
164             CmsXmlColorValue castValue = (CmsXmlColorValue)param;
165             String JavaDoc castColorValue = castValue.getStringValue(cms);
166             String JavaDoc colorValue = values[0].trim();
167             if (CmsStringUtil.isNotEmpty(colorValue)) {
168                 castColorValue = colorValue;
169             }
170             param.setStringValue(cms, String.valueOf(castColorValue));
171         }
172     }
173
174     /**
175      * Check the stored color value to prevent display issues in the generated HTML ouput.<p>
176      *
177      * @param color the color value to check
178      * @return the checked color value
179      */

180     private String JavaDoc checkColor(String JavaDoc color) {
181
182         if (color != null) {
183             if (color.indexOf("#") == -1) {
184                 // add the "#" to the color string
185
color = "#" + color;
186             }
187             int colLength = color.length();
188             if (colLength == 4 || colLength == 7) {
189                 return color;
190             }
191         }
192         return "#FFFFFF";
193     }
194
195     /**
196      * Returns the font color of the input field depending on the selected color value.<p>
197      *
198      * @param backgroundColor the selected color value which is displayed as the input field background
199      * @return the font color to use
200      */

201     private String JavaDoc getInputFontColor(String JavaDoc backgroundColor) {
202
203         if (backgroundColor != null && backgroundColor.indexOf("#") == 0) {
204             // remove the "#" from the color string
205
backgroundColor = backgroundColor.substring(1);
206             int colorValue = 50001;
207             try {
208                 // calculate int value of color
209
colorValue = Integer.parseInt(backgroundColor, 16);
210             } catch (NumberFormatException JavaDoc nf) {
211                 // this should never happen
212
}
213             if (colorValue < 50000) {
214                 // for dark colors set font color to white
215
return "#FFFFFF";
216             } else {
217                 // for other colors use black
218
return "#000000";
219             }
220         }
221         return "#000000";
222     }
223 }
Popular Tags