1 package net.sf.openrocket.gui.scalefigure;
5 import java.awt.Dimension;
7 import java.awt.Graphics;
8 import java.awt.Graphics2D;
9 import java.awt.Rectangle;
10 import java.awt.RenderingHints;
11 import java.awt.event.ComponentAdapter;
12 import java.awt.event.ComponentEvent;
13 import java.awt.event.MouseEvent;
14 import java.awt.event.MouseListener;
15 import java.awt.event.MouseMotionListener;
16 import java.util.EventObject;
18 import javax.swing.BorderFactory;
19 import javax.swing.JComponent;
20 import javax.swing.JPanel;
21 import javax.swing.JScrollPane;
22 import javax.swing.ScrollPaneConstants;
23 import javax.swing.event.ChangeEvent;
24 import javax.swing.event.ChangeListener;
26 import net.sf.openrocket.gui.adaptors.DoubleModel;
27 import net.sf.openrocket.gui.components.UnitSelector;
28 import net.sf.openrocket.unit.Tick;
29 import net.sf.openrocket.unit.Unit;
30 import net.sf.openrocket.unit.UnitGroup;
31 import net.sf.openrocket.util.BugException;
32 import net.sf.openrocket.util.StateChangeListener;
37 * A scroll pane that holds a {@link ScaleFigure} and includes rulers that show
38 * natural units. The figure can be moved by dragging on the figure.
40 * This class implements both <code>MouseListener</code> and
41 * <code>MouseMotionListener</code>. If subclasses require extra functionality
42 * (e.g. checking for clicks) then these methods may be overridden, and only unhandled
43 * events passed to this class.
45 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
47 public class ScaleScrollPane extends JScrollPane
48 implements MouseListener, MouseMotionListener {
50 public static final int RULER_SIZE = 20;
51 public static final int MINOR_TICKS = 3;
52 public static final int MAJOR_TICKS = 30;
55 private JComponent component;
56 private ScaleFigure figure;
58 private DoubleModel rulerUnit;
59 private Ruler horizontalRuler;
60 private Ruler verticalRuler;
62 private final boolean allowFit;
64 private boolean fit = false;
68 * Create a scale scroll pane that allows fitting.
70 * @param component the component to contain (must implement ScaleFigure)
72 public ScaleScrollPane(JComponent component) {
73 this(component, true);
77 * Create a scale scroll pane.
79 * @param component the component to contain (must implement ScaleFigure)
80 * @param allowFit whether automatic fitting of the figure is allowed
82 public ScaleScrollPane(JComponent component, boolean allowFit) {
85 if (!(component instanceof ScaleFigure)) {
86 throw new IllegalArgumentException("component must implement ScaleFigure");
89 this.component = component;
90 this.figure = (ScaleFigure) component;
91 this.allowFit = allowFit;
94 rulerUnit = new DoubleModel(0.0, UnitGroup.UNITS_LENGTH);
95 rulerUnit.addChangeListener(new ChangeListener() {
97 public void stateChanged(ChangeEvent e) {
98 ScaleScrollPane.this.component.repaint();
101 horizontalRuler = new Ruler(Ruler.HORIZONTAL);
102 verticalRuler = new Ruler(Ruler.VERTICAL);
103 this.setColumnHeaderView(horizontalRuler);
104 this.setRowHeaderView(verticalRuler);
106 UnitSelector selector = new UnitSelector(rulerUnit);
107 selector.setFont(new Font("SansSerif", Font.PLAIN, 8));
108 this.setCorner(JScrollPane.UPPER_LEFT_CORNER, selector);
109 this.setCorner(JScrollPane.UPPER_RIGHT_CORNER, new JPanel());
110 this.setCorner(JScrollPane.LOWER_LEFT_CORNER, new JPanel());
111 this.setCorner(JScrollPane.LOWER_RIGHT_CORNER, new JPanel());
113 this.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
116 viewport.addMouseListener(this);
117 viewport.addMouseMotionListener(this);
119 figure.addChangeListener(new StateChangeListener() {
121 public void stateChanged(EventObject e) {
122 horizontalRuler.updateSize();
123 verticalRuler.updateSize();
130 viewport.addComponentListener(new ComponentAdapter() {
132 public void componentResized(ComponentEvent e) {
141 public ScaleFigure getFigure() {
147 * Return whether automatic fitting of the figure is allowed.
149 public boolean isFittingAllowed() {
154 * Return whether the figure is currently automatically fitted within the component bounds.
156 public boolean isFitting() {
161 * Set whether the figure is automatically fitted within the component bounds.
163 * @throws BugException if automatic fitting is disallowed and <code>fit</code> is <code>true</code>
165 public void setFitting(boolean fit) {
166 if (fit && !allowFit) {
167 throw new BugException("Attempting to fit figure not allowing fit.");
171 setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
172 setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
174 Dimension view = viewport.getExtentSize();
175 figure.setScaling(view);
177 setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
178 setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
184 public double getScaling() {
185 return figure.getScaling();
188 public double getScale() {
189 return figure.getAbsoluteScale();
192 public void setScaling(double scale) {
196 figure.setScaling(scale);
197 horizontalRuler.repaint();
198 verticalRuler.repaint();
202 public Unit getCurrentUnit() {
203 return rulerUnit.getCurrentUnit();
207 //////////////// Mouse handlers ////////////////
210 private int dragStartX = 0;
211 private int dragStartY = 0;
212 private Rectangle dragRectangle = null;
215 public void mousePressed(MouseEvent e) {
216 dragStartX = e.getX();
217 dragStartY = e.getY();
218 dragRectangle = viewport.getViewRect();
222 public void mouseReleased(MouseEvent e) {
223 dragRectangle = null;
227 public void mouseDragged(MouseEvent e) {
228 if (dragRectangle == null) {
232 dragRectangle.setLocation(dragStartX - e.getX(), dragStartY - e.getY());
234 dragStartX = e.getX();
235 dragStartY = e.getY();
237 viewport.scrollRectToVisible(dragRectangle);
241 public void mouseClicked(MouseEvent e) {
245 public void mouseEntered(MouseEvent e) {
249 public void mouseExited(MouseEvent e) {
253 public void mouseMoved(MouseEvent e) {
258 //////////////// The view port rulers ////////////////
261 private class Ruler extends JComponent {
262 public static final int HORIZONTAL = 0;
263 public static final int VERTICAL = 1;
265 private final int orientation;
267 public Ruler(int orientation) {
268 this.orientation = orientation;
271 rulerUnit.addChangeListener(new ChangeListener() {
273 public void stateChanged(ChangeEvent e) {
274 Ruler.this.repaint();
280 public void updateSize() {
281 Dimension d = component.getPreferredSize();
282 if (orientation == HORIZONTAL) {
283 setPreferredSize(new Dimension(d.width + 10, RULER_SIZE));
285 setPreferredSize(new Dimension(RULER_SIZE, d.height + 10));
291 private double fromPx(int px) {
292 Dimension origin = figure.getOrigin();
293 if (orientation == HORIZONTAL) {
296 // px = -(px - origin.height);
299 return px / figure.getAbsoluteScale();
302 private int toPx(double l) {
303 Dimension origin = figure.getOrigin();
304 int px = (int) (l * figure.getAbsoluteScale() + 0.5);
305 if (orientation == HORIZONTAL) {
308 px = px + origin.height;
309 // px += origin.height;
316 protected void paintComponent(Graphics g) {
317 super.paintComponent(g);
318 Graphics2D g2 = (Graphics2D) g;
320 Rectangle area = g2.getClipBounds();
322 // Fill area with background color
323 g2.setColor(getBackground());
324 g2.fillRect(area.x, area.y, area.width, area.height + 100);
328 if (orientation == HORIZONTAL) {
330 endpx = area.x + area.width;
333 endpx = area.y + area.height;
336 Unit unit = rulerUnit.getCurrentUnit();
337 double start, end, minor, major;
338 start = fromPx(startpx);
340 minor = MINOR_TICKS / figure.getAbsoluteScale();
341 major = MAJOR_TICKS / figure.getAbsoluteScale();
343 Tick[] ticks = unit.getTicks(start, end, minor, major);
347 g2.setColor(Color.BLACK);
348 g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
349 RenderingHints.VALUE_STROKE_NORMALIZE);
350 g2.setRenderingHint(RenderingHints.KEY_RENDERING,
351 RenderingHints.VALUE_RENDER_QUALITY);
352 g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
353 RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
355 for (Tick t : ticks) {
356 int position = toPx(t.value);
357 drawTick(g2, position, t);
361 private void drawTick(Graphics g, int position, Tick t) {
365 length = RULER_SIZE / 2;
368 length = RULER_SIZE / 3;
370 length = RULER_SIZE / 6;
375 str = rulerUnit.getCurrentUnit().toString(t.value);
377 g.setFont(new Font("SansSerif", Font.BOLD, 9));
379 g.setFont(new Font("SansSerif", Font.PLAIN, 9));
383 if (orientation == HORIZONTAL) {
384 g.drawLine(position, RULER_SIZE - length, position, RULER_SIZE);
386 g.drawString(str, position, RULER_SIZE - length - 1);
388 g.drawLine(RULER_SIZE - length, position, RULER_SIZE, position);
390 g.drawString(str, 1, position - 1);