/* ========================
* JSynoptic : a free Synoptic editor
* ========================
*
* Project Info: http://jsynoptic.sourceforge.net/index.html
*
* This program is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* (C) Copyright 2003, by :
* Corporate:
* Astrium SAS
* EADS CRC
* Individual:
* Nicolas Brodu
*
* $Id: ChartShape.java,v 1.8 2004/03/24 08:50:46 brodu Exp $
*
* Changes
* -------
* 25-Sep-2003 : Initial public release (NB);
*
*/
package jsynoptic.plugins.jfreechart;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
import java.io.ObjectInputValidation;
import java.io.Serializable;
import javax.swing.JTabbedPane;
import jsynoptic.base.Linkable;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.event.ChartChangeEvent;
import org.jfree.chart.event.ChartChangeListener;
import simtools.shapes.AbstractShape;
import simtools.shapes.ShapeListener;
/**
* @author nbrodu
*
* To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
public class ChartShape extends AbstractShape implements Serializable, simtools.diagram.Resizable, ChartChangeListener, ObjectInputValidation, Linkable {
static final long serialVersionUID = -5954721943791327034L;
protected JFreeChart chart;
protected transient ChartRenderingInfo chartRenderingInfo;
// Linkable shape
protected String link;
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
/** Override this so when we're not listened, do not listen to our graph
* Workaround for object deletion
*/
public synchronized void removeListener(ShapeListener sl) {
super.removeListener(sl);
if ((listeners.size()==0) && (chart!=null)) chart.removeChangeListener(this);
}
public ChartShape(JFreeChart chart, int ox, int oy, int width, int height) {
super(ox, oy);
this.chart = chart;
_w = width;
_h = height;
if (chart!=null) chart.addChangeListener(this);
}
public void draw(Graphics2D g) {
Color c = g.getColor();
Stroke stroke = g.getStroke();
if (chartRenderingInfo==null) chartRenderingInfo = new ChartRenderingInfo();
chart.draw(g, new Rectangle2D.Double(_ox,_oy-_h,_w,_h), chartRenderingInfo);
g.setStroke(stroke);
g.setColor(c);
}
public void resize(int dx, int dy) {
_w += dx; _h += dy;
}
/* (non-Javadoc)
* @see org.jfree.chart.event.ChartChangeListener#chartChanged(org.jfree.chart.event.ChartChangeEvent)
*/
public void chartChanged(ChartChangeEvent event) {
notifyChange();
}
/* (non-Javadoc)
* @see simtools.shapes.AbstractShape#cloneShape()
*/
protected AbstractShape cloneShape() {
ChartShape cs = (ChartShape)super.cloneShape();
try {
cs.chart = (JFreeChart)chart.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
cs.chart = chart; // risky!
}
if (cs.chart!=null) cs.chart.addChangeListener(cs);
return cs;
}
/**
* Restores a serialized object.
*
* @param stream the input stream.
*
* @throws IOException if there is an I/O problem.
* @throws ClassNotFoundException if there is a problem loading a class.
*/
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
stream.defaultReadObject();
stream.registerValidation(this,15); // JFreeChart has priority of 10 => register first
}
public void validateObject() throws InvalidObjectException {
if (chart!=null) chart.addChangeListener(this);
}
protected JTabbedPane findTab(Container ct) {
Component[] cmps = ct.getComponents();
for (int i=0; i<cmps.length; ++i) {
if (cmps[i] instanceof JTabbedPane) return (JTabbedPane)cmps[i];
}
// no pane, recurse
for (int i=0; i<cmps.length; ++i) {
JTabbedPane ret = null;
if (cmps[i] instanceof Container) ret = findTab((Container)cmps[i]);
if (ret!=null) return ret;
}
return null;
}
public void debugContainer(Container ct) {
Component[] cmps = ct.getComponents();
for (int i=0; i<cmps.length; ++i) {
System.out.println(cmps[i]);
if (cmps[i] instanceof Container) debugContainer((Container)cmps[i]);
}
}
}