Handle hostnames with upper-case letters
[webmin.git] / file / CbColorWindow.java
1 import java.awt.*;
2 import java.util.*;
3
4 /**A window for choosing a colour, either from a pre-set palette
5  * or from a color cube
6  */
7 class CbColorWindow extends FixedFrame implements CbButtonCallback
8 {
9         CbColorWindowCallback callback;
10         Color col;
11         Vector pal;
12         static Vector defpal = new Vector();
13         Image palimg[] = new Image[12];
14         CbButton palbut[] = new CbButton[12];
15         int curpal = -1;
16         CbButton ok, cancel;
17         CbColorWindowCube ccube;
18
19         static
20         {
21         defpal.addElement(Color.black);
22         defpal.addElement(Color.blue);
23         defpal.addElement(Color.cyan);
24         defpal.addElement(Color.gray);
25         defpal.addElement(Color.green);
26         defpal.addElement(Color.darkGray);
27         defpal.addElement(Color.magenta);
28         defpal.addElement(Color.orange);
29         defpal.addElement(Color.pink);
30         defpal.addElement(Color.red);
31         defpal.addElement(Color.white);
32         defpal.addElement(Color.yellow);
33         }
34
35         CbColorWindow(Color c, CbColorWindowCallback cb)
36         {
37         col = c;
38         callback = cb;
39
40         // Setup color vector
41         pal = callback.palette(this);
42         if (pal == null)
43                 pal = defpal;
44         else if (pal.size() == 0)
45                 for(int i=0; i<12; i++)
46                         pal.addElement(defpal.elementAt(i));
47
48         // Create palette images
49         for(int i=0; i<12; i++) {
50                 palimg[i] = Util.createImage(16, 16);
51                 updatePal(i);
52                 }
53
54         // create UI
55         setLayout(new BorderLayout());
56         Panel bot = new GrayPanel();
57         bot.setLayout(new FlowLayout(FlowLayout.RIGHT));
58         bot.add(ok = new CbButton("Ok", this));
59         bot.add(cancel = new CbButton("Cancel", this));
60         add("South", bot);
61         Panel mid = new BorderPanel(1);
62         mid.setLayout(new BorderLayout());
63         Panel midbot = new GrayPanel();
64         midbot.setLayout(new GridLayout(2, 6, 4, 4));
65         CbButtonGroup g = new CbButtonGroup();
66         for(int i=0; i<12; i++) {
67                 midbot.add(palbut[i] = new CbButton(palimg[i], this));
68                 palbut[i].setGroup(g);
69                 }
70         for(int i=0; i<12; i++)
71                 if (c.equals(pal.elementAt(i))) {
72                         curpal = i;
73                         palbut[i].select();
74                         break;
75                         }
76         mid.add("South", midbot);
77         mid.add("North", ccube = new CbColorWindowCube(this));
78         add("Center", mid);
79
80         pack();
81         show();
82         setTitle("Choose Color...");
83         }
84
85         void updatePal(int i)
86         {
87         Graphics g = palimg[i].getGraphics();
88         g.setColor((Color)pal.elementAt(i));
89         g.fillRect(0, 0, 16, 16);
90         if (palbut[i] != null) palbut[i].repaint();
91         }
92
93         public void click(CbButton b)
94         {
95         if (b == ok) {
96                 callback.chosen(this, col);
97                 super.dispose();
98                 }
99         else if (b == cancel)
100                 dispose();
101         else {
102                 for(int i=0; i<12; i++)
103                         if (b == palbut[i]) {
104                                 curpal = i;
105                                 col = (Color)pal.elementAt(i);
106                                 ccube.red.setPosition(col.getRed());
107                                 ccube.blue.setPosition(col.getBlue());
108                                 ccube.green.setPosition(col.getGreen());
109                                 ccube.swatch.setColor(col);
110                                 }
111                 }
112         }
113
114         public void dispose()
115         {
116         super.dispose();
117         callback.chosen(this, null);
118         }
119
120         public boolean isResizable() { return false; }
121 }
122
123 /**Displays 3 sliders, for red green and blue plus a block to show the
124  * current color
125  */
126 class CbColorWindowCube extends BorderPanel implements CbSliderCallback
127 {
128         CbColorWindow parent;
129         CbSlider red, green, blue;
130         CbColorWindowSwatch swatch;
131
132         CbColorWindowCube(CbColorWindow p)
133         {
134         super(1, Util.body, Util.body);
135         parent = p;
136         setLayout(new BorderLayout());
137         Panel sl = new GrayPanel();
138         sl.setLayout(new GridLayout(3, 1));
139         sl.add(red = new CbSlider(0, 0, 255, p.col.getRed(), this));
140         sl.add(green = new CbSlider(0, 0, 255, p.col.getBlue(), this));
141         sl.add(blue = new CbSlider(0, 0, 255, p.col.getGreen(), this));
142         add("Center", sl);
143         add("East", swatch = new CbColorWindowSwatch(p.col));
144         }
145
146         public void moved(CbSlider s, int p)
147         {
148         moving(s, p);
149         }
150
151         public void moving(CbSlider s, int p)
152         {
153         parent.col = new Color(red.getPosition(), green.getPosition(),
154                                blue.getPosition());
155         swatch.setColor(parent.col);
156         if (parent.curpal != -1) {
157                 parent.pal.setElementAt(parent.col, parent.curpal);
158                 parent.updatePal(parent.curpal);
159                 }
160         }
161 }
162
163
164 interface CbColorWindowCallback
165 {
166         /**This method will be called when the user chooses a colour. If
167          * the user cancels the dialog, then this method will also be chosen
168          * but with null for the color.
169          */
170         public void chosen(CbColorWindow w, Color c);
171
172         /**The chooser keeps a palette of colors that the user can modify,
173          * stored in a vector. The callback class should provide this vector
174          * so as to maintain the palette between color window calls.
175          * If an empty vector is returned, it will be filled with the default
176          * color table (which can be then modified).
177          * If null is returned, the chooser will use it's own internal
178          * vector.
179          */
180         public Vector palette(CbColorWindow w);
181 }
182
183
184 class CbColorWindowSwatch extends BorderPanel
185 {
186         Color col = Color.black;
187         String txt;
188
189         CbColorWindowSwatch(Color c)
190         {
191         super(1);
192         setColor(c);
193         }
194
195         void setColor(Color c)
196         {
197         col = c;
198         txt = col.getRed()+","+col.getGreen()+","+col.getBlue();
199         repaint();
200         }
201
202         public void paint(Graphics g)
203         {
204         super.paint(g);
205         g.setColor(col);
206         g.fillRect(1, 1, size().width-2, size().height-2);
207         g.setColor(Color.white);
208         g.setXORMode(Color.black);
209         g.setFont(Util.f);
210         g.drawString(txt, 3, Util.fnm.getHeight()+1);
211         g.setPaintMode();
212         }
213
214         public void upate(Graphics g) { paint(g); }
215
216         public Dimension preferredSize()
217         {
218         return new Dimension(60, 60);
219         }
220
221         public Dimension minimumSize()
222         {
223         return preferredSize();
224         }
225 }
226