001: /*
002: * This file is part of the Echo Web Application Framework (hereinafter "Echo").
003: * Copyright (C) 2002-2005 NextApp, Inc.
004: *
005: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006: *
007: * The contents of this file are subject to the Mozilla Public License Version
008: * 1.1 (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: * http://www.mozilla.org/MPL/
011: *
012: * Software distributed under the License is distributed on an "AS IS" basis,
013: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014: * for the specific language governing rights and limitations under the
015: * License.
016: *
017: * Alternatively, the contents of this file may be used under the terms of
018: * either the GNU General Public License Version 2 or later (the "GPL"), or
019: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020: * in which case the provisions of the GPL or the LGPL are applicable instead
021: * of those above. If you wish to allow use of your version of this file only
022: * under the terms of either the GPL or the LGPL, and not to allow others to
023: * use your version of this file under the terms of the MPL, indicate your
024: * decision by deleting the provisions above and replace them with the notice
025: * and other provisions required by the GPL or the LGPL. If you do not delete
026: * the provisions above, a recipient may use your version of this file under
027: * the terms of any one of the MPL, the GPL or the LGPL.
028: */
029:
030: package nextapp.echo2.testapp.interactive.testscreen;
031:
032: import nextapp.echo2.app.Border;
033: import nextapp.echo2.app.Color;
034: import nextapp.echo2.app.Column;
035: import nextapp.echo2.app.Component;
036: import nextapp.echo2.app.Extent;
037: import nextapp.echo2.app.Font;
038: import nextapp.echo2.app.Grid;
039: import nextapp.echo2.app.Insets;
040: import nextapp.echo2.app.Label;
041: import nextapp.echo2.app.ListBox;
042: import nextapp.echo2.app.SelectField;
043: import nextapp.echo2.app.SplitPane;
044: import nextapp.echo2.app.event.ActionEvent;
045: import nextapp.echo2.app.event.ActionListener;
046: import nextapp.echo2.app.event.ChangeEvent;
047: import nextapp.echo2.app.event.ChangeListener;
048: import nextapp.echo2.app.event.ListDataEvent;
049: import nextapp.echo2.app.event.ListDataListener;
050: import nextapp.echo2.app.layout.SplitPaneLayoutData;
051: import nextapp.echo2.app.list.AbstractListComponent;
052: import nextapp.echo2.app.list.DefaultListModel;
053: import nextapp.echo2.app.list.ListCellRenderer;
054: import nextapp.echo2.app.list.ListSelectionModel;
055: import nextapp.echo2.app.list.StyledListCell;
056: import nextapp.echo2.testapp.interactive.ButtonColumn;
057: import nextapp.echo2.testapp.interactive.InteractiveApp;
058: import nextapp.echo2.testapp.interactive.StyleUtil;
059:
060: /**
061: * An interactive test for <code>ListBox</code>es.
062: */
063: public class ListBoxTest extends SplitPane {
064:
065: public static final String[] NUMBERS = new String[] { "Zero",
066: "One", "Two", "Three", "Four", "Five", "Six", "Seven",
067: "Eight", "Nine", "Ten" };
068:
069: /**
070: * Interface used to apply style information to all test components.
071: */
072: private interface Applicator {
073:
074: /**
075: * Applies style information.
076: *
077: * @param listComponent the target list component.
078: */
079: public void apply(AbstractListComponent listComponent);
080: }
081:
082: /**
083: * Writes <code>ActionEvent</code>s to console.
084: */
085: private ActionListener actionListener = new ActionListener() {
086:
087: /**
088: * @see nextapp.echo2.app.event.ActionListener#actionPerformed(nextapp.echo2.app.event.ActionEvent)
089: */
090: public void actionPerformed(ActionEvent e) {
091: ((InteractiveApp) getApplicationInstance()).consoleWrite(e
092: .toString());
093: }
094: };
095:
096: /**
097: * Writes <code>ChangeEvent</code>s to console.
098: */
099: private ChangeListener changeListener = new ChangeListener() {
100:
101: /**
102: * @see nextapp.echo2.app.event.ChangeListener#stateChanged(nextapp.echo2.app.event.ChangeEvent)
103: */
104: public void stateChanged(ChangeEvent e) {
105: ((InteractiveApp) getApplicationInstance()).consoleWrite(e
106: .toString());
107: }
108: };
109:
110: /**
111: * Writes <code>ListDataListener</code>s to console.
112: */
113: private ListDataListener listDataListener = new ListDataListener() {
114:
115: /**
116: * @see nextapp.echo2.app.event.ListDataListener#contentsChanged(nextapp.echo2.app.event.ListDataEvent)
117: */
118: public void contentsChanged(ListDataEvent e) {
119: ((InteractiveApp) getApplicationInstance()).consoleWrite(e
120: .toString());
121: }
122:
123: /**
124: * @see nextapp.echo2.app.event.ListDataListener#intervalAdded(nextapp.echo2.app.event.ListDataEvent)
125: */
126: public void intervalAdded(ListDataEvent e) {
127: ((InteractiveApp) getApplicationInstance()).consoleWrite(e
128: .toString());
129: }
130:
131: /**
132: * @see nextapp.echo2.app.event.ListDataListener#intervalRemoved(nextapp.echo2.app.event.ListDataEvent)
133: */
134: public void intervalRemoved(ListDataEvent e) {
135: ((InteractiveApp) getApplicationInstance()).consoleWrite(e
136: .toString());
137: }
138: };
139:
140: private ListCellRenderer evenOddListCellRenderer = new ListCellRenderer() {
141:
142: private Color foreground1 = new Color(0x007f00);
143: private Color background1 = new Color(0xafffaf);
144: private Color foreground2 = new Color(0x7f0000);
145: private Color background2 = new Color(0xffafaf);
146: private Font font1 = new Font(Font.MONOSPACE, Font.BOLD, null);
147:
148: /**
149: * @see nextapp.echo2.app.list.ListCellRenderer#getListCellRendererComponent(nextapp.echo2.app.Component,
150: * java.lang.Object, int)
151: */
152: public Object getListCellRendererComponent(Component list,
153: final Object value, final int index) {
154: return new StyledListCell() {
155:
156: public Color getForeground() {
157: return index % 2 == 0 ? foreground1 : foreground2;
158: }
159:
160: public Font getFont() {
161: return index % 2 == 0 ? font1 : null;
162: }
163:
164: public Color getBackground() {
165: return index % 2 == 0 ? background1 : background2;
166: }
167:
168: public String toString() {
169: return value == null ? null : value.toString();
170: }
171: };
172: }
173: };
174:
175: private Column testColumn;
176: private ListBox listBox1, listBox2;
177: private SelectField selectField1, selectField2;
178:
179: public ListBoxTest() {
180: super (SplitPane.ORIENTATION_HORIZONTAL, new Extent(250,
181: Extent.PX));
182: setStyleName("DefaultResizable");
183:
184: SplitPaneLayoutData splitPaneLayoutData;
185:
186: ButtonColumn controlsColumn = new ButtonColumn();
187: controlsColumn.setStyleName("TestControlsColumn");
188: add(controlsColumn);
189:
190: testColumn = new Column();
191: testColumn.setCellSpacing(new Extent(15));
192: splitPaneLayoutData = new SplitPaneLayoutData();
193: splitPaneLayoutData.setInsets(new Insets(15));
194: testColumn.setLayoutData(splitPaneLayoutData);
195: add(testColumn);
196:
197: listBox1 = new ListBox(NUMBERS);
198: testColumn.add(listBox1);
199:
200: selectField1 = new SelectField(NUMBERS);
201: testColumn.add(selectField1);
202:
203: Grid grid = new Grid();
204: grid.setBorder(new Border(1, Color.BLACK, Border.STYLE_SOLID));
205: testColumn.add(grid);
206:
207: selectField2 = new SelectField(NUMBERS);
208: grid.add(selectField2);
209:
210: listBox2 = new ListBox(NUMBERS);
211: grid.add(listBox2);
212:
213: controlsColumn.add(new Label("Global"));
214:
215: controlsColumn.addButton("Add ActionListener",
216: new ActionListener() {
217: public void actionPerformed(ActionEvent e) {
218: apply(new Applicator() {
219: public void apply(
220: AbstractListComponent listComponent) {
221: listComponent
222: .addActionListener(actionListener);
223: }
224: });
225: }
226: });
227: controlsColumn.addButton("Remove ActionListener",
228: new ActionListener() {
229: public void actionPerformed(ActionEvent e) {
230: apply(new Applicator() {
231: public void apply(
232: AbstractListComponent listComponent) {
233: listComponent
234: .removeActionListener(actionListener);
235: }
236: });
237: }
238: });
239: controlsColumn.addButton("Add ChangeListener",
240: new ActionListener() {
241: public void actionPerformed(ActionEvent e) {
242: apply(new Applicator() {
243: public void apply(
244: AbstractListComponent listComponent) {
245: listComponent.getSelectionModel()
246: .addChangeListener(
247: changeListener);
248: }
249: });
250: }
251: });
252: controlsColumn.addButton("Remove ChangeListener",
253: new ActionListener() {
254: public void actionPerformed(ActionEvent e) {
255: apply(new Applicator() {
256: public void apply(
257: AbstractListComponent listComponent) {
258: listComponent.getSelectionModel()
259: .removeChangeListener(
260: changeListener);
261: }
262: });
263: }
264: });
265: controlsColumn.addButton("Add ListDataListener",
266: new ActionListener() {
267: public void actionPerformed(ActionEvent e) {
268: apply(new Applicator() {
269: public void apply(
270: AbstractListComponent listComponent) {
271: listComponent.getModel()
272: .addListDataListener(
273: listDataListener);
274: }
275: });
276: }
277: });
278: controlsColumn.addButton("Remove ListDataListener",
279: new ActionListener() {
280: public void actionPerformed(ActionEvent e) {
281: apply(new Applicator() {
282: public void apply(
283: AbstractListComponent listComponent) {
284: listComponent.getModel()
285: .removeListDataListener(
286: listDataListener);
287: }
288: });
289: }
290: });
291: controlsColumn.addButton("Toggle Enabled State",
292: new ActionListener() {
293: public void actionPerformed(ActionEvent e) {
294: apply(new Applicator() {
295: public void apply(
296: AbstractListComponent listComponent) {
297: listComponent.setEnabled(!listComponent
298: .isEnabled());
299: }
300: });
301: }
302: });
303: controlsColumn.addButton("Set ListCellRenderer",
304: new ActionListener() {
305: public void actionPerformed(ActionEvent e) {
306: apply(new Applicator() {
307: public void apply(
308: AbstractListComponent listComponent) {
309: listComponent
310: .setCellRenderer(evenOddListCellRenderer);
311: }
312: });
313: }
314: });
315: controlsColumn.addButton("Clear ListCellRenderer",
316: new ActionListener() {
317: public void actionPerformed(ActionEvent e) {
318: apply(new Applicator() {
319: public void apply(
320: AbstractListComponent listComponent) {
321: listComponent
322: .setCellRenderer(AbstractListComponent.DEFAULT_LIST_CELL_RENDERER);
323: }
324: });
325: }
326: });
327: controlsColumn.addButton("Set Border", new ActionListener() {
328: public void actionPerformed(ActionEvent e) {
329: final Border border = StyleUtil.randomBorder();
330: apply(new Applicator() {
331: public void apply(
332: AbstractListComponent listComponent) {
333: listComponent.setBorder(border);
334: }
335: });
336: }
337: });
338: controlsColumn.addButton("Clear Border", new ActionListener() {
339: public void actionPerformed(ActionEvent e) {
340: apply(new Applicator() {
341: public void apply(
342: AbstractListComponent listComponent) {
343: listComponent.setBorder(null);
344: }
345: });
346: }
347: });
348: controlsColumn.addButton("Set Foreground",
349: new ActionListener() {
350: public void actionPerformed(ActionEvent e) {
351: final Color color = StyleUtil.randomColor();
352: apply(new Applicator() {
353: public void apply(
354: AbstractListComponent listComponent) {
355: listComponent.setForeground(color);
356: }
357: });
358: }
359: });
360: controlsColumn.addButton("Set Background",
361: new ActionListener() {
362: public void actionPerformed(ActionEvent e) {
363: final Color color = StyleUtil.randomColor();
364: apply(new Applicator() {
365: public void apply(
366: AbstractListComponent listComponent) {
367: listComponent.setBackground(color);
368: }
369: });
370: }
371: });
372: controlsColumn.addButton("Set Font", new ActionListener() {
373: public void actionPerformed(ActionEvent e) {
374: final Font font = StyleUtil.randomFont();
375: apply(new Applicator() {
376: public void apply(
377: AbstractListComponent listComponent) {
378: listComponent.setFont(font);
379: }
380: });
381: }
382: });
383: controlsColumn.addButton("Clear Font", new ActionListener() {
384: public void actionPerformed(ActionEvent e) {
385: apply(new Applicator() {
386: public void apply(
387: AbstractListComponent listComponent) {
388: listComponent.setFont(null);
389: }
390: });
391: }
392: });
393: controlsColumn.addButton("Set Disabled Border",
394: new ActionListener() {
395: public void actionPerformed(ActionEvent e) {
396: final Border border = StyleUtil.randomBorder();
397: apply(new Applicator() {
398: public void apply(
399: AbstractListComponent listComponent) {
400: listComponent.setDisabledBorder(border);
401: }
402: });
403: }
404: });
405: controlsColumn.addButton("Clear Disabled Border",
406: new ActionListener() {
407: public void actionPerformed(ActionEvent e) {
408: apply(new Applicator() {
409: public void apply(
410: AbstractListComponent listComponent) {
411: listComponent.setDisabledBorder(null);
412: }
413: });
414: }
415: });
416: controlsColumn.addButton("Set Disabled Foreground",
417: new ActionListener() {
418: public void actionPerformed(ActionEvent e) {
419: final Color color = StyleUtil.randomColor();
420: apply(new Applicator() {
421: public void apply(
422: AbstractListComponent listComponent) {
423: listComponent
424: .setDisabledForeground(color);
425: }
426: });
427: }
428: });
429: controlsColumn.addButton("Set Disabled Background",
430: new ActionListener() {
431: public void actionPerformed(ActionEvent e) {
432: final Color color = StyleUtil.randomColor();
433: apply(new Applicator() {
434: public void apply(
435: AbstractListComponent listComponent) {
436: listComponent
437: .setDisabledBackground(color);
438: }
439: });
440: }
441: });
442: controlsColumn.addButton("Set Disabled Font",
443: new ActionListener() {
444: public void actionPerformed(ActionEvent e) {
445: final Font font = StyleUtil.randomFont();
446: apply(new Applicator() {
447: public void apply(
448: AbstractListComponent listComponent) {
449: listComponent.setDisabledFont(font);
450: }
451: });
452: }
453: });
454: controlsColumn.addButton("Clear Disabled Font",
455: new ActionListener() {
456: public void actionPerformed(ActionEvent e) {
457: apply(new Applicator() {
458: public void apply(
459: AbstractListComponent listComponent) {
460: listComponent.setDisabledFont(null);
461: }
462: });
463: }
464: });
465: controlsColumn.addButton("Toggle ToolTip Text",
466: new ActionListener() {
467: public void actionPerformed(ActionEvent e) {
468: apply(new Applicator() {
469: public void apply(
470: AbstractListComponent listComponent) {
471: if (listComponent.getToolTipText() == null) {
472: listComponent
473: .setToolTipText("This is a tool tip.");
474: } else {
475: listComponent.setToolTipText(null);
476: }
477: }
478: });
479: }
480: });
481: controlsColumn.addButton("Enable Rollover Effects",
482: new ActionListener() {
483: public void actionPerformed(ActionEvent e) {
484: apply(new Applicator() {
485: public void apply(
486: AbstractListComponent listComponent) {
487: listComponent.setRolloverEnabled(true);
488: }
489: });
490: }
491: });
492: controlsColumn.addButton("Disable Rollover Effects",
493: new ActionListener() {
494: public void actionPerformed(ActionEvent e) {
495: apply(new Applicator() {
496: public void apply(
497: AbstractListComponent listComponent) {
498: listComponent.setRolloverEnabled(false);
499: }
500: });
501: }
502: });
503: controlsColumn.addButton("Set Rollover Foreground",
504: new ActionListener() {
505: public void actionPerformed(ActionEvent e) {
506: final Color color = StyleUtil.randomColor();
507: apply(new Applicator() {
508: public void apply(
509: AbstractListComponent listComponent) {
510: listComponent
511: .setRolloverForeground(color);
512: }
513: });
514: }
515: });
516: controlsColumn.addButton("Set Rollover Background",
517: new ActionListener() {
518: public void actionPerformed(ActionEvent e) {
519: final Color color = StyleUtil.randomColor();
520: apply(new Applicator() {
521: public void apply(
522: AbstractListComponent listComponent) {
523: listComponent
524: .setRolloverBackground(color);
525: }
526: });
527: }
528: });
529: controlsColumn.addButton("Set Rollover Font",
530: new ActionListener() {
531: public void actionPerformed(ActionEvent e) {
532: final Font font = StyleUtil.randomFont();
533: apply(new Applicator() {
534: public void apply(
535: AbstractListComponent listComponent) {
536: listComponent.setRolloverFont(font);
537: }
538: });
539: }
540: });
541: controlsColumn.addButton("Increase Width (15 px)",
542: new ActionListener() {
543: public void actionPerformed(ActionEvent e) {
544: final Extent width = listBox1.getWidth() == null ? new Extent(
545: 75)
546: : listBox1.getWidth();
547: apply(new Applicator() {
548: public void apply(
549: AbstractListComponent listComponent) {
550: listComponent.setWidth(Extent.add(
551: width, new Extent(15)));
552: }
553: });
554: }
555: });
556: controlsColumn.addButton("Decrease Width (15 px)",
557: new ActionListener() {
558: public void actionPerformed(ActionEvent e) {
559: final Extent width = listBox1.getWidth() == null ? new Extent(
560: 75)
561: : listBox1.getWidth();
562: apply(new Applicator() {
563: public void apply(
564: AbstractListComponent listComponent) {
565: listComponent.setWidth(Extent.add(
566: width, new Extent(-15)));
567: }
568: });
569: }
570: });
571: controlsColumn.addButton("Increase Height (15 px)",
572: new ActionListener() {
573: public void actionPerformed(ActionEvent e) {
574: final Extent height = listBox1.getHeight() == null ? new Extent(
575: 75)
576: : listBox1.getHeight();
577: apply(new Applicator() {
578: public void apply(
579: AbstractListComponent listComponent) {
580: listComponent.setHeight(Extent.add(
581: height, new Extent(15)));
582: }
583: });
584: }
585: });
586: controlsColumn.addButton("Decrease Height (15 px)",
587: new ActionListener() {
588: public void actionPerformed(ActionEvent e) {
589: final Extent height = listBox1.getHeight() == null ? new Extent(
590: 75)
591: : listBox1.getHeight();
592: apply(new Applicator() {
593: public void apply(
594: AbstractListComponent listComponent) {
595: listComponent.setHeight(Extent.add(
596: height, new Extent(-15)));
597: }
598: });
599: }
600: });
601:
602: controlsColumn.addButton("Select Index 0",
603: new ActionListener() {
604: public void actionPerformed(ActionEvent e) {
605: apply(new Applicator() {
606: public void apply(
607: AbstractListComponent listComponent) {
608: if (listComponent instanceof ListBox) {
609: ((ListBox) listComponent)
610: .setSelectedIndices(new int[] { 0 });
611: } else if (listComponent instanceof SelectField) {
612: ((SelectField) listComponent)
613: .setSelectedIndex(0);
614: }
615: }
616: });
617: }
618: });
619:
620: controlsColumn.addButton("Select Index 2",
621: new ActionListener() {
622: public void actionPerformed(ActionEvent e) {
623: apply(new Applicator() {
624: public void apply(
625: AbstractListComponent listComponent) {
626: if (listComponent instanceof ListBox) {
627: ((ListBox) listComponent)
628: .setSelectedIndices(new int[] { 2 });
629: } else if (listComponent instanceof SelectField) {
630: ((SelectField) listComponent)
631: .setSelectedIndex(2);
632: }
633: }
634: });
635: }
636: });
637:
638: controlsColumn.addButton("Select Index 1502",
639: new ActionListener() {
640: public void actionPerformed(ActionEvent e) {
641: apply(new Applicator() {
642: public void apply(
643: AbstractListComponent listComponent) {
644: if (listComponent instanceof ListBox) {
645: ((ListBox) listComponent)
646: .setSelectedIndices(new int[] { 1502 });
647: } else if (listComponent instanceof SelectField) {
648: ((SelectField) listComponent)
649: .setSelectedIndex(1502);
650: }
651: }
652: });
653: }
654: });
655:
656: controlsColumn.addButton("Clear Selections",
657: new ActionListener() {
658: public void actionPerformed(ActionEvent e) {
659: apply(new Applicator() {
660: public void apply(
661: AbstractListComponent listComponent) {
662: if (listComponent instanceof ListBox) {
663: ((ListBox) listComponent)
664: .setSelectedIndices(new int[] {});
665: } else if (listComponent instanceof SelectField) {
666: ((SelectField) listComponent)
667: .setSelectedIndex(-1);
668: }
669: }
670: });
671: }
672: });
673:
674: controlsColumn.addButton("Empty ListModel",
675: new ActionListener() {
676: public void actionPerformed(ActionEvent e) {
677: apply(new Applicator() {
678: public void apply(
679: AbstractListComponent listComponent) {
680: listComponent
681: .setModel(new DefaultListModel());
682: }
683: });
684: }
685: });
686:
687: controlsColumn.addButton("Set ListModel = Numbers",
688: new ActionListener() {
689: public void actionPerformed(ActionEvent e) {
690: apply(new Applicator() {
691: public void apply(
692: AbstractListComponent listComponent) {
693: listComponent
694: .setModel(new DefaultListModel(
695: NUMBERS));
696: }
697: });
698: }
699: });
700: controlsColumn.addButton("Focus SelectField1",
701: new ActionListener() {
702: public void actionPerformed(ActionEvent e) {
703: getApplicationInstance().setFocusedComponent(
704: selectField1);
705: }
706: });
707: controlsColumn.addButton("Focus SelectField2",
708: new ActionListener() {
709: public void actionPerformed(ActionEvent e) {
710: getApplicationInstance().setFocusedComponent(
711: selectField2);
712: }
713: });
714:
715: controlsColumn.add(new Label("ListBox-specific"));
716:
717: controlsColumn.addButton("Toggle Multiple Select",
718: new ActionListener() {
719: public void actionPerformed(ActionEvent e) {
720: final int mode = ListSelectionModel.MULTIPLE_SELECTION == listBox1
721: .getSelectionMode() ? ListSelectionModel.SINGLE_SELECTION
722: : ListSelectionModel.MULTIPLE_SELECTION;
723: apply(new Applicator() {
724: public void apply(
725: AbstractListComponent listComponent) {
726: if (!(listComponent instanceof ListBox)) {
727: return;
728: }
729: ((ListBox) listComponent)
730: .setSelectionMode(mode);
731: }
732: });
733: }
734: });
735: controlsColumn.addButton("Select Even Indices",
736: new ActionListener() {
737: public void actionPerformed(ActionEvent e) {
738: apply(new Applicator() {
739: public void apply(
740: AbstractListComponent listComponent) {
741: if (listComponent instanceof ListBox) {
742: ((ListBox) listComponent)
743: .setSelectedIndices(new int[] {
744: 0, 2, 4, 6, 8, 10 });
745: }
746: }
747: });
748: }
749: });
750: controlsColumn.addButton("Select Odd Indices",
751: new ActionListener() {
752: public void actionPerformed(ActionEvent e) {
753: apply(new Applicator() {
754: public void apply(
755: AbstractListComponent listComponent) {
756: if (listComponent instanceof ListBox) {
757: // Note: Unlike certain amplifiers, this selectfield does not "go up to eleven".
758: // Just want to make sure this is handled.
759: ((ListBox) listComponent)
760: .setSelectedIndices(new int[] {
761: 1, 3, 5, 7, 9, 11 });
762: }
763: }
764: });
765: }
766: });
767: }
768:
769: public void apply(Applicator applicator) {
770: applicator.apply(selectField1);
771: applicator.apply(listBox1);
772: applicator.apply(selectField2);
773: applicator.apply(listBox2);
774: }
775: }
|