001: /*
002: * PropertyWrapperModel.java
003: *
004: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package org.underworldlabs.swing.table;
023:
024: import java.util.Arrays;
025: import java.util.Comparator;
026: import java.util.Enumeration;
027: import java.util.Hashtable;
028: import java.util.Properties;
029: import javax.swing.table.AbstractTableModel;
030: import org.underworldlabs.util.KeyValuePair;
031:
032: /* ----------------------------------------------------------
033: * CVS NOTE: Changes to the CVS repository prior to the
034: * release of version 3.0.0beta1 has meant a
035: * resetting of CVS revision numbers.
036: * ----------------------------------------------------------
037: */
038:
039: /** Simple wrapper class for key/value property values
040: * providing table model and sorting by key or value.
041: *
042: * @author Takis Diakoumis
043: * @version $Revision: 1.4 $
044: * @date $Date: 2006/05/14 06:56:07 $
045: */
046: public class PropertyWrapperModel extends AbstractTableModel {
047:
048: public static final int SORT_BY_KEY = 0;
049: public static final int SORT_BY_VALUE = 1;
050:
051: private static final String[] HEADER = { "Property", "Value" };
052:
053: private int sortBy;
054: private KeyValuePair[] valuePairs;
055:
056: public PropertyWrapperModel() {
057: this (SORT_BY_KEY);
058: }
059:
060: public PropertyWrapperModel(int sortBy) {
061: this .sortBy = sortBy;
062: }
063:
064: public PropertyWrapperModel(Properties values) {
065: this (values, SORT_BY_KEY);
066: }
067:
068: public PropertyWrapperModel(Properties values, int sortBy) {
069: this .sortBy = sortBy;
070: setValues(values);
071: }
072:
073: public PropertyWrapperModel(Hashtable values) {
074: this (values, SORT_BY_KEY);
075: }
076:
077: public PropertyWrapperModel(Hashtable values, int sortBy) {
078: this .sortBy = sortBy;
079: setValues(values, false);
080: }
081:
082: public void setValues(Properties values) {
083: int count = 0;
084: String key = null;
085: valuePairs = new KeyValuePair[values.size()];
086:
087: for (Enumeration i = values.propertyNames(); i
088: .hasMoreElements();) {
089: key = (String) i.nextElement();
090: valuePairs[count++] = new KeyValuePair(key, values
091: .getProperty(key));
092: }
093: fireTableDataChanged();
094: }
095:
096: public void setValues(Hashtable values, boolean sort) {
097: int count = 0;
098: String key = null;
099: valuePairs = new KeyValuePair[values.size()];
100:
101: for (Enumeration i = values.keys(); i.hasMoreElements();) {
102: key = (String) i.nextElement();
103: valuePairs[count++] = new KeyValuePair(key, (String) values
104: .get(key));
105: }
106:
107: if (sort) {
108: sort();
109: } else {
110: fireTableDataChanged();
111: }
112:
113: }
114:
115: public void sort() {
116: if (valuePairs == null || valuePairs.length == 0) {
117: return;
118: }
119: Arrays.sort(valuePairs, new KeyValuePairSorter());
120: fireTableDataChanged();
121: }
122:
123: public void sort(int sortBy) {
124: this .sortBy = sortBy;
125: sort();
126: }
127:
128: public int getColumnCount() {
129: return 2;
130: }
131:
132: public int getRowCount() {
133: if (valuePairs == null) {
134: return 0;
135: }
136: return valuePairs.length;
137: }
138:
139: public boolean isCellEditable(int row, int col) {
140: return true;
141: }
142:
143: public Object getValueAt(int row, int col) {
144: KeyValuePair value = valuePairs[row];
145:
146: if (col == 0) {
147: return value.key;
148: } else {
149: return value.value;
150: }
151:
152: }
153:
154: public String getColumnName(int col) {
155: return HEADER[col];
156: }
157:
158: class KeyValuePairSorter implements Comparator {
159:
160: public int compare(Object obj1, Object obj2) {
161: KeyValuePair pair1 = (KeyValuePair) obj1;
162: KeyValuePair pair2 = (KeyValuePair) obj2;
163:
164: String value1 = null;
165: String value2 = null;
166:
167: if (sortBy == SORT_BY_KEY) {
168: value1 = pair1.key;
169: value2 = pair2.key;
170: } else {
171: value1 = pair1.value;
172: value2 = pair2.value;
173: }
174:
175: int result = value1.compareTo(value2);
176:
177: if (result < 0) {
178: return -1;
179: } else if (result > 0) {
180: return 1;
181: } else {
182: return 0;
183: }
184: }
185:
186: } // class KeyValuePairSorter
187:
188: }
|