diff options
Diffstat (limited to 'source/com/c2kernel/graph/view/PropertyTableModel.java')
| -rwxr-xr-x | source/com/c2kernel/graph/view/PropertyTableModel.java | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/source/com/c2kernel/graph/view/PropertyTableModel.java b/source/com/c2kernel/graph/view/PropertyTableModel.java new file mode 100755 index 0000000..9755cf6 --- /dev/null +++ b/source/com/c2kernel/graph/view/PropertyTableModel.java @@ -0,0 +1,125 @@ +package com.c2kernel.graph.view;
+
+import java.util.*;
+
+import javax.swing.JOptionPane;
+import javax.swing.event.TableModelEvent;
+import javax.swing.table.AbstractTableModel;
+
+import com.c2kernel.utils.Language;
+
+/**************************************************************************
+ *
+ * $Revision: 1.4 $
+ * $Date: 2005/08/02 07:50:10 $
+ *
+ * Copyright (C) 2003 CERN - European Organization for Nuclear Research
+ * All rights reserved.
+ **************************************************************************/
+
+public class PropertyTableModel extends AbstractTableModel {
+
+ private String[] mColumnNames = { Language.translate("Name"), Language.translate("Value") };
+ HashMap sourceMap = new HashMap();
+ ArrayList sortedNameList = new ArrayList();
+ boolean isEditable = false;
+
+ public PropertyTableModel() {
+ super();
+ }
+
+ public int getColumnCount()
+ {
+ return mColumnNames.length;
+ }
+ public String getColumnName(int col)
+ {
+ return mColumnNames[col];
+ }
+ public int getRowCount()
+ {
+ synchronized (sourceMap) {
+ return sourceMap.size();
+ }
+ }
+ public Object getValueAt(int rowIndex, int colIndex)
+ {
+ synchronized (sourceMap) {
+ String rowName = (String)sortedNameList.get(rowIndex);
+ if (colIndex == 0)
+ return rowName;
+ else
+ return sourceMap.get(rowName);
+ }
+ }
+
+ public void setValueAt(Object value, int rowIndex, int colIndex)
+ {
+ synchronized (sourceMap) {
+ if (colIndex == 0) return;
+ String rowName = (String)sortedNameList.get(rowIndex);
+ Class oldElement = sourceMap.get(rowName).getClass();
+ if (oldElement == Float.class && value.getClass() == String.class)
+ try {
+ value = Float.valueOf((String)value);
+ } catch (Exception ex) { }
+ if (value.getClass() != oldElement)
+ JOptionPane.showMessageDialog(null, "This property should contain a "+oldElement.getName()+" not a "+value.getClass().getName(), "Incorrect datatype", JOptionPane.ERROR_MESSAGE);
+ else {
+ sourceMap.put(rowName, value);
+ fireTableCellUpdated(rowIndex, colIndex);
+ }
+ }
+ }
+
+ public void setMap(HashMap props) {
+ synchronized (sourceMap) {
+ sourceMap = props;
+ sortedNameList = new ArrayList(props.size());
+ for (Iterator keys = props.keySet().iterator(); keys.hasNext();)
+ sortedNameList.add(keys.next());
+
+ Collections.sort(sortedNameList, new Comparator() {
+ public int compare(Object o1, Object o2) {
+ return ((String)o1).compareToIgnoreCase((String)o2);
+ }
+ });
+ }
+ fireTableChanged(new TableModelEvent(this));
+ }
+
+ public boolean isCellEditable(int row, int col)
+ {
+ return col==1 && isEditable;
+ }
+
+ /**
+ * @return Returns the isEditable.
+ */
+ public boolean isEditable() {
+ return isEditable;
+ }
+ /**
+ * @param isEditable The isEditable to set.
+ */
+ public void setEditable(boolean isEditable) {
+ this.isEditable = isEditable;
+ }
+
+ /**
+ * @param text
+ * @param object
+ */
+ public void addProperty(String text, Object object) {
+ sourceMap.put(text,object);
+ setMap(sourceMap);
+ }
+
+ /**
+ * @param object
+ */
+ public void delProperty(Object propName) {
+ sourceMap.remove(propName);
+ setMap(sourceMap);
+ }
+}
|
