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