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 /* josh: this should be replaced with matte painters
053 DEFAULT_PAINTS.put(
054 BasicGradientPainter.AERITH, "Aerith");
055 DEFAULT_PAINTS.put(
056 BasicGradientPainter.BLUE_EXPERIENCE, "Blue Experience");
057 DEFAULT_PAINTS.put(
058 BasicGradientPainter.GRAY, "Gray Gradient");
059 DEFAULT_PAINTS.put(
060 BasicGradientPainter.MAC_OSX, "Mac OSX");
061 DEFAULT_PAINTS.put(
062 BasicGradientPainter.MAC_OSX_SELECTED, "Max OSX Selected");
063 DEFAULT_PAINTS.put(
064 BasicGradientPainter.NIGHT_GRAY, "Night Gray");
065 DEFAULT_PAINTS.put(
066 BasicGradientPainter.NIGHT_GRAY_LIGHT, "Night Gray Light");
067 DEFAULT_PAINTS.put(
068 BasicGradientPainter.RED_XP, "Red XP");
069 DEFAULT_PAINTS.put(
070 LinearGradientPainter.BLACK_STAR, "Black Star");
071 DEFAULT_PAINTS.put(
072 LinearGradientPainter.ORANGE_DELIGHT, "Orange Delight");*/
073 }
074
075 /** Creates a new instance of PainterPropertyEditor */
076 public PaintPropertyEditor() {
077 }
078
079 public String[] getTags() {
080 String[] names = DEFAULT_PAINTS.values().toArray(new String[0]);
081 String[] results = new String[names.length+1];
082 results[0] = "<none>";
083 System.arraycopy(names, 0, results, 1, names.length);
084 return results;
085 }
086
087 public Paint getValue() {
088 return (Paint)super.getValue();
089 }
090
091 public String getJavaInitializationString() {
092 Paint paint = getValue();
093 //TODO!!!
094 return paint == null ? "null" :
095 "org.jdesktop.swingx.painter.gradient.LinearGradientPainter.BLACK_STAR";
096 }
097
098 public void setAsText(String text) throws IllegalArgumentException {
099 if (text == null || text.trim().equals("") || text.trim().equalsIgnoreCase("none")
100 || text.trim().equalsIgnoreCase("<none>")
101 || text.trim().equalsIgnoreCase("[none]")) {
102 setValue(null);
103 return;
104 }
105
106 if (text.trim().equalsIgnoreCase("<custom>")) {
107 //do nothing
108 }
109
110 for (Map.Entry<Paint, String> entry : DEFAULT_PAINTS.entrySet()) {
111 if (entry.getValue().equalsIgnoreCase(text)) {
112 setValue(entry.getKey());
113 return;
114 }
115 }
116
117 throw new IllegalArgumentException("The input value " + text + " does" +
118 " not match one of the names of the standard paints");
119 }
120
121 public String getAsText() {
122 Paint p = getValue();
123 if (p == null) {
124 return null;
125 } else if (DEFAULT_PAINTS.containsKey(p)) {
126 return DEFAULT_PAINTS.get(p);
127 } else {
128 return "<custom>";
129 }
130 }
131
132 public void paintValue(Graphics gfx, Rectangle box) {
133 Paint p = getValue();
134 if (p == null) {
135 //do nothing -- in the future draw the checkerboard or something
136 } else {
137 ((Graphics2D)gfx).setPaint(p);
138 gfx.fillRect(box.x, box.y, box.width, box.height);
139 }
140 }
141
142 public boolean isPaintable() {
143 return true;
144 }
145 }