View Javadoc

1   /*
2   Wotonomy: OpenStep design patterns for pure Java applications.
3   Copyright (C) 2000 Blacksmith, Inc.
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  Lesser General Public License for more details.
14  
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, see http://www.gnu.org
17  */
18  
19  package net.wotonomy.ui.swing.components;
20  
21  import java.awt.Component;
22  import java.util.List;
23  import java.util.Vector;
24  
25  import javax.swing.AbstractButton;
26  import javax.swing.JCheckBox;
27  import javax.swing.border.EmptyBorder;
28  
29  /***
30  * CheckButtonPanel is a simple extension of ButtonPanel.
31  * Differences are that it uses JCheckBoxes and the
32  * default alignment is vertical.  The panel defaults to having
33  * no buttons selected.
34  *
35  * @author michael@mpowers.net
36  * @author $Author: cgruber $
37  * @version $Revision: 904 $
38  */
39  public class CheckButtonPanel extends ButtonPanel
40  {
41  /***
42  * Constructs a CheckButtonPanel.  Three buttons are created
43  * so the panel is filled when used in a GUI-builder environment.
44  */
45      public CheckButtonPanel()
46      {
47          super();
48      }
49  
50  /***
51  * Constructs a ButtonPanel using specified buttons.
52  * @param buttonList An array containing the strings to be used in labeling the buttons.
53  */
54      public CheckButtonPanel( String[] buttonList )
55      {
56          super( buttonList );
57      }
58  
59  /***
60  * Overridden to set vertical-center alignment and zero vgap.
61  */
62      protected void initLayout()
63      {
64          super.initLayout();
65          buttonPanelLayout.setAlignment( BetterFlowLayout.CENTER_VERTICAL );
66          buttonPanelLayout.setVgap( 0 );
67      }
68  
69  /***
70  * Overridden to return a JRadioButton.
71  * @param aLabel The label for the component that will be created.
72  * @return The newly created component.
73  */
74      protected Component createComponentWithLabel( String aLabel )
75      {
76          String buttonLabel = aLabel;
77          JCheckBox newButton = new JCheckBox();
78          newButton.setName( aLabel );
79          newButton.setText( buttonLabel );
80          newButton.setActionCommand( aLabel );
81          newButton.addActionListener( this );
82  
83          // reduce insets per java l&f guidelines (was 4 on each side)
84          newButton.setBorder( new EmptyBorder( 1, 4, 1, 4 ) );
85  
86          return newButton;
87      }
88  
89  /***
90  * Sets the value of the button whose name matches the given text value.
91  * @param aName A String matching the name of one of the buttons.
92  * If null, empty, or not matching, nothing happens.
93  * @param aValue A value to set the button.
94  */
95      public void setValue(String aName, boolean aValue)
96      {
97          if ( aName != null )
98          {
99              Component c = null;
100             int count = buttonContainer.getComponentCount();
101             for ( int i = 0; i < count; i++ )
102             {
103                 c = buttonContainer.getComponent( i );
104                 if ( c instanceof AbstractButton )
105                 {
106                     if ( c.getName().equals( aName ) )
107                     {
108                         ((AbstractButton)c).setSelected( aValue );
109                         c.repaint();
110                         return;
111                     }
112                 }
113             }
114         }
115         // null, empty, or not matching - exit.
116         System.out.println( "CheckButtonPanel.setValue: not found: " + aName );
117     }
118 
119 /***
120 * Sets the state of the specified buttons to the specified value.
121 * @param aLabelArray An Array of Strings listing the buttons to be set.
122 * @param aValue The value to which the specified buttons will be set.
123 */
124     public void setValues(String[] aLabelArray, boolean aValue)
125     {
126         if ( aLabelArray != null )
127         {
128             for ( int i = 0; i < aLabelArray.length; i++ )
129             {
130                 setValue( aLabelArray[i], aValue );
131             }
132         }
133     }
134 
135 /***
136 * Convenience method to set all checkboxes on the panel.
137 * @param aValue The value to which all checkboxes on the panel will be set.
138 */
139     public void setAllValues(boolean aValue)
140     {
141         setValues( getLabels(), aValue );
142     }
143 
144 /***
145 * Convenience method to check all boxes on the panel.
146 */
147     public void checkAll()
148     {
149         setAllValues( true );
150     }
151 
152 /***
153 * Convenience method to clear all boxes on the panel.
154 */
155     public void clearAll()
156     {
157         setAllValues( false );
158     }
159 
160 /***
161 * A convenience method to set only those buttons on the entire
162 * panel that should be checked.  Buttons not in the list are unchecked.
163 * @param aLabelArray An Array of Strings listing the buttons to be set.
164 */
165     public void setCheckedValues(String[] aLabelArray)
166     {
167         setAllValues( false );
168         setValues( aLabelArray, true );
169     }
170 
171 /***
172 * Gets the labels of all checkboxes that are checked.
173 * @return A List of Strings containing the labels of the boxes that are checked.
174 */
175     public List getCheckedValueList()
176     {
177         Vector v = new Vector();
178         Component c = null;
179         int count = buttonContainer.getComponentCount();
180         for ( int i = 0; i < count; i++ )
181         {
182             c = buttonContainer.getComponent( i );
183             if ( c instanceof AbstractButton )
184             {
185                 if ( ((AbstractButton)c).isSelected() )
186                 {
187                     v.addElement(c.getName());
188                 }
189             }
190         }
191         return v;
192     }
193 
194 /***
195 * Gets the labels of all checkboxes that are checked.
196 * @return A String Array containing the labels of the boxes that are checked.
197 */
198     public String[] getCheckedValues()
199     {
200         List v = getCheckedValueList();
201         String[] result = new String[ v.size() ];
202         for ( int i = 0; i < v.size(); i++ )
203         {
204             result[i] = (String) v.get(i);
205         }
206         return result;
207     }
208 
209 /***
210 * Gets the value of the specified button.
211 * @param aName A String matching the name of one of the buttons.
212 * @return True if the button is checked, False if it is not checked.
213 * NOTE: If the button is not found in the list, False is returned.
214 */
215     public boolean getValue( String aName )
216     {
217         Component c = null;
218         int count = buttonContainer.getComponentCount();
219         for ( int i = 0; i < count; i++ )
220         {
221             c = buttonContainer.getComponent( i );
222             if ( ( c instanceof AbstractButton ) && ( ((AbstractButton)c).isSelected() ) )
223             {
224                 if ( ((AbstractButton)c).getText().equals( aName ) )
225                 {
226                     return ((AbstractButton)c).isSelected();
227                 }
228             }
229         }
230         return false;
231     }
232 
233     // for testing
234 
235     public static void main( String[] argv )
236     {
237         try
238         {
239             javax.swing.UIManager.setLookAndFeel( javax.swing.UIManager.getSystemLookAndFeelClassName() );
240         }
241         catch (Exception exc)
242         {
243 
244         }
245 
246         javax.swing.JFrame dialog = new javax.swing.JFrame();
247         java.awt.BorderLayout bl = new java.awt.BorderLayout( 20, 20 );
248 
249         CheckButtonPanel panel = new CheckButtonPanel( new String[] { "One", "Two", "Three" } );
250 
251         dialog.getContentPane().setLayout( bl );
252         dialog.getContentPane().add( panel, java.awt.BorderLayout.CENTER );
253         dialog.setLocation( 50, 50 );
254         // dialog.setSize( 450, 150 );
255 
256         panel.setAlignment( BetterFlowLayout.CENTER_VERTICAL );
257         panel.getButton( "One" ).setEnabled( false );
258         panel.setValues( new String[] { "One" }, true );
259         panel.setValue( "Three", true );
260 //        panel.setCheckedValues( new String[] { "Two" } );
261         String[] values = panel.getCheckedValues();
262         for ( int i = 0; i < values.length; i++ )
263         {
264             System.out.println( values[i] );
265         }
266 
267         dialog.pack();
268         dialog.setVisible( true );
269 
270     }
271 }
272