001: //** Copyright Statement ***************************************************
002: //The Salmon Open Framework for Internet Applications (SOFIA)
003: // Copyright (C) 1999 - 2002, Salmon LLC
004: //
005: // This program is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU General Public License version 2
007: // as published by the Free Software Foundation;
008: //
009: // This program is distributed in the hope that it will be useful,
010: // but WITHOUT ANY WARRANTY; without even the implied warranty of
011: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: // GNU General Public License for more details.
013: //
014: // You should have received a copy of the GNU General Public License
015: // along with this program; if not, write to the Free Software
016: // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: //
018: // For more information please visit http://www.salmonllc.com
019: //** End Copyright Statement ***************************************************
020: package com.salmonllc.swing;
021:
022: import com.salmonllc.sql.DataStoreBuffer;
023: import com.salmonllc.sql.ModelChangedEvent;
024: import com.salmonllc.sql.DataStoreException;
025: import com.salmonllc.sql.ModelChangedListener;
026: import com.salmonllc.swing.events.ValueChangedEvent;
027: import com.salmonllc.swing.events.ValueChangedListener;
028:
029: import javax.swing.*;
030: import java.util.Vector;
031: import java.util.StringTokenizer;
032:
033: /**
034: * SOFIA implementation of a JList. This List can be bound to a DataStore column.
035: */
036:
037: public class SList extends JList implements SComponent,
038: ModelChangedListener {
039: private SComponentHelper _helper;
040: private String _dsCol;
041: private DataStoreBuffer _ds;
042:
043: /**
044: * Constructs a <code>JList</code> with an empty model.
045: */
046: public SList() {
047: super (new DefaultListModel());
048: _helper = new SComponentHelper(this );
049: }
050:
051: /**
052: * Constructs a <code>JList</code> that displays the elements in
053: * the specified array. This constructor just delegates to the
054: * <code>ListModel</code> constructor.
055: *
056: * @param listData the array of Objects to be loaded into the data model
057: */
058: public SList(final Object[] listData) {
059: super (new DefaultListModel());
060: for (int i = 0; i < listData.length; i++)
061: addItem(listData[i]);
062: _helper = new SComponentHelper(this );
063: }
064:
065: /**
066: * Constructs a <code>JList</code> that displays the elements in
067: * the specified <code>Vector</code>. This constructor just
068: * delegates to the <code>ListModel</code> constructor.
069: *
070: * @param listData the <code>Vector</code> to be loaded into the
071: * data model
072: */
073: public SList(final Vector listData) {
074: super (new DefaultListModel());
075: for (int i = 0; i < listData.size(); i++)
076: addItem(listData.elementAt(i));
077: _helper = new SComponentHelper(this );
078: }
079:
080: /**
081: * Binds the component to a DataStore column
082: * @param dsb The DataStore to bind to
083: * @param column The column to bind to
084: */
085: public void setColumn(DataStoreBuffer dsb, String column) {
086: _ds = dsb;
087: _dsCol = column;
088: _ds.addModelChangedListener(this );
089: evalColumn();
090: }
091:
092: /**
093: * @see com.salmonllc.sql.ModelChangedListener#modelChanged(com.salmonllc.sql.ModelChangedEvent)
094: */
095: public void modelChanged(ModelChangedEvent evt) {
096: evalColumn();
097: }
098:
099: private void evalColumn() {
100: _helper.setDataDirty(true);
101: try {
102: if (_ds != null && _dsCol != null) {
103: String value = _ds.getFormattedString(_dsCol);
104: getSelectionModel().clearSelection();
105:
106: if (getSelectionMode() != ListSelectionModel.SINGLE_SELECTION
107: && value != null) {
108: int nullIndex = findSelectValue(null);
109: Vector sel = new Vector();
110: StringTokenizer st = new StringTokenizer(value, ",");
111: while (st.hasMoreTokens()) {
112: String item = st.nextToken();
113: int ndx = findSelectValue(item);
114: if (ndx > -1)
115: sel.addElement(new Integer(ndx));
116: else if (nullIndex != -1)
117: sel.addElement(new Integer(nullIndex));
118: }
119: if (sel.size() > 0) {
120: int ndx[] = new int[sel.size()];
121: for (int i = 0; i < sel.size(); i++)
122: ndx[i] = ((Integer) sel.elementAt(i))
123: .intValue();
124: setSelectedIndices(ndx);
125: }
126: } else {
127: int ndx = findSelectValue(value);
128: if (ndx > -1)
129: setSelectedIndex(ndx);
130: }
131: }
132: } catch (Exception e) {
133: getSelectionModel().clearSelection();
134: }
135: _helper.setDataDirty(false);
136:
137: }
138:
139: private int findSelectValue(String value) {
140: ListModel mod = getModel();
141: for (int i = 0; i < mod.getSize(); i++) {
142: Object o = mod.getElementAt(i);
143: if (o.equals(value))
144: return i;
145: }
146: return -1;
147: }
148:
149: /**
150: * Returns the value of the data in the component
151: */
152: public String getValue() {
153: try {
154: if (_ds != null) {
155: if (_helper.isDataDirty())
156: return getSelValue();
157: else
158: return _ds.getFormattedString(_dsCol);
159: }
160: } catch (DataStoreException ex) {
161: ex.printStackTrace();
162: return null;
163: }
164: return getSelValue();
165:
166: }
167:
168: private String getSelValue() {
169: int ndx[] = getSelectedIndices();
170:
171: if (ndx == null)
172: return null;
173: if (ndx.length == 0)
174: return null;
175:
176: StringBuffer ret = new StringBuffer();
177: for (int i = 0; i < ndx.length; i++) {
178: Object o = getModel().getElementAt(ndx[i]);
179: String val = "null";
180: if (o != null) {
181: if (o instanceof SOption) {
182: val = ((SOption) o).getKey();
183: if (val == null)
184: val = "null";
185: } else
186: val = o.toString();
187: }
188: ret.append(val);
189: ret.append(",");
190: }
191: ret.setLength(ret.length() - 1);
192: String r = ret.toString();
193: if (r.equals("null"))
194: return null;
195: else
196: return r;
197: }
198:
199: /**
200: * This method is used by the framework and should not be called directly
201: */
202: public ValueChangedEvent generateValueChangedEvent() {
203: try {
204: if (_ds == null)
205: return null;
206: else
207: return new ValueChangedEvent(this , _ds
208: .getFormattedString(_dsCol), getSelValue(),
209: _ds, _ds.getRow(), _ds.getColumnIndex(_dsCol));
210: } catch (Exception e) {
211: e.printStackTrace();
212: return null;
213: }
214: }
215:
216: /**
217: * This method is used by the framework and should not be called directly
218: */
219: public SComponentHelper getHelper() {
220: return _helper;
221: }
222:
223: /**
224: * This method adds a listener the will be notified when the value in this component changes.
225: * @param l The listener to add.
226: */
227: public void addValueChangedListener(ValueChangedListener l) {
228: _helper.addValueChangedListener(l);
229: }
230:
231: /**
232: * This method removes a listener from the list that will be notified if the text in the component changes.
233: * @param l The listener to remove.
234: */
235: public void removeValueChangedListener(ValueChangedListener l) {
236: _helper.removeValueChangedListener(l);
237: }
238:
239: /**
240: * Adds an item to the List
241: */
242: public void addItem(Object opt) {
243: ((DefaultListModel) getModel()).addElement(opt);
244: }
245:
246: /**
247: * Gets all the items from the list
248: */
249: public Object[] getItems() {
250: ListModel m = getModel();
251: Object o[] = new Object[m.getSize()];
252: for (int i = 0; i < m.getSize(); i++) {
253: o[i] = m.getElementAt(i);
254: }
255: return o;
256: }
257: }
|