001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.client.util.list;
019:
020: import java.util.ArrayList;
021: import java.util.Collections;
022: import java.util.Comparator;
023: import java.util.Iterator;
024: import java.util.List;
025:
026: import javax.swing.AbstractListModel;
027: import javax.swing.MutableComboBoxModel;
028:
029: import de.finix.contelligent.client.i18n.Resources;
030:
031: public class NameValueListModel extends AbstractListModel implements
032: MutableComboBoxModel {
033:
034: public final static boolean SORT_DESCENDING = true;
035:
036: public final static boolean SORT_ASCENDING = false;
037:
038: private List<NameValuePair> nameValuePairs;
039:
040: private NameValuePair selectedNameValuePair = null;
041:
042: // helper
043: final public static NameValueListModel buildEmptyNameValuePairs() {
044: return new NameValueListModel();
045: }
046:
047: // helper
048: final public static NameValueListModel buildNameValuePairs(
049: Object[] value, Object names[]) {
050: ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
051: value.length);
052: for (int i = 0; i < value.length; i++) {
053: nameValuePairs.add(new NameValuePair(names[i], value[i]));
054: }
055: return new NameValueListModel(nameValuePairs);
056: }
057:
058: // helper
059: final public static NameValueListModel buildNameValuePairs(
060: String[] valuesAsNames) {
061: ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
062: valuesAsNames.length);
063: for (int i = 0; i < valuesAsNames.length; i++) {
064: nameValuePairs.add(new NameValuePair(valuesAsNames[i],
065: valuesAsNames[i]));
066: }
067: return new NameValueListModel(nameValuePairs);
068: }
069:
070: // helper
071: final public static NameValueListModel buildNameValuePairs(
072: List valuesAsNames) {
073: ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
074: valuesAsNames.size());
075: for (Iterator it = valuesAsNames.iterator(); it.hasNext();) {
076: String valueAsName = (String) it.next();
077: nameValuePairs.add(new NameValuePair(valueAsName,
078: valueAsName));
079: }
080: return new NameValueListModel(nameValuePairs);
081: }
082:
083: // helper
084: final public static NameValueListModel buildLocaleNameValuePairs(
085: Object[] keys) {
086: ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
087: keys.length);
088: for (int i = 0; i < keys.length; i++) {
089: String key = (String) keys[i];
090: nameValuePairs.add(new NameValuePair(Resources
091: .getLocalString(key), key));
092: }
093: return new NameValueListModel(nameValuePairs);
094: }
095:
096: public NameValueListModel() {
097: this (new ArrayList<NameValuePair>());
098: }
099:
100: public NameValueListModel(NameValueListModel model) {
101: this (model.getData());
102: }
103:
104: public NameValueListModel(List<NameValuePair> nameValuePairs) {
105: super ();
106: this .nameValuePairs = nameValuePairs;
107: }
108:
109: public NameValueListModel(List<NameValuePair> nameValuePairs,
110: final boolean sortDescending) {
111: super ();
112: this .nameValuePairs = nameValuePairs;
113: sort(sortDescending);
114: }
115:
116: public List<NameValuePair> getData() {
117: return nameValuePairs;
118: }
119:
120: public NameValueListModel sort(final boolean sortDescending) {
121: if (nameValuePairs != null
122: && ((NameValuePair) nameValuePairs.get(0)).name instanceof String) {
123: Collections.sort(nameValuePairs,
124: new Comparator<NameValuePair>() {
125: public int compare(NameValuePair n1,
126: NameValuePair n2) {
127: String s1 = (String) n1.name;
128: String s2 = (String) n2.name;
129: return (sortDescending ? s1
130: .compareToIgnoreCase(s2) : s2
131: .compareToIgnoreCase(s1));
132: }
133:
134: public boolean equals(Object obj) {
135: return super .equals(obj);
136: }
137: });
138: }
139: return this ;
140: }
141:
142: public NameValuePair getEntryByValue(Object value) {
143: for (Iterator it = nameValuePairs.iterator(); it.hasNext();) {
144: NameValuePair nameValuePair = (NameValuePair) it.next();
145: if (value.equals(nameValuePair.value))
146: return nameValuePair;
147: }
148: return null;
149: }
150:
151: public NameValuePair getEntryByName(String name) {
152: for (Iterator it = nameValuePairs.iterator(); it.hasNext();) {
153: NameValuePair nameValuePair = (NameValuePair) it.next();
154: if (name.equals(nameValuePair.name))
155: return nameValuePair;
156: }
157: return null;
158: }
159:
160: public final static class NameValuePair {
161: public Object name;
162:
163: public Object value;
164:
165: public NameValuePair(Object name, Object value) {
166: this .name = name;
167: this .value = value;
168: }
169:
170: // used for selection
171: public boolean equals(Object o) {
172: return (o != null && o instanceof NameValuePair && value
173: .equals(((NameValuePair) o).value));
174: }
175:
176: public int hashCode() {
177: return value.hashCode();
178: }
179: }
180:
181: // for AbstractListModel
182:
183: public int getSize() {
184: return nameValuePairs.size();
185: }
186:
187: public Object getElementAt(int i) {
188: return nameValuePairs.get(i);
189: }
190:
191: // for ComboBoxModel
192:
193: public Object getSelectedItem() {
194: return selectedNameValuePair;
195: }
196:
197: public void setSelectedItem(Object nameValuePair) {
198: selectedNameValuePair = (NameValuePair) nameValuePair;
199: fireContentsChanged(this , -1, -1);
200: }
201:
202: // for MutableComboBoxModel
203:
204: public void addElement(Object nameValuePair) {
205: if (nameValuePair == null)
206: return;
207: nameValuePairs.add((NameValuePair) nameValuePair);
208: fireIntervalAdded(this , nameValuePairs.size() - 1,
209: nameValuePairs.size() - 1);
210: }
211:
212: public void insertElementAt(Object nameValuePair, int index) {
213: if (nameValuePair == null)
214: return;
215: nameValuePairs.add(index, (NameValuePair) nameValuePair);
216: fireIntervalAdded(this , index, index);
217: }
218:
219: public void removeElement(Object nameValuePair) {
220: if (nameValuePair == null)
221: return;
222: removeElement(nameValuePair, nameValuePairs
223: .indexOf(nameValuePair));
224: }
225:
226: public void removeElementAt(int index) {
227: removeElement(nameValuePairs.get(index), index);
228: }
229:
230: private void removeElement(Object nameValuePair, int index) {
231: if (nameValuePair == null)
232: return;
233: if (index < 0)
234: return;
235: nameValuePairs.remove(index);
236: if (selectedNameValuePair != null
237: && selectedNameValuePair.equals(nameValuePair)) {
238: selectedNameValuePair = null;
239: }
240: fireIntervalRemoved(this, index, index);
241: }
242:
243: }
|