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.gui.metadata;
019:
020: import java.util.HashSet;
021: import java.util.LinkedList;
022: import java.util.List;
023: import java.util.Set;
024:
025: import javax.swing.table.AbstractTableModel;
026:
027: import de.finix.contelligent.client.base.ContelligentComponent;
028: import de.finix.contelligent.client.i18n.Resources;
029: import de.finix.contelligent.client.util.SortableTableModel;
030:
031: public class MetadataTableModel extends AbstractTableModel implements
032: SortableTableModel {
033:
034: private boolean editable = false;
035:
036: private ContelligentComponent component;
037:
038: private List keys;
039:
040: private List values;
041:
042: private List originalKeys;
043:
044: private List originalValues;
045:
046: private String[] columnNames = { Resources.getLocalString("key"),
047: Resources.getLocalString("value") };
048:
049: public MetadataTableModel(ContelligentComponent component,
050: String visibleGroup) {
051: this .component = component;
052: updateData();
053: }
054:
055: List getKeys() {
056: return keys;
057: }
058:
059: List getValues() {
060: return values;
061: }
062:
063: int addRow() {
064: keys.add("");
065: values.add("");
066: return keys.size();
067: }
068:
069: void deleteRow(int row) {
070: keys.remove(row);
071: values.remove(row);
072: }
073:
074: public boolean isColumnSortable(int column) {
075: return false;
076: }
077:
078: public String getComparable(Object o, int column) {
079: return (String) o;
080: }
081:
082: public synchronized void updateData() {
083: keys = component.getMetadataKeys();
084: values = component.getMetadataValues();
085: originalKeys = component.getMetadataKeys();
086: originalValues = component.getMetadataValues();
087: }
088:
089: private Set findAlterations(List aKeys, List aValues, List bKeys,
090: List bValues) {
091: Set results = new HashSet();
092: for (int i = 0; i < aKeys.size(); i++) {
093: String aKey = (String) aKeys.get(i);
094: String aValue = (String) aValues.get(i);
095: boolean found = false;
096: for (int j = 0; j < bKeys.size(); j++) {
097: String bKey = (String) bKeys.get(j);
098: if (aKey.equals(bKey)) {
099: String bValue = (String) bValues.get(j);
100: if (aValue.equals(bValue)) {
101: found = true;
102: }
103: }
104: }
105: if (!found) {
106: results.add(aKey);
107: }
108: }
109: return results;
110: }
111:
112: public synchronized Set getAlteredKeys() {
113: Set results = new HashSet();
114: // forward check
115: results.addAll(findAlterations(originalKeys, originalValues,
116: keys, values));
117: // reverse check
118: results.addAll(findAlterations(keys, values, originalKeys,
119: originalValues));
120: return results;
121: }
122:
123: public synchronized List getValuesForKey(String key) {
124: List results = new LinkedList();
125: for (int i = 0; i < keys.size(); i++) {
126: if (key.equals((String) keys.get(i))) {
127: results.add((String) values.get(i));
128: }
129: }
130: return results;
131: }
132:
133: public int getColumnCount() {
134: return columnNames.length;
135: }
136:
137: public int getRowCount() {
138: return keys.size();
139: }
140:
141: public String getColumnName(int col) {
142: return columnNames[col];
143: }
144:
145: public Object getValueAt(int row, int col) {
146: if (col == 0) {
147: return keys.get(row);
148: } else {
149: return values.get(row);
150: }
151: }
152:
153: public Class getColumnClass(int c) {
154: return String.class;
155: }
156:
157: public boolean isCellEditable(int row, int col) {
158: return editable;
159: }
160:
161: public void setValueAt(Object value, int row, int col) {
162: component.setModified(true);
163: if (col == 0) {
164: keys.set(row, value);
165: } else {
166: values.set(row, value);
167: }
168: fireTableCellUpdated(row, 0);
169: fireTableCellUpdated(row, 1);
170: }
171:
172: public void setEditable(boolean editable) {
173: this .editable = editable;
174: }
175:
176: public void removeEmptyRows() {
177: int oldSize = keys.size();
178: for (int i = 0; i < keys.size(); i++) {
179: String key = (String) keys.get(i);
180: String value = (String) values.get(i);
181: if ((key == null) || (value == null)) {
182: deleteRow(i);
183: i--;
184: } else if ((key.length() == 0) || (value.length() == 0)) {
185: deleteRow(i);
186: i--;
187: }
188: }
189: if (oldSize != keys.size()) {
190: fireTableStructureChanged();
191: }
192: }
193:
194: }
|