package tim.prune.gui.map;
+import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
+import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.FontMetrics;
import java.awt.Graphics;
+import java.awt.Graphics2D;
import java.awt.Image;
-import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.image.BufferedImage;
-import java.awt.image.RescaleOp;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import tim.prune.data.Coordinate;
import tim.prune.data.DataPoint;
import tim.prune.data.DoubleRange;
+import tim.prune.data.Field;
+import tim.prune.data.FieldList;
import tim.prune.data.Latitude;
import tim.prune.data.Longitude;
+import tim.prune.data.MidpointData;
import tim.prune.data.Selection;
import tim.prune.data.Track;
import tim.prune.data.TrackInfo;
+import tim.prune.function.compress.MarkPointsInRectangleFunction;
+import tim.prune.function.edit.FieldEdit;
+import tim.prune.function.edit.FieldEditList;
import tim.prune.gui.IconManager;
/**
private TrackInfo _trackInfo = null;
/** Selection object */
private Selection _selection = null;
+ /** Object to keep track of midpoints */
+ private MidpointData _midpoints = null;
+ /** Index of point clicked at mouseDown */
+ private int _clickedPoint = -1;
/** Previously selected point */
private int _prevSelectedPoint = -1;
/** Tile manager */
private JCheckBox _autopanCheckBox = null;
/** Checkbox for connecting track points */
private JCheckBox _connectCheckBox = null;
+ /** Checkbox for enable edit mode */
+ private JCheckBox _editmodeCheckBox = null;
/** Right-click popup menu */
private JPopupMenu _popup = null;
/** Top component panel */
private boolean _checkBounds = false;
/** Map position */
private MapPosition _mapPosition = null;
- /** x coordinate of drag from point */
- private int _dragFromX = -1;
- /** y coordinate of drag from point */
- private int _dragFromY = -1;
- /** Flag set to true for right-click dragging */
- private boolean _zoomDragging = false;
- /** x coordinate of drag to point */
- private int _dragToX = -1;
- /** y coordinate of drag to point */
- private int _dragToY = -1;
- /** x coordinate of popup menu */
- private int _popupMenuX = -1;
- /** y coordinate of popup menu */
- private int _popupMenuY = -1;
+ /** coordinates of drag from point */
+ private int _dragFromX = -1, _dragFromY = -1;
+ /** coordinates of drag to point */
+ private int _dragToX = -1, _dragToY = -1;
+ /** coordinates of popup menu */
+ private int _popupMenuX = -1, _popupMenuY = -1;
/** Flag to prevent showing too often the error message about loading maps */
private boolean _shownOsmErrorAlready = false;
+ /** Current drawing mode */
+ private int _drawMode = MODE_DEFAULT;
/** Constant for click sensitivity when selecting nearest point */
private static final int CLICK_SENSITIVITY = 10;
// Colours
private static final Color COLOR_MESSAGES = Color.GRAY;
+ // Drawing modes
+ private static final int MODE_DEFAULT = 0;
+ private static final int MODE_ZOOM_RECT = 1;
+ private static final int MODE_DRAW_POINTS_START = 2;
+ private static final int MODE_DRAW_POINTS_CONT = 3;
+ private static final int MODE_DRAG_POINT = 4;
+ private static final int MODE_CREATE_MIDPOINT = 5;
+ private static final int MODE_MARK_RECTANGLE = 6;
+
+ private static final int INDEX_UNKNOWN = -2;
+
/**
* Constructor
_trackInfo = inTrackInfo;
_track = inTrackInfo.getTrack();
_selection = inTrackInfo.getSelection();
+ _midpoints = new MidpointData();
_mapPosition = new MapPosition();
addMouseListener(this);
addMouseMotionListener(this);
_recalculate = true;
Config.setConfigBoolean(Config.KEY_SHOW_MAP, e.getStateChange() == ItemEvent.SELECTED);
UpdateMessageBroker.informSubscribers(); // to let menu know
+ // If the track is only partially visible and you turn the map off, make the track fully visible again
+ if (e.getStateChange() == ItemEvent.DESELECTED && _transparencySlider.getValue() < 0) {
+ _transparencySlider.setValue(0);
+ }
}
};
- _topPanel = new JPanel();
+ _topPanel = new OverlayPanel();
_topPanel.setLayout(new FlowLayout());
- _topPanel.setOpaque(false);
// Make slider for transparency
- _transparencySlider = new JSlider(0, 5, 0);
+ _transparencySlider = new JSlider(-6, 6, 0);
_transparencySlider.setPreferredSize(new Dimension(100, 20));
_transparencySlider.setMajorTickSpacing(1);
_transparencySlider.setSnapToTicks(true);
_transparencySlider.setOpaque(false);
+ _transparencySlider.setValue(0);
_transparencySlider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e)
{
- _recalculate = true;
- repaint();
+ int val = _transparencySlider.getValue();
+ if (val == 1 || val == -1)
+ _transparencySlider.setValue(0);
+ else {
+ _recalculate = true;
+ repaint();
+ }
}
});
_transparencySlider.setFocusable(false); // stop slider from stealing keyboard focus
_connectCheckBox.setFocusable(false); // stop button from stealing keyboard focus
_topPanel.add(_connectCheckBox);
+ // Add checkbox button for edit mode or not
+ _editmodeCheckBox = new JCheckBox(IconManager.getImageIcon(IconManager.EDIT_MODE_BUTTON), false);
+ _editmodeCheckBox.setSelectedIcon(IconManager.getImageIcon(IconManager.EDIT_MODE_BUTTON_ON));
+ _editmodeCheckBox.setOpaque(false);
+ _editmodeCheckBox.setToolTipText(I18nManager.getText("menu.map.editmode"));
+ _editmodeCheckBox.addItemListener(itemListener);
+ _editmodeCheckBox.setFocusable(false); // stop button from stealing keyboard focus
+ _topPanel.add(_editmodeCheckBox);
+
// Add zoom in, zoom out buttons
- _sidePanel = new JPanel();
+ _sidePanel = new OverlayPanel();
_sidePanel.setLayout(new BoxLayout(_sidePanel, BoxLayout.Y_AXIS));
- _sidePanel.setOpaque(false);
JButton zoomInButton = new JButton(IconManager.getImageIcon(IconManager.ZOOM_IN_BUTTON));
- zoomInButton.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
+ zoomInButton.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
zoomInButton.setContentAreaFilled(false);
zoomInButton.setToolTipText(I18nManager.getText("menu.map.zoomin"));
zoomInButton.addActionListener(new ActionListener() {
zoomInButton.setFocusable(false); // stop button from stealing keyboard focus
_sidePanel.add(zoomInButton);
JButton zoomOutButton = new JButton(IconManager.getImageIcon(IconManager.ZOOM_OUT_BUTTON));
- zoomOutButton.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
+ zoomOutButton.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
zoomOutButton.setContentAreaFilled(false);
zoomOutButton.setToolTipText(I18nManager.getText("menu.map.zoomout"));
zoomOutButton.addActionListener(new ActionListener() {
zoomInItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
+ panMap((_popupMenuX - getWidth()/2)/2, (_popupMenuY - getHeight()/2)/2);
zoomIn();
}});
- zoomInItem.setEnabled(true);
_popup.add(zoomInItem);
JMenuItem zoomOutItem = new JMenuItem(I18nManager.getText("menu.map.zoomout"));
zoomOutItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
+ panMap(-(_popupMenuX - getWidth()/2), -(_popupMenuY - getHeight()/2));
zoomOut();
}});
- zoomOutItem.setEnabled(true);
_popup.add(zoomOutItem);
JMenuItem zoomFullItem = new JMenuItem(I18nManager.getText("menu.map.zoomfull"));
zoomFullItem.addActionListener(new ActionListener() {
_recalculate = true;
repaint();
}});
- zoomFullItem.setEnabled(true);
_popup.add(zoomFullItem);
_popup.addSeparator();
// Set background
newPointItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
- double lat = MapUtils.getLatitudeFromY(_mapPosition.getYFromPixels(_popupMenuY, getHeight()));
- double lon = MapUtils.getLongitudeFromX(_mapPosition.getXFromPixels(_popupMenuX, getWidth()));
- _app.createPoint(new DataPoint(new Latitude(lat, Coordinate.FORMAT_NONE),
- new Longitude(lon, Coordinate.FORMAT_NONE), null));
+ _app.createPoint(createPointFromClick(_popupMenuX, _popupMenuY));
}});
- newPointItem.setEnabled(true);
_popup.add(newPointItem);
+ // draw point series
+ JMenuItem drawPointsItem = new JMenuItem(I18nManager.getText("menu.map.drawpoints"));
+ drawPointsItem.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent e)
+ {
+ _drawMode = MODE_DRAW_POINTS_START;
+ }
+ });
+ _popup.add(drawPointsItem);
}
_yRange = new DoubleRange(MapUtils.getYFromLatitude(_latRange.getMinimum()),
MapUtils.getYFromLatitude(_latRange.getMaximum()));
_mapPosition.zoomToXY(_xRange.getMinimum(), _xRange.getMaximum(), _yRange.getMinimum(), _yRange.getMaximum(),
- getWidth(), getHeight());
+ getWidth(), getHeight());
}
if ((_mapImage == null || _recalculate))
{
paintMapContents();
- _scaleBar.updateScale(_mapPosition.getZoom(), _mapPosition.getCentreTileY());
+ _scaleBar.updateScale(_mapPosition.getZoom(), _mapPosition.getYFromPixels(0, 0));
}
// Draw the prepared image onto the panel
if (_mapImage != null) {
inG.drawImage(_mapImage, 0, 0, getWidth(), getHeight(), null);
}
- // Draw the zoom rectangle if necessary
- if (_zoomDragging)
+
+ switch (_drawMode)
{
- inG.setColor(Color.RED);
- inG.drawLine(_dragFromX, _dragFromY, _dragFromX, _dragToY);
- inG.drawLine(_dragFromX, _dragFromY, _dragToX, _dragFromY);
- inG.drawLine(_dragToX, _dragFromY, _dragToX, _dragToY);
- inG.drawLine(_dragFromX, _dragToY, _dragToX, _dragToY);
+ case MODE_DRAG_POINT:
+ drawDragLines(inG, _selection.getCurrentPointIndex()-1, _selection.getCurrentPointIndex()+1);
+ break;
+
+ case MODE_CREATE_MIDPOINT:
+ drawDragLines(inG, _clickedPoint-1, _clickedPoint);
+ break;
+
+ case MODE_ZOOM_RECT:
+ case MODE_MARK_RECTANGLE:
+ if (_dragFromX != -1 && _dragFromY != -1)
+ {
+ // Draw the zoom rectangle if necessary
+ inG.setColor(Color.RED);
+ inG.drawLine(_dragFromX, _dragFromY, _dragFromX, _dragToY);
+ inG.drawLine(_dragFromX, _dragFromY, _dragToX, _dragFromY);
+ inG.drawLine(_dragToX, _dragFromY, _dragToX, _dragToY);
+ inG.drawLine(_dragFromX, _dragToY, _dragToX, _dragToY);
+ }
+ break;
+
+ case MODE_DRAW_POINTS_CONT:
+ // draw line to mouse position to show drawing mode
+ inG.setColor(Config.getColourScheme().getColour(ColourScheme.IDX_POINT));
+ int prevIndex = _track.getNumPoints()-1;
+ int px = getWidth() / 2 + _mapPosition.getXFromCentre(_track.getX(prevIndex));
+ int py = getHeight() / 2 + _mapPosition.getYFromCentre(_track.getY(prevIndex));
+ inG.drawLine(px, py, _dragToX, _dragToY);
+ break;
}
}
else
}
}
- // Make maps brighter / fainter
- float[] scaleFactors = {1.0f, 1.05f, 1.1f, 1.2f, 1.6f, 2.0f};
- float scaleFactor = scaleFactors[_transparencySlider.getValue()];
- if (scaleFactor > 1.0f)
+ // Make maps brighter / fainter according to slider
+ final int brightnessIndex = Math.max(1, _transparencySlider.getValue()) - 1;
+ if (brightnessIndex > 0)
{
- RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
- hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
- RescaleOp op = new RescaleOp(scaleFactor, 0, hints);
- op.filter(_mapImage, _mapImage);
+ final int[] alphas = {0, 40, 80, 120, 160, 210};
+ Color bgColor = Config.getColourScheme().getColour(ColourScheme.IDX_BACKGROUND);
+ bgColor = new Color(bgColor.getRed(), bgColor.getGreen(), bgColor.getBlue(), alphas[brightnessIndex]);
+ g.setColor(bgColor);
+ g.fillRect(0, 0, getWidth(), getHeight());
}
}
}
private int paintPoints(Graphics inG)
{
// Set up colours
- final Color pointColour = Config.getColourScheme().getColour(ColourScheme.IDX_POINT);
- final Color rangeColour = Config.getColourScheme().getColour(ColourScheme.IDX_SELECTION);
- final Color currentColour = Config.getColourScheme().getColour(ColourScheme.IDX_PRIMARY);
- final Color secondColour = Config.getColourScheme().getColour(ColourScheme.IDX_SECONDARY);
- final Color textColour = Config.getColourScheme().getColour(ColourScheme.IDX_TEXT);
+ final ColourScheme cs = Config.getColourScheme();
+ final int[] opacities = {255, 190, 130, 80, 40, 0};
+ int opacity = 255;
+ if (_transparencySlider.getValue() < 0)
+ opacity = opacities[-1 - _transparencySlider.getValue()];
+ final Color pointColour = makeTransparentColour(cs.getColour(ColourScheme.IDX_POINT), opacity);
+ final Color rangeColour = makeTransparentColour(cs.getColour(ColourScheme.IDX_SELECTION), opacity);
+ final Color currentColour = makeTransparentColour(cs.getColour(ColourScheme.IDX_PRIMARY), opacity);
+ final Color secondColour = makeTransparentColour(cs.getColour(ColourScheme.IDX_SECONDARY), opacity);
+ final Color textColour = makeTransparentColour(cs.getColour(ColourScheme.IDX_TEXT), opacity);
+
+ final int winWidth = getWidth();
+ final int winHeight = getHeight();
+ final int halfWinWidth = winWidth / 2;
+ final int halfWinHeight = winHeight / 2;
+ final int numPoints = _track.getNumPoints();
+ final int[] xPixels = new int[numPoints];
+ final int[] yPixels = new int[numPoints];
+
+ // try to set line width for painting
+ if (inG instanceof Graphics2D)
+ {
+ int lineWidth = Config.getConfigInt(Config.KEY_LINE_WIDTH);
+ if (lineWidth < 1 || lineWidth > 4) {lineWidth = 2;}
+ ((Graphics2D) inG).setStroke(new BasicStroke(lineWidth));
+ }
int pointsPainted = 0;
// draw track points
inG.setColor(pointColour);
boolean prevPointVisible = false, currPointVisible = false;
boolean anyWaypoints = false;
boolean isWaypoint = false;
- for (int i=0; i<_track.getNumPoints(); i++)
+ for (int i=0; i<numPoints; i++)
{
- int px = getWidth() / 2 + _mapPosition.getXFromCentre(_track.getX(i));
- int py = getHeight() / 2 + _mapPosition.getYFromCentre(_track.getY(i));
- currPointVisible = px >= 0 && px < getWidth() && py >= 0 && py < getHeight();
+ // Calculate pixel position of point from its x, y coordinates
+ int px = halfWinWidth + _mapPosition.getXFromCentre(_track.getX(i));
+ int py = halfWinHeight + _mapPosition.getYFromCentre(_track.getY(i));
+ px = wrapLongitudeValue(px, winWidth, _mapPosition.getZoom());
+ // Remember these calculated pixel values so they don't have to be recalculated
+ xPixels[i] = px; yPixels[i] = py;
+
+ currPointVisible = px >= 0 && px < winWidth && py >= 0 && py < winHeight;
isWaypoint = _track.getPoint(i).isWaypoint();
anyWaypoints = anyWaypoints || isWaypoint;
if (currPointVisible)
inG.setColor(textColour);
FontMetrics fm = inG.getFontMetrics();
int nameHeight = fm.getHeight();
- int width = getWidth();
- int height = getHeight();
- if (anyWaypoints) {
+ if (anyWaypoints)
+ {
for (int i=0; i<_track.getNumPoints(); i++)
{
if (_track.getPoint(i).isWaypoint())
{
- int px = getWidth() / 2 + _mapPosition.getXFromCentre(_track.getX(i));
- int py = getHeight() / 2 + _mapPosition.getYFromCentre(_track.getY(i));
- if (px >= 0 && px < getWidth() && py >= 0 && py < getHeight())
+ int px = xPixels[i];
+ int py = yPixels[i];
+ if (px >= 0 && px < winWidth && py >= 0 && py < winHeight)
{
inG.fillRect(px-3, py-3, 6, 6);
pointsPainted++;
{
if (_track.getPoint(i).isWaypoint())
{
- int px = getWidth() / 2 + _mapPosition.getXFromCentre(_track.getX(i));
- int py = getHeight() / 2 + _mapPosition.getYFromCentre(_track.getY(i));
- if (px >= 0 && px < getWidth() && py >= 0 && py < getHeight())
+ int px = xPixels[i];
+ int py = yPixels[i];
+ if (px >= 0 && px < winWidth && py >= 0 && py < winHeight)
{
// Figure out where to draw waypoint name so it doesn't obscure track
String waypointName = _track.getPoint(i).getWaypointName();
// Check each direction in turn right left up down
for (int a=0; a<4; a++)
{
- if (nameXs[a] > 0 && (nameXs[a] + nameWidth) < width
- && nameYs[a] < height && (nameYs[a] - nameHeight) > 0
+ if (nameXs[a] > 0 && (nameXs[a] + nameWidth) < winWidth
+ && nameYs[a] < winHeight && (nameYs[a] - nameHeight) > 0
&& !overlapsPoints(nameXs[a], nameYs[a], nameWidth, nameHeight, textColour))
{
// Found a rectangle to fit - draw name here and quit
}
}
}
- // Loop over points, drawing blobs for photo points
+ // Loop over points, drawing blobs for photo / audio points
inG.setColor(secondColour);
for (int i=0; i<_track.getNumPoints(); i++)
{
- if (_track.getPoint(i).getPhoto() != null)
+ if (_track.getPoint(i).hasMedia())
{
- int px = getWidth() / 2 + _mapPosition.getXFromCentre(_track.getX(i));
- int py = getHeight() / 2 + _mapPosition.getYFromCentre(_track.getY(i));
- if (px >= 0 && px < getWidth() && py >= 0 && py < getHeight())
+ int px = xPixels[i];
+ int py = yPixels[i];
+ if (px >= 0 && px < winWidth && py >= 0 && py < winHeight)
{
inG.drawRect(px-1, py-1, 2, 2);
inG.drawRect(px-2, py-2, 4, 4);
inG.setColor(rangeColour);
for (int i=_selection.getStart(); i<=_selection.getEnd(); i++)
{
- int px = getWidth() / 2 + _mapPosition.getXFromCentre(_track.getX(i));
- int py = getHeight() / 2 + _mapPosition.getYFromCentre(_track.getY(i));
+ int px = xPixels[i];
+ int py = yPixels[i];
inG.drawRect(px-1, py-1, 2, 2);
}
}
- // Draw selected point, crosshairs
+ // Draw crosshairs at selected point
int selectedPoint = _selection.getCurrentPointIndex();
if (selectedPoint >= 0)
{
- int px = getWidth() / 2 + _mapPosition.getXFromCentre(_track.getX(selectedPoint));
- int py = getHeight() / 2 + _mapPosition.getYFromCentre(_track.getY(selectedPoint));
+ int px = xPixels[selectedPoint];
+ int py = yPixels[selectedPoint];
inG.setColor(currentColour);
// crosshairs
- inG.drawLine(px, 0, px, getHeight());
- inG.drawLine(0, py, getWidth(), py);
- // oval
- inG.drawOval(px - 2, py - 2, 4, 4);
- inG.drawOval(px - 3, py - 3, 6, 6);
+ inG.drawLine(px, 0, px, winHeight);
+ inG.drawLine(0, py, winWidth, py);
}
// Return the number of points painted
return pointsPainted;
}
+ /**
+ * Wrap the given pixel value if appropriate and possible
+ * @param inPx Pixel x coordinate
+ * @param inWinWidth window width in pixels
+ * @param inZoom zoom level
+ * @return modified pixel x coordinate
+ */
+ private static int wrapLongitudeValue(int inPx, int inWinWidth, int inZoom)
+ {
+ if (inPx > inWinWidth)
+ {
+ // Pixel is too far right, could we wrap it back onto the screen?
+ int px = inPx;
+ while (px > inWinWidth) {
+ px -= (256 << inZoom);
+ }
+ if (px >= 0) {
+ return px; // successfully wrapped back onto the screen
+ }
+ }
+ else if (inPx < 0)
+ {
+ // Pixel is too far left, could we wrap it back onto the screen?
+ int px = inPx;
+ while (px < 0) {
+ px += (256 << inZoom);
+ }
+ if (px < inWinWidth) {
+ return px; // successfully wrapped back onto the screen
+ }
+ }
+ // Either it's already on the screen or couldn't be wrapped
+ return inPx;
+ }
+
+ /**
+ * Draw the lines while dragging a point
+ * @param inG graphics object
+ * @param inPrevIndex index of point to draw from
+ * @param inNextIndex index of point to draw to
+ */
+ private void drawDragLines(Graphics inG, int inPrevIndex, int inNextIndex)
+ {
+ inG.setColor(Config.getColourScheme().getColour(ColourScheme.IDX_POINT));
+ // line from prev point to cursor
+ if (inPrevIndex > -1 && !_track.getPoint(inPrevIndex+1).getSegmentStart())
+ {
+ final int px = getWidth() / 2 + _mapPosition.getXFromCentre(_track.getX(inPrevIndex));
+ final int py = getHeight() / 2 + _mapPosition.getYFromCentre(_track.getY(inPrevIndex));
+ inG.drawLine(px, py, _dragToX, _dragToY);
+ }
+ if (inNextIndex < _track.getNumPoints() && !_track.getPoint(inNextIndex).getSegmentStart())
+ {
+ final int px = getWidth() / 2 + _mapPosition.getXFromCentre(_track.getX(inNextIndex));
+ final int py = getHeight() / 2 + _mapPosition.getYFromCentre(_track.getY(inNextIndex));
+ inG.drawLine(px, py, _dragToX, _dragToY);
+ }
+ }
/**
* Tests whether there are any dark pixels within the specified x,y rectangle
return false;
}
+ /**
+ * Make a semi-transparent colour for drawing with
+ * @param inColour base colour (fully opaque)
+ * @param inOpacity opacity where 0=invisible and 255=full
+ * @return new colour object
+ */
+ private static Color makeTransparentColour(Color inColour, int inOpacity)
+ {
+ if (inOpacity > 240) return inColour;
+ return new Color(inColour.getRed(), inColour.getGreen(), inColour.getBlue(), inOpacity);
+ }
/**
* Inform that tiles have been updated and the map can be repainted
repaint();
}
+ /**
+ * Create a DataPoint object from the given click coordinates
+ * @param inX x coordinate of click
+ * @param inY y coordinate of click
+ * @return DataPoint with given coordinates and no altitude
+ */
+ private DataPoint createPointFromClick(int inX, int inY)
+ {
+ double lat = MapUtils.getLatitudeFromY(_mapPosition.getYFromPixels(inY, getHeight()));
+ double lon = MapUtils.getLongitudeFromX(_mapPosition.getXFromPixels(inX, getWidth()));
+ return new DataPoint(new Latitude(lat, Coordinate.FORMAT_NONE),
+ new Longitude(lon, Coordinate.FORMAT_NONE), null);
+ }
+
+ /**
+ * Move a DataPoint object to the given mouse coordinates
+ * @param startX start x coordinate of mouse
+ * @param startY start y coordinate of mouse
+ * @param endX end x coordinate of mouse
+ * @param endY end y coordinate of mouse
+ */
+ private void movePointToMouse(int startX, int startY, int endX, int endY )
+ {
+ double lat1 = MapUtils.getLatitudeFromY(_mapPosition.getYFromPixels(startY, getHeight()));
+ double lon1 = MapUtils.getLongitudeFromX(_mapPosition.getXFromPixels(startX, getWidth()));
+ double lat_delta = MapUtils.getLatitudeFromY(_mapPosition.getYFromPixels(endY, getHeight())) - lat1;
+ double lon_delta = MapUtils.getLongitudeFromX(_mapPosition.getXFromPixels(endX, getWidth())) - lon1;
+
+ DataPoint point = _trackInfo.getCurrentPoint();
+ if (point == null) {
+ return;
+ }
+
+ // Make lists for edit and undo, and add each changed field in turn
+ FieldEditList editList = new FieldEditList();
+ FieldEditList undoList = new FieldEditList();
+
+ // Check field list
+ FieldList fieldList = _track.getFieldList();
+ int numFields = fieldList.getNumFields();
+ for (int i=0; i<numFields; i++)
+ {
+ Field field = fieldList.getField(i);
+ if (field == Field.LATITUDE) {
+ editList.addEdit(new FieldEdit(field, Double.toString(point.getLatitude().getDouble() + lat_delta)));
+ undoList.addEdit(new FieldEdit(field, point.getFieldValue(Field.LATITUDE)));
+ }
+ else if (field == Field.LONGITUDE) {
+ editList.addEdit(new FieldEdit(field, Double.toString(point.getLongitude().getDouble() + lon_delta)));
+ undoList.addEdit(new FieldEdit(field, point.getFieldValue(Field.LONGITUDE)));
+ }
+ }
+ _app.completePointEdit(editList, undoList);
+ }
+
+
/**
* @see javax.swing.JComponent#getMinimumSize()
*/
// select point if it's a left-click
if (!inE.isMetaDown())
{
- int pointIndex = _track.getNearestPointIndex(
- _mapPosition.getXFromPixels(inE.getX(), getWidth()),
- _mapPosition.getYFromPixels(inE.getY(), getHeight()),
- _mapPosition.getBoundsFromPixels(CLICK_SENSITIVITY), false);
- // Extend selection for shift-click
- if (inE.isShiftDown()) {
- _trackInfo.extendSelection(pointIndex);
+ if (inE.getClickCount() == 1)
+ {
+ // single click
+ if (_drawMode == MODE_DEFAULT)
+ {
+ int pointIndex = _clickedPoint;
+ if (pointIndex == INDEX_UNKNOWN)
+ {
+ // index hasn't been calculated yet
+ pointIndex = _track.getNearestPointIndex(
+ _mapPosition.getXFromPixels(inE.getX(), getWidth()),
+ _mapPosition.getYFromPixels(inE.getY(), getHeight()),
+ _mapPosition.getBoundsFromPixels(CLICK_SENSITIVITY), false);
+ }
+ // Extend selection for shift-click
+ if (inE.isShiftDown()) {
+ _trackInfo.extendSelection(pointIndex);
+ }
+ else {
+ _trackInfo.selectPoint(pointIndex);
+ }
+ }
+ else if (_drawMode == MODE_DRAW_POINTS_START)
+ {
+ _app.createPoint(createPointFromClick(inE.getX(), inE.getY()));
+ _dragToX = inE.getX();
+ _dragToY = inE.getY();
+ _drawMode = MODE_DRAW_POINTS_CONT;
+ }
+ else if (_drawMode == MODE_DRAW_POINTS_CONT)
+ {
+ DataPoint point = createPointFromClick(inE.getX(), inE.getY());
+ _app.createPoint(point);
+ point.setSegmentStart(false);
+ }
}
- else {
- _trackInfo.selectPoint(pointIndex);
+ else if (inE.getClickCount() == 2)
+ {
+ // double click
+ if (_drawMode == MODE_DEFAULT) {
+ panMap(inE.getX() - getWidth()/2, inE.getY() - getHeight()/2);
+ zoomIn();
+ }
+ else if (_drawMode == MODE_DRAW_POINTS_START || _drawMode == MODE_DRAW_POINTS_CONT) {
+ _drawMode = MODE_DEFAULT;
+ }
}
}
else
_popup.show(this, _popupMenuX, _popupMenuY);
}
}
+ // Reset app mode
+ _app.setCurrentMode(App.AppMode.NORMAL);
+ if (_drawMode == MODE_MARK_RECTANGLE) _drawMode = MODE_DEFAULT;
}
/**
}
/**
- * Ignore mouse pressed events
+ * React to mouse pressed events to initiate a point drag
* @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
*/
public void mousePressed(MouseEvent inE)
{
- // ignore
+ _clickedPoint = INDEX_UNKNOWN;
+ if (_track == null || _track.getNumPoints() <= 0)
+ return;
+ if (!inE.isMetaDown())
+ {
+ // Left mouse drag - check if point is near; if so select it for dragging
+ if (_drawMode == MODE_DEFAULT)
+ {
+ /* Drag points if edit mode is enabled OR ALT is pressed */
+ if (_editmodeCheckBox.isSelected() || inE.isAltDown() || inE.isAltGraphDown())
+ {
+ final double clickX = _mapPosition.getXFromPixels(inE.getX(), getWidth());
+ final double clickY = _mapPosition.getYFromPixels(inE.getY(), getHeight());
+ final double clickSens = _mapPosition.getBoundsFromPixels(CLICK_SENSITIVITY);
+ _clickedPoint = _track.getNearestPointIndex(clickX, clickY, clickSens, false);
+
+ if (_clickedPoint >= 0)
+ {
+ // TODO: maybe use another color of the cross or remove the cross while dragging???
+
+ _trackInfo.selectPoint(_clickedPoint);
+ if (_trackInfo.getCurrentPoint() != null)
+ {
+ _drawMode = MODE_DRAG_POINT;
+ _dragFromX = _dragToX = inE.getX();
+ _dragFromY = _dragToY = inE.getY();
+ }
+ }
+ else
+ {
+ // Not a click on a point, so check half-way between two (connected) trackpoints
+ int midpointIndex = _midpoints.getNearestPointIndex(clickX, clickY, clickSens);
+ if (midpointIndex > 0)
+ {
+ _drawMode = MODE_CREATE_MIDPOINT;
+ _clickedPoint = midpointIndex;
+ _dragFromX = _dragToX = inE.getX();
+ _dragFromY = _dragToY = inE.getY();
+ }
+ }
+ }
+ }
+ }
+ // else right-press ignored
}
/**
public void mouseReleased(MouseEvent inE)
{
_recalculate = true;
- if (_zoomDragging && Math.abs(_dragToX - _dragFromX) > 20 && Math.abs(_dragToY - _dragFromY) > 20)
+
+ if (_drawMode == MODE_DRAG_POINT)
{
- //System.out.println("Finished zoom: " + _dragFromX + ", " + _dragFromY + " to " + _dragToX + ", " + _dragToY);
- _mapPosition.zoomToPixels(_dragFromX, _dragToX, _dragFromY, _dragToY, getWidth(), getHeight());
+ if (Math.abs(_dragToX - _dragFromX) > 2
+ || Math.abs(_dragToY - _dragFromY) > 2)
+ {
+ movePointToMouse(_dragFromX, _dragFromY, _dragToX, _dragToY );
+ }
+ _drawMode = MODE_DEFAULT;
+ }
+ else if (_drawMode == MODE_CREATE_MIDPOINT)
+ {
+ _drawMode = MODE_DEFAULT;
+ _app.createPoint(createPointFromClick(_dragToX, _dragToY), _clickedPoint);
+ }
+ else if (_drawMode == MODE_ZOOM_RECT)
+ {
+ if (Math.abs(_dragToX - _dragFromX) > 20
+ && Math.abs(_dragToY - _dragFromY) > 20)
+ {
+ _mapPosition.zoomToPixels(_dragFromX, _dragToX, _dragFromY, _dragToY, getWidth(), getHeight());
+ }
+ _drawMode = MODE_DEFAULT;
+ }
+ else if (_drawMode == MODE_MARK_RECTANGLE)
+ {
+ // Reset app mode
+ _app.setCurrentMode(App.AppMode.NORMAL);
+ _drawMode = MODE_DEFAULT;
+ // Call a function to mark the points
+ MarkPointsInRectangleFunction marker = new MarkPointsInRectangleFunction(_app);
+ double lon1 = MapUtils.getLongitudeFromX(_mapPosition.getXFromPixels(_dragFromX, getWidth()));
+ double lat1 = MapUtils.getLatitudeFromY(_mapPosition.getYFromPixels(_dragFromY, getHeight()));
+ double lon2 = MapUtils.getLongitudeFromX(_mapPosition.getXFromPixels(_dragToX, getWidth()));
+ double lat2 = MapUtils.getLatitudeFromY(_mapPosition.getYFromPixels(_dragToY, getHeight()));
+ // Invalidate rectangle if pixel coords are (-1,-1)
+ if (_dragFromX < 0 || _dragFromY < 0) {
+ lon1 = lon2;
+ lat1 = lat2;
+ }
+ marker.setRectCoords(lon1, lat1, lon2, lat2);
+ marker.begin();
}
_dragFromX = _dragFromY = -1;
- _zoomDragging = false;
repaint();
}
{
if (!inE.isMetaDown())
{
- // Left mouse drag - pan map by appropriate amount
- _zoomDragging = false;
- if (_dragFromX != -1)
+ // Left mouse drag - either drag the point or pan the map
+ if (_drawMode == MODE_DRAG_POINT || _drawMode == MODE_CREATE_MIDPOINT)
{
- panMap(_dragFromX - inE.getX(), _dragFromY - inE.getY());
+ // move point
+ _dragToX = inE.getX();
+ _dragToY = inE.getY();
_recalculate = true;
repaint();
}
- _dragFromX = inE.getX();
- _dragFromY = inE.getY();
+ else if (_drawMode == MODE_MARK_RECTANGLE)
+ {
+ // draw a rectangle for marking points
+ if (_dragFromX == -1) {
+ _dragFromX = inE.getX();
+ _dragFromY = inE.getY();
+ }
+ _dragToX = inE.getX();
+ _dragToY = inE.getY();
+ repaint();
+ }
+ else
+ {
+ // regular left-drag pans map by appropriate amount
+ if (_dragFromX != -1)
+ {
+ panMap(_dragFromX - inE.getX(), _dragFromY - inE.getY());
+ }
+ _dragFromX = _dragToX = inE.getX();
+ _dragFromY = _dragToY = inE.getY();
+ }
}
else
{
- // Right-click and drag - draw rectangle and control zoom
- _zoomDragging = true;
+ // Right-click and drag - update rectangle
+ _drawMode = MODE_ZOOM_RECT;
if (_dragFromX == -1) {
_dragFromX = inE.getX();
_dragFromY = inE.getY();
*/
public void mouseMoved(MouseEvent inEvent)
{
- // ignore
+ boolean useCrosshairs = false;
+ boolean useResize = false;
+ // Ignore unless we're drawing points
+ if (_drawMode == MODE_DRAW_POINTS_CONT)
+ {
+ _dragToX = inEvent.getX();
+ _dragToY = inEvent.getY();
+ repaint();
+ }
+ else if (_drawMode == MODE_MARK_RECTANGLE) {
+ useResize = true;
+ }
+ else if (_editmodeCheckBox.isSelected() || inEvent.isAltDown() || inEvent.isAltGraphDown())
+ {
+ // Try to find a point or a midpoint at this location, and if there is one
+ // then change the cursor to crosshairs
+ final double clickX = _mapPosition.getXFromPixels(inEvent.getX(), getWidth());
+ final double clickY = _mapPosition.getYFromPixels(inEvent.getY(), getHeight());
+ final double clickSens = _mapPosition.getBoundsFromPixels(CLICK_SENSITIVITY);
+ useCrosshairs = (_track.getNearestPointIndex(clickX, clickY, clickSens, false) >= 0
+ || _midpoints.getNearestPointIndex(clickX, clickY, clickSens) >= 0
+ );
+ }
+ if (useCrosshairs && !isCursorSet()) {
+ setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
+ }
+ else if (useResize && !isCursorSet()) {
+ setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));
+ }
+ else if (!useCrosshairs && !useResize && isCursorSet()) {
+ setCursor(null);
+ }
}
/**
if ((inUpdateType & DataSubscriber.MAPSERVER_CHANGED) > 0) {
_tileManager.resetConfig();
}
+ if ((inUpdateType & (DataSubscriber.DATA_ADDED_OR_REMOVED + DataSubscriber.DATA_EDITED)) > 0) {
+ _midpoints.updateData(_track);
+ }
+ // See if rect mode has been activated
+ if (_app.getCurrentMode() == App.AppMode.DRAWRECT)
+ {
+ _drawMode = MODE_MARK_RECTANGLE;
+ if (!isCursorSet()) {
+ setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));
+ }
+ }
repaint();
// enable or disable components
boolean hasData = _track.getNumPoints() > 0;
// Check for Ctrl key (for Linux/Win) or meta key (Clover key for Mac)
if (inE.isControlDown() || inE.isMetaDown())
{
+ // Shift as well makes things faster
+ final int pointIncrement = inE.isShiftDown()?3:1;
// Check for arrow keys to zoom in and out
if (code == KeyEvent.VK_UP)
zoomIn();
zoomOut();
// Key nav for next/prev point
else if (code == KeyEvent.VK_LEFT && currPointIndex > 0)
- _trackInfo.selectPoint(currPointIndex-1);
+ _trackInfo.incrementPointIndex(-pointIncrement);
else if (code == KeyEvent.VK_RIGHT)
- _trackInfo.selectPoint(currPointIndex+1);
+ _trackInfo.incrementPointIndex(pointIncrement);
else if (code == KeyEvent.VK_PAGE_UP)
_trackInfo.selectPoint(Checker.getPreviousSegmentStart(
_trackInfo.getTrack(), _trackInfo.getSelection().getCurrentPointIndex()));
else if (code == KeyEvent.VK_LEFT)
rightwardsPan = -PAN_DISTANCE;
panMap(rightwardsPan, upwardsPan);
+ // Check for escape
+ if (code == KeyEvent.VK_ESCAPE)
+ _drawMode = MODE_DEFAULT;
// Check for backspace key to delete current point (delete key already handled by menu)
- if (code == KeyEvent.VK_BACK_SPACE && currPointIndex >= 0) {
+ else if (code == KeyEvent.VK_BACK_SPACE && currPointIndex >= 0) {
_app.deleteCurrentPoint();
}
}
public void mouseWheelMoved(MouseWheelEvent inE)
{
int clicks = inE.getWheelRotation();
- if (clicks < 0)
+ if (clicks < 0) {
+ panMap((inE.getX() - getWidth()/2)/2, (inE.getY() - getHeight()/2)/2);
zoomIn();
- else if (clicks > 0)
+ }
+ else if (clicks > 0) {
+ panMap(-(inE.getX() - getWidth()/2), -(inE.getY() - getHeight()/2));
zoomOut();
+ }
}
/**