org.openstreetmap.josm.actions.mapmode
Class DrawAction

java.lang.Object
  extended by javax.swing.AbstractAction
      extended by org.openstreetmap.josm.actions.JosmAction
          extended by org.openstreetmap.josm.actions.mapmode.MapMode
              extended by org.openstreetmap.josm.actions.mapmode.DrawAction
All Implemented Interfaces:
java.awt.event.ActionListener, java.awt.event.AWTEventListener, java.awt.event.MouseListener, java.awt.event.MouseMotionListener, java.io.Serializable, java.lang.Cloneable, java.util.EventListener, javax.swing.Action, SelectionChangedListener, MapViewPaintable, Destroyable

public class DrawAction
extends MapMode
implements MapViewPaintable, SelectionChangedListener, java.awt.event.AWTEventListener

Mapmode to add nodes, create and extend ways.

See Also:
Serialized Form

Nested Class Summary
 class DrawAction.BackSpaceAction
           
private  class DrawAction.SnapChangeAction
           
private  class DrawAction.SnapHelper
           
 
Field Summary
private  Shortcut backspaceShortcut
           
private  Node currentBaseNode
           
private  EastNorth currentMouseEastNorth
           
private  java.awt.Cursor cursorJoinNode
           
private  java.awt.Cursor cursorJoinWay
           
private  boolean drawHelperLine
           
private  boolean drawTargetHighlight
           
private  Node lastUsedNode
           
private  Node mouseOnExistingNode
           
private  java.util.Set<Way> mouseOnExistingWays
           
private  java.awt.Point mousePos
           
private  java.util.Set<OsmPrimitive> newHighlights
           
private  java.util.Set<OsmPrimitive> oldHighlights
           
private  java.awt.Point oldMousePos
           
private  double PHI
           
private  Node previousNode
           
private  java.awt.event.KeyEvent releaseEvent
           
private  java.awt.Point rightClickPressPos
           
private  java.awt.Color selectedColor
           
private  java.util.TreeSet<java.lang.Integer> set
           
private  DrawAction.SnapChangeAction snapChangeAction
           
private  javax.swing.JCheckBoxMenuItem snapCheckboxMenuItem
           
private  DrawAction.SnapHelper snapHelper
           
private  Shortcut snappingShortcut
           
private  javax.swing.Timer timer
           
private  boolean useRepeatedShortcut
           
private  boolean wayIsFinished
           
 
Fields inherited from class org.openstreetmap.josm.actions.mapmode.MapMode
alt, ctrl, cursor, shift
 
Fields inherited from class org.openstreetmap.josm.actions.JosmAction
sc
 
Fields inherited from class javax.swing.AbstractAction
changeSupport, enabled
 
Fields inherited from interface javax.swing.Action
ACCELERATOR_KEY, ACTION_COMMAND_KEY, DEFAULT, DISPLAYED_MNEMONIC_INDEX_KEY, LARGE_ICON_KEY, LONG_DESCRIPTION, MNEMONIC_KEY, NAME, SELECTED_KEY, SHORT_DESCRIPTION, SMALL_ICON
 
Constructor Summary
DrawAction(MapFrame mapFrame)
           
 
Method Summary
private  void addHighlighting()
          Takes the data from computeHelperLine to determine which ways/nodes should be highlighted (if feature enabled).
private  javax.swing.JCheckBoxMenuItem addMenuItem()
           
private static void adjustNode(java.util.Collection<Pair<Node,Node>> segs, Node n)
          Adjusts the position of a node to lie on a segment (or a segment intersection).
private  void computeHelperLine()
          This method prepares data required for painting the "helper line" from the last used position to the mouse cursor.
 void destroy()
          Called when the object has been destroyed.
(package private) static double det(double a, double b, double c, double d)
           
private  void determineCurrentBaseNodeAndPreviousNode(java.util.Collection<OsmPrimitive> selection)
          Helper function that sets fields currentBaseNode and previousNode
private  void doKeyPressEvent(java.awt.event.KeyEvent e)
           
private  void doKeyReleaseEvent(java.awt.event.KeyEvent e)
           
 void enterMode()
           
 void eventDispatched(java.awt.AWTEvent event)
          redraw to (possibly) get rid of helper line if selection changes.
 void exitMode()
           
