root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / panels / ColorChooserPanel.java @ 6117
History | View | Annotate | Download (6.43 KB)
1 | 6117 | jaume | |
---|---|---|---|
2 | /*
|
||
3 | * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
|
||
4 | * for visualizing and manipulating spatial features with geometry and attributes.
|
||
5 | *
|
||
6 | * Copyright (C) 2003 Vivid Solutions
|
||
7 | *
|
||
8 | * This program is free software; you can redistribute it and/or
|
||
9 | * modify it under the terms of the GNU General Public License
|
||
10 | * as published by the Free Software Foundation; either version 2
|
||
11 | * of the License, or (at your option) any later version.
|
||
12 | *
|
||
13 | * This program is distributed in the hope that it will be useful,
|
||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
16 | * GNU General Public License for more details.
|
||
17 | *
|
||
18 | * You should have received a copy of the GNU General Public License
|
||
19 | * along with this program; if not, write to the Free Software
|
||
20 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||
21 | *
|
||
22 | * For more information, contact:
|
||
23 | *
|
||
24 | * Vivid Solutions
|
||
25 | * Suite #1A
|
||
26 | * 2328 Government Street
|
||
27 | * Victoria BC V8T 5G5
|
||
28 | * Canada
|
||
29 | *
|
||
30 | * (250)385-6040
|
||
31 | * www.vividsolutions.com
|
||
32 | */
|
||
33 | |||
34 | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
||
35 | *
|
||
36 | * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
||
37 | *
|
||
38 | * This program is free software; you can redistribute it and/or
|
||
39 | * modify it under the terms of the GNU General Public License
|
||
40 | * as published by the Free Software Foundation; either version 2
|
||
41 | * of the License, or (at your option) any later version.
|
||
42 | *
|
||
43 | * This program is distributed in the hope that it will be useful,
|
||
44 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
45 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
46 | * GNU General Public License for more details.
|
||
47 | *
|
||
48 | * You should have received a copy of the GNU General Public License
|
||
49 | * along with this program; if not, write to the Free Software
|
||
50 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
51 | *
|
||
52 | * For more information, contact:
|
||
53 | *
|
||
54 | * Generalitat Valenciana
|
||
55 | * Conselleria d'Infraestructures i Transport
|
||
56 | * Av. Blasco Ib??ez, 50
|
||
57 | * 46010 VALENCIA
|
||
58 | * SPAIN
|
||
59 | *
|
||
60 | * +34 963862235
|
||
61 | * gvsig@gva.es
|
||
62 | * www.gvsig.gva.es
|
||
63 | *
|
||
64 | * or
|
||
65 | *
|
||
66 | * IVER T.I. S.A
|
||
67 | * Salamanca 50
|
||
68 | * 46005 Valencia
|
||
69 | * Spain
|
||
70 | *
|
||
71 | * +34 963163400
|
||
72 | * dac@iver.es
|
||
73 | */
|
||
74 | package com.iver.cit.gvsig.gui.panels; |
||
75 | |||
76 | import java.awt.Color; |
||
77 | import java.awt.Dimension; |
||
78 | import java.awt.GridBagConstraints; |
||
79 | import java.awt.GridBagLayout; |
||
80 | import java.awt.Insets; |
||
81 | import java.awt.event.ActionEvent; |
||
82 | import java.awt.event.ActionListener; |
||
83 | import java.util.ArrayList; |
||
84 | import java.util.Iterator; |
||
85 | |||
86 | import javax.swing.BorderFactory; |
||
87 | import javax.swing.JButton; |
||
88 | import javax.swing.JColorChooser; |
||
89 | import javax.swing.JPanel; |
||
90 | import javax.swing.SwingUtilities; |
||
91 | |||
92 | |||
93 | /**
|
||
94 | * A custom Colour Scheme Browser - custom choices based on JColorChooser.
|
||
95 | */
|
||
96 | |||
97 | public class ColorChooserPanel extends JPanel { |
||
98 | GridBagLayout gridBagLayout1 = new GridBagLayout(); |
||
99 | JButton changeButton = new JButton(); |
||
100 | |||
101 | |||
102 | //{DEFECT-PREVENTION} In accessors, it's better to use "workbench = newWorkbench"
|
||
103 | //rather than "this.workbench = workbench", because otherwise if you misspell the
|
||
104 | //argument, Java won't complain! We should do this everywhere. [Jon Aquino]
|
||
105 | JPanel outerColorPanel = new JPanel(); |
||
106 | ColorPanel colorPanel = new ColorPanel();
|
||
107 | GridBagLayout gridBagLayout2 = new GridBagLayout(); |
||
108 | private Color color = Color.black; |
||
109 | private int alpha; |
||
110 | private ArrayList actionListeners = new ArrayList(); |
||
111 | |||
112 | public ColorChooserPanel() {
|
||
113 | try {
|
||
114 | jbInit(); |
||
115 | colorPanel.setLineColor(null);
|
||
116 | changeButton.setToolTipText("Browse");
|
||
117 | } catch (Exception ex) { |
||
118 | ex.printStackTrace(); |
||
119 | } |
||
120 | } |
||
121 | |||
122 | void jbInit() throws Exception { |
||
123 | this.setLayout(gridBagLayout1);
|
||
124 | changeButton.setMargin(new Insets(0, 0, 0, 0)); |
||
125 | changeButton.setText(" ... ");
|
||
126 | changeButton.addActionListener(new java.awt.event.ActionListener() {
|
||
127 | public void actionPerformed(ActionEvent e) { |
||
128 | changeButton_actionPerformed(e); |
||
129 | } |
||
130 | }); |
||
131 | outerColorPanel.setBorder(BorderFactory.createLoweredBevelBorder());
|
||
132 | outerColorPanel.setPreferredSize(new Dimension(60, 20)); |
||
133 | outerColorPanel.setBackground(Color.white);
|
||
134 | outerColorPanel.setLayout(gridBagLayout2); |
||
135 | colorPanel.setMargin(0);
|
||
136 | colorPanel.setPreferredSize(new Dimension(45, 8)); |
||
137 | this.add(changeButton,
|
||
138 | new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, |
||
139 | GridBagConstraints.CENTER, GridBagConstraints.NONE, |
||
140 | new Insets(0, 2, 0, 0), 0, 0)); |
||
141 | this.add(outerColorPanel,
|
||
142 | new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, |
||
143 | GridBagConstraints.CENTER, GridBagConstraints.NONE, |
||
144 | new Insets(0, 0, 0, 0), 0, 0)); |
||
145 | outerColorPanel.add(colorPanel, |
||
146 | new GridBagConstraints(0, 0, 1, 1, 0, 0, |
||
147 | GridBagConstraints.CENTER, GridBagConstraints.NONE, |
||
148 | new Insets(0, 0, 0, 0), 0, 0)); |
||
149 | } |
||
150 | |||
151 | void changeButton_actionPerformed(ActionEvent e) { |
||
152 | Color newColor = JColorChooser.showDialog(SwingUtilities.windowForComponent(this), "Choose Colour", color); |
||
153 | |||
154 | if (newColor == null) { |
||
155 | return;
|
||
156 | } |
||
157 | |||
158 | setColor(newColor); |
||
159 | fireActionPerformed(); |
||
160 | } |
||
161 | |||
162 | public void setColor(Color color) { |
||
163 | this.color = color;
|
||
164 | updateColorPanel(); |
||
165 | } |
||
166 | |||
167 | private void updateColorPanel() { |
||
168 | |||
169 | colorPanel.setFillColor(new Color(color.getRed(), color.getGreen(), color.getBlue(),alpha)); |
||
170 | |||
171 | |||
172 | colorPanel.repaint(); |
||
173 | } |
||
174 | |||
175 | public void addActionListener(ActionListener l) { |
||
176 | actionListeners.add(l); |
||
177 | } |
||
178 | |||
179 | public void removeActionListener(ActionListener l) { |
||
180 | actionListeners.remove(l); |
||
181 | } |
||
182 | |||
183 | protected void fireActionPerformed() { |
||
184 | for (Iterator i = actionListeners.iterator(); i.hasNext();) { |
||
185 | ActionListener l = (ActionListener) i.next(); |
||
186 | l.actionPerformed(new ActionEvent(this, 0, null)); |
||
187 | } |
||
188 | } |
||
189 | |||
190 | public Color getColor() { |
||
191 | return color;
|
||
192 | } |
||
193 | |||
194 | //{CLARITY} Might be clearer to say "alpha" rather than "alpha".
|
||
195 | //This occurs in other places too. [Jon Aquino]
|
||
196 | public void setAlpha(int alpha) { |
||
197 | this.alpha = alpha;
|
||
198 | updateColorPanel(); |
||
199 | } |
||
200 | |||
201 | public void setEnabled(boolean newEnabled) { |
||
202 | changeButton.setEnabled(newEnabled); |
||
203 | } |
||
204 | } |