Coverage Report - net.wotonomy.ui.swing.components.CheckButtonPanel
 
Classes in this File Line Coverage Branch Coverage Complexity
CheckButtonPanel
0% 
0% 
2.214
 
 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  0
         super();
 48  0
     }
 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  0
         super( buttonList );
 57  0
     }
 58  
 
 59  
 /**
 60  
 * Overridden to set vertical-center alignment and zero vgap.
 61  
 */
 62  
     protected void initLayout()
 63  
     {
 64  0
         super.initLayout();
 65  0
         buttonPanelLayout.setAlignment( BetterFlowLayout.CENTER_VERTICAL );
 66  0
         buttonPanelLayout.setVgap( 0 );
 67  0
     }
 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  0
         String buttonLabel = aLabel;
 77  0
         JCheckBox newButton = new JCheckBox();
 78  0
         newButton.setName( aLabel );
 79  0
         newButton.setText( buttonLabel );
 80  0
         newButton.setActionCommand( aLabel );
 81  0
         newButton.addActionListener( this );
 82  
 
 83  
         // reduce insets per java l&f guidelines (was 4 on each side)
 84  0
         newButton.setBorder( new EmptyBorder( 1, 4, 1, 4 ) );
 85  
 
 86  0
         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  0
         if ( aName != null )
 98  
         {
 99  0
             Component c = null;
 100  0
             int count = buttonContainer.getComponentCount();
 101  0
             for ( int i = 0; i < count; i++ )
 102  
             {
 103  0
                 c = buttonContainer.getComponent( i );
 104  0
                 if ( c instanceof AbstractButton )
 105  
                 {
 106  0
                     if ( c.getName().equals( aName ) )
 107  
                     {
 108  0
                         ((AbstractButton)c).setSelected( aValue );
 109  0
                         c.repaint();
 110  0
                         return;
 111  
                     }
 112  
                 }
 113  
             }
 114  
         }
 115  
         // null, empty, or not matching - exit.
 116  0
         System.out.println( "CheckButtonPanel.setValue: not found: " + aName );
 117  0
     }
 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  0
         if ( aLabelArray != null )
 127  
         {
 128  0
             for ( int i = 0; i < aLabelArray.length; i++ )
 129  
             {
 130  0
                 setValue( aLabelArray[i], aValue );
 131  
             }
 132  
         }
 133  0
     }
 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  0
         setValues( getLabels(), aValue );
 142  0
     }
 143  
 
 144  
 /**
 145  
 * Convenience method to check all boxes on the panel.
 146  
 */
 147  
     public void checkAll()
 148  
     {
 149  0
         setAllValues( true );
 150  0
     }
 151  
 
 152  
 /**
 153  
 * Convenience method to clear all boxes on the panel.
 154  
 */
 155  
     public void clearAll()
 156  
     {
 157  0
         setAllValues( false );
 158  0
     }
 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  0
         setAllValues( false );
 168  0
         setValues( aLabelArray, true );
 169  0
     }
 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  0
         Vector v = new Vector();
 178  0
         Component c = null;
 179  0
         int count = buttonContainer.getComponentCount();
 180  0
         for ( int i = 0; i < count; i++ )
 181  
         {
 182  0
             c = buttonContainer.getComponent( i );
 183  0
             if ( c instanceof AbstractButton )
 184  
             {
 185  0
                 if ( ((AbstractButton)c).isSelected() )
 186  
                 {
 187  0
                     v.addElement(c.getName());
 188  
                 }
 189  
             }
 190  
         }
 191  0
         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  0
         List v = getCheckedValueList();
 201  0
         String[] result = new String[ v.size() ];
 202  0
         for ( int i = 0; i < v.size(); i++ )
 203  
         {
 204  0
             result[i] = (String) v.get(i);
 205  
         }
 206  0
         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  0
         Component c = null;
 218  0
         int count = buttonContainer.getComponentCount();
 219  0
         for ( int i = 0; i < count; i++ )
 220  
         {
 221  0
             c = buttonContainer.getComponent( i );
 222  0
             if ( ( c instanceof AbstractButton ) && ( ((AbstractButton)c).isSelected() ) )
 223  
             {
 224  0
                 if ( ((AbstractButton)c).getText().equals( aName ) )
 225  
                 {
 226  0
                     return ((AbstractButton)c).isSelected();
 227  
                 }
 228  
             }
 229  
         }
 230  0
         return false;
 231  
     }
 232  
 
 233  
     // for testing
 234  
 
 235  
     public static void main( String[] argv )
 236  
     {
 237  
         try
 238  
         {
 239  0
             javax.swing.UIManager.setLookAndFeel( javax.swing.UIManager.getSystemLookAndFeelClassName() );
 240  
         }
 241  0
         catch (Exception exc)
 242  
         {
 243  
 
 244  0
         }
 245  
 
 246  0
         javax.swing.JFrame dialog = new javax.swing.JFrame();
 247  0
         java.awt.BorderLayout bl = new java.awt.BorderLayout( 20, 20 );
 248  
 
 249  0
         CheckButtonPanel panel = new CheckButtonPanel( new String[] { "One", "Two", "Three" } );
 250  
 
 251  0
         dialog.getContentPane().setLayout( bl );
 252  0
         dialog.getContentPane().add( panel, java.awt.BorderLayout.CENTER );
 253  0
         dialog.setLocation( 50, 50 );
 254  
         // dialog.setSize( 450, 150 );
 255  
 
 256  0
         panel.setAlignment( BetterFlowLayout.CENTER_VERTICAL );
 257  0
         panel.getButton( "One" ).setEnabled( false );
 258  0
         panel.setValues( new String[] { "One" }, true );
 259  0
         panel.setValue( "Three", true );
 260  
 //        panel.setCheckedValues( new String[] { "Two" } );
 261  0
         String[] values = panel.getCheckedValues();
 262  0
         for ( int i = 0; i < values.length; i++ )
 263  
         {
 264  0
             System.out.println( values[i] );
 265  
         }
 266  
 
 267  0
         dialog.pack();
 268  0
         dialog.setVisible( true );
 269  
 
 270  0
     }
 271  
 }
 272