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;
17 import javax.swing.BorderFactory;
18 import javax.swing.JComponent;
19 import javax.swing.JPanel;
20 import javax.swing.JScrollPane;
21 import javax.swing.JViewport;
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;
36 * A scroll pane that holds a {@link ScaleFigure} and includes rulers that show
37 * natural units. The figure can be moved by dragging on the figure.
39 * This class implements both <code>MouseListener</code> and
40 * <code>MouseMotionListener</code>. If subclasses require extra functionality
41 * (e.g. checking for clicks) then these methods may be overridden, and only unhandled
42 * events passed to this class.
44 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
46 public class ScaleScrollPane extends JScrollPane
47 implements MouseListener, MouseMotionListener {
49 public static final int RULER_SIZE = 20;
50 public static final int MINOR_TICKS = 3;
51 public static final int MAJOR_TICKS = 30;
54 private JComponent component;
55 private ScaleFigure figure;
56 private JViewport viewport;
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 = this.getViewport();
117 viewport.addMouseListener(this);
118 viewport.addMouseMotionListener(this);
120 figure.addChangeListener(new ChangeListener() {
122 public void stateChanged(ChangeEvent e) {
123 horizontalRuler.updateSize();
124 verticalRuler.updateSize();
131 viewport.addComponentListener(new ComponentAdapter() {
133 public void componentResized(ComponentEvent e) {
142 public ScaleFigure getFigure() {
148 * Return whether automatic fitting of the figure is allowed.
150 public boolean isFittingAllowed() {
155 * Return whether the figure is currently automatically fitted within the component bounds.
157 public boolean isFitting() {
162 * Set whether the figure is automatically fitted within the component bounds.
164 * @throws BugException if automatic fitting is disallowed and <code>fit</code> is <code>true</code>
166 public void setFitting(boolean fit) {
167 if (fit && !allowFit) {
168 throw new BugException("Attempting to fit figure not allowing fit.");
172 setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
173 setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
175 Dimension view = viewport.getExtentSize();
176 figure.setScaling(view);
178 setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
179 setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
185 public double getScaling() {
186 return figure.getScaling();
189 public double getScale() {
190 return figure.getAbsoluteScale();
193 public void setScaling(double scale) {
197 figure.setScaling(scale);
198 horizontalRuler.repaint();
199 verticalRuler.repaint();
203 public Unit getCurrentUnit() {
204 return rulerUnit.getCurrentUnit();
208 //////////////// Mouse handlers ////////////////
211 private int dragStartX = 0;
212 private int dragStartY = 0;
213 private Rectangle dragRectangle = null;
216 public void mousePressed(MouseEvent e) {
217 dragStartX = e.getX();
218 dragStartY = e.getY();
219 dragRectangle = viewport.getViewRect();
223 public void mouseReleased(MouseEvent e) {
224 dragRectangle = null;
228 public void mouseDragged(MouseEvent e) {
229 if (dragRectangle == null) {
233 dragRectangle.setLocation(dragStartX - e.getX(), dragStartY - e.getY());
235 dragStartX = e.getX();
236 dragStartY = e.getY();
238 viewport.scrollRectToVisible(dragRectangle);
242 public void mouseClicked(MouseEvent e) {
246 public void mouseEntered(MouseEvent e) {
250 public void mouseExited(MouseEvent e) {
254 public void mouseMoved(MouseEvent e) {
259 //////////////// The view port rulers ////////////////
262 private class Ruler extends JComponent {
263 public static final int HORIZONTAL = 0;
264 public static final int VERTICAL = 1;
266 private final int orientation;
268 public Ruler(int orientation) {
269 this.orientation = orientation;
272 rulerUnit.addChangeListener(new ChangeListener() {
274 public void stateChanged(ChangeEvent e) {
275 Ruler.this.repaint();
281 public void updateSize() {
282 Dimension d = component.getPreferredSize();
283 if (orientation == HORIZONTAL) {
284 setPreferredSize(new Dimension(d.width + 10, RULER_SIZE));
286 setPreferredSize(new Dimension(RULER_SIZE, d.height + 10));
292 private double fromPx(int px) {
293 Dimension origin = figure.getOrigin();
294 if (orientation == HORIZONTAL) {
297 // px = -(px - origin.height);
300 return px / figure.getAbsoluteScale();
303 private int toPx(double l) {
304 Dimension origin = figure.getOrigin();
305 int px = (int) (l * figure.getAbsoluteScale() + 0.5);
306 if (orientation == HORIZONTAL) {
309 px = px + origin.height;
310 // px += origin.height;
317 protected void paintComponent(Graphics g) {
318 super.paintComponent(g);
319 Graphics2D g2 = (Graphics2D) g;
321 Rectangle area = g2.getClipBounds();
323 // Fill area with background color
324 g2.setColor(getBackground());
325 g2.fillRect(area.x, area.y, area.width, area.height + 100);
329 if (orientation == HORIZONTAL) {
331 endpx = area.x + area.width;
334 endpx = area.y + area.height;
337 Unit unit = rulerUnit.getCurrentUnit();
338 double start, end, minor, major;
339 start = fromPx(startpx);
341 minor = MINOR_TICKS / figure.getAbsoluteScale();
342 major = MAJOR_TICKS / figure.getAbsoluteScale();
344 Tick[] ticks = unit.getTicks(start, end, minor, major);
348 g2.setColor(Color.BLACK);
349 g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
350 RenderingHints.VALUE_STROKE_NORMALIZE);
351 g2.setRenderingHint(RenderingHints.KEY_RENDERING,
352 RenderingHints.VALUE_RENDER_QUALITY);
353 g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
354 RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
356 for (Tick t : ticks) {
357 int position = toPx(t.value);
358 drawTick(g2, position, t);
362 private void drawTick(Graphics g, int position, Tick t) {
366 length = RULER_SIZE / 2;
369 length = RULER_SIZE / 3;
371 length = RULER_SIZE / 6;
376 str = rulerUnit.getCurrentUnit().toString(t.value);
378 g.setFont(new Font("SansSerif", Font.BOLD, 9));
380 g.setFont(new Font("SansSerif", Font.PLAIN, 9));
384 if (orientation == HORIZONTAL) {
385 g.drawLine(position, RULER_SIZE - length, position, RULER_SIZE);
387 g.drawString(str, position, RULER_SIZE - length - 1);
389 g.drawLine(RULER_SIZE - length, position, RULER_SIZE, position);
391 g.drawString(str, 1, position - 1);