ListDemo.java  
1   /*
2    * @(#)ListDemo.java    1.14 04/07/26
3    * 
4    * Copyright (c) 2004 Sun Microsystems, Inc. All Rights Reserved.
5    * 
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions are met:
8    * 
9    * -Redistribution of source code must retain the above copyright notice, this
10   *  list of conditions and the following disclaimer.
11   * 
12   * -Redistribution in binary form must reproduce the above copyright notice, 
13   *  this list of conditions and the following disclaimer in the documentation
14   *  and/or other materials provided with the distribution.
15   * 
16   * Neither the name of Sun Microsystems, Inc. or the names of contributors may 
17   * be used to endorse or promote products derived from this software without 
18   * specific prior written permission.
19   * 
20   * This software is provided "AS IS," without a warranty of any kind. ALL 
21   * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22   * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23   * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24   * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25   * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26   * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST 
27   * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, 
28   * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY 
29   * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, 
30   * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31   * 
32   * You acknowledge that this software is not designed, licensed or intended
33   * for use in the design, construction, operation or maintenance of any
34   * nuclear facility.
35   */
36  
37  /*
38   * @(#)ListDemo.java    1.14 04/07/26
39   */
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  /**
59   * List Demo. This demo shows that it is not
60   * always necessary to have an array of objects
61   * as big as the size of the list stored.
62   *
63   * Indeed, in this example, there is no array
64   * kept for the list data, rather it is generated
65   * on the fly as only those elements are needed.
66   *
67   * @version 1.14 07/26/04
68   * @author Jeff Dinkins
69   */
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      /**
84       * main method allows us to run as a standalone demo.
85       */
86      public static void main(String[] args) {
87      ListDemo demo = new ListDemo(null);
88      demo.mainImpl();
89      }
90  
91      /**
92       * ListDemo Constructor
93       */
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     // Create the list
115     list = new JList();
116     list.setCellRenderer(new CompanyLogoListCellRenderer());
117     listModel = new GeneratedListModel(this);
118     list.setModel(listModel);
119         
120     // Set the preferred row count. This affects the preferredSize
121     // of the JList when it's in a scrollpane.
122     list.setVisibleRowCount(22);
123 
124     // Add list to a scrollpane
125     JScrollPane scrollPane = new JScrollPane(list);
126     listPanel.add(scrollPane);
127     listPanel.add(Box.createRigidArea(VGAP10));
128 
129     // Add the control panel (holds the prefix/suffix list and prefix/suffix checkboxes)
130     JPanel controlPanel = createControlPanel();
131     centerPanel.add(createControlPanel());
132 
133 
134     // create prefixes and suffixes
135     addPrefix("Tera", true);  
136     addPrefix("Micro", false);     
137     addPrefix("Southern", false);       
138     addPrefix("Net", true);   
139     addPrefix("YoYo", true);       
140     addPrefix("Northern", false);       
141     addPrefix("Tele", false); 
142     addPrefix("Eastern", false);   
143     addPrefix("Neo", false);            
144     addPrefix("Digi", false); 
145     addPrefix("National", false);  
146     addPrefix("Compu", true);          
147     addPrefix("Meta", true);  
148     addPrefix("Info", false);      
149     addPrefix("Western", false);        
150     addPrefix("Data", false); 
151     addPrefix("Atlantic", false); 
152     addPrefix("Advanced", false);        
153     addPrefix("Euro", false);      
154     addPrefix("Pacific", false);   
155     addPrefix("Mobile", false);       
156     addPrefix("In", false);        
157     addPrefix("Computa", false);          
158     addPrefix("Digital", false);   
159     addPrefix("Analog", false);       
160 
161     addSuffix("Tech", true);      
162     addSuffix("Soft", true);      
163     addSuffix("Telecom", true);
164     addSuffix("Solutions", false); 
165     addSuffix("Works", true);     
166     addSuffix("Dyne", false);
167     addSuffix("Services", false);  
168     addSuffix("Vers", false);      
169     addSuffix("Devices", false);
170     addSuffix("Software", false);  
171     addSuffix("Serv", false);      
172     addSuffix("Systems", true);
173     addSuffix("Dynamics", true);  
174     addSuffix("Net", false);       
175     addSuffix("Sys", false);
176     addSuffix("Computing", false); 
177     addSuffix("Scape", false);     
178     addSuffix("Com", false);
179     addSuffix("Ware", false);      
180     addSuffix("Widgets", false);   
181     addSuffix("Media", false);     
182     addSuffix("Computer", false);
183     addSuffix("Hardware", false);  
184     addSuffix("Gizmos", false);    
185     addSuffix("Concepts", false);
186     }
187 
188     void updateDragEnabled(boolean dragEnabled) {
189         list.setDragEnabled(dragEnabled);
190     }
191 
192     public JPanel createControlPanel() {
193     JPanel controlPanel = new JPanel() {
194         Insets insets = new Insets(0, 4, 10, 10);
195         public Insets getInsets() {
196         return insets;
197         }
198     };
199     controlPanel.setLayout(new BoxLayout(controlPanel, BoxLayout.X_AXIS));
200 
201     JPanel prefixPanel = new JPanel();
202     prefixPanel.setLayout(new BoxLayout(prefixPanel, BoxLayout.Y_AXIS));
203     prefixPanel.add(new JLabel(getString("ListDemo.prefixes")));
204 
205     JPanel suffixPanel = new JPanel();
206     suffixPanel.setLayout(new BoxLayout(suffixPanel, BoxLayout.Y_AXIS));
207     suffixPanel.add(new JLabel(getString("ListDemo.suffixes")));
208 
209     prefixList = new JPanel() {
210         Insets insets = new Insets(0, 4, 0, 0);
211         public Insets getInsets() {
212         return insets;
213         }
214     };
215     prefixList.setLayout(new BoxLayout(prefixList, BoxLayout.Y_AXIS));
216     JScrollPane scrollPane = new JScrollPane(prefixList);
217         scrollPane.getVerticalScrollBar().setUnitIncrement(10);
218     prefixPanel.add(scrollPane);
219     prefixPanel.add(Box.createRigidArea(HGAP10));
220 
221     suffixList = new JPanel() {
222         Insets insets = new Insets(0, 4, 0, 0);
223         public Insets getInsets() {
224         return insets;
225         }
226     };
227     suffixList.setLayout(new BoxLayout(suffixList, BoxLayout.Y_AXIS));
228     scrollPane = new JScrollPane(suffixList);
229         scrollPane.getVerticalScrollBar().setUnitIncrement(10);
230     suffixPanel.add(scrollPane);
231     suffixPanel.add(Box.createRigidArea(HGAP10));
232 
233     controlPanel.add(prefixPanel);
234     controlPanel.add(Box.createRigidArea(HGAP15));
235     controlPanel.add(suffixPanel);
236     return controlPanel;
237     }
238 
239     public void addPrefix(String prefix, boolean selected) {
240     if(prefixAction == null) {
241         prefixAction = new UpdatePrefixListAction(listModel);
242     }
243     JCheckBox cb = (JCheckBox) prefixList.add(new JCheckBox(prefix));
244     checkboxes.addElement(cb);
245     cb.setSelected(selected);
246     cb.addActionListener(prefixAction);
247     if(selected) {
248         listModel.addPrefix(prefix);
249     }
250     }
251 
252     public void addSuffix(String suffix, boolean selected) {
253     if(suffixAction == null) {
254         suffixAction = new UpdateSuffixListAction(listModel);
255     }
256     JCheckBox cb = (JCheckBox) suffixList.add(new JCheckBox(suffix));
257     checkboxes.addElement(cb);
258     cb.setSelected(selected);
259     cb.addActionListener(suffixAction);
260     if(selected) {
261         listModel.addSuffix(suffix);
262     }
263     }
264 
265     class UpdatePrefixListAction extends AbstractAction {
266     GeneratedListModel listModel;
267         protected UpdatePrefixListAction(GeneratedListModel listModel) {
268         this.listModel = listModel;
269         }
270     
271         public void actionPerformed(ActionEvent e) {
272         JCheckBox cb = (JCheckBox) e.getSource();
273         if(cb.isSelected()) {
274         listModel.addPrefix(cb.getText());
275         } else {
276         listModel.removePrefix(cb.getText());
277         }
278     }
279     }
280 
281     class UpdateSuffixListAction extends AbstractAction {
282     GeneratedListModel listModel;
283         protected UpdateSuffixListAction(GeneratedListModel listModel) {
284         this.listModel = listModel;
285         }
286     
287         public void actionPerformed(ActionEvent e) {
288         JCheckBox cb = (JCheckBox) e.getSource();
289         if(cb.isSelected()) {
290         listModel.addSuffix(cb.getText());
291         } else {
292         listModel.removeSuffix(cb.getText());
293         }
294     }
295     }
296 
297     
298     class GeneratedListModel extends AbstractListModel {
299     ListDemo demo;
300     Permuter permuter;
301 
302     public Vector prefix = new Vector();
303     public Vector suffix = new Vector();
304 
305     public GeneratedListModel (ListDemo demo) {
306         this.demo = demo;
307     }
308 
309     private void update() {
310         permuter = new Permuter(getSize());
311         fireContentsChanged(this, 0, getSize());
312     }
313 
314     public void addPrefix(String s) {
315         if(!prefix.contains(s)) {
316         prefix.addElement(s);
317         update();
318         }
319     }
320 
321     public void removePrefix(String s) {
322         prefix.removeElement(s);
323         update();
324     }
325 
326     public void addSuffix(String s) {
327         if(!suffix.contains(s)) {
328         suffix.addElement(s);
329         update();
330         }
331     }
332 
333     public void removeSuffix(String s) {
334         suffix.removeElement(s);
335         update();
336     }
337 
338     public int getSize() {
339         return prefix.size() * suffix.size();
340     }
341 
342     public Object getElementAt(int index) {
343         if(permuter == null) {
344         update();
345         }
346         // morph the index to another int -- this has the benefit of
347         // causing the list to look random.
348         int j = permuter.map(index);
349         int ps = prefix.size();
350         int ss = suffix.size();
351         return (String) prefix.elementAt(j%ps) + (String) suffix.elementAt((j/ps)%ss);
352     }
353     }
354 
355     ImageIcon images[] = new ImageIcon[7];
356     void loadImages() {
357         images[0] = createImageIcon("list/red.gif",  getString("ListDemo.red"));
358         images[1] = createImageIcon("list/blue.gif",  getString("ListDemo.blue"));
359         images[2] = createImageIcon("list/yellow.gif",  getString("ListDemo.yellow"));
360         images[3] = createImageIcon("list/green.gif",  getString("ListDemo.green"));
361         images[4] = createImageIcon("list/gray.gif",  getString("ListDemo.gray"));
362         images[5] = createImageIcon("list/cyan.gif",  getString("ListDemo.cyan"));
363         images[6] = createImageIcon("list/magenta.gif",  getString("ListDemo.magenta"));
364     }
365 
366     class CompanyLogoListCellRenderer extends DefaultListCellRenderer {
367        public Component getListCellRendererComponent(
368             JList list,
369             Object value,
370             int index,
371             boolean isSelected,
372             boolean cellHasFocus)
373         {
374             Component retValue = super.getListCellRendererComponent(
375         list, value, index, isSelected, cellHasFocus
376         );
377             setIcon(images[index%7]);
378         return retValue;
379         }
380     }
381 
382 
383 }
384