private  Node findNodeToContinueFrom(Node selectedNode, Way selectedWay)
          Finds a node to continue drawing from.
private  void finishDrawing()
          This function should be called when the user wishes to finish his current draw action.
 Node getCurrentBaseNode()
           
 java.util.Collection<OsmPrimitive> getInProgressSelection()
          Get selected primitives, while draw action is in progress.
 java.lang.String getModeHelpText()
           
static Way getWayForNode(Node n)
           
private  void insertNodeIntoAllNearbySegments(java.util.List<WaySegment> wss, Node n, java.util.Collection<OsmPrimitive> newSelection, java.util.Collection<Command> cmds, java.util.ArrayList<Way> replacedWays, java.util.ArrayList<Way> reuseWays)
           
private  boolean isSelfContainedWay(Way selectedWay, Node currentNode, Node targetNode)
          Prevent creation of ways that look like this: <----> This happens if users want to draw a no-exit-sideway from the main way like this: ^ |<----> | The solution isn't ideal because the main way will end in the side way, which is bad for navigation software ("drive straight on") but at least easier to fix.
 boolean layerIsSupported(Layer l)
           
 void mouseDragged(java.awt.event.MouseEvent e)
           
 void mouseExited(java.awt.event.MouseEvent e)
          Repaint on mouse exit so that the helper line goes away.
 void mouseMoved(java.awt.event.MouseEvent e)
           
 void mousePressed(java.awt.event.MouseEvent e)
           
 void mouseReleased(java.awt.event.MouseEvent e)
          If user clicked with the left button, add a node at the current mouse position.
 void paint(java.awt.Graphics2D g, MapView mv, Bounds box)
          Paint the dataset using the engine set.
(package private)  void processKeyEvent(java.awt.event.KeyEvent e)
           
private static void pruneSuccsAndReverse(java.util.List<java.lang.Integer> is)
           
private  boolean redrawIfRequired()
          Checks if a map redraw is required and does so if needed.
private  boolean removeHighlighting()
          Removes target highlighting from primitives.
 void selectionChanged(java.util.Collection<? extends OsmPrimitive> newSelection)
          redraw to (possibly) get rid of helper line if selection changes.
private  void showStatusInfo(double angle, double hdg, double distance, boolean activeFlag)
           
private  void tryAgain(java.awt.event.MouseEvent e)
           
private  void tryToMoveNodeOnIntersection(java.util.List<WaySegment> wss, Node n)
           
protected  void updateEnabledState()
          Override in subclasses to update the enabled state of the action when something in the JOSM state changes, i.e.
 
Methods inherited from class org.openstreetmap.josm.actions.mapmode.MapMode
actionPerformed, mouseClicked, mouseEntered, updateKeyModifiers, updateKeyModifiers, updateKeyModifiers, updateStatusLine
 
Methods inherited from class org.openstreetmap.josm.actions.JosmAction
getCurrentDataSet, getEditLayer, getShortcut, initEnabledState, installAdapters, setTooltip, updateEnabledState
 
Methods inherited from class javax.swing.AbstractAction
addPropertyChangeListener, clone, firePropertyChange, getKeys, getPropertyChangeListeners, getValue, isEnabled, putValue, removePropertyChangeListener, setEnabled
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

cursorJoinNode

private final java.awt.Cursor cursorJoinNode

cursorJoinWay

private final java.awt.Cursor cursorJoinWay

lastUsedNode

private Node lastUsedNode

PHI

private double PHI

mouseOnExistingNode

private Node mouseOnExistingNode

mouseOnExistingWays

private java.util.Set<Way> mouseOnExistingWays

oldHighlights

private java.util.Set<OsmPrimitive> oldHighlights

newHighlights

private java.util.Set<OsmPrimitive> newHighlights

drawHelperLine

private boolean drawHelperLine

wayIsFinished

private boolean wayIsFinished

drawTargetHighlight

private boolean drawTargetHighlight

mousePos

private java.awt.Point mousePos

oldMousePos

private java.awt.Point oldMousePos

selectedColor

private java.awt.Color selectedColor

currentBaseNode

private Node currentBaseNode

previousNode

private Node previousNode

currentMouseEastNorth

private EastNorth currentMouseEastNorth

snapHelper

