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.jfreechart;
025    
026    import java.awt.Dimension;
027    import java.awt.Graphics2D;
028    import java.awt.geom.Rectangle2D;
029    import java.io.File;
030    
031    import javax.swing.JFrame;
032    
033    import org.jfree.chart.ChartPanel;
034    import org.ujmp.core.enums.FileFormat;
035    import org.ujmp.core.exceptions.MatrixException;
036    import org.ujmp.gui.MatrixGUIObject;
037    import org.ujmp.gui.interfaces.CanRenderGraph;
038    import org.ujmp.gui.io.ExportJPEG;
039    import org.ujmp.gui.io.ExportPDF;
040    
041    public abstract class AbstractChartPanel extends ChartPanel implements
042                    CanRenderGraph {
043            private static final long serialVersionUID = -7609107739440534835L;
044    
045            private ChartConfiguration config = null;
046    
047            private MatrixGUIObject matrix = null;
048    
049            public AbstractChartPanel(MatrixGUIObject matrix, ChartConfiguration config) {
050                    super(null, true);
051                    this.matrix = matrix;
052                    this.config = config;
053                    setPreferredSize(new Dimension(800, 600));
054                    setMaximumDrawWidth(2000);
055                    setMaximumDrawHeight(2000);
056                    redraw();
057            }
058    
059            public synchronized void renderGraph(Graphics2D g2d) {
060                    Rectangle2D r = new Rectangle2D.Double(0, 0, getWidth(), getHeight());
061                    try {
062                            redraw();
063                    } catch (Exception e) {
064                    }
065                    getChart().draw(g2d, r);
066            }
067    
068            public synchronized void export(FileFormat fileFormat, File file) {
069                    JFrame frame = null;
070                    if (isVisible()) {
071                            frame = new JFrame();
072                            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
073                            frame.setContentPane(this);
074                            frame.setSize(800, 600);
075                            frame.setVisible(true);
076                            try {
077                                    redraw();
078                                    repaint();
079                            } catch (Exception e) {
080                            }
081                    }
082                    if (FileFormat.JPG.equals(fileFormat)) {
083                            try {
084                                    ExportJPEG.save(file, this);
085                            } catch (Exception e) {
086                            }
087                    } else if (FileFormat.PDF.equals(fileFormat)) {
088                            try {
089                                    ExportPDF.save(file, this);
090                            } catch (Exception e) {
091                            }
092                    } else {
093                            throw new MatrixException("FileFormat not yet supported: "
094                                            + fileFormat);
095                    }
096                    if (frame != null) {
097                            frame.setVisible(false);
098                            frame = null;
099                    }
100            }
101    
102            // public synchronized void redraw() {
103    
104            // ((MatrixGUIObject) m.getGUIObject()).getRowSelectionModel()
105            // .addListSelectionListener(this);
106    
107            // addComponentListener(this);
108    
109            // updatePopupMenu();
110    
111            // dataset.addChangeListener(this);
112    
113            // if (showBorder) {
114            // setBorder(BorderFactory.createTitledBorder("Chart"));
115            // }
116    
117            // chart.setBackgroundPaint(UIManager.getColor("Panel.background"));
118    
119            // plot.addChangeListener(this);
120            //
121            // zeroMarker.setPaint(new Color(0, 0, 0, 128));
122            // plot.addRangeMarker(zeroMarker, Layer.FOREGROUND);
123            //
124            // try {
125            // plot.addRangeMarker(dataset.getMeanMarker(0));
126            // plot.addRangeMarker(dataset.getStandardDeviationMarker(0));
127            // plot.addRangeMarker(dataset.getMinMaxMarker(0));
128            // } catch (Exception e) {
129            // System.out.println("error in VariableChartPanel");
130            // }
131    
132            // rangeSelection.setPaint(new Color(200, 200, 235, 128));
133            // rangeSelection.setLabelPaint(new Color(0, 0, 0));
134            // rangeSelection.setLabelAnchor(RectangleAnchor.TOP);
135            // rangeSelection.setLabelTextAnchor(TextAnchor.TOP_CENTER);
136            // rangeSelection.setOutlinePaint(new Color(50, 50, 235));
137            // plot.addDomainMarker(rangeSelection, Layer.FOREGROUND);
138    
139            // legend = chart.getLegend();
140            // chart.clearSubtitles();
141            // }
142    
143            public abstract void redraw();
144    
145            public synchronized ChartConfiguration getConfig() {
146                    return config;
147            }
148    
149            public synchronized void setConfig(ChartConfiguration config) {
150                    this.config = config;
151                    redraw();
152            }
153    
154            public MatrixGUIObject getMatrix() {
155                    return matrix;
156            }
157    
158            public synchronized void setMatrix(MatrixGUIObject matrix) {
159                    this.matrix = matrix;
160                    redraw();
161            }
162    
163    }