001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * Copyright (C) 2003 Vivid Solutions
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * For more information, contact:
022: *
023: * Vivid Solutions
024: * Suite #1A
025: * 2328 Government Street
026: * Victoria BC V8T 5G5
027: * Canada
028: *
029: * (250)385-6040
030: * www.vividsolutions.com
031: */
032:
033: package com.vividsolutions.jump.workbench.ui;
034:
035: import java.awt.Color;
036: import java.awt.Dimension;
037: import java.awt.GridBagConstraints;
038: import java.awt.GridBagLayout;
039: import java.awt.Insets;
040: import java.awt.event.ActionEvent;
041: import java.awt.event.ActionListener;
042: import java.util.ArrayList;
043: import java.util.Iterator;
044:
045: import javax.swing.BorderFactory;
046: import javax.swing.JButton;
047: import javax.swing.JColorChooser;
048: import javax.swing.JPanel;
049: import javax.swing.SwingUtilities;
050:
051: import com.vividsolutions.jump.I18N;
052:
053: /**
054: * A custom Colour Scheme Browser - custom choices based on JColorChooser.
055: */
056:
057: public class ColorChooserPanel extends JPanel {
058: GridBagLayout gridBagLayout1 = new GridBagLayout();
059: JButton changeButton = new JButton();
060:
061: //{DEFECT-PREVENTION} In accessors, it's better to use "workbench = newWorkbench"
062: //rather than "this.workbench = workbench", because otherwise if you misspell the
063: //argument, Java won't complain! We should do this everywhere. [Jon Aquino]
064: JPanel outerColorPanel = new JPanel();
065: ColorPanel colorPanel = new ColorPanel();
066: GridBagLayout gridBagLayout2 = new GridBagLayout();
067: private Color color = Color.black;
068: private int alpha;
069: private ArrayList actionListeners = new ArrayList();
070:
071: public ColorChooserPanel() {
072: try {
073: jbInit();
074: colorPanel.setLineColor(null);
075: changeButton.setToolTipText(I18N
076: .get("ui.ColorChooserPanel.browse"));
077: } catch (Exception ex) {
078: ex.printStackTrace();
079: }
080: }
081:
082: void jbInit() throws Exception {
083: this .setLayout(gridBagLayout1);
084: changeButton.setMargin(new Insets(0, 0, 0, 0));
085: changeButton.setText(" ... ");
086: changeButton
087: .addActionListener(new java.awt.event.ActionListener() {
088: public void actionPerformed(ActionEvent e) {
089: changeButton_actionPerformed(e);
090: }
091: });
092: outerColorPanel.setBorder(BorderFactory
093: .createLoweredBevelBorder());
094: outerColorPanel.setPreferredSize(new Dimension(60, 20));
095: outerColorPanel.setBackground(Color.white);
096: outerColorPanel.setLayout(gridBagLayout2);
097: colorPanel.setMargin(0);
098: colorPanel.setPreferredSize(new Dimension(45, 8));
099: this .add(changeButton, new GridBagConstraints(2, 0, 1, 1, 0.0,
100: 0.0, GridBagConstraints.CENTER,
101: GridBagConstraints.NONE, new Insets(0, 2, 0, 0), 0, 0));
102: this .add(outerColorPanel, new GridBagConstraints(1, 0, 1, 1,
103: 0.0, 0.0, GridBagConstraints.CENTER,
104: GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
105: outerColorPanel.add(colorPanel, new GridBagConstraints(0, 0, 1,
106: 1, 0, 0, GridBagConstraints.CENTER,
107: GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
108: }
109:
110: void changeButton_actionPerformed(ActionEvent e) {
111: Color newColor = JColorChooser.showDialog(SwingUtilities
112: .windowForComponent(this ), I18N
113: .get("ui.ColorChooserPanel.choose-color"), color);
114:
115: if (newColor == null) {
116: return;
117: }
118:
119: setColor(newColor);
120: fireActionPerformed();
121: }
122:
123: public void setColor(Color color) {
124: this .color = color;
125: updateColorPanel();
126: }
127:
128: private void updateColorPanel() {
129: colorPanel.setFillColor(GUIUtil.alphaColor(color, alpha));
130: colorPanel.repaint();
131: }
132:
133: public void addActionListener(ActionListener l) {
134: actionListeners.add(l);
135: }
136:
137: public void removeActionListener(ActionListener l) {
138: actionListeners.remove(l);
139: }
140:
141: protected void fireActionPerformed() {
142: for (Iterator i = actionListeners.iterator(); i.hasNext();) {
143: ActionListener l = (ActionListener) i.next();
144: l.actionPerformed(new ActionEvent(this , 0, null));
145: }
146: }
147:
148: public Color getColor() {
149: return color;
150: }
151:
152: //{CLARITY} Might be clearer to say "alpha" rather than "alpha".
153: //This occurs in other places too. [Jon Aquino]
154: public void setAlpha(int alpha) {
155: this .alpha = alpha;
156: updateColorPanel();
157: }
158:
159: public void setEnabled(boolean newEnabled) {
160: changeButton.setEnabled(newEnabled);
161: }
162: }
|