001: /*
002: * <copyright>
003: *
004: * Copyright 2001-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026: package org.cougaar.tools.csmart.ui.experiment;
027:
028: import org.cougaar.tools.csmart.core.property.Property;
029:
030: import javax.swing.*;
031: import javax.swing.table.AbstractTableModel;
032: import javax.swing.table.DefaultTableCellRenderer;
033: import javax.swing.table.TableCellRenderer;
034: import java.awt.*;
035: import java.util.ArrayList;
036: import java.util.Collections;
037: import java.util.Comparator;
038: import java.util.List;
039:
040: public abstract class PropTableModelBase extends AbstractTableModel {
041: public static final int LABEL_COL = 0;
042: public static final int VALUE_COL = 1;
043: public static final int NCOLS = 2;
044: private List properties = new ArrayList();
045:
046: protected TableCellRenderer myRenderer = new DefaultTableCellRenderer() {
047: public Component getTableCellRendererComponent(JTable table,
048: Object value, boolean isSelected, boolean hasFocus,
049: int row, int column) {
050: Component result = super .getTableCellRendererComponent(
051: table, value, isSelected, hasFocus, row, column);
052: if (result instanceof JComponent) {
053: JComponent jc = (JComponent) result;
054: Property prop = getProperty(row);
055: if (column == LABEL_COL && prop.getHelp() != null) {
056: jc.setToolTipText("Click for help");
057: } else {
058: jc.setToolTipText(prop.getToolTip());
059: }
060: }
061: return result;
062: }
063: };
064:
065: public TableCellRenderer getCellRenderer() {
066: return myRenderer;
067: }
068:
069: public Property getProperty(int row) {
070: return (Property) properties.get(row);
071: }
072:
073: public void addProperty(Property prop) {
074: int sz = properties.size();
075: properties.add(prop);
076: // ensure that properties are in alphabetical order
077: Collections.sort(properties, propertyComparator);
078: fireTableRowsInserted(sz, sz);
079: }
080:
081: private static Comparator propertyComparator = new Comparator() {
082: public int compare(Object o1, Object o2) {
083: Property p1 = (Property) o1;
084: Property p2 = (Property) o2;
085: return p1.getName().compareTo(p2.getName());
086: }
087: };
088:
089: public void removeProperty(Property prop) {
090: int sz = properties.indexOf(prop);
091: if (sz >= 0) {
092: properties.remove(sz);
093: fireTableRowsDeleted(sz, sz);
094: }
095: }
096:
097: public void clear() {
098: int sz = properties.size();
099: properties.clear();
100: if (sz > 0)
101: fireTableRowsDeleted(0, sz - 1);
102: }
103:
104: public int getColumnCount() {
105: return NCOLS;
106: }
107:
108: public Class getColumnClass(int col) {
109: switch (col) {
110: case LABEL_COL:
111: return String.class;
112: case VALUE_COL:
113: return String.class;
114: }
115: return null;
116: }
117:
118: public String getColumnName(int col) {
119: switch (col) {
120: case LABEL_COL:
121: return "Name";
122: case VALUE_COL:
123: return "Value";
124: }
125: return null;
126: }
127:
128: public int getRowCount() {
129: return properties.size();
130: }
131:
132: public boolean isCellEditable(int row, int col) {
133: return col == VALUE_COL;
134: }
135:
136: protected abstract String render(Property prop);
137:
138: protected abstract void setValue(Property prop, Object newValue);
139:
140: public Object getValueAt(int row, int col) {
141: Property prop = getProperty(row);
142: switch (col) {
143: case LABEL_COL:
144: return prop.getLabel();
145: case VALUE_COL:
146: return render(prop);
147: }
148: return null;
149: }
150:
151: public void setValueAt(Object newValue, int row, int col) {
152: Property prop = getProperty(row);
153: setValue(prop, newValue);
154: }
155:
156: public int getRowForProperty(Property p) {
157: return properties.indexOf(p);
158: }
159: }
|