42 import java.awt.*; 43 import java.awt.event.*; 44 import java.beans.*; 45 import java.util.*; 46 import java.io.*; 47 import java.applet.*; 48 import java.net.*; 49 50 /** 51 * List Demo. This demo shows that it is not 52 * always necessary to have an array of objects 53 * as big as the size of the list stored. 54 * 55 * Indeed, in this example, there is no array 56 * kept for the list data, rather it is generated 57 * on the fly as only those elements are needed. 58 * 59 * @author Jeff Dinkins 60 */ 61 public class ListDemo extends DemoModule { 62 JList list; 63 64 JPanel prefixList; 65 JPanel suffixList; 66 67 Action prefixAction; 68 Action suffixAction; 69 70 GeneratedListModel listModel; 71 72 Vector checkboxes = new Vector(); 73 74 /** 75 * main method allows us to run as a standalone demo. 76 */ 77 public static void main(String[] args) { 78 ListDemo demo = new ListDemo(null); 79 demo.mainImpl(); 80 } 81 82 /** 83 * ListDemo Constructor 84 */ 85 public ListDemo(SwingSet2 swingset) { 86 super(swingset, "ListDemo", "toolbar/JList.gif"); 87 88 loadImages(); 89 90 JLabel description = new JLabel(getString("ListDemo.description")); 91 getDemoPanel().add(description, BorderLayout.NORTH); 92 93 JPanel centerPanel = new JPanel(); 94 centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.X_AXIS)); 95 centerPanel.add(Box.createRigidArea(HGAP10)); 96 getDemoPanel().add(centerPanel, BorderLayout.CENTER); 97 98 JPanel listPanel = new JPanel(); 99 listPanel.setLayout(new BoxLayout(listPanel, BoxLayout.Y_AXIS)); 100 listPanel.add(Box.createRigidArea(VGAP10)); 101 102 centerPanel.add(listPanel); 103 centerPanel.add(Box.createRigidArea(HGAP30)); 104 105 // Create the list 106 list = new JList(); 107 list.setCellRenderer(new CompanyLogoListCellRenderer()); 108 listModel = new GeneratedListModel(this); 109 list.setModel(listModel); 110 111 // Set the preferred row count. This affects the preferredSize 112 // of the JList when it's in a scrollpane. 113 list.setVisibleRowCount(22); 114 115 // Add list to a scrollpane 116 JScrollPane scrollPane = new JScrollPane(list); 117 listPanel.add(scrollPane); 118 listPanel.add(Box.createRigidArea(VGAP10)); 119 120 // Add the control panel (holds the prefix/suffix list and prefix/suffix checkboxes) 121 centerPanel.add(createControlPanel()); 122 123 // create prefixes and suffixes 124 addPrefix("Tera", true); 125 addPrefix("Micro", false); 126 addPrefix("Southern", false); 276 } 277 } 278 279 class UpdateSuffixListAction extends AbstractAction { 280 GeneratedListModel listModel; 281 protected UpdateSuffixListAction(GeneratedListModel listModel) { 282 this.listModel = listModel; 283 } 284 285 public void actionPerformed(ActionEvent e) { 286 JCheckBox cb = (JCheckBox) e.getSource(); 287 if(cb.isSelected()) { 288 listModel.addSuffix(cb.getText()); 289 } else { 290 listModel.removeSuffix(cb.getText()); 291 } 292 } 293 } 294 295 296 class GeneratedListModel extends AbstractListModel { 297 ListDemo demo; 298 Permuter permuter; 299 300 public Vector prefix = new Vector(); 301 public Vector suffix = new Vector(); 302 303 public GeneratedListModel (ListDemo demo) { 304 this.demo = demo; 305 } 306 307 private void update() { 308 permuter = new Permuter(getSize()); 309 fireContentsChanged(this, 0, getSize()); 310 } 311 312 public void addPrefix(String s) { 313 if(!prefix.contains(s)) { 314 prefix.addElement(s); 315 update(); 316 } 317 } 318 319 public void removePrefix(String s) { 320 prefix.removeElement(s); 321 update(); 322 } 323 324 public void addSuffix(String s) { 325 if(!suffix.contains(s)) { 326 suffix.addElement(s); 327 update(); 328 } 329 } 330 331 public void removeSuffix(String s) { 332 suffix.removeElement(s); 333 update(); 334 } 335 336 public int getSize() { 337 return prefix.size() * suffix.size(); 338 } 339 340 public Object getElementAt(int index) { 341 if(permuter == null) { 342 update(); 343 } 344 // morph the index to another int -- this has the benefit of 345 // causing the list to look random. 346 int j = permuter.map(index); 347 int ps = prefix.size(); 348 int ss = suffix.size(); 349 return (String) prefix.elementAt(j%ps) + (String) suffix.elementAt((j/ps)%ss); 350 } 351 } 352 353 ImageIcon[] images = new ImageIcon[7]; 354 void loadImages() { 355 images[0] = createImageIcon("list/red.gif", getString("ListDemo.red")); 356 images[1] = createImageIcon("list/blue.gif", getString("ListDemo.blue")); 357 images[2] = createImageIcon("list/yellow.gif", getString("ListDemo.yellow")); 358 images[3] = createImageIcon("list/green.gif", getString("ListDemo.green")); 359 images[4] = createImageIcon("list/gray.gif", getString("ListDemo.gray")); 360 images[5] = createImageIcon("list/cyan.gif", getString("ListDemo.cyan")); 361 images[6] = createImageIcon("list/magenta.gif", getString("ListDemo.magenta")); 362 } 363 364 class CompanyLogoListCellRenderer extends DefaultListCellRenderer { 365 public Component getListCellRendererComponent( 366 JList list, 367 Object value, 368 int index, 369 boolean isSelected, 370 boolean cellHasFocus) 371 { 372 Component retValue = super.getListCellRendererComponent( 373 list, value, index, isSelected, cellHasFocus 374 ); 375 setIcon(images[index%7]); 376 return retValue; 377 } 378 } 379 } | 42 import java.awt.*; 43 import java.awt.event.*; 44 import java.beans.*; 45 import java.util.*; 46 import java.io.*; 47 import java.applet.*; 48 import java.net.*; 49 50 /** 51 * List Demo. This demo shows that it is not 52 * always necessary to have an array of objects 53 * as big as the size of the list stored. 54 * 55 * Indeed, in this example, there is no array 56 * kept for the list data, rather it is generated 57 * on the fly as only those elements are needed. 58 * 59 * @author Jeff Dinkins 60 */ 61 public class ListDemo extends DemoModule { 62 JList<String> list; 63 64 JPanel prefixList; 65 JPanel suffixList; 66 67 Action prefixAction; 68 Action suffixAction; 69 70 GeneratedListModel listModel; 71 72 Vector<JCheckBox> checkboxes = new Vector<>(); 73 74 /** 75 * main method allows us to run as a standalone demo. 76 */ 77 public static void main(String[] args) { 78 ListDemo demo = new ListDemo(null); 79 demo.mainImpl(); 80 } 81 82 /** 83 * ListDemo Constructor 84 */ 85 public ListDemo(SwingSet2 swingset) { 86 super(swingset, "ListDemo", "toolbar/JList.gif"); 87 88 loadImages(); 89 90 JLabel description = new JLabel(getString("ListDemo.description")); 91 getDemoPanel().add(description, BorderLayout.NORTH); 92 93 JPanel centerPanel = new JPanel(); 94 centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.X_AXIS)); 95 centerPanel.add(Box.createRigidArea(HGAP10)); 96 getDemoPanel().add(centerPanel, BorderLayout.CENTER); 97 98 JPanel listPanel = new JPanel(); 99 listPanel.setLayout(new BoxLayout(listPanel, BoxLayout.Y_AXIS)); 100 listPanel.add(Box.createRigidArea(VGAP10)); 101 102 centerPanel.add(listPanel); 103 centerPanel.add(Box.createRigidArea(HGAP30)); 104 105 // Create the list 106 list = new JList<>(); 107 list.setCellRenderer(new CompanyLogoListCellRenderer()); 108 listModel = new GeneratedListModel(this); 109 list.setModel(listModel); 110 111 // Set the preferred row count. This affects the preferredSize 112 // of the JList when it's in a scrollpane. 113 list.setVisibleRowCount(22); 114 115 // Add list to a scrollpane 116 JScrollPane scrollPane = new JScrollPane(list); 117 listPanel.add(scrollPane); 118 listPanel.add(Box.createRigidArea(VGAP10)); 119 120 // Add the control panel (holds the prefix/suffix list and prefix/suffix checkboxes) 121 centerPanel.add(createControlPanel()); 122 123 // create prefixes and suffixes 124 addPrefix("Tera", true); 125 addPrefix("Micro", false); 126 addPrefix("Southern", false); 276 } 277 } 278 279 class UpdateSuffixListAction extends AbstractAction { 280 GeneratedListModel listModel; 281 protected UpdateSuffixListAction(GeneratedListModel listModel) { 282 this.listModel = listModel; 283 } 284 285 public void actionPerformed(ActionEvent e) { 286 JCheckBox cb = (JCheckBox) e.getSource(); 287 if(cb.isSelected()) { 288 listModel.addSuffix(cb.getText()); 289 } else { 290 listModel.removeSuffix(cb.getText()); 291 } 292 } 293 } 294 295 296 class GeneratedListModel extends AbstractListModel<String> { 297 ListDemo demo; 298 Permuter permuter; 299 300 public Vector<String> prefix = new Vector<>(); 301 public Vector<String> suffix = new Vector<>(); 302 303 public GeneratedListModel (ListDemo demo) { 304 this.demo = demo; 305 } 306 307 private void update() { 308 permuter = new Permuter(getSize()); 309 fireContentsChanged(this, 0, getSize()); 310 } 311 312 public void addPrefix(String s) { 313 if(!prefix.contains(s)) { 314 prefix.addElement(s); 315 update(); 316 } 317 } 318 319 public void removePrefix(String s) { 320 prefix.removeElement(s); 321 update(); 322 } 323 324 public void addSuffix(String s) { 325 if(!suffix.contains(s)) { 326 suffix.addElement(s); 327 update(); 328 } 329 } 330 331 public void removeSuffix(String s) { 332 suffix.removeElement(s); 333 update(); 334 } 335 336 public int getSize() { 337 return prefix.size() * suffix.size(); 338 } 339 340 public String getElementAt(int index) { 341 if(permuter == null) { 342 update(); 343 } 344 // morph the index to another int -- this has the benefit of 345 // causing the list to look random. 346 int j = permuter.map(index); 347 int ps = prefix.size(); 348 int ss = suffix.size(); 349 return (String) prefix.elementAt(j%ps) + (String) suffix.elementAt((j/ps)%ss); 350 } 351 } 352 353 ImageIcon[] images = new ImageIcon[7]; 354 void loadImages() { 355 images[0] = createImageIcon("list/red.gif", getString("ListDemo.red")); 356 images[1] = createImageIcon("list/blue.gif", getString("ListDemo.blue")); 357 images[2] = createImageIcon("list/yellow.gif", getString("ListDemo.yellow")); 358 images[3] = createImageIcon("list/green.gif", getString("ListDemo.green")); 359 images[4] = createImageIcon("list/gray.gif", getString("ListDemo.gray")); 360 images[5] = createImageIcon("list/cyan.gif", getString("ListDemo.cyan")); 361 images[6] = createImageIcon("list/magenta.gif", getString("ListDemo.magenta")); 362 } 363 364 class CompanyLogoListCellRenderer extends DefaultListCellRenderer { 365 public Component getListCellRendererComponent( 366 JList<?> list, 367 Object value, 368 int index, 369 boolean isSelected, 370 boolean cellHasFocus) 371 { 372 Component retValue = super.getListCellRendererComponent( 373 list, value, index, isSelected, cellHasFocus 374 ); 375 setIcon(images[index%7]); 376 return retValue; 377 } 378 } 379 } |