001 package com.croftsoft.core.gui; 002 003 import java.awt.*; 004 import java.awt.event.*; 005 006 import com.croftsoft.core.lang.NullArgumentException; 007 008 /********************************************************************* 009 * Allows the user to select an item from a list. 010 * 011 * <p> 012 * Java 1.1 compatible. 013 * </p> 014 * 015 * @version 016 * 2001-08-08 017 * @since 018 * 2001-05-03 019 * @author 020 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 021 *********************************************************************/ 022 023 public final class SelectPanel 024 extends Panel 025 ////////////////////////////////////////////////////////////////////// 026 ////////////////////////////////////////////////////////////////////// 027 { 028 029 private static final String BUTTON_TEXT_SELECT = "Select"; 030 031 private static final String BUTTON_TEXT_CANCEL = "Cancel"; 032 033 private static final String BUTTON_TEXT_PREVIOUS = "Previous"; 034 035 private static final String BUTTON_TEXT_NEXT = "Next"; 036 037 // 038 039 private ActionListener selectButtonActionListener; 040 041 private ActionListener cancelButtonActionListener; 042 043 private ActionListener previousButtonActionListener; 044 045 private ActionListener nextButtonActionListener; 046 047 private Color backgroundColor; 048 049 // 050 051 private Label panelLabel; 052 053 private List list; 054 055 private Button selectButton; 056 057 private Button cancelButton; 058 059 private Button previousButton; 060 061 private Button nextButton; 062 063 // 064 065 private int selectedIndex = -1; 066 067 ////////////////////////////////////////////////////////////////////// 068 ////////////////////////////////////////////////////////////////////// 069 070 /********************************************************************* 071 * Main constructor. 072 * 073 * @param selectButtonActionListener 074 * 075 * May be null. 076 * 077 * @param cancelButtonActionListener 078 * 079 * May be null. 080 * 081 * @param previousButtonActionListener 082 * 083 * May be null. 084 * 085 * @param nextButtonActionListener 086 * 087 * May be null. 088 * 089 * @param items 090 * 091 * May be null. 092 * 093 * @param title 094 * 095 * May be null. 096 * 097 * @param backgroundColor 098 * 099 * May be null. 100 *********************************************************************/ 101 public SelectPanel ( 102 ActionListener selectButtonActionListener, 103 ActionListener cancelButtonActionListener, 104 ActionListener previousButtonActionListener, 105 ActionListener nextButtonActionListener, 106 String [ ] items, 107 boolean enablePreviousButton, 108 boolean enableNextButton, 109 String title, 110 Color backgroundColor ) 111 ////////////////////////////////////////////////////////////////////// 112 { 113 super ( new BorderLayout ( ) ); 114 115 this.selectButtonActionListener = selectButtonActionListener; 116 117 this.cancelButtonActionListener = cancelButtonActionListener; 118 119 this.previousButtonActionListener = previousButtonActionListener; 120 121 this.nextButtonActionListener = nextButtonActionListener; 122 123 setTitle ( title ); 124 125 this.backgroundColor = backgroundColor; 126 127 if ( backgroundColor != null ) 128 { 129 setBackground ( backgroundColor ); 130 } 131 132 // create button panel 133 134 Panel buttonPanel = new ButtonPanel1 ( 135 new Button [ ] { 136 selectButton = new Button ( BUTTON_TEXT_SELECT ), 137 previousButton = new Button ( BUTTON_TEXT_PREVIOUS ), 138 nextButton = new Button ( BUTTON_TEXT_NEXT ), 139 cancelButton = new Button ( BUTTON_TEXT_CANCEL ) }, 140 backgroundColor ); 141 142 selectButton.setEnabled ( false ); 143 144 cancelButton.setEnabled ( false ); 145 146 previousButton.setEnabled ( false ); 147 148 nextButton.setEnabled ( false ); 149 150 if ( selectButtonActionListener != null ) 151 { 152 selectButton.addActionListener ( selectButtonActionListener ); 153 } 154 155 if ( cancelButtonActionListener != null ) 156 { 157 cancelButton.addActionListener ( cancelButtonActionListener ); 158 } 159 160 if ( previousButtonActionListener != null ) 161 { 162 previousButton.addActionListener ( previousButtonActionListener ); 163 } 164 165 if ( nextButtonActionListener != null ) 166 { 167 nextButton.addActionListener ( nextButtonActionListener ); 168 } 169 170 add ( buttonPanel, BorderLayout.SOUTH ); 171 172 // 173 174 setItems ( items, enablePreviousButton, enableNextButton ); 175 } 176 177 /********************************************************************* 178 * Convenience constructor. 179 *********************************************************************/ 180 public SelectPanel ( Color backgroundColor ) 181 ////////////////////////////////////////////////////////////////////// 182 { 183 this ( 184 ( ActionListener ) null, 185 ( ActionListener ) null, 186 ( ActionListener ) null, 187 ( ActionListener ) null, 188 ( String [ ] ) null, 189 false, 190 false, 191 ( String ) null, 192 backgroundColor ); 193 } 194 195 ////////////////////////////////////////////////////////////////////// 196 ////////////////////////////////////////////////////////////////////// 197 198 public int getSelectedIndex ( ) 199 ////////////////////////////////////////////////////////////////////// 200 { 201 return list.getSelectedIndex ( ); 202 } 203 204 public String getSelectedItem ( ) 205 ////////////////////////////////////////////////////////////////////// 206 { 207 return list.getSelectedItem ( ); 208 } 209 210 ////////////////////////////////////////////////////////////////////// 211 ////////////////////////////////////////////////////////////////////// 212 213 public synchronized void setTitle ( String title ) 214 ////////////////////////////////////////////////////////////////////// 215 { 216 if ( title == null ) 217 { 218 if ( panelLabel != null ) 219 { 220 remove ( panelLabel ); 221 } 222 } 223 else 224 { 225 if ( panelLabel == null ) 226 { 227 panelLabel = new Label ( title ); 228 229 add ( panelLabel, BorderLayout.NORTH ); 230 } 231 else 232 { 233 panelLabel.setText ( title ); 234 } 235 } 236 } 237 238 public synchronized void setItems ( 239 String [ ] items, 240 boolean enablePreviousButton, 241 boolean enableNextButton ) 242 ////////////////////////////////////////////////////////////////////// 243 { 244 resetList ( ); 245 246 if ( items != null ) 247 { 248 for ( int i = 0; i < items.length; i++ ) 249 { 250 list.add ( items [ i ] ); 251 } 252 } 253 254 previousButton.setEnabled ( enablePreviousButton ); 255 256 nextButton .setEnabled ( enableNextButton ); 257 } 258 259 public synchronized void replaceCancelButtonActionListener ( 260 ActionListener actionListener ) 261 ////////////////////////////////////////////////////////////////////// 262 { 263 replaceButtonActionListener ( 264 cancelButtonActionListener, actionListener, cancelButton ); 265 266 cancelButtonActionListener = actionListener; 267 } 268 269 public synchronized void replaceSelectButtonActionListener ( 270 ActionListener actionListener ) 271 ////////////////////////////////////////////////////////////////////// 272 { 273 replaceButtonActionListener ( 274 selectButtonActionListener, actionListener, selectButton ); 275 276 selectButtonActionListener = actionListener; 277 } 278 279 public synchronized void replacePreviousButtonActionListener ( 280 ActionListener actionListener ) 281 ////////////////////////////////////////////////////////////////////// 282 { 283 replaceButtonActionListener ( 284 previousButtonActionListener, actionListener, previousButton ); 285 286 selectButtonActionListener = actionListener; 287 } 288 289 public synchronized void replaceNextButtonActionListener ( 290 ActionListener actionListener ) 291 ////////////////////////////////////////////////////////////////////// 292 { 293 replaceButtonActionListener ( 294 nextButtonActionListener, actionListener, nextButton ); 295 296 nextButtonActionListener = actionListener; 297 } 298 299 ////////////////////////////////////////////////////////////////////// 300 // private methods 301 ////////////////////////////////////////////////////////////////////// 302 303 private void replaceButtonActionListener ( 304 ActionListener oldActionListener, 305 ActionListener newActionListener, 306 Button button ) 307 ////////////////////////////////////////////////////////////////////// 308 { 309 if ( oldActionListener != null ) 310 { 311 button.removeActionListener ( oldActionListener ); 312 } 313 314 if ( newActionListener == null ) 315 { 316 button.setEnabled ( false ); 317 } 318 else 319 { 320 button.setEnabled ( true ); 321 322 button.addActionListener ( newActionListener ); 323 } 324 } 325 326 private synchronized void resetList ( ) 327 ////////////////////////////////////////////////////////////////////// 328 { 329 selectButton .setEnabled ( false ); 330 331 previousButton.setEnabled ( false ); 332 333 nextButton .setEnabled ( false ); 334 335 if ( list != null ) 336 { 337 remove ( list ); 338 } 339 340 selectedIndex = -1; 341 342 list = new List ( 4, false ); 343 344 if ( backgroundColor != null ) 345 { 346 list.setBackground ( backgroundColor ); 347 } 348 349 list.addItemListener ( 350 new ItemListener ( ) 351 { 352 public void itemStateChanged ( ItemEvent itemEvent ) 353 { 354 handleItemStateChanged ( ); 355 } 356 } ); 357 358 add ( list, BorderLayout.CENTER ); 359 360 validate ( ); 361 } 362 363 private synchronized void handleItemStateChanged ( ) 364 ////////////////////////////////////////////////////////////////////// 365 { 366 int selectedIndex = list.getSelectedIndex ( ); 367 368 if ( selectedIndex > -1 ) 369 { 370 if ( selectedIndex == this.selectedIndex ) 371 { 372 list.deselect ( selectedIndex ); 373 374 this.selectedIndex = -1; 375 376 selectButton.setEnabled ( false ); 377 } 378 else 379 { 380 this.selectedIndex = selectedIndex; 381 382 selectButton.setEnabled ( selectButtonActionListener != null ); 383 } 384 } 385 } 386 387 ////////////////////////////////////////////////////////////////////// 388 ////////////////////////////////////////////////////////////////////// 389 }