001: /*
002: * GWT-Ext Widget Library
003: * Copyright(c) 2007-2008, GWT-Ext.
004: * licensing@gwt-ext.com
005: *
006: * http://www.gwt-ext.com/license
007: */
008:
009: package com.gwtext.client.widgets;
010:
011: import com.google.gwt.core.client.JavaScriptObject;
012: import com.google.gwt.user.client.DOM;
013: import com.gwtext.client.util.JavaScriptObjectHelper;
014: import com.gwtext.client.widgets.event.ColorPaletteListener;
015:
016: /**
017: * Simple color palette class for choosing colors.
018: */
019: public class ColorPalette extends Component {
020:
021: private static JavaScriptObject configPrototype;
022:
023: static {
024: init();
025: }
026:
027: private static native void init()/*-{
028: var c = new $wnd.Ext.ColorPalette();
029: @com.gwtext.client.widgets.ColorPalette::configPrototype = c.initialConfig;
030: }-*/;
031:
032: protected JavaScriptObject getConfigPrototype() {
033: return configPrototype;
034: }
035:
036: public String getXType() {
037: return "colorpalette";
038: }
039:
040: /**
041: * Create a new CollorPalette.
042: */
043: public ColorPalette() {
044: }
045:
046: public ColorPalette(JavaScriptObject jsObj) {
047: super (jsObj);
048: }
049:
050: protected native JavaScriptObject create(JavaScriptObject config)/*-{
051: return new $wnd.Ext.ColorPalette(config);
052: }-*/;
053:
054: private static ColorPalette instance(JavaScriptObject jsObj) {
055: return new ColorPalette(jsObj);
056: }
057:
058: /**
059: * Selects the specified color in the palette (fires the select event).
060: *
061: * @param color A valid 6-digit color hex code (# will be stripped if included)
062: */
063: public void select(String color) {
064: if (isRendered()) {
065: selectRendered(color);
066: } else {
067: setValue(color);
068: }
069: }
070:
071: private native void selectRendered(String color) /*-{
072: var cp = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
073: cp.select(color);
074: }-*/;
075:
076: /**
077: * Add a ColorPalette listener.
078: *
079: * @param listener the listener
080: */
081: public native void addListener(ColorPaletteListener listener) /*-{
082: this.@com.gwtext.client.widgets.Component::addListener(Lcom/gwtext/client/widgets/event/ComponentListener;)(listener);
083: var componentJ = this;
084:
085: this.@com.gwtext.client.widgets.Component::addListener(Ljava/lang/String;Lcom/google/gwt/core/client/JavaScriptObject;)('select',
086: function(cp, color) {
087: listener.@com.gwtext.client.widgets.event.ColorPaletteListener::onSelect(Lcom/gwtext/client/widgets/ColorPalette;Ljava/lang/String;)(componentJ, color);
088: }
089: );
090: }-*/;
091:
092: // --- config properties ---
093:
094: /**
095: * Return true if reselecting is allowed.
096: *
097: * @return true if reselecting is allowed
098: */
099: public boolean isAllowReselect() {
100: return getAttributeAsBoolean("allowReselect");
101: }
102:
103: /**
104: * If set to true then reselecting a color that is already selected fires the selection event.
105: *
106: * @param allowReselect true to allow reselect
107: */
108: public void setAllowReselect(boolean allowReselect) {
109: setAttribute("allowReselect", allowReselect, true);
110: }
111:
112: /**
113: * @return the CSS of the containing element
114: */
115: public String getItemCls() {
116: return getAttribute("itemCls");
117: }
118:
119: /**
120: * The CSS class to apply to the containing element (defaults to "x-color-palette").
121: *
122: * @param itemCls the item CSS class
123: */
124: public void setItemCls(String itemCls) {
125: if (!isRendered()) {
126: setAttribute("itemCls", itemCls, true);
127: } else {
128: DOM.setElementAttribute(getElement(), "className", itemCls);
129: setAttribute("itemCls", itemCls, true, true);
130: }
131: }
132:
133: /**
134: * The highlighted color.
135: *
136: * @return the color value
137: */
138: public String getValue() {
139: return getAttribute("value");
140: }
141:
142: /**
143: * The color to highlight (should be a valid 6-digit color hex code without the # symbol).
144: * Note that the hex codes are case-sensitive.
145: *
146: * @param value the initial color
147: */
148: public void setValue(String value) {
149: if (isRendered()) {
150: select(value);
151: setAttribute("value", value, true, true);
152: } else {
153: setAttribute("value", value, true);
154: }
155: }
156:
157: /**
158: * An array of 6-digit color hex code strings (without the # symbol). This array can contain any number of colors,
159: * and each hex code should be unique. The width of the palette is controlled via CSS by adjusting the width property
160: * of the 'x-color-palette' class (or assigning a custom class), so you can balance the number of colors with the width
161: * setting until the box is symmetrical.
162: *
163: * @return the colors array
164: */
165: public String[] getColors() {
166: return JavaScriptObjectHelper.getAttributeAsStringArray(config,
167: "colors");
168: }
169:
170: /**
171: * Set the colors array.
172: *
173: * @param colors the colors array
174: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
175: */
176: public void setColors(String[] colors) throws IllegalStateException {
177: setAttribute("colors", JavaScriptObjectHelper
178: .convertToJavaScriptArray(colors), true);
179: }
180: }
|