1 36 37 40 41 42 import javax.swing.*; 43 import javax.swing.event.*; 44 import javax.swing.text.*; 45 import javax.swing.border.*; 46 import javax.swing.colorchooser.*; 47 import javax.swing.filechooser.*; 48 import javax.accessibility.*; 49 50 import java.awt.*; 51 import java.awt.event.*; 52 import java.beans.*; 53 import java.util.*; 54 import java.io.*; 55 import java.applet.*; 56 import java.net.*; 57 58 70 public class ListDemo extends DemoModule { 71 JList list; 72 73 JPanel prefixList; 74 JPanel suffixList; 75 76 Action prefixAction; 77 Action suffixAction; 78 79 GeneratedListModel listModel; 80 81 Vector checkboxes = new Vector(); 82 83 86 public static void main(String [] args) { 87 ListDemo demo = new ListDemo(null); 88 demo.mainImpl(); 89 } 90 91 94 public ListDemo(SwingSet2 swingset) { 95 super(swingset, "ListDemo", "toolbar/JList.gif"); 96 97 loadImages(); 98 99 JLabel description = new JLabel(getString("ListDemo.description")); 100 getDemoPanel().add(description, BorderLayout.NORTH); 101 102 JPanel centerPanel = new JPanel(); 103 centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.X_AXIS)); 104 centerPanel.add(Box.createRigidArea(HGAP10)); 105 getDemoPanel().add(centerPanel, BorderLayout.CENTER); 106 107 JPanel listPanel = new JPanel(); 108 listPanel.setLayout(new BoxLayout(listPanel, BoxLayout.Y_AXIS)); 109 listPanel.add(Box.createRigidArea(VGAP10)); 110 111 centerPanel.add(listPanel); 112 centerPanel.add(Box.createRigidArea(HGAP30)); 113 114 list = new JList(); 116 list.setCellRenderer(new CompanyLogoListCellRenderer()); 117 listModel = new GeneratedListModel(this); 118 list.setModel(listModel); 119 120 list.setVisibleRowCount(22); 123 124 JScrollPane scrollPane = new JScrollPane(list); 126 listPanel.add(scrollPane); 127 listPanel.add(Box.createRigidArea(VGAP10)); 128 129 centerPanel.add(createControlPanel()); 131 132 addPrefix("Tera", true); 134 addPrefix("Micro", false); 135 addPrefix("Southern", false); 136 addPrefix("Net", true); 137 addPrefix("YoYo", true); 138 addPrefix("Northern", false); 139 addPrefix("Tele", false); 140 addPrefix("Eastern", false); 141 addPrefix("Neo", false); 142 addPrefix("Digi", false); 143 addPrefix("National", false); 144 addPrefix("Compu", true); 145 addPrefix("Meta", true); 146 addPrefix("Info", false); 147 addPrefix("Western", false); 148 addPrefix("Data", false); 149 addPrefix("Atlantic", false); 150 addPrefix("Advanced", false); 151 addPrefix("Euro", false); 152 addPrefix("Pacific", false); 153 addPrefix("Mobile", false); 154 addPrefix("In", false); 155 addPrefix("Computa", false); 156 addPrefix("Digital", false); 157 addPrefix("Analog", false); 158 159 addSuffix("Tech", true); 160 addSuffix("Soft", true); 161 addSuffix("Telecom", true); 162 addSuffix("Solutions", false); 163 addSuffix("Works", true); 164 addSuffix("Dyne", false); 165 addSuffix("Services", false); 166 addSuffix("Vers", false); 167 addSuffix("Devices", false); 168 addSuffix("Software", false); 169 addSuffix("Serv", false); 170 addSuffix("Systems", true); 171 addSuffix("Dynamics", true); 172 addSuffix("Net", false); 173 addSuffix("Sys", false); 174 addSuffix("Computing", false); 175 addSuffix("Scape", false); 176 addSuffix("Com", false); 177 addSuffix("Ware", false); 178 addSuffix("Widgets", false); 179 addSuffix("Media", false); 180 addSuffix("Computer", false); 181 addSuffix("Hardware", false); 182 addSuffix("Gizmos", false); 183 addSuffix("Concepts", false); 184 } 185 186 void updateDragEnabled(boolean dragEnabled) { 187 list.setDragEnabled(dragEnabled); 188 } 189 190 public JPanel createControlPanel() { 191 JPanel controlPanel = new JPanel() { 192 Insets insets = new Insets(0, 4, 10, 10); 193 public Insets getInsets() { 194 return insets; 195 } 196 }; 197 controlPanel.setLayout(new BoxLayout(controlPanel, BoxLayout.X_AXIS)); 198 199 JPanel prefixPanel = new JPanel(); 200 prefixPanel.setLayout(new BoxLayout(prefixPanel, BoxLayout.Y_AXIS)); 201 prefixPanel.add(new JLabel(getString("ListDemo.prefixes"))); 202 203 JPanel suffixPanel = new JPanel(); 204 suffixPanel.setLayout(new BoxLayout(suffixPanel, BoxLayout.Y_AXIS)); 205 suffixPanel.add(new JLabel(getString("ListDemo.suffixes"))); 206 207 prefixList = new JPanel() { 208 Insets insets = new Insets(0, 4, 0, 0); 209 public Insets getInsets() { 210 return insets; 211 } 212 }; 213 prefixList.setLayout(new BoxLayout(prefixList, BoxLayout.Y_AXIS)); 214 JScrollPane scrollPane = new JScrollPane(prefixList); 215 scrollPane.getVerticalScrollBar().setUnitIncrement(10); 216 prefixPanel.add(scrollPane); 217 prefixPanel.add(Box.createRigidArea(HGAP10)); 218 219 suffixList = new JPanel() { 220 Insets insets = new Insets(0, 4, 0, 0); 221 public Insets getInsets() { 222 return insets; 223 } 224 }; 225 suffixList.setLayout(new BoxLayout(suffixList, BoxLayout.Y_AXIS)); 226 scrollPane = new JScrollPane(suffixList); 227 scrollPane.getVerticalScrollBar().setUnitIncrement(10); 228 suffixPanel.add(scrollPane); 229 suffixPanel.add(Box.createRigidArea(HGAP10)); 230 231 controlPanel.add(prefixPanel); 232 controlPanel.add(Box.createRigidArea(HGAP15)); 233 controlPanel.add(suffixPanel); 234 return controlPanel; 235 } 236 237 private FocusListener listFocusListener = new FocusAdapter() { 238 public void focusGained(FocusEvent e) { 239 JComponent c = (JComponent)e.getComponent(); 240 c.scrollRectToVisible(new Rectangle(0, 0, c.getWidth(), c.getHeight())); 241 } 242 }; 243 244 public void addPrefix(String prefix, boolean selected) { 245 if(prefixAction == null) { 246 prefixAction = new UpdatePrefixListAction(listModel); 247 } 248 final JCheckBox cb = (JCheckBox) prefixList.add(new JCheckBox(prefix)); 249 checkboxes.addElement(cb); 250 cb.setSelected(selected); 251 cb.addActionListener(prefixAction); 252 if(selected) { 253 listModel.addPrefix(prefix); 254 } 255 cb.addFocusListener(listFocusListener); 256 } 257 258 public void addSuffix(String suffix, boolean selected) { 259 if(suffixAction == null) { 260 suffixAction = new UpdateSuffixListAction(listModel); 261 } 262 final JCheckBox cb = (JCheckBox) suffixList.add(new JCheckBox(suffix)); 263 checkboxes.addElement(cb); 264 cb.setSelected(selected); 265 cb.addActionListener(suffixAction); 266 if(selected) { 267 listModel.addSuffix(suffix); 268 } 269 cb.addFocusListener(listFocusListener); 270 } 271 272 class UpdatePrefixListAction extends AbstractAction { 273 GeneratedListModel listModel; 274 protected UpdatePrefixListAction(GeneratedListModel listModel) { 275 this.listModel = listModel; 276 } 277 278 public void actionPerformed(ActionEvent e) { 279 JCheckBox cb = (JCheckBox) e.getSource(); 280 if(cb.isSelected()) { 281 listModel.addPrefix(cb.getText()); 282 } else { 283 listModel.removePrefix(cb.getText()); 284 } 285 } 286 } 287 288 class UpdateSuffixListAction extends AbstractAction { 289 GeneratedListModel listModel; 290 protected UpdateSuffixListAction(GeneratedListModel listModel) { 291 this.listModel = listModel; 292 } 293 294 public void actionPerformed(ActionEvent e) { 295 JCheckBox cb = (JCheckBox) e.getSource(); 296 if(cb.isSelected()) { 297 listModel.addSuffix(cb.getText()); 298 } else { 299 listModel.removeSuffix(cb.getText()); 300 } 301 } 302 } 303 304 305 class GeneratedListModel extends AbstractListModel { 306 ListDemo demo; 307 Permuter permuter; 308 309 public Vector prefix = new Vector(); 310 public Vector suffix = new Vector(); 311 312 public GeneratedListModel (ListDemo demo) { 313 this.demo = demo; 314 } 315 316 private void update() { 317 permuter = new Permuter(getSize()); 318 fireContentsChanged(this, 0, getSize()); 319 } 320 321 public void addPrefix(String s) { 322 if(!prefix.contains(s)) { 323 prefix.addElement(s); 324 update(); 325 } 326 } 327 328 public void removePrefix(String s) { 329 prefix.removeElement(s); 330 update(); 331 } 332 333 public void addSuffix(String s) { 334 if(!suffix.contains(s)) { 335 suffix.addElement(s); 336 update(); 337 } 338 } 339 340 public void removeSuffix(String s) { 341 suffix.removeElement(s); 342 update(); 343 } 344 345 public int getSize() { 346 return prefix.size() * suffix.size(); 347 } 348 349 public Object getElementAt(int index) { 350 if(permuter == null) { 351 update(); 352 } 353 int j = permuter.map(index); 356 int ps = prefix.size(); 357 int ss = suffix.size(); 358 return (String ) prefix.elementAt(j%ps) + (String ) suffix.elementAt((j/ps)%ss); 359 } 360 } 361 362 ImageIcon images[] = new ImageIcon[7]; 363 void loadImages() { 364 images[0] = createImageIcon("list/red.gif", getString("ListDemo.red")); 365 images[1] = createImageIcon("list/blue.gif", getString("ListDemo.blue")); 366 images[2] = createImageIcon("list/yellow.gif", getString("ListDemo.yellow")); 367 images[3] = createImageIcon("list/green.gif", getString("ListDemo.green")); 368 images[4] = createImageIcon("list/gray.gif", getString("ListDemo.gray")); 369 images[5] = createImageIcon("list/cyan.gif", getString("ListDemo.cyan")); 370 images[6] = createImageIcon("list/magenta.gif", getString("ListDemo.magenta")); 371 } 372 373 class CompanyLogoListCellRenderer extends DefaultListCellRenderer { 374 public Component getListCellRendererComponent( 375 JList list, 376 Object value, 377 int index, 378 boolean isSelected, 379 boolean cellHasFocus) 380 { 381 Component retValue = super.getListCellRendererComponent( 382 list, value, index, isSelected, cellHasFocus 383 ); 384 setIcon(images[index%7]); 385 return retValue; 386 } 387 } 388 } 389
| Popular Tags
|