001: /*
002: * PainterPropertyEditor.java
003: *
004: * Created on March 21, 2006, 11:26 AM
005: *
006: * To change this template, choose Tools | Template Manager
007: * and open the template in the editor.
008: */
009:
010: package org.jdesktop.swingx.editors;
011:
012: import java.awt.Color;
013: import java.awt.Graphics;
014: import java.awt.Graphics2D;
015: import java.awt.Paint;
016: import java.awt.Rectangle;
017: import java.beans.PropertyEditorSupport;
018: import java.util.HashMap;
019: import java.util.Map;
020: import org.jdesktop.swingx.painter.gradient.BasicGradientPainter;
021: import org.jdesktop.swingx.painter.gradient.LinearGradientPainter;
022:
023: /**
024: * Two parts to this property editor. The first part is a simple dropdown.
025: * The second part is a complicated UI for constructing multiple "layers" of
026: * various different Painters, including gradient paints.
027: *
028: * @author Richard
029: */
030: public class PaintPropertyEditor extends PropertyEditorSupport {
031: private static Map<Paint, String> DEFAULT_PAINTS = new HashMap<Paint, String>();
032: static {
033: //add the default paints
034: DEFAULT_PAINTS.put(Color.BLACK, "Black");
035: DEFAULT_PAINTS.put(Color.BLUE, "Blue");
036: DEFAULT_PAINTS.put(Color.CYAN, "Cyan");
037: DEFAULT_PAINTS.put(Color.DARK_GRAY, "Dark Gray");
038: DEFAULT_PAINTS.put(Color.GRAY, "Gray");
039: DEFAULT_PAINTS.put(Color.GREEN, "Green");
040: DEFAULT_PAINTS.put(Color.LIGHT_GRAY, "Light Gray");
041: DEFAULT_PAINTS.put(Color.MAGENTA, "Magenta");
042: DEFAULT_PAINTS.put(Color.ORANGE, "Orange");
043: DEFAULT_PAINTS.put(Color.PINK, "Pink");
044: DEFAULT_PAINTS.put(Color.RED, "Red");
045: DEFAULT_PAINTS.put(Color.WHITE, "White");
046: DEFAULT_PAINTS.put(Color.YELLOW, "Yellow");
047: DEFAULT_PAINTS.put(new Color(1f, 1f, 1f, 0f), "Transparent");
048: // DEFAULT_PAINTS.put(
049: // BasicGradientPainter.WHITE_TO_CONTROL_HORZONTAL, "White->Control (horizontal)");
050: // DEFAULT_PAINTS.put(
051: // BasicGradientPainter.WHITE_TO_CONTROL_VERTICAL, "White->Control (vertical)");
052: DEFAULT_PAINTS.put(BasicGradientPainter.AERITH, "Aerith");
053: DEFAULT_PAINTS.put(BasicGradientPainter.BLUE_EXPERIENCE,
054: "Blue Experience");
055: DEFAULT_PAINTS.put(BasicGradientPainter.GRAY, "Gray Gradient");
056: DEFAULT_PAINTS.put(BasicGradientPainter.MAC_OSX, "Mac OSX");
057: DEFAULT_PAINTS.put(BasicGradientPainter.MAC_OSX_SELECTED,
058: "Max OSX Selected");
059: DEFAULT_PAINTS.put(BasicGradientPainter.NIGHT_GRAY,
060: "Night Gray");
061: DEFAULT_PAINTS.put(BasicGradientPainter.NIGHT_GRAY_LIGHT,
062: "Night Gray Light");
063: DEFAULT_PAINTS.put(BasicGradientPainter.RED_XP, "Red XP");
064: DEFAULT_PAINTS.put(LinearGradientPainter.BLACK_STAR,
065: "Black Star");
066: DEFAULT_PAINTS.put(LinearGradientPainter.ORANGE_DELIGHT,
067: "Orange Delight");
068: }
069:
070: /** Creates a new instance of PainterPropertyEditor */
071: public PaintPropertyEditor() {
072: }
073:
074: public String[] getTags() {
075: String[] names = DEFAULT_PAINTS.values().toArray(new String[0]);
076: String[] results = new String[names.length + 1];
077: results[0] = "<none>";
078: System.arraycopy(names, 0, results, 1, names.length);
079: return results;
080: }
081:
082: public Paint getValue() {
083: return (Paint) super .getValue();
084: }
085:
086: public String getJavaInitializationString() {
087: Paint paint = getValue();
088: //TODO!!!
089: return paint == null ? "null"
090: : "org.jdesktop.swingx.painter.gradient.LinearGradientPainter.BLACK_STAR";
091: }
092:
093: public void setAsText(String text) throws IllegalArgumentException {
094: if (text == null || text.trim().equals("")
095: || text.trim().equalsIgnoreCase("none")
096: || text.trim().equalsIgnoreCase("<none>")
097: || text.trim().equalsIgnoreCase("[none]")) {
098: setValue(null);
099: return;
100: }
101:
102: if (text.trim().equalsIgnoreCase("<custom>")) {
103: //do nothing
104: }
105:
106: for (Map.Entry<Paint, String> entry : DEFAULT_PAINTS.entrySet()) {
107: if (entry.getValue().equalsIgnoreCase(text)) {
108: setValue(entry.getKey());
109: return;
110: }
111: }
112:
113: throw new IllegalArgumentException("The input value " + text
114: + " does"
115: + " not match one of the names of the standard paints");
116: }
117:
118: public String getAsText() {
119: Paint p = getValue();
120: if (p == null) {
121: return null;
122: } else if (DEFAULT_PAINTS.containsKey(p)) {
123: return DEFAULT_PAINTS.get(p);
124: } else {
125: return "<custom>";
126: }
127: }
128:
129: public void paintValue(Graphics gfx, Rectangle box) {
130: Paint p = getValue();
131: if (p == null) {
132: //do nothing -- in the future draw the checkerboard or something
133: } else {
134: ((Graphics2D) gfx).setPaint(p);
135: gfx.fillRect(box.x, box.y, box.width, box.height);
136: }
137: }
138:
139: public boolean isPaintable() {
140: return true;
141: }
142: }
|