001    /*
002     * Copyright (C) 2008-2010 by Holger Arndt
003     *
004     * This file is part of the Universal Java Matrix Package (UJMP).
005     * See the NOTICE file distributed with this work for additional
006     * information regarding copyright ownership and licensing.
007     *
008     * UJMP is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU Lesser General Public License as
010     * published by the Free Software Foundation; either version 2
011     * of the License, or (at your option) any later version.
012     *
013     * UJMP is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016     * GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public
019     * License along with UJMP; if not, write to the
020     * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021     * Boston, MA  02110-1301  USA
022     */
023    
024    package org.ujmp.gui.plot;
025    
026    import java.awt.Color;
027    import java.awt.Component;
028    import java.awt.Dimension;
029    import java.awt.Graphics;
030    import java.awt.Graphics2D;
031    import java.util.ConcurrentModificationException;
032    
033    import javax.swing.JPanel;
034    import javax.swing.JTable;
035    import javax.swing.event.EventListenerList;
036    import javax.swing.event.ListSelectionEvent;
037    import javax.swing.event.ListSelectionListener;
038    import javax.swing.table.TableCellRenderer;
039    
040    import org.ujmp.core.Matrix;
041    import org.ujmp.gui.MatrixGUIObject;
042    import org.ujmp.gui.interfaces.CanBeUpdated;
043    import org.ujmp.gui.util.UIDefaults;
044    import org.ujmp.gui.util.UpdateListener;
045    
046    public class MatrixPlot extends JPanel implements TableCellRenderer,
047                    CanBeUpdated, ListSelectionListener {
048            private static final long serialVersionUID = -3845070497558608841L;
049    
050            private EventListenerList listenerList = null;
051    
052            private PlotSettings plotSettings = null;
053    
054            private XAxis xAxis = null;
055    
056            private YAxis yAxis = null;
057    
058            private ZeroAxis zeroAxis = null;
059    
060            private XGrid xGrid = null;
061    
062            private YGrid yGrid = null;
063    
064            private Traces traces = null;
065    
066            private PlotBackground plotBackground = null;
067    
068            private Selection selection = null;
069    
070            private RunningAveragePlot runningAveragePlot = null;
071    
072            public MatrixPlot(Matrix m) {
073                    this((MatrixGUIObject) m.getGUIObject(), false);
074            }
075    
076            public MatrixPlot(MatrixGUIObject m, boolean registerListeners) {
077                    this();
078                    plotSettings.setMatrixGUIObject(m);
079                    if (registerListeners) {
080                            m.getRowSelectionModel().addListSelectionListener(this);
081                    }
082            }
083    
084            public MatrixPlot() {
085                    this.plotSettings = new PlotSettings();
086                    this.xAxis = new XAxis(plotSettings);
087                    this.yAxis = new YAxis(plotSettings);
088                    this.zeroAxis = new ZeroAxis(plotSettings);
089                    this.xGrid = new XGrid(plotSettings);
090                    this.yGrid = new YGrid(plotSettings);
091                    this.plotBackground = new PlotBackground(plotSettings);
092                    this.traces = new Traces(plotSettings);
093                    this.selection = new Selection(plotSettings);
094                    this.runningAveragePlot = new RunningAveragePlot(plotSettings);
095                    setPreferredSize(new Dimension(800, 600));
096                    setMinimumSize(new Dimension(50, 50));
097            }
098    
099            public void addUpdateListener(UpdateListener l) {
100                    getListenerList().add(UpdateListener.class, l);
101            }
102    
103            public void removeUpdateListener(UpdateListener l) {
104                    getListenerList().remove(UpdateListener.class, l);
105            }
106    
107            public EventListenerList getListenerList() {
108                    if (listenerList == null) {
109                            listenerList = new EventListenerList();
110                    }
111                    return listenerList;
112            }
113    
114            public void fireUpdated() {
115                    if (listenerList != null) {
116                            for (Object o : listenerList.getListenerList()) {
117                                    if (o instanceof UpdateListener)
118                                            ((UpdateListener) o).updated();
119                            }
120                    }
121            }
122    
123            public void paintComponent(Graphics g) {
124                    try {
125                            super.paintComponent(g);
126                            MatrixGUIObject guiObject = plotSettings.getMatrixGUIObject();
127                            Graphics2D g2d = (Graphics2D) g;
128                            g2d.addRenderingHints(UIDefaults.AALIAS);
129    
130                            if (guiObject == null) {
131                                    return;
132                            }
133    
134                            plotSettings.setHeight(getHeight());
135                            plotSettings.setWidth(getWidth());
136                            plotSettings.setMinXValue(0.0);
137                            plotSettings.setMaxXValue(guiObject.getRowCount() - 1);
138                            plotSettings.setMinYValue(guiObject.getEstimatedMinValue(100));
139                            plotSettings.setMaxYValue(guiObject.getEstimatedMaxValue(100));
140    
141                            if (plotSettings.isShowPlotBackGround()) {
142                                    plotBackground.paintComponent(g);
143                            }
144    
145                            if (plotSettings.isShowXGrid()) {
146                                    xGrid.paintComponent(g);
147                            }
148                            if (plotSettings.isShowYGrid()) {
149                                    yGrid.paintComponent(g);
150                            }
151                            if (plotSettings.isShowXAxis()) {
152                                    xAxis.paintComponent(g);
153                            }
154                            if (plotSettings.isShowYAxis()) {
155                                    yAxis.paintComponent(g);
156                            }
157                            if (plotSettings.isShowZeroAxis()) {
158                                    zeroAxis.paintComponent(g);
159                            }
160                            if (plotSettings.isShowSelection()) {
161                                    selection.paintComponent(g);
162                            }
163                            if (plotSettings.isShowRunningAverage()) {
164                                    runningAveragePlot.paintComponent(g);
165                            }
166    
167                            traces.paintComponent(g);
168                    } catch (ConcurrentModificationException e) {
169                            // not too bad
170                    } catch (Exception e) {
171                            e.printStackTrace();
172                    }
173            }
174    
175            public Component getTableCellRendererComponent(JTable table, Object value,
176                            boolean isSelected, boolean hasFocus, int row, int column) {
177                    if (value instanceof MatrixGUIObject) {
178                            plotSettings.setMatrixGUIObject((MatrixGUIObject) value);
179                    } else if (value instanceof Matrix) {
180                            plotSettings.setMatrixGUIObject((MatrixGUIObject) ((Matrix) value)
181                                            .getGUIObject());
182                    }
183    
184                    if (isSelected) {
185                            plotSettings.setPlotBackGroundColor(table.getSelectionBackground());
186                    } else {
187                            plotSettings.setPlotBackGroundColor(new Color(216, 213, 196));
188                    }
189                    return this;
190            }
191    
192            public PlotSettings getPlotSettings() {
193                    return plotSettings;
194            }
195    
196            public void valueChanged(ListSelectionEvent e) {
197                    fireUpdated();
198            }
199    
200    }