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;
35 * A scroll pane that holds a {@link ScaleFigure} and includes rulers that show
36 * natural units. The figure can be moved by dragging on the figure.
38 * This class implements both <code>MouseListener</code> and
39 * <code>MouseMotionListener</code>. If subclasses require extra functionality
40 * (e.g. checking for clicks) then these methods may be overridden, and only unhandled
41 * events passed to this class.
43 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
45 public class ScaleScrollPane extends JScrollPane
46 implements MouseListener, MouseMotionListener {
48 public static final int RULER_SIZE = 20;
49 public static final int MINOR_TICKS = 3;
50 public static final int MAJOR_TICKS = 30;
53 private JComponent component;
54 private ScaleFigure figure;
55 private JViewport viewport;
57 private DoubleModel rulerUnit;
58 private Ruler horizontalRuler;
59 private Ruler verticalRuler;
61 private final boolean allowFit;
63 private boolean fit = false;
66 public ScaleScrollPane(JComponent component) {
67 this(component, true);
70 public ScaleScrollPane(JComponent component, boolean allowFit) {
72 // super(component, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
73 // JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
75 if (!(component instanceof ScaleFigure)) {
76 throw new IllegalArgumentException("component must implement ScaleFigure");
79 this.component = component;
80 this.figure = (ScaleFigure)component;
81 this.allowFit = allowFit;
84 rulerUnit = new DoubleModel(0.0,UnitGroup.UNITS_LENGTH);
85 rulerUnit.addChangeListener(new ChangeListener() {
87 public void stateChanged(ChangeEvent e) {
88 ScaleScrollPane.this.component.repaint();
91 horizontalRuler = new Ruler(Ruler.HORIZONTAL);
92 verticalRuler = new Ruler(Ruler.VERTICAL);
93 this.setColumnHeaderView(horizontalRuler);
94 this.setRowHeaderView(verticalRuler);
96 UnitSelector selector = new UnitSelector(rulerUnit);
97 selector.setFont(new Font("SansSerif", Font.PLAIN, 8));
98 this.setCorner(JScrollPane.UPPER_LEFT_CORNER, selector);
99 this.setCorner(JScrollPane.UPPER_RIGHT_CORNER, new JPanel());
100 this.setCorner(JScrollPane.LOWER_LEFT_CORNER, new JPanel());
101 this.setCorner(JScrollPane.LOWER_RIGHT_CORNER, new JPanel());
103 this.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
106 viewport = this.getViewport();
107 viewport.addMouseListener(this);
108 viewport.addMouseMotionListener(this);
110 figure.addChangeListener(new ChangeListener() {
112 public void stateChanged(ChangeEvent e) {
113 horizontalRuler.updateSize();
114 verticalRuler.updateSize();
121 viewport.addComponentListener(new ComponentAdapter() {
123 public void componentResized(ComponentEvent e) {
132 public ScaleFigure getFigure() {
137 public boolean isFittingAllowed() {
141 public boolean isFitting() {
145 public void setFitting(boolean fit) {
146 if (fit && !allowFit) {
147 throw new RuntimeException("Attempting to fit figure not allowing fit.");
151 setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
152 setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
154 Dimension view = viewport.getExtentSize();
155 figure.setScaling(view);
157 setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
158 setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
164 public double getScaling() {
165 return figure.getScaling();
168 public double getScale() {
169 return figure.getAbsoluteScale();
172 public void setScaling(double scale) {
176 figure.setScaling(scale);
177 horizontalRuler.repaint();
178 verticalRuler.repaint();
182 public Unit getCurrentUnit() {
183 return rulerUnit.getCurrentUnit();
187 //////////////// Mouse handlers ////////////////
190 private int dragStartX=0;
191 private int dragStartY=0;
192 private Rectangle dragRectangle = null;
195 public void mousePressed(MouseEvent e) {
196 dragStartX = e.getX();
197 dragStartY = e.getY();
198 dragRectangle = viewport.getViewRect();
202 public void mouseReleased(MouseEvent e) {
203 dragRectangle = null;
207 public void mouseDragged(MouseEvent e) {
208 if (dragRectangle==null) {
212 dragRectangle.setLocation(dragStartX-e.getX(),dragStartY-e.getY());
214 dragStartX = e.getX();
215 dragStartY = e.getY();
217 viewport.scrollRectToVisible(dragRectangle);
221 public void mouseClicked(MouseEvent e) {
225 public void mouseEntered(MouseEvent e) {
229 public void mouseExited(MouseEvent e) {
233 public void mouseMoved(MouseEvent e) {
238 //////////////// The view port rulers ////////////////
241 private class Ruler extends JComponent {
242 public static final int HORIZONTAL = 0;
243 public static final int VERTICAL = 1;
245 private final int orientation;
247 public Ruler(int orientation) {
248 this.orientation = orientation;
251 rulerUnit.addChangeListener(new ChangeListener() {
253 public void stateChanged(ChangeEvent e) {
254 Ruler.this.repaint();
260 public void updateSize() {
261 Dimension d = component.getPreferredSize();
262 if (orientation == HORIZONTAL) {
263 setPreferredSize(new Dimension(d.width+10,RULER_SIZE));
265 setPreferredSize(new Dimension(RULER_SIZE,d.height+10));
271 private double fromPx(int px) {
272 Dimension origin = figure.getOrigin();
273 if (orientation == HORIZONTAL) {
276 // px = -(px - origin.height);
279 return px/figure.getAbsoluteScale();
282 private int toPx(double l) {
283 Dimension origin = figure.getOrigin();
284 int px = (int)(l * figure.getAbsoluteScale() + 0.5);
285 if (orientation == HORIZONTAL) {
288 px = px + origin.height;
289 // px += origin.height;
296 protected void paintComponent(Graphics g) {
297 super.paintComponent(g);
298 Graphics2D g2 = (Graphics2D)g;
300 Rectangle area = g2.getClipBounds();
302 // Fill area with background color
303 g2.setColor(getBackground());
304 g2.fillRect(area.x, area.y, area.width, area.height+100);
308 if (orientation == HORIZONTAL) {
310 endpx = area.x+area.width;
313 endpx = area.y+area.height;
316 Unit unit = rulerUnit.getCurrentUnit();
317 double start,end,minor,major;
318 start = fromPx(startpx);
320 minor = MINOR_TICKS/figure.getAbsoluteScale();
321 major = MAJOR_TICKS/figure.getAbsoluteScale();
323 Tick[] ticks = unit.getTicks(start, end, minor, major);
327 g2.setColor(Color.BLACK);
328 g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
329 RenderingHints.VALUE_STROKE_NORMALIZE);
330 g2.setRenderingHint(RenderingHints.KEY_RENDERING,
331 RenderingHints.VALUE_RENDER_QUALITY);
332 g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
333 RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
335 for (Tick t: ticks) {
336 int position = toPx(t.value);
337 drawTick(g2,position,t);
341 private void drawTick(Graphics g, int position, Tick t) {
345 length = RULER_SIZE/2;
348 length = RULER_SIZE/3;
350 length = RULER_SIZE/6;
355 str = rulerUnit.getCurrentUnit().toString(t.value);
357 g.setFont(new Font("SansSerif", Font.BOLD, 9));
359 g.setFont(new Font("SansSerif", Font.PLAIN, 9));
363 if (orientation == HORIZONTAL) {
364 g.drawLine(position, RULER_SIZE-length, position, RULER_SIZE);
366 g.drawString(str, position, RULER_SIZE-length-1);
368 g.drawLine(RULER_SIZE-length, position, RULER_SIZE, position);
370 g.drawString(str, 1, position-1);