KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nextapp > echo2 > testapp > interactive > testscreen > ListBoxTest


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package nextapp.echo2.testapp.interactive.testscreen;
31
32 import nextapp.echo2.app.Border;
33 import nextapp.echo2.app.Color;
34 import nextapp.echo2.app.Column;
35 import nextapp.echo2.app.Component;
36 import nextapp.echo2.app.Extent;
37 import nextapp.echo2.app.Font;
38 import nextapp.echo2.app.Grid;
39 import nextapp.echo2.app.Insets;
40 import nextapp.echo2.app.Label;
41 import nextapp.echo2.app.ListBox;
42 import nextapp.echo2.app.SelectField;
43 import nextapp.echo2.app.SplitPane;
44 import nextapp.echo2.app.event.ActionEvent;
45 import nextapp.echo2.app.event.ActionListener;
46 import nextapp.echo2.app.event.ChangeEvent;
47 import nextapp.echo2.app.event.ChangeListener;
48 import nextapp.echo2.app.event.ListDataEvent;
49 import nextapp.echo2.app.event.ListDataListener;
50 import nextapp.echo2.app.layout.SplitPaneLayoutData;
51 import nextapp.echo2.app.list.AbstractListComponent;
52 import nextapp.echo2.app.list.DefaultListModel;
53 import nextapp.echo2.app.list.ListCellRenderer;
54 import nextapp.echo2.app.list.ListSelectionModel;
55 import nextapp.echo2.app.list.StyledListCell;
56 import nextapp.echo2.testapp.interactive.ButtonColumn;
57 import nextapp.echo2.testapp.interactive.InteractiveApp;
58 import nextapp.echo2.testapp.interactive.StyleUtil;
59
60 /**
61  * An interactive test for <code>ListBox</code>es.
62  */

