KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > xjlib > appkit > swing > XJColorChooser


1 package org.antlr.xjlib.appkit.swing;
2
3 import org.antlr.xjlib.appkit.frame.XJDialog;
4
5 import javax.swing.*;
6 import javax.swing.event.ChangeEvent JavaDoc;
7 import javax.swing.event.ChangeListener JavaDoc;
8 import java.awt.*;
9
10 /*
11
12 [The "BSD licence"]
13 Copyright (c) 2005-2006 Jean Bovet
14 All rights reserved.
15
16 Redistribution and use in source and binary forms, with or without
17 modification, are permitted provided that the following conditions
18 are met:
19
20 1. Redistributions of source code must retain the above copyright
21 notice, this list of conditions and the following disclaimer.
22 2. Redistributions in binary form must reproduce the above copyright
23 notice, this list of conditions and the following disclaimer in the
24 documentation and/or other materials provided with the distribution.
25 3. The name of the author may not be used to endorse or promote products
26 derived from this software without specific prior written permission.
27
28 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
29 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
32 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
33 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
37 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38
39 */

40
41 public class XJColorChooser extends XJDialog {
42
43     JColorChooser cc;
44
45     JPanel targetPanel;
46     Color oldTargetColor = Color.black;
47
48     public XJColorChooser(Container owner, boolean modal) {
49         this(owner, modal, null);
50     }
51
52     public XJColorChooser(Container owner, boolean modal, JPanel targetPanel) {
53         super(owner, modal);
54
55         setTitle("Choose a color");
56         setSize(500, 400);
57
58         if(targetPanel != null) {
59             this.targetPanel = targetPanel;
60             this.oldTargetColor = targetPanel.getBackground();
61         }
62
63         cc = new JColorChooser(oldTargetColor);
64         cc.getSelectionModel().addChangeListener(new ChangeListener JavaDoc() {
65             public void stateChanged(ChangeEvent JavaDoc e) {
66                 updateTargetColor();
67             }
68         });
69         getContentPane().add(cc, BorderLayout.CENTER);
70
71         JButton cancel = new JButton("Cancel");
72         setCancelButton(cancel);
73
74         JButton ok = new JButton("OK");
75         setOKButton(ok);
76         setDefaultButton(ok);
77
78         Box box = Box.createHorizontalBox();
79         box.add(Box.createHorizontalGlue());
80         box.add(cancel);
81         box.add(ok);
82         box.add(Box.createHorizontalStrut(15));
83         getContentPane().add(box, BorderLayout.SOUTH);
84     }
85
86     protected void updateTargetColor() {
87         updateTargetColor(cc.getColor());
88     }
89
90     protected void updateTargetColor(Color c) {
91         if(targetPanel != null)
92             targetPanel.setBackground(c);
93     }
94
95     public void dialogWillCloseCancel() {
96         updateTargetColor(oldTargetColor);
97     }
98
99     public void dialogWillCloseOK() {
100         updateTargetColor();
101     }
102
103     public Color getColor() {
104         return cc.getColor();
105     }
106 }
107
Popular Tags