summaryrefslogtreecommitdiff
path: root/src/main/java/com/c2kernel/gui/graph/view/PropertyTableModel.java
blob: 616c302b014774ff5cafb7a6bbc2fb3a00f55a18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package com.c2kernel.gui.graph.view;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import javax.swing.JOptionPane;
import javax.swing.event.TableModelEvent;
import javax.swing.table.AbstractTableModel;

import com.c2kernel.utils.CastorHashMap;
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 final String[] mColumnNames = { Language.translate("Name"), Language.translate("Value"), Language.translate("Abstract") };
    public CastorHashMap sourceMap = new CastorHashMap();
    public ArrayList<String> sortedNameList = new ArrayList<String>();
    boolean isEditable = false;

    public PropertyTableModel() {
        super();
    }

    @Override
	public int getColumnCount()
    {
        return mColumnNames.length;
    }
    @Override
	public String getColumnName(int col)
    {
        return mColumnNames[col];
    }
    @Override
	public int getRowCount()
    {
        synchronized (sourceMap) {
            return sourceMap.size();
        }
    }
    @Override
	public Object getValueAt(int rowIndex, int colIndex)
    {
        synchronized (sourceMap) {
            String rowName = sortedNameList.get(rowIndex);
            switch (colIndex) {
            case 0:
            	return rowName;
            case 1:
            	return sourceMap.get(rowName);
            case 2:
            	return sourceMap.getAbstract().contains(rowName);
            default:
            	return "";
            }
        }
    }

    @Override
	public void setValueAt(Object value, int rowIndex, int colIndex)
    {
        synchronized (sourceMap) {
            if (colIndex == 0) return;
            String rowName = sortedNameList.get(rowIndex);
            if (colIndex == 1) {
	            Class<? extends Object> oldElement = sourceMap.get(rowName).getClass();
	            if (oldElement == Double.class && value.getClass() == String.class)
	        		try {
	        			value = Double.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);
            }
            else if (colIndex == 2) {
            	Boolean boolVal = (Boolean)value;
            	if (boolVal)
            		sourceMap.getAbstract().add(rowName);
            	else
            		sourceMap.getAbstract().remove(rowName);
            }
        }
        fireTableCellUpdated(rowIndex, colIndex);
    }

    @Override
	public Class<?> getColumnClass(int columnIndex) {
		switch (columnIndex) {
		case 2:
			return Boolean.class;
		default:
			return String.class;
		}
	}

	public void setMap(CastorHashMap props) {
        synchronized (sourceMap) {
            sourceMap = props;
            sortedNameList = new ArrayList<String>(props.size());
            for (String string : props.keySet())
				sortedNameList.add(string);

            Collections.sort(sortedNameList, new Comparator<String>() {
                @Override
				public int compare(String o1, String o2) {
                    return (o1.compareToIgnoreCase(o2));
                }
            });
        }
        fireTableChanged(new TableModelEvent(this));
    }

    @Override
	public boolean isCellEditable(int row, int col)
    {
        return col>0 && 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, boolean isAbstract) {
        sourceMap.put(text, object, isAbstract);
        setMap(sourceMap);
    }

    /**
     * @param object
     */
    public void delProperty(Object propName) {
        sourceMap.remove(propName);
        setMap(sourceMap);
    }
}