001: package org.conform.wings.editor;
002:
003: import java.awt.GridBagConstraints;
004: import java.awt.event.ActionEvent;
005: import java.awt.event.ActionListener;
006: import java.util.ArrayList;
007: import java.util.List;
008:
009: import javax.swing.ListModel;
010: import javax.swing.event.ChangeEvent;
011: import javax.swing.event.ChangeListener;
012: import javax.swing.event.ListDataListener;
013:
014: import org.wings.SButton;
015: import org.wings.SContainer;
016: import org.wings.SGridBagLayout;
017: import org.wings.SList;
018: import org.wings.SListCellRenderer;
019:
020: public class SubSet extends SContainer implements ActionListener {
021:
022: protected SubSetModel model;
023: private ChangeListener adapter = new ChangeListener() {
024: public void stateChanged(ChangeEvent e) {
025: ((IncludedModel) included.getModel()).list = null;
026: ((ExcludedModel) excluded.getModel()).list = null;
027: }
028: };
029: protected SList included;
030: protected SList excluded;
031: private SButton include;
032: private SButton exclude;
033:
034: public SubSet() {
035: super (new SGridBagLayout());
036: GridBagConstraints constraints = new GridBagConstraints();
037:
038: included = new SList();
039: included.setModel(new IncludedModel());
040: constraints.gridwidth = GridBagConstraints.RELATIVE;
041: constraints.gridheight = 2;
042: add(included, constraints);
043:
044: include = new SButton(">>>");
045: include.addActionListener(this );
046: constraints.gridheight = 1;
047: add(include, constraints);
048:
049: exclude = new SButton("<<<");
050: exclude.addActionListener(this );
051: constraints.gridheight = 1;
052: add(exclude, constraints);
053:
054: excluded = new SList();
055: excluded.setModel(new ExcludedModel());
056: constraints.gridwidth = GridBagConstraints.REMAINDER;
057: constraints.gridheight = 2;
058: add(excluded, constraints);
059: }
060:
061: public SubSetModel getModel() {
062: return model;
063: }
064:
065: public void setModel(SubSetModel model) {
066: if (this .model != null)
067: this .model.removeChangeListener(adapter);
068: this .model = model;
069: model.addChangeListener(adapter);
070: }
071:
072: public void actionPerformed(ActionEvent e) {
073: if (include == e.getSource()) {
074: Object[] objects = excluded.getSelectedValues();
075: for (int i = 0; i < objects.length; i++) {
076: Object object = objects[i];
077: model.include(object);
078: }
079: } else if (exclude == e.getSource()) {
080: Object[] objects = included.getSelectedValues();
081: for (int i = 0; i < objects.length; i++) {
082: Object object = objects[i];
083: model.exclude(object);
084: }
085: }
086: }
087:
088: public void setRenderer(SListCellRenderer renderer) {
089: included.setCellRenderer(renderer);
090: excluded.setCellRenderer(renderer);
091: }
092:
093: class IncludedModel implements ListModel {
094: private List list;
095:
096: private List getList() {
097: if (list == null)
098: list = new ArrayList(model.getIncludedSubSet());
099: return list;
100: }
101:
102: public int getSize() {
103: return getList().size();
104: }
105:
106: public Object getElementAt(int index) {
107: return getList().get(index);
108: }
109:
110: public void addListDataListener(ListDataListener l) {
111: }
112:
113: public void removeListDataListener(ListDataListener l) {
114: }
115: }
116:
117: class ExcludedModel implements ListModel {
118: private List list;
119:
120: private List getList() {
121: if (list == null)
122: list = new ArrayList(model.getExcludedSubSet());
123: return list;
124: }
125:
126: public int getSize() {
127: return getList().size();
128: }
129:
130: public Object getElementAt(int index) {
131: return getList().get(index);
132: }
133:
134: public void addListDataListener(ListDataListener l) {
135: }
136:
137: public void removeListDataListener(ListDataListener l) {
138: }
139: }
140: }
|