001    // GraphLab Project: http://graphlab.sharif.edu
002    // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology
003    // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/
004    package graphlab.ui.components.gpropertyeditor;
005    
006    import javax.swing.table.AbstractTableModel;
007    import java.util.Vector;
008    
009    /**
010     * it is the Table Model which is used to create a property editor. it has t columns and multiple rows,
011     * each row is a property, first column is the name of property and second column is the value.
012     * the names are stored as Object.
013     *
014     * @author Azin Azadi
015     *         Email :
016     */
017    
018    public class GPropertyTableModel extends AbstractTableModel {
019    
020        /**
021         *
022         */
023        private static final long serialVersionUID = -1101017043084681545L;
024    
025        /**
026         * constructor
027         */
028        public GPropertyTableModel() {
029        }
030    
031        /**
032         * the number of properties, stored
033         */
034        public int getRowCount() {
035            return values.size();
036        }
037    
038        public String getColumnName(int column) {
039            return (column == 0 ? "Name" : "Value");
040        }
041    
042        /*return 2**/
043        public int getColumnCount() {
044            return 2;
045        }
046    
047        Vector values = new Vector();
048        Vector names = new Vector();
049    
050        /**
051         * clears all values ans names of the table
052         */
053        public void clear() {
054            values.clear();
055            names.clear();
056            fireTableStructureChanged();
057        }
058    
059        /**
060         * adds a row to the table
061         */
062        public void addData(Object name, Object value) {
063            names.add(name);
064            values.add(value);
065            super.fireTableDataChanged();
066        }
067    
068        /**
069         * sets the value of the given row(name). returns false if the given name does not exist in the properties
070         */
071        public boolean setValue(Object name, Object value) {
072            int ri = names.indexOf(name);
073            //System.out.println(name+" "+ri);
074            if (ri != -1) {
075                setValueAt(value, ri, ri);
076                super.fireTableDataChanged();
077                return true;
078            }
079            return false;
080        }
081    
082        @Override
083        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
084            //System.out.println(""+aValue);
085            values.set(rowIndex, aValue);
086            fireTableRowsUpdated(rowIndex, rowIndex);
087    //        super.fireTableDataChanged();
088        }
089    
090        /**
091         * the second column cells(values) are editable, and the first are not
092         */
093        @Override
094        public boolean isCellEditable(int rowIndex, int columnIndex) {
095            return columnIndex == 1;
096        }
097    
098        @Override
099        public Class<?> getColumnClass(int columnIndex) {
100            //super.getColumnClass(columnIndex);
101            if (columnIndex == 0) {
102                return String.class;
103            } else {
104                return GCellEditor.class;
105            }
106        }
107    
108    
109        public Object getValueAt(int rowIndex, int columnIndex) {
110            if (columnIndex == 1) {
111                return values.get(rowIndex);
112            } else {
113                return names.get(rowIndex);
114            }
115        }
116    
117    }