1 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 ; 40 41 50 public class CmsColorpickerWidget extends A_CmsWidget { 51 52 55 public CmsColorpickerWidget() { 56 57 this(""); 59 } 60 61 66 public CmsColorpickerWidget(String configuration) { 67 68 super(configuration); 69 } 70 71 74 public String getDialogIncludes(CmsObject cms, I_CmsWidgetDialog widgetDialog) { 75 76 return getJSIncludeFile(CmsWorkplace.getSkinUri() + "components/widgets/colorpicker.js"); 77 } 78 79 82 public String getDialogInitCall(CmsObject cms, I_CmsWidgetDialog widgetDialog) { 83 84 return "\tinitColorPicker();\n"; 85 } 86 87 90 public String getDialogInitMethod(CmsObject cms, I_CmsWidgetDialog widgetDialog) { 91 92 StringBuffer result = new StringBuffer (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 107 public String getDialogWidget(CmsObject cms, I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param) { 108 109 StringBuffer result = new StringBuffer (16); 110 result.append("<td class=\"xmlTd\">"); 111 String colorValue = param.getStringValue(cms); 112 String 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 148 public I_CmsWidget newInstance() { 149 150 return new CmsColorpickerWidget(getConfiguration()); 151 } 152 153 156 public void setEditorValue( 157 CmsObject cms, 158 Map formParameters, 159 I_CmsWidgetDialog widgetDialog, 160 I_CmsWidgetParameter param) { 161 162 String [] values = (String [])formParameters.get(param.getId()); 163 if ((values != null) && (values.length > 0)) { 164 CmsXmlColorValue castValue = (CmsXmlColorValue)param; 165 String castColorValue = castValue.getStringValue(cms); 166 String colorValue = values[0].trim(); 167 if (CmsStringUtil.isNotEmpty(colorValue)) { 168 castColorValue = colorValue; 169 } 170 param.setStringValue(cms, String.valueOf(castColorValue)); 171 } 172 } 173 174 180 private String checkColor(String color) { 181 182 if (color != null) { 183 if (color.indexOf("#") == -1) { 184 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 201 private String getInputFontColor(String backgroundColor) { 202 203 if (backgroundColor != null && backgroundColor.indexOf("#") == 0) { 204 backgroundColor = backgroundColor.substring(1); 206 int colorValue = 50001; 207 try { 208 colorValue = Integer.parseInt(backgroundColor, 16); 210 } catch (NumberFormatException nf) { 211 } 213 if (colorValue < 50000) { 214 return "#FFFFFF"; 216 } else { 217 return "#000000"; 219 } 220 } 221 return "#000000"; 222 } 223 } | Popular Tags |