KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ListDemo


1 /*
2  * @(#)ListDemo.java 1.17 05/11/17
3  *
4  * Copyright (c) 2006 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.17 05/11/17
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.17 11/17/05
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 JavaDoc[] 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
centerPanel.add(createControlPanel());
131
132     // create prefixes and suffixes
133
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 JavaDoc 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 JavaDoc 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 JavaDoc s) {
322         if(!prefix.contains(s)) {
323         prefix.addElement(s);
324         update();
325         }
326     }
327
328     public void removePrefix(String JavaDoc s) {
329         prefix.removeElement(s);
330         update();
331     }
332
333     public void addSuffix(String JavaDoc s) {
334         if(!suffix.contains(s)) {
335         suffix.addElement(s);
336         update();
337         }
338     }
339
340     public void removeSuffix(String JavaDoc s) {
341         suffix.removeElement(s);
342         update();
343     }
344
345     public int getSize() {
346         return prefix.size() * suffix.size();
347     }
348
349     public Object JavaDoc getElementAt(int index) {
350         if(permuter == null) {
351         update();
352         }
353         // morph the index to another int -- this has the benefit of
354
// causing the list to look random.
355
int j = permuter.map(index);
356         int ps = prefix.size();
357         int ss = suffix.size();
358         return (String JavaDoc) prefix.elementAt(j%ps) + (String JavaDoc) 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 JavaDoc 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