KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > rero > dck > items > ColorInput


1 package rero.dck.items;
2
3 import java.awt.*;
4 import java.awt.event.*;
5
6 import javax.swing.*;
7 import javax.swing.event.*;
8
9 import rero.dck.*;
10 import rero.config.*;
11
12 public class ColorInput extends SuperInput implements ActionListener
13 {
14    protected SolidIcon colorIcon;
15    protected JButton button;
16    protected Color initial;
17
18    public ColorInput(String JavaDoc _variable, Color _initial, String JavaDoc text, char mnemonic)
19    {
20       setLayout(new FlowLayout(FlowLayout.CENTER));
21
22       initial = _initial;
23       variable = _variable;
24
25       colorIcon = new SolidIcon(initial, 18, 18);
26       button = new JButton(text, colorIcon);
27       button.setMnemonic(mnemonic);
28
29       button.addActionListener(this);
30       add(button);
31    }
32
33    public void actionPerformed(ActionEvent ev)
34    {
35       Color temp = JColorChooser.showDialog(button, "Select color...", colorIcon.getColor());
36       if (temp != null)
37       {
38          colorIcon.setColor(temp);
39          button.repaint();
40          notifyParent();
41       }
42    }
43
44    public void save()
45    {
46       ClientState.getClientState().setColor(getVariable(), colorIcon.getColor());
47    }
48
49    public void refresh()
50    {
51       colorIcon.setColor(ClientState.getClientState().getColor(getVariable(), initial));
52       button.repaint();
53    }
54
55    public int getEstimatedWidth()
56    {
57       return 0;
58    }
59
60    public void setAlignWidth(int width)
61    {
62    }
63
64
65    public JComponent getComponent()
66    {
67       return this;
68    }
69
70    protected static class SolidIcon implements Icon
71    {
72       private int width, height;
73       private Color color;
74
75       public SolidIcon(Color c, int w, int h)
76       {
77          width = w;
78          height = h;
79          color = c;
80       }
81
82       public Color getColor()
83       {
84          return color;
85       }
86
87       public void setColor(Color c)
88       {
89          color = c;
90       }
91
92       public int getIconWidth() { return width; }
93       public int getIconHeight() { return height; }
94
95       public void paintIcon(Component c, Graphics g, int x, int y)
96       {
97          g.setColor(color);
98          g.fillRect(x, y, width-1, height-1);
99       }
100    }
101 }
102
103
Popular Tags