Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / Panels / ColorChooserPanel.java @ 312

History | View | Annotate | Download (5.24 KB)

1

    
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
package com.iver.cit.gvsig.gui.Panels;
35

    
36
import java.awt.Color;
37
import java.awt.Dimension;
38
import java.awt.GridBagConstraints;
39
import java.awt.GridBagLayout;
40
import java.awt.Insets;
41
import java.awt.event.ActionEvent;
42
import java.awt.event.ActionListener;
43
import java.util.ArrayList;
44
import java.util.Iterator;
45

    
46
import javax.swing.BorderFactory;
47
import javax.swing.JButton;
48
import javax.swing.JColorChooser;
49
import javax.swing.JPanel;
50
import javax.swing.SwingUtilities;
51

    
52

    
53
/**
54
 * A custom Colour Scheme Browser - custom choices based on JColorChooser.
55
 */
56

    
57
public class ColorChooserPanel extends JPanel {
58
    GridBagLayout gridBagLayout1 = new GridBagLayout();
59
    JButton changeButton = new JButton();
60

    
61

    
62
    //{DEFECT-PREVENTION} In accessors, it's better to use "workbench = newWorkbench"
63
    //rather than "this.workbench = workbench", because otherwise if you misspell the
64
    //argument, Java won't complain! We should do this everywhere. [Jon Aquino]
65
    JPanel outerColorPanel = new JPanel();
66
    ColorPanel colorPanel = new ColorPanel();
67
    GridBagLayout gridBagLayout2 = new GridBagLayout();
68
    private Color color = Color.black;
69
    private int alpha;
70
    private ArrayList actionListeners = new ArrayList();
71

    
72
    public ColorChooserPanel() {
73
        try {
74
            jbInit();
75
            colorPanel.setLineColor(null);
76
            changeButton.setToolTipText("Browse");
77
        } catch (Exception ex) {
78
            ex.printStackTrace();
79
        }
80
    }
81

    
82
    void jbInit() throws Exception {
83
        this.setLayout(gridBagLayout1);
84
        changeButton.setMargin(new Insets(0, 0, 0, 0));
85
        changeButton.setText("   ...   ");
86
        changeButton.addActionListener(new java.awt.event.ActionListener() {
87
                public void actionPerformed(ActionEvent e) {
88
                    changeButton_actionPerformed(e);
89
                }
90
            });
91
        outerColorPanel.setBorder(BorderFactory.createLoweredBevelBorder());
92
        outerColorPanel.setPreferredSize(new Dimension(60, 20));
93
        outerColorPanel.setBackground(Color.white);
94
        outerColorPanel.setLayout(gridBagLayout2);
95
        colorPanel.setMargin(0);
96
        colorPanel.setPreferredSize(new Dimension(45, 8));
97
        this.add(changeButton,
98
            new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0,
99
                GridBagConstraints.CENTER, GridBagConstraints.NONE,
100
                new Insets(0, 2, 0, 0), 0, 0));
101
        this.add(outerColorPanel,
102
            new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
103
                GridBagConstraints.CENTER, GridBagConstraints.NONE,
104
                new Insets(0, 0, 0, 0), 0, 0));
105
        outerColorPanel.add(colorPanel,
106
            new GridBagConstraints(0, 0, 1, 1, 0, 0,
107
                GridBagConstraints.CENTER, GridBagConstraints.NONE,
108
                new Insets(0, 0, 0, 0), 0, 0));
109
    }
110

    
111
    void changeButton_actionPerformed(ActionEvent e) {
112
        Color newColor = JColorChooser.showDialog(SwingUtilities.windowForComponent(this), "Choose Colour", color);
113

    
114
        if (newColor == null) {
115
            return;
116
        }
117

    
118
        setColor(newColor);
119
        fireActionPerformed();
120
    }
121

    
122
    public void setColor(Color color) {
123
        this.color = color;
124
        updateColorPanel();
125
    }
126

    
127
    private void updateColorPanel() {
128
            
129
        colorPanel.setFillColor(new Color(color.getRed(), color.getGreen(), color.getBlue(),alpha));
130
                
131
        
132
        colorPanel.repaint();
133
    }
134

    
135
    public void addActionListener(ActionListener l) {
136
        actionListeners.add(l);
137
    }
138

    
139
    public void removeActionListener(ActionListener l) {
140
        actionListeners.remove(l);
141
    }
142

    
143
    protected void fireActionPerformed() {
144
        for (Iterator i = actionListeners.iterator(); i.hasNext();) {
145
            ActionListener l = (ActionListener) i.next();
146
            l.actionPerformed(new ActionEvent(this, 0, null));
147
        }
148
    }
149

    
150
    public Color getColor() {
151
        return color;
152
    }
153

    
154
    //{CLARITY} Might be clearer to say "alpha" rather than "alpha".
155
    //This occurs in other places too. [Jon Aquino]
156
    public void setAlpha(int alpha) {
157
        this.alpha = alpha;
158
        updateColorPanel();
159
    }
160

    
161
    public void setEnabled(boolean newEnabled) {
162
        changeButton.setEnabled(newEnabled);
163
    }
164
}