1 /*
2 * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
6 * conditions are met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistribution in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials
14 * provided with the distribution.
15 *
16 * Neither the name of Sun Microsystems, Inc. or the names of
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * This software is provided "AS IS," without a warranty of any
21 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
22 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
24 * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY
25 * DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT OF OR
26 * RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE OR
27 * ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE
28 * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT,
29 * SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
30 * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF
31 * THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS
32 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
33 *
34 * You acknowledge that this software is not designed, licensed or
35 * intended for use in the design, construction, operation or
36 * maintenance of any nuclear facility.
37 */
38 package javax.swing.beaninfo;
39
40 import org.modama.framework.tools.Resources;
41
42 import java.awt.Color;
43 import java.awt.Component;
44 import java.awt.Dimension;
45 import java.awt.Insets;
46 import java.awt.Graphics;
47 import java.awt.Rectangle;
48
49 import java.awt.event.ActionListener;
50 import java.awt.event.ActionEvent;
51 import java.awt.event.MouseAdapter;
52 import java.awt.event.MouseEvent;
53 import java.awt.event.WindowAdapter;
54 import java.awt.event.WindowEvent;
55
56 import javax.swing;
57
58 import javax.swing.plaf.metal.MetalComboBoxIcon;
59
60 /**
61 * Swing version of a Color property editor.
62 *
63 *
64 * @version 1.3 02/27/02
65 * @author Tom Santos
66 * @author Mark Davidson
67 */
68 public class SwingColorEditor extends SwingEditorSupport {
69 private JTextField rgbValue;
70 private JButton colorChooserButton;
71 private Color color = Color.black;
72
73 private ChooserComboButton colorChooserCombo;
74
75 public SwingColorEditor() {
76 createComponents();
77 addComponentListeners();
78 }
79
80 private void addComponentListeners(){
81 rgbValue.addActionListener(new ActionListener(){
82 public void actionPerformed(ActionEvent e){
83 try {
84 setAsText(getAsText());
85 }
86 catch (IllegalArgumentException e2){
87 JOptionPane.showMessageDialog(panel.getParent(),
88 e2.toString());
89 }
90 }
91 });
92
93 colorChooserButton.addActionListener(new ActionListener(){
94 public void actionPerformed(ActionEvent e){
95 color = JColorChooser.showDialog(panel.getParent(), "Color Chooser", color);
96 setValue(color);
97 }
98 });
99 }
100
101 public boolean isPaintable() {
102 return true;
103 }
104
105 /**
106 * Paints a representation of the value into a given area of screen.
107 */
108 public void paintValue(Graphics g, Rectangle rect) {
109 Color oldColor = g.getColor();
110 g.setColor(Color.black);
111 g.drawRect(rect.x, rect.y, rect.width-3, rect.height-3);
112 g.setColor(color);
113 g.fillRect(rect.x+1, rect.y+1, rect.width-4, rect.height-4);
114 g.setColor(oldColor);
115 }
116
117 private void createComponents(){
118 UIDefaults table = UIManager.getDefaults();
119 /*
120 table.put("beaninfo.ColorIcon", LookAndFeel.makeIcon(getClass(), "icons/ColorIcon.gif"));
121 table.put("beaninfo.ColorPressedIcon", LookAndFeel.makeIcon(getClass(), "icons/ColorPressedIcon.gif"));
122 Icon colorIcon = UIManager.getIcon("beaninfo.ColorIcon");
123 Icon colorPressedIcon = UIManager.getIcon("beaninfo.ColorPressedIcon");
124 */
125 Icon colorIcon = new ImageIcon(Resources.getResource("icons/ColorIcon.gif"));
126 Icon colorPressedIcon = new ImageIcon(Resources.getResource("icons/ColorPressedIcon.gif"));
127
128 rgbValue = new JTextField();
129 colorChooserCombo = new ChooserComboButton();
130 colorChooserButton = new JButton(colorIcon);
131
132 Dimension d = new Dimension(colorIcon.getIconWidth(), colorIcon.getIconHeight());
133 rgbValue.setPreferredSize(SwingEditorSupport.MEDIUM_DIMENSION);
134 rgbValue.setMaximumSize(SwingEditorSupport.MEDIUM_DIMENSION);
135 rgbValue.setMinimumSize(SwingEditorSupport.MEDIUM_DIMENSION);
136 colorChooserCombo.setPreferredSize(SwingEditorSupport.SMALL_DIMENSION);
137 colorChooserCombo.setMaximumSize(SwingEditorSupport.SMALL_DIMENSION);
138 colorChooserCombo.setMinimumSize(SwingEditorSupport.SMALL_DIMENSION);
139
140 colorChooserButton.setPressedIcon(colorPressedIcon);
141 colorChooserButton.setToolTipText("press to bring up color chooser");
142 colorChooserButton.setMargin(SwingEditorSupport.BUTTON_MARGIN);
143 colorChooserButton.setBorderPainted(false);
144 colorChooserButton.setContentAreaFilled(false);
145
146 setAlignment(rgbValue);
147 setAlignment(colorChooserCombo);
148 setAlignment(colorChooserButton);
149 panel = new JPanel();
150 panel.setLayout(new BoxLayout(panel,BoxLayout.X_AXIS));
151 panel.add(rgbValue);
152 panel.add(Box.createRigidArea(new Dimension(5,0)));
153 panel.add(colorChooserCombo);
154 panel.add(Box.createRigidArea(new Dimension(5,0)));
155 panel.add(colorChooserButton);
156 panel.setAlignmentX(Component.LEFT_ALIGNMENT);
157 }
158
159 public String getJavaInitializationString(){
160 return "new java.awt.Color(" + getAsText() + ")";
161 }
162
163 public String getAsText(){
164 return rgbValue.getText();
165 }
166
167 public void setAsText(String s) throws java.lang.IllegalArgumentException {
168 int c1 = s.indexOf(',');
169 int c2 = s.indexOf(',', c1+1);
170 if (c1 < 0 || c2 < 0) {
171 // Invalid string.
172 throw new IllegalArgumentException(s);
173 }
174 try {
175 int r = Integer.parseInt(s.substring(0,c1));
176 int g = Integer.parseInt(s.substring(c1+1, c2));
177 int b = Integer.parseInt(s.substring(c2+1));
178 setValue(new Color(r,g,b));
179 } catch (Exception ex) {
180 throw new IllegalArgumentException(s);
181 }
182 }
183
184 public void setValue(Object value){
185 super.setValue(value);
186 editorChangeValue(value);
187 }
188
189 public void editorChangeValue(Object value){
190 Color c = (Color)value;
191 if (c == null) {
192 rgbValue.setText(" ");
193 colorChooserCombo.setBackground(panel.getBackground());
194 return;
195 }
196 color = c;
197 // set the combo rect foreground color
198 // and the textfield to the rgb value
199 rgbValue.setText("" + c.getRed() + "," + c.getGreen() + "," + c.getBlue());
200 colorChooserCombo.setBackground(c);
201 }
202
203 public SwingColorEditor getSwingColorEditor(){
204 return this;
205 }
206
207
208 // for testing
209
210 // custom combolike rect button
211 class ChooserComboButton extends JButton {
212 ChooserComboPopup popup;
213 Icon comboIcon = new MetalComboBoxIcon();
214
215 public ChooserComboButton(){
216 super ("");
217 popup = new ChooserComboPopup(getSwingColorEditor());
218 addMouseListener(new PopupListener());
219 }
220
221
222 public void paintComponent(Graphics g){
223 super.paintComponent( g );
224 Insets insets = getInsets();
225
226 int width = getWidth() - (insets.left + insets.right);
227 int height = getHeight() - (insets.top + insets.bottom);
228
229 if ( height <= 0 || width <= 0 ) {
230 return;
231 }
232
233 int left = insets.left;
234 int top = insets.top;
235 int right = left + (width - 1);
236 int bottom = top + (height - 1);
237
238 int iconWidth = 0;
239 int iconLeft = right;
240
241 // Paint the icon
242 if ( comboIcon != null ) {
243 iconWidth = comboIcon.getIconWidth();
244 int iconHeight = comboIcon.getIconHeight();
245 int iconTop = 0;
246 iconTop = (top + ((bottom - top) / 2)) - (iconHeight / 2);
247 comboIcon.paintIcon( this, g, iconLeft, iconTop );
248 }
249
250 }
251
252 class PopupListener extends MouseAdapter {
253 public void mouseReleased(MouseEvent e){
254 // bring up ChooserComboPopup
255 // bring it up at the comopnent height location!
256 JComponent c = (JComponent)e.getComponent();
257 popup.show(c,0,0);
258 }
259 }
260 }
261
262 public static void main(String argv[]){
263 JFrame f = new JFrame();
264 f.addWindowListener(new WindowAdapter(){
265 public void windowClosing(WindowEvent e){
266 JFrame frame = (JFrame)e.getSource();
267 frame.dispose();
268 System.exit(0);
269 }
270 });
271
272 SwingColorEditor editor = new SwingColorEditor();
273 f.getContentPane().add(editor.getCustomEditor());
274 f.pack();
275 f.show();
276 }
277 }