001 package com.croftsoft.core.gui; 002 003 import java.awt.*; 004 import java.awt.event.*; 005 import java.util.*; 006 import javax.swing.*; 007 008 import com.croftsoft.core.lang.NullArgumentException; 009 010 /********************************************************************* 011 * A JPanel of JCheckBoxes with identifying JLabels. 012 * 013 * <p> 014 * Example: 015 * <code> 016 * <pre> 017 * CheckBoxPanel checkBoxPanel = new CheckBoxPanel ( 018 * new String [ ] { "From", "To", "Date", "Subject" } ); 019 * </pre> 020 * </code> 021 * </p> 022 * 023 * @version 024 * 2001-10-12 025 * @since 026 * 2001-08-08 027 * @author 028 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 029 *********************************************************************/ 030 031 public final class CheckBoxPanel 032 extends JPanel 033 ////////////////////////////////////////////////////////////////////// 034 ////////////////////////////////////////////////////////////////////// 035 { 036 037 private final Map labelNameToCheckBoxMap = new HashMap ( ); 038 039 ////////////////////////////////////////////////////////////////////// 040 ////////////////////////////////////////////////////////////////////// 041 042 public CheckBoxPanel ( 043 String [ ] labelNames, 044 String [ ] descriptors, 045 Color panelBackgroundColor ) 046 ////////////////////////////////////////////////////////////////////// 047 { 048 super ( new GridBagLayout ( ), true ); 049 050 NullArgumentException.check ( labelNames ); 051 052 if ( panelBackgroundColor != null ) 053 { 054 setBackground ( panelBackgroundColor ); 055 } 056 057 if ( descriptors != null ) 058 { 059 if ( descriptors.length != labelNames.length ) 060 { 061 throw new IllegalArgumentException ( 062 "descriptors.length != labelNames.length" ); 063 } 064 } 065 066 GridBagConstraints gridBagConstraints = new GridBagConstraints ( ); 067 068 gridBagConstraints.fill = GridBagConstraints.HORIZONTAL; 069 070 // gridBagConstraints.ipadx = 10; 071 072 // gridBagConstraints.ipady = 10; 073 074 Insets checkBoxInsets = new Insets ( 0, 10, 0, 0 ); 075 076 Insets labelInsets = new Insets ( 0, 0, 0, 10 ); 077 078 for ( int i = 0; i < labelNames.length; i++ ) 079 { 080 gridBagConstraints.gridx = 0; 081 082 gridBagConstraints.gridy = i; 083 084 gridBagConstraints.weightx = 0.0; 085 086 gridBagConstraints.insets = checkBoxInsets; 087 088 JCheckBox jCheckBox = new JCheckBox ( ); 089 090 add ( jCheckBox, gridBagConstraints ); 091 092 gridBagConstraints.gridx = 1; 093 094 if ( descriptors == null ) 095 { 096 gridBagConstraints.weightx = 1.0; 097 } 098 099 gridBagConstraints.insets = labelInsets; 100 101 String labelName = labelNames [ i ]; 102 103 labelNameToCheckBoxMap.put ( labelName, jCheckBox ); 104 105 add ( new JLabel ( labelName ), gridBagConstraints ); 106 107 if ( descriptors != null ) 108 { 109 gridBagConstraints.gridx = 2; 110 111 gridBagConstraints.weightx = 1.0; 112 113 add ( new JLabel ( descriptors [ i ] ), gridBagConstraints ); 114 } 115 116 /* 117 Is this necessary? If so, modify in LabeledFieldsPanel2 as well. 118 119 if ( panelBackgroundColor != null ) 120 { 121 labelName.setBackground ( panelBackgroundColor ); 122 } 123 */ 124 } 125 } 126 127 public CheckBoxPanel ( String [ ] labelNames ) 128 ////////////////////////////////////////////////////////////////////// 129 { 130 this ( labelNames, null, null ); 131 } 132 133 ////////////////////////////////////////////////////////////////////// 134 // accessors 135 ////////////////////////////////////////////////////////////////////// 136 137 public String [ ] getLabelNames ( ) 138 ////////////////////////////////////////////////////////////////////// 139 { 140 return ( String [ ] ) 141 labelNameToCheckBoxMap.keySet ( ).toArray ( new String [ ] { } ); 142 } 143 144 public String [ ] getSelectedLabelNames ( ) 145 ////////////////////////////////////////////////////////////////////// 146 { 147 java.util.List selectedLabelNamesList = new ArrayList ( ); 148 149 Iterator iterator = labelNameToCheckBoxMap.keySet ( ).iterator ( ); 150 151 while ( iterator.hasNext ( ) ) 152 { 153 String labelName = ( String ) iterator.next ( ); 154 155 JCheckBox jCheckBox 156 = ( JCheckBox ) labelNameToCheckBoxMap.get ( labelName ); 157 158 if ( jCheckBox.isSelected ( ) ) 159 { 160 selectedLabelNamesList.add ( labelName ); 161 } 162 } 163 164 return ( String [ ] ) 165 selectedLabelNamesList.toArray ( new String [ ] { } ); 166 } 167 168 public boolean isSelected ( String labelName ) 169 ////////////////////////////////////////////////////////////////////// 170 { 171 NullArgumentException.check ( labelName ); 172 173 JCheckBox jCheckBox 174 = ( JCheckBox ) labelNameToCheckBoxMap.get ( labelName ); 175 176 if ( jCheckBox == null ) 177 { 178 throw new IllegalArgumentException ( 179 "unknown labelName: " + labelName ); 180 } 181 182 return jCheckBox.isSelected ( ); 183 } 184 185 ////////////////////////////////////////////////////////////////////// 186 // mutators 187 ////////////////////////////////////////////////////////////////////// 188 189 public void setAllSelected ( boolean selected ) 190 ////////////////////////////////////////////////////////////////////// 191 { 192 Iterator iterator = labelNameToCheckBoxMap.values ( ).iterator ( ); 193 194 while ( iterator.hasNext ( ) ) 195 { 196 JCheckBox jCheckBox = ( JCheckBox ) iterator.next ( ); 197 198 jCheckBox.setSelected ( selected ); 199 } 200 } 201 202 public void setSelected ( 203 String labelName, 204 boolean selected ) 205 ////////////////////////////////////////////////////////////////////// 206 { 207 NullArgumentException.check ( labelName ); 208 209 JCheckBox jCheckBox 210 = ( JCheckBox ) labelNameToCheckBoxMap.get ( labelName ); 211 212 if ( jCheckBox == null ) 213 { 214 throw new IllegalArgumentException ( 215 "unknown labelName: " + labelName ); 216 } 217 218 jCheckBox.setSelected ( selected ); 219 } 220 221 ////////////////////////////////////////////////////////////////////// 222 ////////////////////////////////////////////////////////////////////// 223 }