001: /*
002: * Copyright 2000,2005 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013: package wingset;
014:
015: import org.wings.*;
016:
017: import javax.swing.*;
018: import javax.swing.event.ListDataListener;
019: import java.awt.*;
020: import java.awt.event.ActionEvent;
021: import java.io.Serializable;
022: import java.util.Date;
023:
024: /**
025: * @author <a href="mailto:haaf@mercatis.de">Armin Haaf</a>
026: */
027: public class ListExample extends WingSetPane {
028: private final static SResourceIcon javaCup = new SResourceIcon(
029: "org/wings/icons/JavaCup.gif");
030: private final ListModel listModel = createListModel();
031: private ComponentControls controls;
032: private SList singleSelectionList;
033: private SList multiSelectionList;
034: private SComboBox comboBox;
035: private SList anchorList;
036:
037: protected SComponent createControls() {
038: controls = new ListControls();
039: return controls;
040: }
041:
042: public SComponent createExample() {
043: SPanel panel = new SPanel(new SGridLayout(2, 2, 40, 40));
044: panel.add(createListSingleSelExample());
045: panel.add(createListMultSelExample());
046: panel.add(createComboBoxExample());
047: panel.add(createAnchorListExample());
048:
049: return panel;
050: }
051:
052: public SContainer createListSingleSelExample() {
053: SContainer cont = new SPanel(new SFlowDownLayout());
054: cont.add(new SLabel("List with single selection"));
055: cont.add(new SSpacer(1, 5));
056: singleSelectionList = new SList();
057: singleSelectionList.setName("single");
058: singleSelectionList.setSelectionMode(SList.SINGLE_SELECTION);
059: addListElements(singleSelectionList);
060: cont.add(singleSelectionList);
061: controls.addControllable(singleSelectionList);
062:
063: return cont;
064: }
065:
066: public SContainer createListMultSelExample() {
067: SContainer cont = new SPanel(new SFlowDownLayout());
068: cont.add(new SLabel("List with multiple selection"));
069: cont.add(new SSpacer(1, 5));
070: SList multiSelectionList = new SList();
071: this .multiSelectionList = multiSelectionList;
072: this .multiSelectionList.setName("multiple");
073: multiSelectionList.setSelectionMode(SList.MULTIPLE_SELECTION);
074: addListElements(multiSelectionList);
075: cont.add(multiSelectionList);
076: controls.addControllable(multiSelectionList);
077:
078: return cont;
079: }
080:
081: public SContainer createComboBoxExample() {
082: SContainer cont = new SPanel(new SFlowDownLayout());
083: cont.setVerticalAlignment(SConstants.TOP_ALIGN);
084: cont.add(new SLabel("ComboBox"));
085: cont.add(new SSpacer(1, 5));
086: comboBox = new SComboBox();
087: comboBox.setName("combo");
088: addComboBoxElements(comboBox);
089: cont.add(comboBox);
090: controls.addControllable(comboBox);
091:
092: return cont;
093: }
094:
095: public SContainer createAnchorListExample() {
096: SContainer cont = new SPanel(new SFlowDownLayout());
097: cont.add(new SLabel("AnchorList"));
098: cont.add(new SSpacer(1, 5));
099: SList anchorList = new SList();
100: this .anchorList = anchorList;
101: this .anchorList.setName("noform");
102: anchorList.setShowAsFormComponent(false);
103: anchorList.setSelectionMode(SList.SINGLE_SELECTION);
104: addAnchorElements(anchorList);
105: cont.add(anchorList);
106: controls.addControllable(anchorList);
107:
108: return cont;
109: }
110:
111: public void addListElements(SList list) {
112: list.setListData(createElements());
113: }
114:
115: public void addComboBoxElements(SComboBox comboBox) {
116: comboBox.setModel(new DefaultComboBoxModel(createElements()));
117: }
118:
119: public static Object[] createElements() {
120: SLabel color = new SLabel("");
121: color.setForeground(Color.green);
122: color.setText(Color.green.toString());
123: Object[] values = { "Element 1", color, "Element 3",
124: new Date(), "Element 5" };
125:
126: return values;
127: }
128:
129: public static ListModel createListModel() {
130: final SLabel img = new SLabel("This element has an icon",
131: javaCup);
132:
133: final SLabel color = new SLabel("");
134: color.setForeground(Color.green);
135: color.setText(Color.green.toString());
136:
137: ListModel listModel = new MyListModel(color, img);
138:
139: return listModel;
140: }
141:
142: public void addAnchorElements(SList list) {
143: list.setModel(listModel);
144: list.setType(SList.ORDER_TYPE_NORMAL);
145: }
146:
147: private final static class MyListModel implements ListModel,
148: Serializable {
149: private final Object[] values;
150:
151: public MyListModel(SLabel color, SLabel img) {
152: values = new Object[] { "Element 1", color, "Element 3",
153: new Date(), "Element 5", img };
154: }
155:
156: public int getSize() {
157: return values.length;
158: }
159:
160: public Object getElementAt(int i) {
161: return values[i];
162: }
163:
164: public void addListDataListener(ListDataListener l) {
165: }
166:
167: public void removeListDataListener(ListDataListener l) {
168: }
169: }
170:
171: class ListControls extends ComponentControls {
172: public ListControls() {
173: formComponentCheckBox.setVisible(false);
174:
175: final SCheckBox enabled = new SCheckBox("enabled");
176: enabled.setSelected(true);
177: enabled
178: .addActionListener(new java.awt.event.ActionListener() {
179: public void actionPerformed(ActionEvent e) {
180: singleSelectionList.setEnabled(enabled
181: .isSelected());
182: multiSelectionList.setEnabled(enabled
183: .isSelected());
184: comboBox.setEnabled(enabled.isSelected());
185: anchorList.setEnabled(enabled.isSelected());
186: }
187: });
188: addControl(enabled);
189: }
190: }
191: }
|