private final DrawAction.SnapHelper snapHelper

backspaceShortcut

private Shortcut backspaceShortcut

snappingShortcut

private final Shortcut snappingShortcut

snapChangeAction

private final DrawAction.SnapChangeAction snapChangeAction

snapCheckboxMenuItem

private final javax.swing.JCheckBoxMenuItem snapCheckboxMenuItem

useRepeatedShortcut

private boolean useRepeatedShortcut

set

private final java.util.TreeSet<java.lang.Integer> set

releaseEvent

private java.awt.event.KeyEvent releaseEvent

timer

private javax.swing.Timer timer

rightClickPressPos

private java.awt.Point rightClickPressPos
Constructor Detail

DrawAction

public DrawAction(MapFrame mapFrame)
Method Detail

addMenuItem

private javax.swing.JCheckBoxMenuItem addMenuItem()

redrawIfRequired

private boolean redrawIfRequired()
Checks if a map redraw is required and does so if needed. Also updates the status bar


enterMode

public void enterMode()
Overrides:
enterMode in class MapMode

exitMode

public void exitMode()
Overrides:
exitMode in class MapMode

eventDispatched

public void eventDispatched(java.awt.AWTEvent event)
redraw to (possibly) get rid of helper line if selection changes.

Specified by:
eventDispatched in interface java.awt.event.AWTEventListener

processKeyEvent

void processKeyEvent(java.awt.event.KeyEvent e)

doKeyPressEvent

private void doKeyPressEvent(java.awt.event.KeyEvent e)

doKeyReleaseEvent

private void doKeyReleaseEvent(java.awt.event.KeyEvent e)

selectionChanged

public void selectionChanged(java.util.Collection<? extends OsmPrimitive> newSelection)
redraw to (possibly) get rid of helper line if selection changes.

Specified by:
selectionChanged in interface SelectionChangedListener
Parameters:
newSelection - The new selection.

tryAgain

private void tryAgain(java.awt.event.MouseEvent e)

finishDrawing

private void finishDrawing()
This function should be called when the user wishes to finish his current draw action. If Potlatch Style is enabled, it will switch to select tool, otherwise simply disable the helper line until the user chooses to draw something else.


mousePressed

public void mousePressed(java.awt.event.MouseEvent e)
Specified by:
mousePressed in interface java.awt.event.MouseListener
Overrides:
mousePressed in class MapMode

mouseReleased

public void mouseReleased(java.awt.event.MouseEvent e)
If user clicked with the left button, add a node at the current mouse position. If in nodeway mode, insert the node into the way.

Specified by:
mouseReleased in interface java.awt.event.MouseListener
Overrides:
mouseReleased in class MapMode

insertNodeIntoAllNearbySegments

private void insertNodeIntoAllNearbySegments(java.util.List<WaySegment> wss,
                                             Node n,
                                             java.util.Collection<OsmPrimitive> newSelection,
                                             java.util.Collection<Command> cmds,
                                             java.util.ArrayList<Way> replacedWays,
                                             java.util.ArrayList<Way> reuseWays)

isSelfContainedWay

private boolean isSelfContainedWay(Way selectedWay,
                                   Node currentNode,
                                   Node targetNode)
Prevent creation of ways that look like this: <----> This happens if users want to draw a no-exit-sideway from the main way like this: ^ |<----> | The solution isn't ideal because the main way will end in the side way, which is bad for navigation software ("drive straight on") but at least easier to fix. Maybe users will fix it on their own, too. At least it's better than producing an error.

Parameters:
Way - the way to check
Node - the current node (i.e. the one the connection will be made from)
Node - the target node (i.e. the one the connection will be made to)
Returns:
Boolean True if this would create a selfcontaining way, false otherwise.

findNodeToContinueFrom

private Node findNodeToContinueFrom(Node selectedNode,
                                    Way selectedWay)
Finds a node to continue drawing from. Decision is based upon given node and way.

Parameters:
selectedNode - Currently selected node, may be null
selectedWay - Currently selected way, may be null
Returns:
Node if a suitable node is found, null otherwise

mouseDragged

public void mouseDragged(java.awt.event.MouseEvent e)
Specified by:
mouseDragged in interface java.awt.event.MouseMotionListener
Overrides:
mouseDragged in class MapMode

