001    /* EVolve - an Extensible Software Visualization Framework
002     * Copyright (C) 2001-2002 Qin Wang
003     *
004     * This library is free software; you can redistribute it and/or
005     * modify it under the terms of the GNU Library General Public
006     * License as published by the Free Software Foundation; either
007     * version 2 of the License, or (at your option) any later version.
008     *
009     * This library is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
012     * Library General Public License for more details.
013     *
014     * You should have received a copy of the GNU Library General Public
015     * License along with this library; if not, write to the
016     * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
017     * Boston, MA 02111-1307, USA.
018     */
019    
020    /*
021     * EVolve is distributed at http://www.sable.mcgill.ca/EVolve/
022     */
023    
024    package EVolve.visualization;
025    
026    import javax.swing.table.AbstractTableModel;
027    
028    /**
029     * Model of a two-column table.
030     */
031    public class TableVizModel extends AbstractTableModel implements Cloneable{
032        private String[] name; // names of the columns
033        private String[] left; // left column
034        private int[] right; // right column
035    
036        /**
037         * Creates a table model.
038         */
039        public TableVizModel() {
040            name = new String[2];
041            name[0] = "Left Column";
042            name[1] = "Right Column";
043            left = new String[0];
044            right = new int[0];
045        }
046    
047        /**
048         * Gets the number of columns.
049         *
050         * @return  number of columns
051         */
052        public int getColumnCount() {
053            return name.length;
054        }
055    
056        /**
057         * Gets the name of a column
058         *
059         * @param   col the column
060         * @return  name of the column
061         */
062        public String getColumnName(int col) {
063            return name[col];
064        }
065    
066        /**
067         * Gets the value of the table.
068         *
069         * @param   row the row
070         * @param   col the column
071         * @return  the value
072         */
073        public Object getValueAt(int row, int col) {
074            if (col == 0) {
075                return left[row];
076            } else {
077                return new Integer(right[row]);
078            }
079        }
080    
081        /**
082         * Gets the number of rows.
083         *
084         * @return  number of rows
085         */
086        public int getRowCount() {
087            return left.length;
088        }
089    
090        /**
091         * Whether the cell is editable.
092         *
093         * @param   row the row
094         * @param   col the column
095         * @return  false, not editable
096         */
097        public boolean isCellEditable(int row, int col) {
098            return false;
099        }
100    
101        /**
102         * Sets the names of the columns.
103         *
104         * @param   nameLeft name of the left column
105         * @param   nameRight name of the right column
106         */
107        public void setName(String nameLeft, String nameRight) {
108            name[0] = nameLeft;
109            name[1] = nameRight;
110        }
111    
112        /**
113         * Sets the value of the table.
114         *
115         * @param   left the left column
116         * @param   right the right column
117         */
118        public void setValue(String[] left, int[] right) {
119            this.left = left;
120            this.right = right;
121        }
122    
123        public Object clone() {
124            TableVizModel o = null;
125            try {
126                o = (TableVizModel)super.clone();
127            } catch (CloneNotSupportedException e) {
128                e.printStackTrace();
129                return null;
130            }
131            if (name != null) {
132                o.name = new String[name.length];
133                for (int i=0; i<name.length; i++)
134                    o.name[i] = name[i];
135            }
136            if (left != null) {
137                o.left = new String[left.length];
138                for (int i=0; i<left.length; i++)
139                    o.left[i] = left[i];
140            }
141            if (right != null) {
142                o.right = new int[right.length];
143                for (int i=0; i<right.length; i++)
144                    o.right[i] = right[i];
145            }
146    
147            return o;
148        }
149    }