4 import java.awt.Dimension;
5 import java.awt.FontMetrics;
6 import java.awt.Graphics;
7 import java.awt.event.ActionEvent;
8 import java.awt.event.ActionListener;
9 import java.awt.event.KeyEvent;
10 import java.awt.event.KeyListener;
11 import java.awt.event.MouseEvent;
12 import java.awt.event.MouseMotionListener;
13 import java.awt.event.MouseWheelEvent;
14 import java.awt.event.MouseWheelListener;
15 import java.awt.image.BufferedImage;
17 import javax.swing.JCheckBoxMenuItem;
18 import javax.swing.JMenuItem;
19 import javax.swing.JPopupMenu;
22 import tim.prune.DataSubscriber;
23 import tim.prune.I18nManager;
24 import tim.prune.data.DataPoint;
25 import tim.prune.data.TrackInfo;
29 * Display component for the main map
31 public class MapChart extends GenericChart implements MouseWheelListener, KeyListener, MouseMotionListener
34 private static final int POINT_RADIUS = 4;
35 private static final int CLICK_SENSITIVITY = 10;
36 private static final double ZOOM_SCALE_FACTOR = 1.2;
37 private static final int PAN_DISTANCE = 10;
38 private static final int LIMIT_WAYPOINT_NAMES = 40;
41 private static final Color COLOR_BG = Color.WHITE;
42 private static final Color COLOR_POINT = Color.BLUE;
43 private static final Color COLOR_CURR_RANGE = Color.GREEN;
44 private static final Color COLOR_CROSSHAIRS = Color.RED;
45 private static final Color COLOR_WAYPT_NAME = Color.BLACK;
48 private App _app = null;
49 private BufferedImage _image = null;
50 private JPopupMenu _popup = null;
51 private JCheckBoxMenuItem _autoPanMenuItem = null;
52 private JCheckBoxMenuItem _connectPointsMenuItem = null;
53 private int _numPoints = -1;
54 private double _scale;
55 private double _offsetX, _offsetY, _zoomScale;
56 private int _lastSelectedPoint = -1;
57 private int _dragStartX = -1, _dragStartY = -1;
58 private int _zoomDragFromX = -1, _zoomDragFromY = -1;
59 private int _zoomDragToX = -1, _zoomDragToY = -1;
60 private boolean _zoomDragging = false;
65 * @param inApp App object for callbacks
66 * @param inTrackInfo track info object
68 public MapChart(App inApp, TrackInfo inTrackInfo)
73 addMouseListener(this);
74 addMouseWheelListener(this);
75 addMouseMotionListener(this);
78 MINIMUM_SIZE = new Dimension(200, 250);
84 * Override track updating to refresh image
86 public void dataUpdated(byte inUpdateType)
88 // Check if number of points has changed or data has been edited
89 if (_track.getNumPoints() != _numPoints || (inUpdateType & DATA_EDITED) > 0)
92 _lastSelectedPoint = -1;
93 _numPoints = _track.getNumPoints();
95 super.dataUpdated(inUpdateType);
100 * Override paint method to draw map
101 * @param inG graphics object
103 public void paint(Graphics inG)
111 int width = getWidth();
112 int height = getHeight();
115 // Find x and y ranges, and scale to fit
116 double scaleX = (_track.getXRange().getMaximum() - _track.getXRange().getMinimum())
117 / (width - 2 * (BORDER_WIDTH + POINT_RADIUS));
118 double scaleY = (_track.getYRange().getMaximum() - _track.getYRange().getMinimum())
119 / (height - 2 * (BORDER_WIDTH + POINT_RADIUS));
121 if (scaleY > _scale) _scale = scaleY;
123 // Autopan if necessary
124 int selectedPoint = _trackInfo.getSelection().getCurrentPointIndex();
125 if (_autoPanMenuItem.isSelected() && selectedPoint >= 0 && selectedPoint != _lastSelectedPoint)
127 // Autopan is enabled and a point is selected - work out x and y to see if it's within range
128 x = width/2 + (int) ((_track.getX(selectedPoint) - _offsetX) / _scale * _zoomScale);
129 y = height/2 - (int) ((_track.getY(selectedPoint) - _offsetY) / _scale * _zoomScale);
130 if (x <= BORDER_WIDTH)
133 _offsetX -= (width / 4 - x) * _scale / _zoomScale;
136 else if (x >= (width - BORDER_WIDTH))
139 _offsetX += (x - width * 3/4) * _scale / _zoomScale;
142 if (y <= BORDER_WIDTH)
145 _offsetY += (height / 4 - y) * _scale / _zoomScale;
148 else if (y >= (height - BORDER_WIDTH))
151 _offsetY -= (y - height * 3/4) * _scale / _zoomScale;
155 _lastSelectedPoint = selectedPoint;
157 // Create background if necessary
158 if (_image == null || width != _image.getWidth() || height != _image.getHeight())
160 createBackgroundImage();
162 // return if image has been set to null by other thread
163 if (_image == null) {return;}
165 // draw buffered image onto g
166 inG.drawImage(_image, 0, 0, width, height, COLOR_BG, null);
168 // draw selected range, if any
169 if (_trackInfo.getSelection().hasRangeSelected() && !_zoomDragging)
171 int rangeStart = _trackInfo.getSelection().getStart();
172 int rangeEnd = _trackInfo.getSelection().getEnd();
173 inG.setColor(COLOR_CURR_RANGE);
174 for (int i=rangeStart; i<=rangeEnd; i++)
176 x = width/2 + (int) ((_track.getX(i) - _offsetX) / _scale * _zoomScale);
177 y = height/2 - (int) ((_track.getY(i) - _offsetY) / _scale * _zoomScale);
178 if (x > BORDER_WIDTH && x < (width - BORDER_WIDTH)
179 && y < (height - BORDER_WIDTH) && y > BORDER_WIDTH)
181 inG.drawRect(x - 2, y - 2, 4, 4);
186 // Highlight selected point
187 if (selectedPoint >= 0 && !_zoomDragging)
189 inG.setColor(COLOR_CROSSHAIRS);
190 x = width/2 + (int) ((_track.getX(selectedPoint) - _offsetX) / _scale * _zoomScale);
191 y = height/2 - (int) ((_track.getY(selectedPoint) - _offsetY) / _scale * _zoomScale);
192 if (x > BORDER_WIDTH && x < (width - BORDER_WIDTH)
193 && y < (height - BORDER_WIDTH) && y > BORDER_WIDTH)
195 // Draw cross-hairs for current point
196 inG.drawLine(x, BORDER_WIDTH, x, height - BORDER_WIDTH);
197 inG.drawLine(BORDER_WIDTH, y, width - BORDER_WIDTH, y);
199 // Show selected point afterwards to make sure it's on top
200 inG.drawOval(x - 2, y - 2, 4, 4);
201 inG.drawOval(x - 3, y - 3, 6, 6);
205 // Draw rectangle for dragging zoom area
208 inG.setColor(COLOR_CROSSHAIRS);
209 inG.drawLine(_zoomDragFromX, _zoomDragFromY, _zoomDragFromX, _zoomDragToY);
210 inG.drawLine(_zoomDragFromX, _zoomDragFromY, _zoomDragToX, _zoomDragFromY);
211 inG.drawLine(_zoomDragToX, _zoomDragFromY, _zoomDragToX, _zoomDragToY);
212 inG.drawLine(_zoomDragFromX, _zoomDragToY, _zoomDragToX, _zoomDragToY);
215 // Attempt to grab keyboard focus if possible
216 //requestFocus(); (causes problems here)
221 * Plot the points onto an offscreen image
222 * which doesn't have to be redrawn when the selection changes
224 private void createBackgroundImage()
226 int width = getWidth();
227 int height = getHeight();
229 int lastX = 0, lastY = 0;
231 if (_image == null || _image.getWidth() != width || _image.getHeight() != height) {
232 _image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
234 Graphics bufferedG = _image.getGraphics();
235 super.paint(bufferedG);
237 // Loop and show all points
238 int numPoints = _track.getNumPoints();
239 bufferedG.setColor(COLOR_POINT);
240 int halfWidth = width/2;
241 int halfHeight = height/2;
242 boolean currPointTrackpoint = false, lastPointTrackpoint = false;
243 for (int i=0; i<numPoints; i++)
245 x = halfWidth + (int) ((_track.getX(i) - _offsetX) / _scale * _zoomScale);
246 y = halfHeight - (int) ((_track.getY(i) - _offsetY) / _scale * _zoomScale);
247 if (x > BORDER_WIDTH && x < (width - BORDER_WIDTH)
248 && y < (height - BORDER_WIDTH) && y > BORDER_WIDTH)
250 // draw block for point (a bit faster than circles)
251 bufferedG.drawRect(x - 2, y - 2, 3, 3);
253 // See whether to connect the point with previous one or not
254 currPointTrackpoint = !_track.getPoint(i).isWaypoint() && _track.getPoint(i).getPhoto() == null;
255 if (_connectPointsMenuItem.isSelected() && currPointTrackpoint && lastPointTrackpoint)
257 bufferedG.drawLine(lastX, lastY, x, y);
259 lastPointTrackpoint = currPointTrackpoint;
262 lastPointTrackpoint = false;
264 lastX = x; lastY = y;
267 // Loop again and show waypoints with names
268 bufferedG.setColor(COLOR_WAYPT_NAME);
269 FontMetrics fm = bufferedG.getFontMetrics();
270 int nameHeight = fm.getHeight();
271 int numWaypointNamesShown = 0;
272 for (int i=0; i<numPoints; i++)
274 DataPoint point = _track.getPoint(i);
275 String waypointName = point.getWaypointName();
276 if (waypointName != null && !waypointName.equals(""))
278 // escape if nothing more to do
279 if (numWaypointNamesShown >= LIMIT_WAYPOINT_NAMES || _image == null) {break;}
280 // calculate coordinates of point
281 x = halfWidth + (int) ((_track.getX(i) - _offsetX) / _scale * _zoomScale);
282 y = halfHeight - (int) ((_track.getY(i) - _offsetY) / _scale * _zoomScale);
283 if (x > BORDER_WIDTH && x < (width - BORDER_WIDTH)
284 && y < (height - BORDER_WIDTH) && y > BORDER_WIDTH)
286 bufferedG.fillOval(x - 3, y - 3, 6, 6);
287 // Figure out where to draw name so it doesn't obscure track
288 int nameWidth = fm.stringWidth(waypointName);
289 if (nameWidth < (width - 2 * BORDER_WIDTH))
291 boolean drawnName = false;
292 // Make arrays for coordinates right left up down
293 int[] nameXs = {x + 2, x - nameWidth - 2, x - nameWidth/2, x - nameWidth/2};
294 int[] nameYs = {y + (nameHeight/2), y + (nameHeight/2), y - 2, y + nameHeight + 2};
295 for (int extraSpace = 4; extraSpace < 13 && !drawnName; extraSpace+=2)
297 // Shift arrays for coordinates right left up down
298 nameXs[0] += 2; nameXs[1] -= 2;
299 nameYs[2] -= 2; nameYs[3] += 2;
300 // Check each direction in turn right left up down
301 for (int a=0; a<4; a++)
303 if (nameXs[a] > BORDER_WIDTH && (nameXs[a] + nameWidth) < (width - BORDER_WIDTH)
304 && nameYs[a] < (height - BORDER_WIDTH) && (nameYs[a] - nameHeight) > BORDER_WIDTH
305 && !overlapsPoints(nameXs[a], nameYs[a], nameWidth, nameHeight))
307 // Found a rectangle to fit - draw name here and quit
308 bufferedG.drawString(waypointName, nameXs[a], nameYs[a]);
323 * Tests whether there are any data points within the specified x,y rectangle
324 * @param inX left X coordinate
325 * @param inY bottom Y coordinate
326 * @param inWidth width of rectangle
327 * @param inHeight height of rectangle
328 * @return true if there's at least one data point in the rectangle
330 private boolean overlapsPoints(int inX, int inY, int inWidth, int inHeight)
334 // loop over x coordinate of rectangle
335 for (int x=0; x<inWidth; x++)
337 // loop over y coordinate of rectangle
338 for (int y=0; y<inHeight; y++)
340 int pixelColor = _image.getRGB(inX + x, inY - y);
341 if (pixelColor != -1) return true;
345 catch (NullPointerException e) {
346 // ignore null pointers, just return false
353 * Make the popup menu for right-clicking the map
355 private void makePopup()
357 _popup = new JPopupMenu();
358 JMenuItem zoomIn = new JMenuItem(I18nManager.getText("menu.map.zoomin"));
359 zoomIn.addActionListener(new ActionListener() {
360 public void actionPerformed(ActionEvent e)
364 zoomIn.setEnabled(true);
366 JMenuItem zoomOut = new JMenuItem(I18nManager.getText("menu.map.zoomout"));
367 zoomOut.addActionListener(new ActionListener() {
368 public void actionPerformed(ActionEvent e)
372 zoomOut.setEnabled(true);
374 JMenuItem zoomFull = new JMenuItem(I18nManager.getText("menu.map.zoomfull"));
375 zoomFull.addActionListener(new ActionListener() {
376 public void actionPerformed(ActionEvent e)
380 zoomFull.setEnabled(true);
381 _popup.add(zoomFull);
382 _connectPointsMenuItem = new JCheckBoxMenuItem(I18nManager.getText("menu.map.connect"));
383 _connectPointsMenuItem.addActionListener(new ActionListener() {
384 public void actionPerformed(ActionEvent e)
387 dataUpdated(DataSubscriber.ALL);
390 _connectPointsMenuItem.setSelected(false);
391 _popup.add(_connectPointsMenuItem);
392 _autoPanMenuItem = new JCheckBoxMenuItem(I18nManager.getText("menu.map.autopan"));
393 _autoPanMenuItem.setSelected(true);
394 _popup.add(_autoPanMenuItem);
399 * Zoom map to full scale
401 private void zoomToFullScale()
407 dataUpdated(DataSubscriber.ALL);
412 * Zoom map either in or out by one step
413 * @param inZoomIn true to zoom in, false for out
415 private void zoomMap(boolean inZoomIn)
420 _zoomScale *= ZOOM_SCALE_FACTOR;
425 _zoomScale /= ZOOM_SCALE_FACTOR;
426 if (_zoomScale < 0.5) _zoomScale = 0.5;
429 dataUpdated(DataSubscriber.ALL);
434 * Pan the map by the specified amounts
435 * @param inUp upwards pan
436 * @param inRight rightwards pan
438 private void panMap(int inUp, int inRight)
440 double panFactor = _scale / _zoomScale;
441 _offsetY = _offsetY + (inUp * panFactor);
442 _offsetX = _offsetX - (inRight * panFactor);
443 // Limit pan to sensible range??
451 * React to click on map display
452 * @param inE mouse event
454 public void mouseClicked(MouseEvent inE)
459 int xClick = inE.getX();
460 int yClick = inE.getY();
461 // Check click is within main area (not in border)
462 if (xClick > BORDER_WIDTH && yClick > BORDER_WIDTH && xClick < (getWidth() - BORDER_WIDTH)
463 && yClick < (getHeight() - BORDER_WIDTH))
465 // Check left click or right click
466 if (inE.isMetaDown())
468 // Only show popup if track has data
469 if (_track != null && _track.getNumPoints() > 0)
470 _popup.show(this, xClick, yClick);
474 // Find point within range of click point
475 double pointX = (xClick - getWidth()/2) * _scale / _zoomScale + _offsetX;
476 double pointY = (getHeight()/2 - yClick) * _scale / _zoomScale + _offsetY;
477 int selectedPointIndex = _track.getNearestPointIndex(
478 pointX, pointY, CLICK_SENSITIVITY * _scale, false);
479 // Select the given point (or deselect if no point was found)
480 _trackInfo.getSelection().selectPoint(selectedPointIndex);
488 * Respond to mouse released to reset dragging
489 * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
491 public void mouseReleased(MouseEvent e)
493 _dragStartX = _dragStartY = -1;
496 if (_zoomDragFromX >= 0 || _zoomDragFromY >= 0)
498 // zoom area marked out - calculate offset and zoom
499 int xPan = (getWidth() - _zoomDragFromX - e.getX()) / 2;
500 int yPan = (getHeight() - _zoomDragFromY - e.getY()) / 2;
501 double xZoom = Math.abs(getWidth() * 1.0 / (e.getX() - _zoomDragFromX));
502 double yZoom = Math.abs(getHeight() * 1.0 / (e.getY() - _zoomDragFromY));
503 double extraZoom = (xZoom>yZoom?yZoom:xZoom);
504 // deselect point if selected (to stop autopan)
505 _trackInfo.getSelection().selectPoint(-1);
506 // Pan first to ensure pan occurs with correct scale
508 // Then zoom in and request repaint
509 _zoomScale = _zoomScale * extraZoom;
513 _zoomDragFromX = _zoomDragFromY = -1;
514 _zoomDragging = false;
520 * Respond to mouse wheel events to zoom the map
521 * @see java.awt.event.MouseWheelListener#mouseWheelMoved(java.awt.event.MouseWheelEvent)
523 public void mouseWheelMoved(MouseWheelEvent e)
525 zoomMap(e.getWheelRotation() < 0);
530 * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
532 public void keyPressed(KeyEvent e)
534 int code = e.getKeyCode();
535 // Check for meta key
536 if (e.isControlDown())
538 // Check for arrow keys to zoom in and out
539 if (code == KeyEvent.VK_UP)
541 else if (code == KeyEvent.VK_DOWN)
543 // Key nav for next/prev point
544 else if (code == KeyEvent.VK_LEFT)
545 _trackInfo.getSelection().selectPreviousPoint();
546 else if (code == KeyEvent.VK_RIGHT)
547 _trackInfo.getSelection().selectNextPoint();
551 // Check for arrow keys to pan
553 if (code == KeyEvent.VK_UP)
554 upwardsPan = PAN_DISTANCE;
555 else if (code == KeyEvent.VK_DOWN)
556 upwardsPan = -PAN_DISTANCE;
557 int rightwardsPan = 0;
558 if (code == KeyEvent.VK_RIGHT)
559 rightwardsPan = -PAN_DISTANCE;
560 else if (code == KeyEvent.VK_LEFT)
561 rightwardsPan = PAN_DISTANCE;
562 panMap(upwardsPan, rightwardsPan);
563 // Check for delete key to delete current point
564 if (code == KeyEvent.VK_DELETE && _trackInfo.getSelection().getCurrentPointIndex() >= 0)
566 _app.deleteCurrentPoint();
567 // reset last selected point to trigger autopan
568 _lastSelectedPoint = -1;
575 * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
577 public void keyReleased(KeyEvent e)
584 * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
586 public void keyTyped(KeyEvent e)
593 * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
595 public void mouseDragged(MouseEvent e)
601 int xShift = e.getX() - _dragStartX;
602 int yShift = e.getY() - _dragStartY;
603 panMap(yShift, xShift);
605 _dragStartX = e.getX();
606 _dragStartY = e.getY();
610 // Right click-and-drag for zoom
611 if (_zoomDragFromX < 0 || _zoomDragFromY < 0)
613 _zoomDragFromX = e.getX();
614 _zoomDragFromY = e.getY();
618 _zoomDragToX = e.getX();
619 _zoomDragToY = e.getY();
620 _zoomDragging = true;
628 * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
630 public void mouseMoved(MouseEvent e)