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 manipulate a list of items via control buttons. 010 * 011 * <p> 012 * Java 1.1 compatible. 013 * </p> 014 * 015 * @version 016 * 2001-08-08 017 * @since 018 * 2001-06-04 019 * @author 020 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 021 *********************************************************************/ 022 023 public class ListControlPanel 024 extends Panel 025 implements ItemListener 026 ////////////////////////////////////////////////////////////////////// 027 ////////////////////////////////////////////////////////////////////// 028 { 029 030 private Button [ ] buttons; 031 032 private Color backgroundColor; 033 034 // 035 036 private Label panelLabel; 037 038 private List list; 039 040 // 041 042 private int selectedIndex = -1; 043 044 ////////////////////////////////////////////////////////////////////// 045 ////////////////////////////////////////////////////////////////////// 046 047 /********************************************************************* 048 * Main constructor. 049 * 050 * @param buttons 051 * 052 * Must not be null. 053 * 054 * @param items 055 * 056 * May be null. 057 * 058 * @param title 059 * 060 * May be null. 061 * 062 * @param backgroundColor 063 * 064 * May be null. 065 *********************************************************************/ 066 public ListControlPanel ( 067 Button [ ] buttons, 068 String [ ] items, 069 String title, 070 Color backgroundColor ) 071 ////////////////////////////////////////////////////////////////////// 072 { 073 super ( new BorderLayout ( ) ); 074 075 NullArgumentException.check ( this.buttons = buttons ); 076 077 setTitle ( title ); 078 079 this.backgroundColor = backgroundColor; 080 081 if ( backgroundColor != null ) 082 { 083 setBackground ( backgroundColor ); 084 } 085 086 // create button panel 087 088 Panel buttonPanel = new ButtonPanel1 ( buttons, backgroundColor ); 089 090 add ( buttonPanel, BorderLayout.SOUTH ); 091 092 // 093 094 setItems ( items ); 095 } 096 097 public ListControlPanel ( 098 String [ ] buttonLabels, 099 String title, 100 Color backgroundColor ) 101 ////////////////////////////////////////////////////////////////////// 102 { 103 this ( ButtonLib.createButtonArray ( buttonLabels ), 104 null, title, backgroundColor ); 105 } 106 107 ////////////////////////////////////////////////////////////////////// 108 ////////////////////////////////////////////////////////////////////// 109 110 public Button [ ] getButtons ( ) 111 ////////////////////////////////////////////////////////////////////// 112 { 113 return buttons; 114 } 115 116 public int getSelectedIndex ( ) 117 ////////////////////////////////////////////////////////////////////// 118 { 119 return list.getSelectedIndex ( ); 120 } 121 122 public String getSelectedItem ( ) 123 ////////////////////////////////////////////////////////////////////// 124 { 125 return list.getSelectedItem ( ); 126 } 127 128 ////////////////////////////////////////////////////////////////////// 129 ////////////////////////////////////////////////////////////////////// 130 131 public synchronized void setTitle ( String title ) 132 ////////////////////////////////////////////////////////////////////// 133 { 134 if ( title == null ) 135 { 136 if ( panelLabel != null ) 137 { 138 remove ( panelLabel ); 139 } 140 } 141 else 142 { 143 if ( panelLabel == null ) 144 { 145 panelLabel = new Label ( title ); 146 147 add ( panelLabel, BorderLayout.NORTH ); 148 } 149 else 150 { 151 panelLabel.setText ( title ); 152 } 153 } 154 } 155 156 public synchronized void setItems ( String [ ] items ) 157 ////////////////////////////////////////////////////////////////////// 158 { 159 resetList ( ); 160 161 if ( items != null ) 162 { 163 for ( int i = 0; i < items.length; i++ ) 164 { 165 list.add ( items [ i ] ); 166 } 167 } 168 } 169 170 ////////////////////////////////////////////////////////////////////// 171 ////////////////////////////////////////////////////////////////////// 172 173 public synchronized void itemStateChanged ( ItemEvent itemEvent ) 174 ////////////////////////////////////////////////////////////////////// 175 { 176 int selectedIndex = list.getSelectedIndex ( ); 177 178 if ( selectedIndex > -1 ) 179 { 180 if ( selectedIndex == this.selectedIndex ) 181 { 182 list.deselect ( selectedIndex ); 183 184 this.selectedIndex = -1; 185 } 186 else 187 { 188 this.selectedIndex = selectedIndex; 189 } 190 } 191 } 192 193 ////////////////////////////////////////////////////////////////////// 194 // private methods 195 ////////////////////////////////////////////////////////////////////// 196 197 private synchronized void resetList ( ) 198 ////////////////////////////////////////////////////////////////////// 199 { 200 if ( list != null ) 201 { 202 remove ( list ); 203 } 204 205 selectedIndex = -1; 206 207 list = new List ( 4, false ); 208 209 if ( backgroundColor != null ) 210 { 211 list.setBackground ( backgroundColor ); 212 } 213 214 list.addItemListener ( this ); 215 216 add ( list, BorderLayout.CENTER ); 217 218 validate ( ); 219 } 220 221 ////////////////////////////////////////////////////////////////////// 222 ////////////////////////////////////////////////////////////////////// 223 }