KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > salsa > menu > ColorMenu


1 /*
2 ** Salsa - Swing Add-On Suite
3 ** Copyright (c) 2001, 2002 by Gerald Bauer
4 **
5 ** This program is free software.
6 **
7 ** You may redistribute it and/or modify it under the terms of the GNU
8 ** General Public License as published by the Free Software Foundation.
9 ** Version 2 of the license should be included with this distribution in
10 ** the file LICENSE, as well as License.html. If the license is not
11 ** included with this distribution, you may find a copy at the FSF web
12 ** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
13 ** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
14 **
15 ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
16 ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
17 ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
18 ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
19 ** REDISTRIBUTION OF THIS SOFTWARE.
20 **
21 */

22
23 package salsa.menu;
24
25 import java.awt.*;
26 import java.awt.event.*;
27 import java.util.*;
28 import javax.swing.*;
29 import javax.swing.border.*;
30 import javax.swing.event.*;
31
32 public class ColorMenu extends JMenu
33 {
34    Border _activeBorder;
35
36    Map _colorPanes;
37    Border _selectedBorder;
38    ColorPane _selectedColorPane;
39    Border _unselectedBorder;
40
41    public ColorMenu( String JavaDoc name )
42    {
43       super( name );
44
45       _unselectedBorder = new CompoundBorder(
46             new MatteBorder( 1, 1, 1, 1, getBackground() ),
47             new BevelBorder( BevelBorder.LOWERED, Color.WHITE, Color.GRAY ) );
48
49       _selectedBorder = new CompoundBorder(
50             new MatteBorder( 2, 2, 2, 2, Color.RED ),
51             new MatteBorder( 1, 1, 1, 1, getBackground() ) );
52
53       _activeBorder = new CompoundBorder(
54             new MatteBorder( 2, 2, 2, 2, Color.BLUE ),
55             new MatteBorder( 1, 1, 1, 1, getBackground() ) );
56
57       JPanel p = new JPanel();
58       p.setBorder( new EmptyBorder( 5, 5, 5, 5 ) );
59       p.setLayout( new GridLayout( 8, 8 ) );
60       _colorPanes = new HashMap();
61
62       int values[] = new int[]{0, 128, 192, 255};
63
64       for( int r = 0; r < values.length; r++ )
65          for( int g = 0; g < values.length; g++ )
66             for( int b = 0; b < values.length; b++ )
67             {
68                Color color = new Color( values[r], values[g], values[b] );
69                ColorPane colorPane = new ColorPane( color );
70                p.add( colorPane );
71                _colorPanes.put( color, colorPane );
72             }
73
74       add( p );
75
76    }
77
78    public void setColor( Color c )
79    {
80       Object JavaDoc obj = _colorPanes.get( c );
81       if( obj == null )
82          return;
83
84       if( _selectedColorPane != null )
85          _selectedColorPane.setSelected( false );
86
87       _selectedColorPane = ( ColorPane ) obj;
88       _selectedColorPane.setSelected( true );
89
90    }
91
92    public Color getColor()
93    {
94       if( _selectedColorPane == null )
95          return null;
96
97       return _selectedColorPane.getColor();
98    }
99
100    public void doSelection()
101    {
102       fireActionPerformed( new ActionEvent( this,
103             ActionEvent.ACTION_PERFORMED, getActionCommand() ) );
104    }
105
106    class ColorPane extends JPanel implements MouseListener
107    {
108       Color _color;
109       boolean _isSelected;
110
111       public ColorPane( Color color )
112       {
113          _color = color;
114          setBackground( color );
115          setBorder( _unselectedBorder );
116          String JavaDoc msg = "rgb( " + color.getRed() + ", "
117                 + color.getGreen() + ", "
118                 + color.getBlue() + " )";
119          setToolTipText( msg );
120          addMouseListener( this );
121       }
122
123       public void setSelected( boolean isSelected )
124       {
125          _isSelected = isSelected;
126          if( _isSelected )
127             setBorder( _selectedBorder );
128          else
129             setBorder( _unselectedBorder );
130       }
131
132       public Color getColor()
133       {
134          return _color;
135       }
136
137       public Dimension getMaximumSize()
138       {
139          return getPreferredSize();
140       }
141
142       public Dimension getMinimumSize()
143       {
144          return getPreferredSize();
145       }
146
147       public Dimension getPreferredSize()
148       {
149          return new Dimension( 15, 15 );
150       }
151
152       public boolean isSelected()
153       {
154          return _isSelected;
155       }
156
157       public void mouseClicked( MouseEvent ev ) { }
158
159       public void mouseEntered( MouseEvent ev )
160       {
161          setBorder( _activeBorder );
162       }
163
164       public void mouseExited( MouseEvent ev )
165       {
166          setBorder( _isSelected ? _selectedBorder : _unselectedBorder );
167       }
168
169       public void mousePressed( MouseEvent ev ) { }
170
171       public void mouseReleased( MouseEvent ev )
172       {
173          setColor( _color );
174          MenuSelectionManager.defaultManager().clearSelectedPath();
175          doSelection();
176       }
177    }
178 }
179
Popular Tags