1 /* 2 * Copyright (c) 1996, 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.beans.editors; 27 28 import java.awt.*; 29 import java.beans.*; 30 31 public class ColorEditor extends Panel implements PropertyEditor { 32 private static final long serialVersionUID = 1781257185164716054L; 33 34 public ColorEditor() { 35 setLayout(null); 36 37 ourWidth = hPad; 38 39 // Create a sample color block bordered in black 40 Panel p = new Panel(); 41 p.setLayout(null); 42 p.setBackground(Color.black); 43 sample = new Canvas(); 44 p.add(sample); 45 sample.reshape(2, 2, sampleWidth, sampleHeight); 46 add(p); 47 p.reshape(ourWidth, 2, sampleWidth+4, sampleHeight+4); 48 ourWidth += sampleWidth + 4 + hPad; 49 50 text = new TextField("", 14); 51 add(text); 52 text.reshape(ourWidth,0,100,30); 53 ourWidth += 100 + hPad; 54 55 choser = new Choice(); 56 int active = 0; 57 for (int i = 0; i < colorNames.length; i++) { 58 choser.addItem(colorNames[i]); 59 } 60 add(choser); 61 choser.reshape(ourWidth,0,100,30); 62 ourWidth += 100 + hPad; 63 64 resize(ourWidth,40); 65 } 66 67 public void setValue(Object o) { 68 Color c = (Color)o; 69 changeColor(c); 70 } 71 72 public Dimension preferredSize() { 73 return new Dimension(ourWidth, 40); 74 } 75 76 public boolean keyUp(Event e, int key) { 77 if (e.target == text) { 78 try { 79 setAsText(text.getText()); 80 } catch (IllegalArgumentException ex) { 81 // Quietly ignore. 82 } 83 } 84 return (false); 85 } 86 87 public void setAsText(String s) throws java.lang.IllegalArgumentException { 88 if (s == null) { 89 changeColor(null); 90 return; 91 } 92 int c1 = s.indexOf(','); 93 int c2 = s.indexOf(',', c1+1); 94 if (c1 < 0 || c2 < 0) { 95 // Invalid string. 96 throw new IllegalArgumentException(s); 97 } 98 try { 99 int r = Integer.parseInt(s.substring(0,c1)); 100 int g = Integer.parseInt(s.substring(c1+1, c2)); 101 int b = Integer.parseInt(s.substring(c2+1)); 102 Color c = new Color(r,g,b); 103 changeColor(c); 104 } catch (Exception ex) { 105 throw new IllegalArgumentException(s); 106 } 107 108 } 109 110 public boolean action(Event e, Object arg) { 111 if (e.target == choser) { 112 changeColor(colors[choser.getSelectedIndex()]); 113 } 114 return false; 115 } 116 117 public String getJavaInitializationString() { 118 return (this.color != null) 119 ? "new java.awt.Color(" + this.color.getRGB() + ",true)" 120 : "null"; 121 } 122 123 124 private void changeColor(Color c) { 125 126 if (c == null) { 127 this.color = null; 128 this.text.setText(""); 129 return; 130 } 131 132 color = c; 133 134 text.setText("" + c.getRed() + "," + c.getGreen() + "," + c.getBlue()); 135 136 int active = 0; 137 for (int i = 0; i < colorNames.length; i++) { 138 if (color.equals(colors[i])) { 139 active = i; 140 } 141 } 142 choser.select(active); 143 144 sample.setBackground(color); 145 sample.repaint(); 146 147 support.firePropertyChange("", null, null); 148 } 149 150 public Object getValue() { 151 return color; 152 } 153 154 public boolean isPaintable() { 155 return true; 156 } 157 158 public void paintValue(java.awt.Graphics gfx, java.awt.Rectangle box) { 159 Color oldColor = gfx.getColor(); 160 gfx.setColor(Color.black); 161 gfx.drawRect(box.x, box.y, box.width-3, box.height-3); 162 gfx.setColor(color); 163 gfx.fillRect(box.x+1, box.y+1, box.width-4, box.height-4); 164 gfx.setColor(oldColor); 165 } 166 167 public String getAsText() { 168 return (this.color != null) 169 ? this.color.getRed() + "," + this.color.getGreen() + "," + this.color.getBlue() 170 : null; 171 } 172 173 public String[] getTags() { 174 return null; 175 } 176 177 public java.awt.Component getCustomEditor() { 178 return this; 179 } 180 181 public boolean supportsCustomEditor() { 182 return true; 183 } 184 185 public void addPropertyChangeListener(PropertyChangeListener l) { 186 support.addPropertyChangeListener(l); 187 } 188 189 public void removePropertyChangeListener(PropertyChangeListener l) { 190 support.removePropertyChangeListener(l); 191 } 192 193 194 private String colorNames[] = { " ", "white", "lightGray", "gray", "darkGray", 195 "black", "red", "pink", "orange", 196 "yellow", "green", "magenta", "cyan", 197 "blue"}; 198 private Color colors[] = { null, Color.white, Color.lightGray, Color.gray, Color.darkGray, 199 Color.black, Color.red, Color.pink, Color.orange, 200 Color.yellow, Color.green, Color.magenta, Color.cyan, 201 Color.blue}; 202 203 private Canvas sample; 204 private int sampleHeight = 20; 205 private int sampleWidth = 40; 206 private int hPad = 5; 207 private int ourWidth; 208 209 private Color color; 210 private TextField text; 211 private Choice choser; 212 213 private PropertyChangeSupport support = new PropertyChangeSupport(this); 214 }