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