001: /*
002: * Gruntspud
003: *
004: * Copyright (C) 2002 Brett Smith.
005: *
006: * Written by: Brett Smith <t_magicthize@users.sourceforge.net>
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Library General Public License
010: * as published by the Free Software Foundation; either version 2 of
011: * the License, or (at your option) any later version.
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU Library General Public License for more details.
016: *
017: * You should have received a copy of the GNU Library General Public
018: * License along with this program; if not, write to the Free Software
019: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
020: */
021:
022: package gruntspud;
023:
024: import java.util.StringTokenizer;
025: import java.util.Vector;
026:
027: import javax.swing.AbstractListModel;
028: import javax.swing.ComboBoxModel;
029:
030: /**
031: * Description of the Class
032: *
033: *@author magicthize
034: *@created 26 May 2002
035: */
036: public class StringListModel extends AbstractListModel implements
037: ComboBoxModel {
038: //
039: private Object sel;
040: private Vector strings;
041: private boolean adjusting;
042:
043: /**
044: * Constructor for the StringListModel object
045: *
046: *@param stringList Description of the Parameter
047: */
048: public StringListModel(String stringList, boolean noSelection) {
049: strings = new Vector();
050:
051: StringTokenizer t = new StringTokenizer(stringList, "|");
052:
053: while (t.hasMoreTokens()) {
054: String r = t.nextToken();
055:
056: if (r.startsWith("&")) {
057: r = r.substring(1);
058:
059: if (!noSelection) {
060: sel = r;
061: }
062: }
063:
064: addString(r, noSelection);
065: }
066:
067: if ((sel == null) && (getSize() > 0) && !noSelection) {
068: sel = getElementAt(0);
069: }
070: }
071:
072: /**
073: * DOCUMENT ME!
074: *
075: * @param s DOCUMENT ME!
076: *
077: * @return DOCUMENT ME!
078: */
079: public boolean containsString(String s) {
080: return indexOfString(s) != -1;
081: }
082:
083: /**
084: * DOCUMENT ME!
085: *
086: * @param s DOCUMENT ME!
087: *
088: * @return DOCUMENT ME!
089: */
090: public int indexOfString(String s) {
091: int z = getSize();
092:
093: for (int i = 0; i < z; i++) {
094: if (s.equals(strings.elementAt(i).toString())) {
095: return i;
096: }
097: }
098:
099: return -1;
100: }
101:
102: /**
103: * DOCUMENT ME!
104: *
105: * @return DOCUMENT ME!
106: */
107: public String getListAsPropertyString() {
108: StringBuffer buf = new StringBuffer();
109:
110: for (int i = getSize() - 1; i >= 0; i--) {
111: if (i != (getSize() - 1)) {
112: buf.append('|');
113:
114: }
115: String string = (String) getElementAt(i);
116:
117: if (sel == string) {
118: buf.append('&');
119:
120: }
121: buf.append(string);
122: }
123:
124: return buf.toString();
125: }
126:
127: /**
128: * DOCUMENT ME!
129: *
130: * @param i DOCUMENT ME!
131: *
132: * @return DOCUMENT ME!
133: */
134: public Object getElementAt(int i) {
135: return strings.elementAt(i);
136: }
137:
138: /**
139: * DOCUMENT ME!
140: *
141: * @param string DOCUMENT ME!
142: */
143: public void addString(String string) {
144: addString(string, false);
145: }
146:
147: /**
148: * DOCUMENT ME!
149: *
150: * @param string DOCUMENT ME!
151: * @param noSelection DOCUMENT ME!
152: */
153: public void addString(String string, boolean noSelection) {
154: for (int i = 0; i < getSize(); i++) {
155: if (getElementAt(i).equals(string)) {
156: strings.removeElementAt(i);
157:
158: break;
159: }
160: }
161:
162: strings.insertElementAt(string, 0);
163:
164: while (strings.size() > 20) {
165: strings.removeElementAt(20);
166:
167: }
168: if ((sel == null) && !noSelection) {
169: sel = (strings.size() > 0) ? getElementAt(0) : null;
170:
171: }
172: fireContentsChanged(this , 0, getSize());
173: }
174:
175: /**
176: * DOCUMENT ME!
177: *
178: * @param string DOCUMENT ME!
179: */
180: public void removeString(String string) {
181: int idx = indexOfString(string);
182:
183: if (idx != -1) {
184: strings.removeElementAt(idx);
185: fireIntervalRemoved(this , idx, idx);
186:
187: if ((getSelectedItem() != null)
188: && getSelectedItem().equals(string)
189: && (getSize() > 0)) {
190: setSelectedItem(getElementAt(0));
191: }
192: }
193: }
194:
195: /**
196: * DOCUMENT ME!
197: *
198: * @return DOCUMENT ME!
199: */
200: public int getSize() {
201: return strings.size();
202: }
203:
204: /**
205: * DOCUMENT ME!
206: *
207: * @return DOCUMENT ME!
208: */
209: public Object getSelectedItem() {
210: return sel;
211: }
212:
213: /**
214: * DOCUMENT ME!
215: *
216: * @param o DOCUMENT ME!
217: */
218: public void setSelectedItem(Object o) {
219: Object old = sel;
220: this .sel = o;
221:
222: if (sel != old) {
223: adjusting = true;
224: fireContentsChanged(this , 0, getSize());
225: adjusting = false;
226: }
227: }
228:
229: public boolean isAdjusting() {
230: return adjusting;
231: }
232:
233: /**
234: * DOCUMENT ME!
235: *
236: * @param stringListPropertyString DOCUMENT ME!
237: *
238: * @return DOCUMENT ME!
239: */
240: public static String getSelectedItemForStringListPropertyString(
241: String stringListPropertyString) {
242: return (String) (new StringListModel(stringListPropertyString,
243: false).getSelectedItem());
244: }
245: }
|