1 package net.sf.openrocket.gui.scalefigure;
4 import java.awt.Dimension;
5 import java.util.EventListener;
6 import java.util.EventObject;
7 import java.util.LinkedList;
10 import javax.swing.JPanel;
12 import net.sf.openrocket.gui.util.GUIUtil;
13 import net.sf.openrocket.util.StateChangeListener;
16 public abstract class AbstractScaleFigure extends JPanel implements ScaleFigure {
18 // Number of pixels to leave at edges when fitting figure
19 private static final int DEFAULT_BORDER_PIXELS_WIDTH = 30;
20 private static final int DEFAULT_BORDER_PIXELS_HEIGHT = 20;
23 protected final double dpi;
25 protected double scale = 1.0;
26 protected double scaling = 1.0;
28 protected int borderPixelsWidth = DEFAULT_BORDER_PIXELS_WIDTH;
29 protected int borderPixelsHeight = DEFAULT_BORDER_PIXELS_HEIGHT;
31 protected final List<EventListener> listeners = new LinkedList<EventListener>();
34 public AbstractScaleFigure() {
35 this.dpi = GUIUtil.getDPI();
37 this.scale = dpi / 0.0254 * scaling;
39 setBackground(Color.WHITE);
45 public abstract void updateFigure();
47 public abstract double getFigureWidth();
49 public abstract double getFigureHeight();
53 public double getScaling() {
58 public double getAbsoluteScale() {
63 public void setScaling(double scaling) {
64 if (Double.isInfinite(scaling) || Double.isNaN(scaling))
70 if (Math.abs(this.scaling - scaling) < 0.01)
72 this.scaling = scaling;
73 this.scale = dpi / 0.0254 * scaling;
78 public void setScaling(Dimension bounds) {
79 double zh = 1, zv = 1;
80 int w = bounds.width - 2 * borderPixelsWidth - 20;
81 int h = bounds.height - 2 * borderPixelsHeight - 20;
88 zh = (w) / getFigureWidth();
89 zv = (h) / getFigureHeight();
91 double s = Math.min(zh, zv) / dpi * 0.0254 - 0.001;
103 public Dimension getBorderPixels() {
104 return new Dimension(borderPixelsWidth, borderPixelsHeight);
108 public void setBorderPixels(int width, int height) {
109 this.borderPixelsWidth = width;
110 this.borderPixelsHeight = height;
115 public void addChangeListener(EventListener listener) {
116 listeners.add(0, listener);
120 public void removeChangeListener(EventListener listener) {
121 listeners.remove(listener);
124 private EventObject changeEvent = null;
126 protected void fireChangeEvent() {
127 if (changeEvent == null)
128 changeEvent = new EventObject(this);
129 // Copy the list before iterating to prevent concurrent modification exceptions.
130 EventListener[] list = listeners.toArray(new EventListener[0]);
131 for (EventListener l : list) {
132 if (l instanceof StateChangeListener) {
133 ((StateChangeListener) l).stateChanged(changeEvent);