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 java.awt.BorderLayout;
027    import java.awt.Color;
028    import java.awt.Component;
029    import javax.swing.*;
030    import EVolve.data.Element;
031    import EVolve.data.ValueComparator;
032    import EVolve.util.xmlutils.datastructures.SerializedVisualization;
033    
034    /**
035     * Two-column table.
036     */
037    public class TableViz extends Visualization {
038        private ReferenceDimension leftColumn; // dimension, left column
039        private ValueDimension rightColumn; // dimension, right column
040    
041        private JTable table; // the table
042        private TableVizModel model; // table model
043    
044        private int[] value; // value of the right column
045    
046        private JCheckBox selectionOptions[], chkSelectOccurredEntities;
047    
048        /**
049         * Creates a table.
050         */
051        public TableViz() {
052            model = new TableVizModel();
053            table = new JTable(model);
054    
055            JScrollPane scrollPane = new JScrollPane(table);
056            scrollPane.setBackground(Color.white);
057            scrollPane.getViewport().setBackground(Color.white);
058            ((JPanel)panel).add(scrollPane, BorderLayout.CENTER);
059    
060            addPopupTrigger(table);
061            addPopupTrigger(scrollPane.getViewport());
062        }
063    
064        public Dimension[] createDimension() {
065            leftColumn = new ReferenceDimension(true);
066            rightColumn = new ValueDimension();
067    
068            Dimension[] returnVal = new Dimension[2];
069            returnVal[0] = leftColumn;
070            returnVal[1] = rightColumn;
071    
072            leftColumn.addSortSchemes("Value");
073    
074            return returnVal;
075        }
076    
077        protected Component createPanel() {
078            JPanel returnVal = new JPanel(new BorderLayout());
079            returnVal.setBackground(Color.white);
080            return returnVal;
081        }
082    
083        protected JPanel createConfigurationPanel() {
084            return null;
085        }
086    
087        protected void updateConfiguration() {
088            model = new TableVizModel();
089            model.setName(leftColumn.getName(), rightColumn.getName());
090            table.setModel(model);
091        }
092    
093        public void preVisualize() {
094            value = new int[leftColumn.getMaxEntityNumber()];
095            for (int i = 0; i < value.length; i++) {
096                value[i] = 0;
097            }
098        }
099    
100        public void receiveElement(Element element) {
101            if (element.isOptional()) return;
102    
103            value[(int)leftColumn.getField(element)] += rightColumn.getField(element);
104        }
105    
106        public void visualize() {
107            leftColumn.addComparator(new ValueComparator("Value", false, value, leftColumn.getEntityName2IntMap()));
108            leftColumn.selectComparator(comboSortSchemes[0].getSelectedItem().toString());
109            sort();
110        }
111    
112        public void sort() {
113            String[] sortedName = new String[leftColumn.getEntityNumber()];
114            int[] sortedValue = new int[leftColumn.getEntityNumber()];
115    
116            for (int i = 0; i < leftColumn.getMaxEntityNumber(); i++) {
117                int sortedIndex = leftColumn.getSortedIndex(i);
118                if (sortedIndex != -1) {
119                    sortedName[sortedIndex] = leftColumn.getEntity(sortedIndex).getName();
120                    sortedValue[sortedIndex] = value[i];
121                }
122            }
123    
124            model = new TableVizModel();
125            model.setName(leftColumn.getName(), rightColumn.getName());
126            model.setValue(sortedName, sortedValue);
127            table.setModel(model);
128        }
129    
130        public void makeSelection() {
131            preMakeSelection();
132            if (selectionName==null) return;
133            leftColumn.makeSelection(selectionName,subjectDefinition.getType(),table.getSelectedRows());
134        }
135    
136        public SerializedVisualization getCurrentConfiguration() {
137            SerializedVisualization data = super.getCurrentConfiguration();
138            data.xAxis = leftColumn.getName();
139            data.yAxis = rightColumn.getName();
140            data.xAxisSortScheme = leftColumn.getSelectedComparatorName();
141    
142            return data;
143        }
144    
145        public ReferenceDimension getLinkableDimension(int dim) {
146            return null;
147        }
148    
149        public AutoImage getImage() {
150            return null;
151        }
152    
153        public long getxMax() {
154            return -1;
155        }
156    
157        public void clearMagnifier() {
158            return;
159        }
160    
161        public void setImage(AutoImage newImage) {
162            return;
163        }
164    
165        public JCheckBox[] createSelectionOptions() {
166            if (selectionOptions != null) return selectionOptions;
167    
168            chkSelectOccurredEntities = new JCheckBox("Occurred Entities");
169            chkSelectOccurredEntities.setSelected(true);
170            chkSelectOccurredEntities.setEnabled(false);
171    
172            selectionOptions = new JCheckBox[1];
173            selectionOptions[0] = chkSelectOccurredEntities;
174    
175            return selectionOptions;
176        }
177    
178        public Object clone() {
179            TableViz o = (TableViz)super.clone();
180    
181            o.leftColumn = (ReferenceDimension)leftColumn.clone();
182            o.rightColumn = (ValueDimension)rightColumn.clone();
183            o.dimension[0] = o.leftColumn;
184            o.dimension[1] = o.rightColumn;
185            o.panelConfiguration = o.createConfigurationPanel();
186            o.panel = new JPanel(new BorderLayout());
187            o.panel.setBackground(Color.white);
188            o.createDialog();
189            o.createMenu();
190    
191            if (value!=null) {
192                o.value = new int[value.length];
193                for (int i=0; i<value.length; i++)
194                    o.value[i] = value[i];
195            }
196    
197            o.model = (TableVizModel)model.clone();
198            o.table = new JTable(o.model);
199    
200            JScrollPane scrollPane = new JScrollPane(o.table);
201            scrollPane.setBackground(Color.white);
202            scrollPane.getViewport().setBackground(Color.white);
203            ((JPanel)o.panel).add(scrollPane, BorderLayout.CENTER);
204    
205            o.addPopupTrigger(o.table);
206            o.addPopupTrigger(scrollPane.getViewport());
207            return o;
208        }
209    }