mouseMoved

public void mouseMoved(java.awt.event.MouseEvent e)
Specified by:
mouseMoved in interface java.awt.event.MouseMotionListener
Overrides:
mouseMoved in class MapMode

computeHelperLine

private void computeHelperLine()
This method prepares data required for painting the "helper line" from the last used position to the mouse cursor. It duplicates some code from mouseReleased() (FIXME).


showStatusInfo

private void showStatusInfo(double angle,
                            double hdg,
                            double distance,
                            boolean activeFlag)

determineCurrentBaseNodeAndPreviousNode

private void determineCurrentBaseNodeAndPreviousNode(java.util.Collection<OsmPrimitive> selection)
Helper function that sets fields currentBaseNode and previousNode

Parameters:
selection - uses also lastUsedNode field

mouseExited

public void mouseExited(java.awt.event.MouseEvent e)
Repaint on mouse exit so that the helper line goes away.

Specified by:
mouseExited in interface java.awt.event.MouseListener
Overrides:
mouseExited in class MapMode

getWayForNode

public static Way getWayForNode(Node n)
Returns:
If the node is the end of exactly one way, return this. null otherwise.

getCurrentBaseNode

public Node getCurrentBaseNode()

pruneSuccsAndReverse

private static void pruneSuccsAndReverse(java.util.List<java.lang.Integer> is)

adjustNode

private static void adjustNode(java.util.Collection<Pair<Node,Node>> segs,
                               Node n)
Adjusts the position of a node to lie on a segment (or a segment intersection). If one or more than two segments are passed, the node is adjusted to lie on the first segment that is passed. If two segments are passed, the node is adjusted to be at their intersection. No action is taken if no segments are passed.

Parameters:
segs - the segments to use as a reference when adjusting
n - the node to adjust

det

static double det(double a,
                  double b,
                  double c,
                  double d)

tryToMoveNodeOnIntersection

private void tryToMoveNodeOnIntersection(java.util.List<WaySegment> wss,
                                         Node n)

addHighlighting

private void addHighlighting()
Takes the data from computeHelperLine to determine which ways/nodes should be highlighted (if feature enabled). Also sets the target cursor if appropriate. It adds the to-be- highlighted primitives to newHighlights but does not actually highlight them. This work is done in redrawIfRequired. This means, calling addHighlighting() without redrawIfRequired() will leave the data in an inconsistent state. The status bar derives its information from oldHighlights, so in order to update the status bar both addHighlighting() and repaintIfRequired() are needed, since former fills newHighlights and latter processes them into oldHighlights.


removeHighlighting

private boolean removeHighlighting()
Removes target highlighting from primitives. Issues repaint if required. Returns true if a repaint has been issued.


paint

public void paint(java.awt.Graphics2D g,
                  MapView mv,
                  Bounds box)
Description copied from interface: MapViewPaintable
Paint the dataset using the engine set.

Specified by:
paint in interface MapViewPaintable
mv - The object that can translate GeoPoints to screen coordinates.

getModeHelpText

public java.lang.String getModeHelpText()
Overrides:
getModeHelpText in class MapMode

getInProgressSelection

public java.util.Collection<OsmPrimitive> getInProgressSelection()
Get selected primitives, while draw action is in progress. While drawing a way, technically the last node is selected. This is inconvenient when the user tries to add tags to the way using a keyboard shortcut. In that case, this method returns the current way as selection, to work around this issue. Otherwise the normal selection of the current data layer is returned.


layerIsSupported

public boolean layerIsSupported(Layer l)
Overrides:
layerIsSupported in class MapMode

updateEnabledState

protected void updateEnabledState()
Description copied from class: JosmAction
Override in subclasses to update the enabled state of the action when something in the JOSM state changes, i.e. when a layer is removed or added. See JosmAction.updateEnabledState(Collection) to respond to changes in the collection of selected primitives. Default behavior is empty.

Overrides:
updateEnabledState in class JosmAction
See Also:
JosmAction.updateEnabledState(Collection), JosmAction.initEnabledState()

destroy

public void destroy()
Description copied from interface: Destroyable
Called when the object has been destroyed.

Specified by:
destroy in interface Destroyable
Overrides:
destroy in class JosmAction


JOSM