KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > swingset > ListDemo


1 /*
2  * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * -Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * -Redistribution in binary form must reproduct the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the distribution.
14  *
15  * Neither the name of Sun Microsystems, Inc. or the names of contributors
16  * may be used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * This software is provided "AS IS," without a warranty of any kind. ALL
20  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
21  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
22  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
23  * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
24  * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
25  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
26  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
27  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
28  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
29  * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
30  *
31  * You acknowledge that Software is not designed, licensed or intended for
32  * use in the design, construction, operation or maintenance of any nuclear
33  * facility.
34  */

35
36 /*
37  * @(#)ListDemo.java 1.11 03/01/23
38  */

39 package demo.swingset;
40
41 import swingwtx.swing.*;
42 import swingwtx.swing.event.*;
43 import swingwtx.swing.text.*;
44 import swingwtx.swing.border.*;
45 import swingwtx.swing.colorchooser.*;
46 import swingwtx.swing.filechooser.*;
47 import javax.accessibility.*;
48
49 import swingwt.awt.*;
50 import swingwt.awt.event.*;
51 import java.beans.*;
52 import java.util.*;
53 import java.io.*;
54 import java.applet.*;
55 import java.net.*;
56
57 /**
58  * List Demo. This demo shows that it is not
59  * always necessary to have an array of objects
60  * as big as the size of the list stored.
61  *
62  * Indeed, in this example, there is no array
63  * kept for the list data, rather it is generated
64  * on the fly as only those elements are needed.
65  *
66  * @version 1.11 01/23/03
67  * @author Jeff Dinkins
68  */

