001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.vmd.midp.propertyeditors;
043:
044: import java.beans.PropertyChangeListener;
045: import java.lang.reflect.InvocationTargetException;
046: import org.netbeans.modules.vmd.api.model.PropertyValue;
047: import org.netbeans.modules.vmd.midp.components.MidpTypes;
048: import org.netbeans.modules.vmd.midp.propertyeditors.api.usercode.PropertyEditorElement;
049: import org.netbeans.modules.vmd.midp.propertyeditors.api.usercode.PropertyEditorUserCode;
050: import org.openide.explorer.propertysheet.InplaceEditor;
051: import java.awt.Color;
052: import java.awt.Component;
053: import java.awt.Graphics;
054: import java.awt.Rectangle;
055: import java.awt.event.ActionListener;
056: import java.beans.PropertyEditor;
057: import java.util.Collections;
058: import java.util.regex.Pattern;
059: import javax.swing.*;
060: import org.openide.DialogDisplayer;
061: import org.openide.NotifyDescriptor;
062: import org.openide.awt.Mnemonics;
063: import org.openide.explorer.propertysheet.PropertyEnv;
064: import org.openide.explorer.propertysheet.PropertyModel;
065: import org.openide.util.NbBundle;
066:
067: /**
068: *
069: * @author Karol Harezlak
070: */
071: public class PropertyEditorColorChooser extends PropertyEditorUserCode
072: implements PropertyEditorElement {
073:
074: private static final String ERROR_WARNING = NbBundle.getMessage(
075: PropertyEditorColorChooser.class, "MSG_ILLEGAL_FORMATING"); // NOI18N
076: private static final String BUTTON_NAME = NbBundle.getMessage(
077: PropertyEditorColorChooser.class, "LBL_BUTTON_NAME"); // NOI18N
078:
079: private JColorChooser customEditorElement;
080: private JRadioButton radioButton;
081: private InplaceEditor inplaceEditor;
082: private boolean supportsCustomEditor;
083:
084: public PropertyEditorColorChooser(boolean supportsCustomEditor) {
085: super (NbBundle.getMessage(PropertyEditorColorChooser.class,
086: "LBL_COLOR_CHOOSER_UCLABEL")); // NOI18N
087: this .supportsCustomEditor = supportsCustomEditor;
088:
089: initElements(Collections
090: .<PropertyEditorElement> singleton(this ));
091: }
092:
093: @Override
094: public InplaceEditor getInplaceEditor() {
095: if (inplaceEditor == null) {
096: inplaceEditor = new PickColorInplaceEditor();
097: }
098: return inplaceEditor;
099: }
100:
101: @Override
102: public void paintValue(Graphics g, Rectangle box) {
103: PropertyValue value = (PropertyValue) super .getValue();
104: Integer rgb = null;
105: if (value.getPrimitiveValue() instanceof Integer) {
106: rgb = (Integer) value.getPrimitiveValue();
107: } else {
108: return;
109: }
110:
111: g.drawRect(box.x + 1, box.y + 2, 11, 11);
112: Color color = new Color(rgb);
113: StringBuffer str = new StringBuffer();
114: str.append('['); //NOI18N
115: str.append(color.getRed());
116: str.append(','); //NOI18N
117: str.append(color.getGreen());
118: str.append(','); //NOI18N
119: str.append(color.getBlue());
120: str.append(']'); //NOI18N
121: g.drawString(str.toString(), box.x + 18, box.y + 11);
122: g.setColor(color);
123: g.fillRect(box.x + 2, box.y + 3, 10, 10);
124: g.dispose();
125: }
126:
127: @Override
128: public boolean supportsCustomEditor() {
129: if (!supportsCustomEditor) {
130: return false;
131: }
132: return super .supportsCustomEditor();
133: }
134:
135: public JComponent getCustomEditorComponent() {
136: if (customEditorElement == null) {
137: customEditorElement = new JColorChooser();
138: }
139: return customEditorElement;
140: }
141:
142: public JRadioButton getRadioButton() {
143: if (radioButton == null) {
144: radioButton = new JRadioButton(BUTTON_NAME);
145: Mnemonics.setLocalizedText(radioButton, BUTTON_NAME);
146: }
147: return radioButton;
148: }
149:
150: @Override
151: public boolean isPaintable() {
152: PropertyValue propertyValue = (PropertyValue) getValue();
153: return propertyValue.getKind() == PropertyValue.Kind.VALUE;
154: }
155:
156: public boolean isVerticallyResizable() {
157: return true;
158: }
159:
160: public boolean isInitiallySelected() {
161: return false;
162: }
163:
164: @Override
165: public String getAsText() {
166: if (isCurrentValueAUserCodeType()) {
167: return USER_CODE_TEXT;
168: } else if (isCurrentValueANull()) {
169: return NULL_TEXT;
170: }
171: PropertyValue value = (PropertyValue) getValue();
172: if (value.getKind() == PropertyValue.Kind.VALUE
173: && value.getPrimitiveValue() instanceof Integer) {
174: return getFormatedRGB((Integer) value.getPrimitiveValue());
175: }
176: throw new IllegalStateException();
177: }
178:
179: private String getFormatedRGB(Integer rgb) {
180: Color color = new Color(rgb);
181: StringBuffer stringRGB = new StringBuffer();
182: stringRGB.append('['); //NOI18N
183: stringRGB.append(color.getRed());
184: stringRGB.append(','); //NOI18N
185: stringRGB.append(color.getGreen());
186: stringRGB.append(','); //NOI18N
187: stringRGB.append(color.getBlue());
188: stringRGB.append(']'); //NOI18N
189: return stringRGB.toString();
190: }
191:
192: public void setTextForPropertyValue(String text) {
193: }
194:
195: public String getTextForPropertyValue() {
196: return ""; //NOI18N
197: }
198:
199: public void updateState(PropertyValue value) {
200: radioButton.setSelected(!isCurrentValueAUserCodeType());
201: if (value == null
202: || value.getKind() != PropertyValue.Kind.VALUE
203: || value.getKind() == PropertyValue.Kind.NULL) {
204: return;
205: }
206: if (!(value.getPrimitiveValue() instanceof Integer)) {
207: throw new IllegalStateException();
208: }
209: customEditorElement.setColor(new Color((Integer) value
210: .getPrimitiveValue()));
211: }
212:
213: private void saveValue(Integer rgb) {
214: PropertyValue rgbValue = MidpTypes.createIntegerValue(rgb);
215: setValue(rgbValue);
216: JTextField tf = (JTextField) inplaceEditor.getComponent();
217: tf.setText(getFormatedRGB(rgb));
218: }
219:
220: @Override
221: public void customEditorOKButtonPressed() {
222: super .customEditorOKButtonPressed();
223: if (radioButton.isSelected()) {
224: saveValue(customEditorElement.getColor().getRGB());
225: }
226: }
227:
228: @Override
229: public boolean canWrite() {
230: return MidpPropertyEditorSupport
231: .singleSelectionEditAsTextOnly();
232: }
233:
234: private class PickColorInplaceEditor implements InplaceEditor {
235:
236: private PropertyModel model;
237: private JTextField component;
238:
239: public void connect(PropertyEditor pe, PropertyEnv env) {
240: }
241:
242: public JComponent getComponent() {
243: if (component == null) {
244: component = new JTextField();
245: PropertyValue value = (PropertyValue) getValue();
246: if (value.getKind() == PropertyValue.Kind.VALUE
247: && value.getPrimitiveValue() instanceof Integer) {
248: component.setText(getFormatedRGB((Integer) value
249: .getPrimitiveValue()));
250: }
251: }
252: component.selectAll();
253: return component;
254: }
255:
256: public void clear() {
257: }
258:
259: public Object getValue() {
260: return PropertyEditorColorChooser.this .getValue();
261: }
262:
263: public void setValue(Object o) {
264: }
265:
266: public boolean supportsTextEntry() {
267: return true;
268: }
269:
270: public void reset() {
271: }
272:
273: public void addActionListener(ActionListener al) {
274: }
275:
276: public void removeActionListener(ActionListener al) {
277: }
278:
279: public KeyStroke[] getKeyStrokes() {
280: return null;
281: }
282:
283: public PropertyEditor getPropertyEditor() {
284: return PropertyEditorColorChooser.this ;
285: }
286:
287: public PropertyModel getPropertyModel() {
288: if (model == null) {
289: model = new ColorPropertyModel(component);
290: }
291: return model;
292: }
293:
294: public void setPropertyModel(PropertyModel pm) {
295: }
296:
297: public boolean isKnownComponent(Component c) {
298: return true;
299: }
300: }
301:
302: private class ColorPropertyModel implements PropertyModel {
303:
304: private JTextField component;
305:
306: ColorPropertyModel(JTextField component) {
307: this .component = component;
308: }
309:
310: public Object getValue() throws InvocationTargetException {
311: return component.getText();
312: }
313:
314: public void setValue(Object o) throws InvocationTargetException {
315: String text = component.getText();
316: text = text.trim().replaceAll(
317: Pattern.compile("[\\[$\\]]").pattern(), ""); // NOI18N
318: if (Pattern.compile("[^0123456789,]").matcher(text).find()
319: || text.split(",").length != 3) { // NOI18N
320: PropertyValue value = (PropertyValue) o;
321: component.setText(getFormatedRGB((Integer) value
322: .getPrimitiveValue()));
323: wrongValueWarning((PropertyValue) o, text);
324: return;
325: }
326: int[] colors = new int[3];
327: int i = 0;
328: for (String number : text.split(",")) { //NOI18N
329: try {
330: colors[i++] = Integer.valueOf(number);
331: } catch (NumberFormatException ex) {
332: wrongValueWarning((PropertyValue) o, text);
333: return;
334: }
335: if (Integer.valueOf(number) < 0
336: || Integer.valueOf(number) > 255) {
337: wrongValueWarning((PropertyValue) o, text);
338: return;
339: }
340: }
341: int rgb = new Color(colors[0], colors[1], colors[2])
342: .getRGB();
343: PropertyEditorColorChooser.this .saveValue(rgb);
344: PropertyEditorColorChooser.this .invokeSaveToModel();
345: }
346:
347: public Class getPropertyType() {
348: return PropertyEditorColorChooser.class;
349: }
350:
351: public Class getPropertyEditorClass() {
352: return PropertyEditorColorChooser.class;
353: }
354:
355: public void addPropertyChangeListener(PropertyChangeListener l) {
356: }
357:
358: public void removePropertyChangeListener(
359: PropertyChangeListener l) {
360: }
361:
362: private void wrongValueWarning(PropertyValue value, String text) {
363: component.setText(getFormatedRGB((Integer) value
364: .getPrimitiveValue()));
365: DialogDisplayer.getDefault().notify(
366: new NotifyDescriptor.Message(ERROR_WARNING + ' '
367: + text)); // NOI18N
368: }
369: }
370: }
|