summaryrefslogtreecommitdiff
path: root/source/com/c2kernel/graph/view/PropertyTableModel.java
blob: b1e69b1123b0eb40d3833b52fcd9bff688379483 (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
package com.c2kernel.graph.view;

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

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<String, Object> sourceMap = new HashMap<String, Object>();
    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);
            if (colIndex == 0)
                return rowName;
            else
                return sourceMap.get(rowName);
        }
    }

    @Override
	public void setValueAt(Object value, int rowIndex, int colIndex)
    {
        synchronized (sourceMap) {
            if (colIndex == 0) return;
            String rowName = sortedNameList.get(rowIndex);
            Class<? extends Object> 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<String, Object> 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==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);
    }
}