69 public class ListDemo extends DemoModule {
70     JList list;
71
72     JPanel prefixList;
73     JPanel suffixList;
74
75     Action prefixAction;
76     Action suffixAction;
77
78     GeneratedListModel listModel;
79
80     Vector checkboxes = new Vector();
81
82     /**
83      * main method allows us to run as a standalone demo.
84      */

85     public static void main(String JavaDoc[] args) {
86     ListDemo demo = new ListDemo(null);
87     demo.mainImpl();
88     }
89
90     /**
91      * ListDemo Constructor
92      */

93     public ListDemo(SwingSet2 swingset) {
94     super(swingset, "ListDemo", "toolbar/JList.gif");
95
96     loadImages();
97
98     JLabel description = new JLabel(getString("ListDemo.description"));
99     getDemoPanel().add(description, BorderLayout.NORTH);
100
101     JPanel centerPanel = new JPanel();
102     centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.X_AXIS));
103     centerPanel.add(Box.createRigidArea(HGAP10));
104     getDemoPanel().add(centerPanel, BorderLayout.CENTER);
105
106     JPanel listPanel = new JPanel();
107     listPanel.setLayout(new BoxLayout(listPanel, BoxLayout.Y_AXIS));
108     listPanel.add(Box.createRigidArea(VGAP10));
109
110     centerPanel.add(listPanel);
111     centerPanel.add(Box.createRigidArea(HGAP30));
112
113     // Create the list
114
list = new JList();
115     list.setCellRenderer(new CompanyLogoListCellRenderer());
116     listModel = new GeneratedListModel(this);
117     list.setModel(listModel);
118
119     // Set the preferred row count. This affects the preferredSize
120
// of the JList when it's in a scrollpane.
121
list.setVisibleRowCount(22);
122
123     // Add list to a scrollpane
124
JScrollPane scrollPane = new JScrollPane(list);
125     listPanel.add(scrollPane);
126     listPanel.add(Box.createRigidArea(VGAP10));
127
128     // Add the control panel (holds the prefix/suffix list and prefix/suffix checkboxes)
129
JPanel controlPanel = createControlPanel();
130     centerPanel.add(createControlPanel());
131
132
133     // create prefixes and suffixes
134
addPrefix("Tera", true);
135     addPrefix("Micro", false);
136     addPrefix("Southern", false);
137     addPrefix("Net", true);
138     addPrefix("YoYo", true);
139     addPrefix("Northern", false);
140     addPrefix("Tele", false);
141     addPrefix("Eastern", false);
142     addPrefix("Neo", false);
143     addPrefix("Digi", false);
144     addPrefix("National", false);
145     addPrefix("Compu", true);
146     addPrefix("Meta", true);
147     addPrefix("Info", false);
148     addPrefix("Western", false);
149     addPrefix("Data", false);
150     addPrefix("Atlantic", false);
151     addPrefix("Advanced", false);
152     addPrefix("Euro", false);
153     addPrefix("Pacific", false);
154     addPrefix("Mobile", false);
155     addPrefix("In", false);
156     addPrefix("Computa", false);
157     addPrefix("Digital", false);
158     addPrefix("Analog", false);
159
160     addSuffix("Tech", true);
161     addSuffix("Soft", true);
162     addSuffix("Telecom", true);
163     addSuffix("Solutions", false);
164     addSuffix("Works", true);
165     addSuffix("Dyne", false);
166     addSuffix("Services", false);
167     addSuffix("Vers", false);
168     addSuffix("Devices", false);
169     addSuffix("Software", false);
170     addSuffix("Serv", false);
171     addSuffix("Systems", true);
172     addSuffix("Dynamics", true);
173     addSuffix("Net", false);
174     addSuffix("Sys", false);
175     addSuffix("Computing", false);
176     addSuffix("Scape", false);
177     addSuffix("Com", false);
178     addSuffix("Ware", false);
179     addSuffix("Widgets", false);
180     addSuffix("Media", false);
181     addSuffix("Computer", false);
182     addSuffix("Hardware", false);
183     addSuffix("Gizmos", false);
184     addSuffix("Concepts", false);
185     }
186
187     public JPanel createControlPanel() {
188     JPanel controlPanel = new JPanel() {
189         Insets insets = new Insets(0, 4, 10, 10);
190         public Insets getInsets() {
191         return insets;
192         }
193     };
194     controlPanel.setLayout(new BoxLayout(controlPanel, BoxLayout.X_AXIS));
195
196     JPanel prefixPanel = new JPanel();
197     prefixPanel.setLayout(new BoxLayout(prefixPanel, BoxLayout.Y_AXIS));
198     prefixPanel.add(new JLabel(getString("ListDemo.prefixes")));
199
200     JPanel suffixPanel = new JPanel();
201     suffixPanel.setLayout(new BoxLayout(suffixPanel, BoxLayout.Y_AXIS));
202     suffixPanel.add(new JLabel(getString("ListDemo.suffixes")));
203
204     prefixList = new JPanel() {
205         Insets insets = new Insets(0, 4, 0, 0);
206         public Insets getInsets() {
207         return insets;
208         }
209     };
210     prefixList.setLayout(new BoxLayout(prefixList, BoxLayout.Y_AXIS));
211     JScrollPane scrollPane = new JScrollPane(prefixList);
212         scrollPane.getVerticalScrollBar().setUnitIncrement(10);
213     prefixPanel.add(scrollPane);
214     prefixPanel.add(Box.createRigidArea(HGAP10));
215
216     suffixList = new JPanel() {
217         Insets insets = new Insets(0, 4, 0, 0);
218         public Insets getInsets() {
219         return insets;
220         }
221     };
222     suffixList.setLayout(new BoxLayout(suffixList, BoxLayout.Y_AXIS));
223     scrollPane = new JScrollPane(suffixList);
224         scrollPane.getVerticalScrollBar().setUnitIncrement(10);
225     suffixPanel.add(scrollPane);
226     suffixPanel.add(Box.createRigidArea(HGAP10));
227
228     controlPanel.add(prefixPanel);
229     controlPanel.add(Box.createRigidArea(HGAP15));
230     controlPanel.add(suffixPanel);
231     return controlPanel;
232     }
233
234     public void addPrefix(String JavaDoc prefix, boolean selected) {
235     if(prefixAction == null) {
236         prefixAction = new UpdatePrefixListAction(listModel);
237     }
238     JCheckBox cb = (JCheckBox) prefixList.add(new JCheckBox(prefix));
239     checkboxes.addElement(cb);
240     cb.setSelected(selected);
241     cb.addActionListener(prefixAction);
242     if(selected) {
243         listModel.addPrefix(prefix);
244     }
245     }
246
247     public void addSuffix(String JavaDoc suffix, boolean selected) {
248     if(suffixAction == null) {
249         suffixAction = new UpdateSuffixListAction(listModel);
250     }
251     JCheckBox cb = (JCheckBox) suffixList.add(new JCheckBox(suffix));
252     checkboxes.addElement(cb);
253     cb.setSelected(selected);
254     cb.addActionListener(suffixAction);
255     if(selected) {
256         listModel.addSuffix(suffix);
257     }
258     }
259
260     class UpdatePrefixListAction extends AbstractAction {
261     GeneratedListModel listModel;
262         protected UpdatePrefixListAction(GeneratedListModel listModel) {
263         this.listModel = listModel;
264         }
265     
266         public void actionPerformed(ActionEvent e) {
267         JCheckBox cb = (JCheckBox) e.getSource();
268         if(cb.isSelected()) {
269         listModel.addPrefix(cb.getText());
270         } else {
271         listModel.removePrefix(cb.getText());
272         }
273     }
274     }
275
276     class UpdateSuffixListAction extends AbstractAction {
277     GeneratedListModel listModel;
278         protected UpdateSuffixListAction(GeneratedListModel listModel) {
279         this.listModel = listModel;
280         }
281     
282         public void actionPerformed(ActionEvent e) {
283         JCheckBox cb = (JCheckBox) e.getSource();
284         if(cb.isSelected()) {
285         listModel.addSuffix(cb.getText());
286         } else {
287         listModel.removeSuffix(cb.getText());
288         }
289     }
290     }
291
292     
293     class GeneratedListModel extends AbstractListModel {
294     ListDemo demo;
295     Permuter permuter;
296
297     public Vector prefix = new Vector();
298     public Vector suffix = new Vector();
299
300     public GeneratedListModel (ListDemo demo) {
301         this.demo = demo;
302     }
303
304     private void update() {
305         permuter = new Permuter(getSize());
306         fireContentsChanged(this, 0, getSize());
307     }
308
309     public void addPrefix(String JavaDoc s) {
310         if(!prefix.contains(s)) {
311         prefix.addElement(s);
312         update();
313         }
314     }
315
316     public void removePrefix(String JavaDoc s) {
317         prefix.removeElement(s);
318         update();
319     }
320
321     public void addSuffix(String JavaDoc s) {
322         if(!suffix.contains(s)) {
323         suffix.addElement(s);
324         update();
325         }
326     }
327
328     public void removeSuffix(String JavaDoc s) {
329         suffix.removeElement(s);
330         update();
331     }
332
333     public int getSize() {
334         return prefix.size() * suffix.size();
335     }
336
337     public Object JavaDoc getElementAt(int index) {
338         if(permuter == null) {
339         update();
340         }
341         // morph the index to another int -- this has the benefit of
342
// causing the list to look random.
343
int j = permuter.map(index);
344         int ps = prefix.size();
345         int ss = suffix.size();
346         return (String JavaDoc) prefix.elementAt(j%ps) + (String JavaDoc) suffix.elementAt((j/ps)%ss);
347     }
348     }
349
350     ImageIcon images[] = new ImageIcon[7];
351     void loadImages() {
352         images[0] = createImageIcon("list/red.gif", getString("ListDemo.red"));
353         images[1] = createImageIcon("list/blue.gif", getString("ListDemo.blue"));
354         images[2] = createImageIcon("list/yellow.gif", getString("ListDemo.yellow"));
355         images[3] = createImageIcon("list/green.gif", getString("ListDemo.green"));
356         images[4] = createImageIcon("list/gray.gif", getString("ListDemo.gray"));
357         images[5] = createImageIcon("list/cyan.gif", getString("ListDemo.cyan"));
358         images[6] = createImageIcon("list/magenta.gif", getString("ListDemo.magenta"));
359     }
360
361     class CompanyLogoListCellRenderer extends DefaultListCellRenderer {
362        public Component getListCellRendererComponent(
363             JList list,
364             Object JavaDoc value,
365             int index,
366             boolean isSelected,
367             boolean cellHasFocus)
368         {
369             Component retValue = super.getListCellRendererComponent(
370         list, value, index, isSelected, cellHasFocus
371         );
372             setIcon(images[index%7]);
373         return retValue;
374         }
375     }
376
377
378 }
379
Popular Tags