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.ui.console.CSMARTConsoleModel;
029: import org.cougaar.tools.csmart.util.ReadOnlyProperties;
030:
031: import javax.swing.table.AbstractTableModel;
032: import java.util.ArrayList;
033: import java.util.Enumeration;
034: import java.util.Properties;
035:
036: /**
037: * Creates a table of Node Arguments;
038: * first column is Argument name
039: * second column is Argument Value
040: */
041: public class NodeArgumentTableModel extends AbstractTableModel {
042: Properties props;
043: ArrayList names = new ArrayList();
044: ArrayList values = new ArrayList();
045: ArrayList globalFlags;
046: boolean isLocal;
047:
048: public NodeArgumentTableModel(Properties props, boolean isLocal) {
049: this .props = props;
050: this .isLocal = isLocal;
051: if (isLocal)
052: globalFlags = new ArrayList();
053: Enumeration keys = props.propertyNames();
054: while (keys.hasMoreElements()) {
055: String key = (String) keys.nextElement();
056: if (!key.equals(CSMARTConsoleModel.COMMAND_ARGUMENTS)) {
057: names.add(key);
058: values.add(props.getProperty(key));
059: if (isLocal) {
060: // if local value is null, then display *
061: // to indicate value is from global properties
062: if (props.get(key) == null)
063: globalFlags.add("*");
064: else
065: globalFlags.add("");
066: }
067: }
068: }
069: }
070:
071: /**
072: * Check for editability of read only property values.
073: */
074: public boolean isCellEditable(int row, int col) {
075: if ((col == 0 || col == 1)) {
076: if (props instanceof ReadOnlyProperties) {
077: String name = (String) names.get(row);
078: if (props.getProperty(name) == null)
079: return true;
080: else
081: return !((ReadOnlyProperties) props)
082: .isReadOnly(name);
083: } else
084: return true;
085: } else
086: return false;
087: }
088:
089: public int getRowCount() {
090: return names.size();
091: }
092:
093: public int getColumnCount() {
094: if (isLocal)
095: return 3;
096: else
097: return 2;
098: }
099:
100: public Object getValueAt(int row, int column) {
101: if (row < 0 || row >= getRowCount() || column < 0
102: || column >= getColumnCount())
103: return null; // index out of range
104: if (column == 0)
105: return names.get(row);
106: else if (column == 1)
107: return values.get(row);
108: else if (column == 2)
109: return globalFlags.get(row);
110: else
111: return null;
112: }
113:
114: public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
115: if (rowIndex < 0 || rowIndex >= getRowCount()
116: || columnIndex < 0 || columnIndex >= getColumnCount())
117: return; // index out of range
118:
119: // trim whitespace off of the value.
120: if (aValue instanceof String) {
121: aValue = ((String) aValue).trim();
122:
123: // AppServer cant handle values containing whitespace
124: if (((String) aValue).indexOf(' ') != -1) {
125: // Now what? Reject edit? Tell user?
126: return;
127: }
128: }
129:
130: // See if the value really changed. If not, return
131: if ((aValue == null && getValueAt(rowIndex, columnIndex) == null)
132: || (aValue != null && aValue.equals(getValueAt(
133: rowIndex, columnIndex)))) {
134: return;
135: }
136:
137: // changing the name or value makes it local
138: if (columnIndex == 0) { // it's a name
139: // What if aValue is now null or empty? Is that allowed? I think not.
140: if (aValue == null || aValue.toString().equals("")) {
141: return;
142: }
143: names.set(rowIndex, aValue);
144: } else if (columnIndex == 1) { // it's a value
145: values.set(rowIndex, aValue);
146: } else if (columnIndex == 2)
147: return; // global flag is not editable
148:
149: fireTableCellUpdated(rowIndex, columnIndex);
150: if (isLocal) {
151: globalFlags.set(rowIndex, "");
152: fireTableCellUpdated(rowIndex, 2);
153: }
154: }
155:
156: public String getColumnName(int column) {
157: if (column >= getColumnCount())
158: return null;
159: if (column == 0)
160: return "Name";
161: else if (column == 1)
162: return "Value";
163: else if (column == 2)
164: return "Global";
165: else
166: return null; // invalid column index
167: }
168:
169: /**
170: * If name exists, set value to new value, else add new row.
171: */
172: public void addRow(String name, String value) {
173: if (name == null || value == null)
174: return;
175:
176: // Trim excess whitespace off of value and name
177: if (value != null)
178: value = value.trim();
179: if (name != null)
180: name = name.trim();
181:
182: int index = names.indexOf(name);
183: if (index == -1) {
184: names.add(name);
185: values.add(value);
186: if (isLocal)
187: globalFlags.add("");
188: fireTableRowsInserted(index, index);
189: } else {
190: if (isCellEditable(index, 0)) {
191: values.set(index, value);
192: if (isLocal)
193: globalFlags.set(index, "");
194: fireTableRowsUpdated(index, index);
195: }
196: }
197: }
198:
199: public void removeRow(int row) {
200: if (isCellEditable(row, 0)) {
201: names.remove(row);
202: values.remove(row);
203: if (isLocal)
204: globalFlags.remove(row);
205: fireTableRowsDeleted(row, row);
206: }
207: }
208:
209: /**
210: * Update properties based on table values.
211: * @return true if any modifications, false otherwise
212: */
213: public boolean updateProperties() {
214: boolean isModified = false;
215: // add new or modified properties
216: for (int i = 0; i < names.size(); i++) {
217: String name = (String) names.get(i);
218: if (name.length() == 0)
219: continue; // ignore blank names
220: String value = props.getProperty(name);
221: if (value == null || !value.equals(values.get(i))) {
222: props.setProperty(name, (String) values.get(i));
223: isModified = true;
224: }
225: }
226: // remove properties
227: Enumeration keys = props.propertyNames();
228: while (keys.hasMoreElements()) {
229: String key = (String) keys.nextElement();
230: if (key.equals(CSMARTConsoleModel.COMMAND_ARGUMENTS))
231: continue;
232: if (!names.contains(key)) {
233: props.remove(key);
234: isModified = true;
235: }
236: }
237: return isModified;
238: }
239: }
|