63 public class ListBoxTest extends SplitPane {
64     
65     public static final String JavaDoc[] NUMBERS = new String JavaDoc[] { "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",
66                 "Nine", "Ten" };
67
68     /**
69      * Interface used to apply style information to all test components.
70      */

71     private interface Applicator {
72         
73         /**
74          * Applies style information.
75          *
76          * @param listComponent the target list component.
77          */

78         public void apply(AbstractListComponent listComponent);
79     }
80     
81     /**
82      * Writes <code>ActionEvent</code>s to console.
83      */

84     private ActionListener actionListener = new ActionListener() {
85
86         /**
87          * @see nextapp.echo2.app.event.ActionListener#actionPerformed(nextapp.echo2.app.event.ActionEvent)
88          */

89         public void actionPerformed(ActionEvent e) {
90             ((InteractiveApp) getApplicationInstance()).consoleWrite(e.toString());
91         }
92     };
93     
94     /**
95      * Writes <code>ChangeEvent</code>s to console.
96      */

97     private ChangeListener changeListener = new ChangeListener() {
98
99         /**
100          * @see nextapp.echo2.app.event.ChangeListener#stateChanged(nextapp.echo2.app.event.ChangeEvent)
101          */

102         public void stateChanged(ChangeEvent e) {
103             ((InteractiveApp) getApplicationInstance()).consoleWrite(e.toString());
104         }
105     };
106     
107     /**
108      * Writes <code>ListDataListener</code>s to console.
109      */

110     private ListDataListener listDataListener = new ListDataListener() {
111         
112         /**
113          * @see nextapp.echo2.app.event.ListDataListener#contentsChanged(nextapp.echo2.app.event.ListDataEvent)
114          */

115         public void contentsChanged(ListDataEvent e) {
116             ((InteractiveApp) getApplicationInstance()).consoleWrite(e.toString());
117         }
118
119         /**
120          * @see nextapp.echo2.app.event.ListDataListener#intervalAdded(nextapp.echo2.app.event.ListDataEvent)
121          */

122         public void intervalAdded(ListDataEvent e) {
123             ((InteractiveApp) getApplicationInstance()).consoleWrite(e.toString());
124         }
125
126         /**
127          * @see nextapp.echo2.app.event.ListDataListener#intervalRemoved(nextapp.echo2.app.event.ListDataEvent)
128          */

129         public void intervalRemoved(ListDataEvent e) {
130             ((InteractiveApp) getApplicationInstance()).consoleWrite(e.toString());
131         }
132     };
133     
134     private ListCellRenderer evenOddListCellRenderer = new ListCellRenderer(){
135     
136         private Color foreground1 = new Color(0x007f00);
137         private Color background1 = new Color(0xafffaf);
138         private Color foreground2 = new Color(0x7f0000);
139         private Color background2 = new Color(0xffafaf);
140         private Font font1 = new Font(Font.MONOSPACE, Font.BOLD, null);
141         
142         /**
143          * @see nextapp.echo2.app.list.ListCellRenderer#getListCellRendererComponent(nextapp.echo2.app.Component,
144          * java.lang.Object, int)
145          */

146         public Object JavaDoc getListCellRendererComponent(Component list, final Object JavaDoc value, final int index) {
147             return new StyledListCell() {
148             
149                 public Color getForeground() {
150                     return index % 2 == 0 ? foreground1 : foreground2;
151                 }
152             
153                 public Font getFont() {
154                     return index % 2 == 0 ? font1 : null;
155                 }
156             
157                 public Color getBackground() {
158                     return index % 2 == 0 ? background1 : background2;
159                 }
160                 
161                 public String JavaDoc toString() {
162                     return value == null ? null : value.toString();
163                 }
164             };
165         }
166     };
167     
168     private Column testColumn;
169     private ListBox listBox1, listBox2;
170     private SelectField selectField1, selectField2;
171
172     public ListBoxTest() {
173         super(SplitPane.ORIENTATION_HORIZONTAL, new Extent(250, Extent.PX));
174         setStyleName("DefaultResizable");
175
176         SplitPaneLayoutData splitPaneLayoutData;
177
178         ButtonColumn controlsColumn = new ButtonColumn();
179         controlsColumn.setStyleName("TestControlsColumn");
180         add(controlsColumn);
181
182         testColumn = new Column();
183         testColumn.setCellSpacing(new Extent(15));
184         splitPaneLayoutData = new SplitPaneLayoutData();
185         splitPaneLayoutData.setInsets(new Insets(15));
186         testColumn.setLayoutData(splitPaneLayoutData);
187         add(testColumn);
188
189         listBox1 = new ListBox(NUMBERS);
190         testColumn.add(listBox1);
191
192         selectField1 = new SelectField(NUMBERS);
193         testColumn.add(selectField1);
194         
195         Grid grid = new Grid();
196         grid.setBorder(new Border(1, Color.BLACK, Border.STYLE_SOLID));
197         testColumn.add(grid);
198         
199         selectField2 = new SelectField(NUMBERS);
200         grid.add(selectField2);
201         
202         listBox2 = new ListBox(NUMBERS);
203         grid.add(listBox2);
204         
205         controlsColumn.add(new Label("Global"));
206
207         controlsColumn.addButton("Add ActionListener", new ActionListener() {
208             public void actionPerformed(ActionEvent e) {
209                 apply(new Applicator() {
210                     public void apply(AbstractListComponent listComponent) {
211                         listComponent.addActionListener(actionListener);
212                     }
213                 });
214             }
215         });
216         controlsColumn.addButton("Remove ActionListener", new ActionListener() {
217             public void actionPerformed(ActionEvent e) {
218                 apply(new Applicator() {
219                     public void apply(AbstractListComponent listComponent) {
220                         listComponent.removeActionListener(actionListener);
221                     }
222                 });
223             }
224         });
225         controlsColumn.addButton("Add ChangeListener", new ActionListener() {
226             public void actionPerformed(ActionEvent e) {
227                 apply(new Applicator() {
228                     public void apply(AbstractListComponent listComponent) {
229                         listComponent.getSelectionModel().addChangeListener(changeListener);
230                     }
231                 });
232             }
233         });
234         controlsColumn.addButton("Remove ChangeListener", new ActionListener() {
235             public void actionPerformed(ActionEvent e) {
236                 apply(new Applicator() {
237                     public void apply(AbstractListComponent listComponent) {
238                         listComponent.getSelectionModel().removeChangeListener(changeListener);
239                     }
240                 });
241             }
242         });
243         controlsColumn.addButton("Add ListDataListener", new ActionListener() {
244             public void actionPerformed(ActionEvent e) {
245                 apply(new Applicator() {
246                     public void apply(AbstractListComponent listComponent) {
247                         listComponent.getModel().addListDataListener(listDataListener);
248                     }
249                 });
250             }
251         });
252         controlsColumn.addButton("Remove ListDataListener", new ActionListener() {
253             public void actionPerformed(ActionEvent e) {
254                 apply(new Applicator() {
255                     public void apply(AbstractListComponent listComponent) {
256                         listComponent.getModel().removeListDataListener(listDataListener);
257                     }
258                 });
259             }
260         });
261         controlsColumn.addButton("Toggle Enabled State", new ActionListener() {
262             public void actionPerformed(ActionEvent e) {
263                 apply(new Applicator() {
264                     public void apply(AbstractListComponent listComponent) {
265                         listComponent.setEnabled(!listComponent.isEnabled());
266                     }
267                 });
268             }
269         });
270         controlsColumn.addButton("Set ListCellRenderer", new ActionListener() {
271             public void actionPerformed(ActionEvent e) {
272                 apply(new Applicator() {
273                     public void apply(AbstractListComponent listComponent) {
274                         listComponent.setCellRenderer(evenOddListCellRenderer);
275                     }
276                 });
277             }
278         });
279         controlsColumn.addButton("Clear ListCellRenderer", new ActionListener() {
280             public void actionPerformed(ActionEvent e) {
281                 apply(new Applicator() {
282                     public void apply(AbstractListComponent listComponent) {
283                         listComponent.setCellRenderer(AbstractListComponent.DEFAULT_LIST_CELL_RENDERER);
284                     }
285                 });
286             }
287         });
288         controlsColumn.addButton("Set Border", new ActionListener() {
289             public void actionPerformed(ActionEvent e) {
290                 final Border border = StyleUtil.randomBorder();
291                 apply(new Applicator() {
292                     public void apply(AbstractListComponent listComponent) {
293                         listComponent.setBorder(border);
294                     }
295                 });
296             }
297         });
298         controlsColumn.addButton("Clear Border", new ActionListener() {
299             public void actionPerformed(ActionEvent e) {
300                 apply(new Applicator() {
301                     public void apply(AbstractListComponent listComponent) {
302                         listComponent.setBorder(null);
303                     }
304                 });
305             }
306         });
307         controlsColumn.addButton("Set Foreground", new ActionListener() {
308             public void actionPerformed(ActionEvent e) {
309                 final Color color = StyleUtil.randomColor();
310                 apply(new Applicator() {
311                     public void apply(AbstractListComponent listComponent) {
312                         listComponent.setForeground(color);
313                     }
314                 });
315             }
316         });
317         controlsColumn.addButton("Set Background", new ActionListener() {
318             public void actionPerformed(ActionEvent e) {
319                 final Color color = StyleUtil.randomColor();
320                 apply(new Applicator() {
321                     public void apply(AbstractListComponent listComponent) {
322                         listComponent.setBackground(color);
323                     }
324                 });
325             }
326         });
327         controlsColumn.addButton("Set Font", new ActionListener() {
328             public void actionPerformed(ActionEvent e) {
329                 final Font font = StyleUtil.randomFont();
330                 apply(new Applicator() {
331                     public void apply(AbstractListComponent listComponent) {
332                         listComponent.setFont(font);
333                     }
334                 });
335             }
336         });
337         controlsColumn.addButton("Clear Font", new ActionListener() {
338             public void actionPerformed(ActionEvent e) {
339                 apply(new Applicator() {
340                     public void apply(AbstractListComponent listComponent) {
341                         listComponent.setFont(null);
342                     }
343                 });
344             }
345         });
346         controlsColumn.addButton("Set Disabled Border", new ActionListener() {
347             public void actionPerformed(ActionEvent e) {
348                 final Border border = StyleUtil.randomBorder();
349                 apply(new Applicator() {
350                     public void apply(AbstractListComponent listComponent) {
351                         listComponent.setDisabledBorder(border);
352                     }
353                 });
354             }
355         });
356         controlsColumn.addButton("Clear Disabled Border", new ActionListener() {
357             public void actionPerformed(ActionEvent e) {
358                 apply(new Applicator() {
359                     public void apply(AbstractListComponent listComponent) {
360                         listComponent.setDisabledBorder(null);
361                     }
362                 });
363             }
364         });
365         controlsColumn.addButton("Set Disabled Foreground", new ActionListener() {
366             public void actionPerformed(ActionEvent e) {
367                 final Color color = StyleUtil.randomColor();
368                 apply(new Applicator() {
369                     public void apply(AbstractListComponent listComponent) {
370                         listComponent.setDisabledForeground(color);
371                     }
372                 });
373             }
374         });
375         controlsColumn.addButton("Set Disabled Background", new ActionListener() {
376             public void actionPerformed(ActionEvent e) {
377                 final Color color = StyleUtil.randomColor();
378                 apply(new Applicator() {
379                     public void apply(AbstractListComponent listComponent) {
380                         listComponent.setDisabledBackground(color);
381                     }
382                 });
383             }
384         });
385         controlsColumn.addButton("Set Disabled Font", new ActionListener() {
386             public void actionPerformed(ActionEvent e) {
387                 final Font font = StyleUtil.randomFont();
388                 apply(new Applicator() {
389                     public void apply(AbstractListComponent listComponent) {
390                         listComponent.setDisabledFont(font);
391                     }
392                 });
393             }
394         });
395         controlsColumn.addButton("Clear Disabled Font", new ActionListener() {
396             public void actionPerformed(ActionEvent e) {
397                 apply(new Applicator() {
398                     public void apply(AbstractListComponent listComponent) {
399                         listComponent.setDisabledFont(null);
400                     }
401                 });
402             }
403         });
404         controlsColumn.addButton("Toggle ToolTip Text", new ActionListener(){
405             public void actionPerformed(ActionEvent e) {
406                 apply(new Applicator() {
407                     public void apply(AbstractListComponent listComponent) {
408                         if (listComponent.getToolTipText() == null) {
409                             listComponent.setToolTipText("This is a tool tip.");
410                         } else {
411                             listComponent.setToolTipText(null);
412                         }
413                     }
414                 });
415             }
416         });
417         controlsColumn.addButton("Enable Rollover Effects", new ActionListener() {
418             public void actionPerformed(ActionEvent e) {
419                 apply(new Applicator() {
420                     public void apply(AbstractListComponent listComponent) {
421                         listComponent.setRolloverEnabled(true);
422                     }
423                 });
424             }
425         });
426         controlsColumn.addButton("Disable Rollover Effects", new ActionListener() {
427             public void actionPerformed(ActionEvent e) {
428                 apply(new Applicator() {
429                     public void apply(AbstractListComponent listComponent) {
430                         listComponent.setRolloverEnabled(false);
431                     }
432                 });
433             }
434         });
435         controlsColumn.addButton("Set Rollover Foreground", new ActionListener() {
436             public void actionPerformed(ActionEvent e) {
437                 final Color color = StyleUtil.randomColor();
438                 apply(new Applicator() {
439                     public void apply(AbstractListComponent listComponent) {
440                         listComponent.setRolloverForeground(color);
441                     }
442                 });
443             }
444         });
445         controlsColumn.addButton("Set Rollover Background", new ActionListener() {
446             public void actionPerformed(ActionEvent e) {
447                 final Color color = StyleUtil.randomColor();
448                 apply(new Applicator() {
449                     public void apply(AbstractListComponent listComponent) {
450                         listComponent.setRolloverBackground(color);
451                     }
452                 });
453             }
454         });
455         controlsColumn.addButton("Set Rollover Font", new ActionListener() {
456             public void actionPerformed(ActionEvent e) {
457                 final Font font = StyleUtil.randomFont();
458                 apply(new Applicator() {
459                     public void apply(AbstractListComponent listComponent) {
460                         listComponent.setRolloverFont(font);
461                     }
462                 });
463             }
464         });
465         controlsColumn.addButton("Increase Width (15 px)", new ActionListener() {
466             public void actionPerformed(ActionEvent e) {
467                 final Extent width = listBox1.getWidth() == null ? new Extent(75) : listBox1.getWidth();
468                 apply(new Applicator() {
469                     public void apply(AbstractListComponent listComponent) {
470                         listComponent.setWidth(Extent.add(width, new Extent(15)));
471                     }
472                 });
473             }
474         });
475         controlsColumn.addButton("Decrease Width (15 px)", new ActionListener() {
476             public void actionPerformed(ActionEvent e) {
477                 final Extent width = listBox1.getWidth() == null ? new Extent(75) : listBox1.getWidth();
478                 apply(new Applicator() {
479                     public void apply(AbstractListComponent listComponent) {
480                         listComponent.setWidth(Extent.add(width, new Extent(-15)));
481                     }
482                 });
483             }
484         });
485         controlsColumn.addButton("Increase Height (15 px)", new ActionListener() {
486             public void actionPerformed(ActionEvent e) {
487                 final Extent height = listBox1.getHeight() == null ? new Extent(75) : listBox1.getHeight();
488                 apply(new Applicator() {
489                     public void apply(AbstractListComponent listComponent) {
490                         listComponent.setHeight(Extent.add(height, new Extent(15)));
491                     }
492                 });
493             }
494         });
495         controlsColumn.addButton("Decrease Height (15 px)", new ActionListener() {
496             public void actionPerformed(ActionEvent e) {
497                 final Extent height = listBox1.getHeight() == null ? new Extent(75) : listBox1.getHeight();
498                 apply(new Applicator() {
499                     public void apply(AbstractListComponent listComponent) {
500                         listComponent.setHeight(Extent.add(height, new Extent(-15)));
501                     }
502                 });
503             }
504         });
505
506         controlsColumn.addButton("Select Index 0", new ActionListener() {
507             public void actionPerformed(ActionEvent e) {
508                 apply(new Applicator(){
509                     public void apply(AbstractListComponent listComponent) {
510                         if (listComponent instanceof ListBox) {
511                             ((ListBox) listComponent).setSelectedIndices(new int[] {0});
512                         } else if (listComponent instanceof SelectField) {
513                             ((SelectField) listComponent).setSelectedIndex(0);
514                         }
515                     }
516                 });
517             }
518         });
519         
520         controlsColumn.addButton("Select Index 2", new ActionListener() {
521             public void actionPerformed(ActionEvent e) {
522                 apply(new Applicator(){
523                     public void apply(AbstractListComponent listComponent) {
524                         if (listComponent instanceof ListBox) {
525                             ((ListBox) listComponent).setSelectedIndices(new int[] {2});
526                         } else if (listComponent instanceof SelectField) {
527                             ((SelectField) listComponent).setSelectedIndex(2);
528                         }
529                     }
530                 });
531             }
532         });
533         
534         controlsColumn.addButton("Select Index 1502", new ActionListener() {
535             public void actionPerformed(ActionEvent e) {
536                 apply(new Applicator(){
537                     public void apply(AbstractListComponent listComponent) {
538                         if (listComponent instanceof ListBox) {
539                             ((ListBox) listComponent).setSelectedIndices(new int[] {1502});
540                         } else if (listComponent instanceof SelectField) {
541                             ((SelectField) listComponent).setSelectedIndex(1502);
542                         }
543                     }
544                 });
545             }
546         });
547         
548         controlsColumn.addButton("Clear Selections", new ActionListener() {
549             public void actionPerformed(ActionEvent e) {
550                 apply(new Applicator(){
551                     public void apply(AbstractListComponent listComponent) {
552                         if (listComponent instanceof ListBox) {
553                             ((ListBox) listComponent).setSelectedIndices(new int[] {});
554                         } else if (listComponent instanceof SelectField) {
555                             ((SelectField) listComponent).setSelectedIndex(-1);
556                         }
557                     }
558                 });
559             }
560         });
561         
562         controlsColumn.addButton("Empty ListModel", new ActionListener() {
563             public void actionPerformed(ActionEvent e) {
564                 apply(new Applicator(){
565                     public void apply(AbstractListComponent listComponent) {
566                         listComponent.setModel(new DefaultListModel());
567                     }
568                 });
569             }
570         });
571         
572         controlsColumn.addButton("Set ListModel = Numbers", new ActionListener() {
573             public void actionPerformed(ActionEvent e) {
574                 apply(new Applicator(){
575                     public void apply(AbstractListComponent listComponent) {
576                         listComponent.setModel(new DefaultListModel(NUMBERS));
577                     }
578                 });
579             }
580         });
581         controlsColumn.addButton("Focus SelectField1", new ActionListener() {
582             public void actionPerformed(ActionEvent e) {
583                 getApplicationInstance().setFocusedComponent(selectField1);
584             }
585         });
586         controlsColumn.addButton("Focus SelectField2", new ActionListener() {
587             public void actionPerformed(ActionEvent e) {
588                 getApplicationInstance().setFocusedComponent(selectField2);
589             }
590         });
591         
592         controlsColumn.add(new Label("ListBox-specific"));
593         
594         controlsColumn.addButton("Toggle Multiple Select", new ActionListener() {
595             public void actionPerformed(ActionEvent e) {
596                 final int mode = ListSelectionModel.MULTIPLE_SELECTION == listBox1.getSelectionMode()
597                         ? ListSelectionModel.SINGLE_SELECTION : ListSelectionModel.MULTIPLE_SELECTION;
598                 apply(new Applicator() {
599                     public void apply(AbstractListComponent listComponent) {
600                         if (!(listComponent instanceof ListBox)) {
601                             return;
602                         }
603                         ((ListBox) listComponent).setSelectionMode(mode);
604                     }
605                 });
606             }
607         });
608         controlsColumn.addButton("Select Even Indices", new ActionListener() {
609             public void actionPerformed(ActionEvent e) {
610                 apply(new Applicator(){
611                     public void apply(AbstractListComponent listComponent) {
612                         if (listComponent instanceof ListBox) {
613                             ((ListBox) listComponent).setSelectedIndices(new int[] { 0, 2, 4, 6, 8, 10 });
614                         }
615                     }
616                 });
617             }
618         });
619         controlsColumn.addButton("Select Odd Indices", new ActionListener() {
620             public void actionPerformed(ActionEvent e) {
621                 apply(new Applicator(){
622                     public void apply(AbstractListComponent listComponent) {
623                         if (listComponent instanceof ListBox) {
624                             // Note: Unlike certain amplifiers, this selectfield does not "go up to eleven".
625
// Just want to make sure this is handled.
626
((ListBox) listComponent).setSelectedIndices(new int[] { 1, 3, 5, 7, 9, 11 });
627                         }
628                     }
629                 });
630             }
631         });
632     }
633     
634     public void apply(Applicator applicator) {
635         applicator.apply(selectField1);
636         applicator.apply(listBox1);
637         applicator.apply(selectField2);
638         applicator.apply(listBox2);
639     }
640 }
Popular Tags