001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.impl.wsdl.panels.request;
014:
015: import java.util.ArrayList;
016: import java.util.List;
017:
018: import javax.swing.table.AbstractTableModel;
019: import javax.swing.table.TableModel;
020:
021: import com.eviware.soapui.support.types.StringToStringMap;
022:
023: /**
024: * TableModel for StringToString Maps
025: *
026: * @author ole.matzura
027: */
028:
029: public class StringToStringMapTableModel extends AbstractTableModel
030: implements TableModel {
031: private StringToStringMap data;
032: private final String keyCaption;
033: private final String valueCaption;
034: private List<String> keyList;
035: private final boolean editable;
036:
037: public StringToStringMapTableModel(StringToStringMap data,
038: String keyCaption, String valueCaption, boolean editable) {
039: this .data = data;
040: this .keyCaption = keyCaption;
041: this .valueCaption = valueCaption;
042: this .editable = editable;
043:
044: keyList = data == null ? new ArrayList<String>()
045: : new ArrayList<String>(data.keySet());
046: }
047:
048: public int getColumnCount() {
049: return 2;
050: }
051:
052: public String getColumnName(int arg0) {
053: return arg0 == 0 ? keyCaption : valueCaption;
054: }
055:
056: public boolean isCellEditable(int arg0, int arg1) {
057: return editable;
058: }
059:
060: public Class<?> getColumnClass(int arg0) {
061: return String.class;
062: }
063:
064: public void setValueAt(Object arg0, int arg1, int arg2) {
065: String oldKey = keyList.get(arg1);
066:
067: if (arg2 == 0) {
068: String value = data.get(oldKey);
069:
070: data.remove(oldKey);
071: data.put(arg0.toString(), value);
072:
073: keyList.set(arg1, arg0.toString());
074: } else {
075: data.put(oldKey, arg0.toString());
076: }
077:
078: fireTableCellUpdated(arg1, arg2);
079: }
080:
081: public int getRowCount() {
082: return data == null ? 0 : data.size();
083: }
084:
085: public Object getValueAt(int arg0, int arg1) {
086: String str = keyList.get(arg0);
087: return arg1 == 0 ? str : data.get(str);
088: }
089:
090: public void add(String key, String value) {
091: if (keyList.contains(key)) {
092: data.put(key, value);
093: fireTableCellUpdated(keyList.indexOf(key), 1);
094: } else {
095: data.put(key, value);
096: keyList.add(key);
097: fireTableRowsInserted(keyList.size(), keyList.size());
098: }
099: }
100:
101: public void remove(int row) {
102: String key = keyList.get(row);
103: keyList.remove(row);
104: data.remove(key);
105:
106: fireTableRowsDeleted(row, row);
107: }
108:
109: public StringToStringMap getData() {
110: return new StringToStringMap(data);
111: }
112:
113: public void setData(StringToStringMap data) {
114: this .data = data == null ? new StringToStringMap() : data;
115:
116: keyList = new ArrayList<String>(this.data.keySet());
117: fireTableDataChanged();
118: }
119: }
|