Changeset 33026 in osm


Ignore:
Timestamp:
2016-10-04T00:28:32+02:00 (8 years ago)
Author:
donvip
Message:

refactor duplicated code

Location:
applications/editors/josm/plugins/CommandLine/src/CommandLine
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/CommandLine/src/CommandLine/AnyAction.java

    r32779 r33026  
    22package CommandLine;
    33
    4 import java.awt.AWTEvent;
    5 import java.awt.Cursor;
    6 import java.awt.EventQueue;
    74import java.awt.Point;
    8 import java.awt.Toolkit;
    9 import java.awt.event.AWTEventListener;
    10 import java.awt.event.KeyEvent;
    11 import java.awt.event.MouseEvent;
    125
    136import org.openstreetmap.josm.Main;
    14 import org.openstreetmap.josm.actions.mapmode.MapMode;
    15 import org.openstreetmap.josm.data.osm.DataSet;
    167import org.openstreetmap.josm.data.osm.OsmPrimitive;
    178import org.openstreetmap.josm.gui.MapFrame;
    18 import org.openstreetmap.josm.tools.ImageProvider;
    199
    20 public class AnyAction extends MapMode implements AWTEventListener {
    21     private final CommandLine parentPlugin;
    22     private final Cursor cursorNormal, cursorActive;
    23     private Cursor currentCursor;
    24     private Point mousePos;
    25     private OsmPrimitive nearestPrimitive;
    26     private boolean isCtrlDown;
     10public class AnyAction extends AbstractOsmAction<OsmPrimitive> {
    2711
    2812    public AnyAction(MapFrame mapFrame, CommandLine parentPlugin) {
    29         super(null, "addsegment.png", null, mapFrame, ImageProvider.getCursor("normal", "selection"));
    30         this.parentPlugin = parentPlugin;
    31         cursorNormal = ImageProvider.getCursor("normal", "selection");
    32         cursorActive = ImageProvider.getCursor("normal", "joinnode");
    33         currentCursor = cursorNormal;
    34         nearestPrimitive = null;
    35     }
    36 
    37     @Override public void enterMode() {
    38         super.enterMode();
    39         currentCursor = cursorNormal;
    40         Main.map.mapView.addMouseListener(this);
    41         Main.map.mapView.addMouseMotionListener(this);
    42         try {
    43             Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.KEY_EVENT_MASK);
    44         } catch (SecurityException ex) {
    45             Main.warn(ex);
    46         }
    47     }
    48 
    49     @Override public void exitMode() {
    50         super.exitMode();
    51         Main.map.mapView.removeMouseListener(this);
    52         Main.map.mapView.removeMouseMotionListener(this);
    53         try {
    54             Toolkit.getDefaultToolkit().removeAWTEventListener(this);
    55         } catch (SecurityException ex) {
    56             Main.warn(ex);
    57         }
     13        super(mapFrame, parentPlugin, "joinnode");
    5814    }
    5915
    6016    @Override
    61     public void mouseMoved(MouseEvent e) {
    62         if (!Main.map.mapView.isActiveLayerDrawable())
    63             return;
    64         processMouseEvent(e);
    65         updCursor();
    66         Main.map.mapView.repaint();
    67         super.mouseMoved(e);
    68     }
    69 
    70     @Override
    71     public void mousePressed(MouseEvent e) {
    72         if (!Main.map.mapView.isActiveLayerDrawable())
    73             return;
    74         processMouseEvent(e);
    75         if (nearestPrimitive != null) {
    76             DataSet ds = Main.getLayerManager().getEditDataSet();
    77             if (isCtrlDown) {
    78                 ds.clearSelection(nearestPrimitive);
    79                 Main.map.mapView.repaint();
    80             } else {
    81                 int maxInstances = parentPlugin.currentCommand.parameters.get(parentPlugin.currentCommand.currentParameterNum).maxInstances;
    82                 switch (maxInstances) {
    83                 case 0:
    84                     ds.addSelected(nearestPrimitive);
    85                     Main.map.mapView.repaint();
    86                     break;
    87                 case 1:
    88                     ds.addSelected(nearestPrimitive);
    89                     Main.map.mapView.repaint();
    90                     parentPlugin.loadParameter(nearestPrimitive, true);
    91                     exitMode();
    92                     break;
    93                 default:
    94                     if (ds.getSelected().size() < maxInstances) {
    95                         ds.addSelected(nearestPrimitive);
    96                         Main.map.mapView.repaint();
    97                     } else
    98                         Main.info("Maximum instances!");
    99                 }
    100             }
    101         }
    102         super.mousePressed(e);
    103     }
    104 
    105     @Override
    106     public void eventDispatched(AWTEvent arg0) {
    107         if (!(arg0 instanceof KeyEvent))
    108             return;
    109         KeyEvent ev = (KeyEvent) arg0;
    110         isCtrlDown = (ev.getModifiersEx() & KeyEvent.CTRL_DOWN_MASK) != 0;
    111         if (ev.getKeyCode() == KeyEvent.VK_ESCAPE && ev.getID() == KeyEvent.KEY_PRESSED) {
    112             ev.consume();
    113             cancelDrawing();
    114         }
    115     }
    116 
    117     private void updCursor() {
    118         if (mousePos != null) {
    119             if (!Main.isDisplayingMapView())
    120                 return;
    121             nearestPrimitive = Main.map.mapView.getNearestNodeOrWay(mousePos, OsmPrimitive::isUsable, false);
    122             if (nearestPrimitive != null) {
    123                 setCursor(cursorActive);
    124             } else {
    125                 setCursor(cursorNormal);
    126             }
    127         }
    128     }
    129 
    130     private void processMouseEvent(MouseEvent e) {
    131         if (e != null) {
    132             mousePos = e.getPoint();
    133         }
    134     }
    135 
    136     private void setCursor(final Cursor c) {
    137         if (currentCursor.equals(c))
    138             return;
    139         try {
    140             // We invoke this to prevent strange things from happening
    141             EventQueue.invokeLater(new Runnable() {
    142                 @Override
    143                 public void run() {
    144                     // Don't change cursor when mode has changed already
    145                     if (!(Main.map.mapMode instanceof AnyAction))
    146                         return;
    147                     Main.map.mapView.setCursor(c);
    148                 }
    149             });
    150             currentCursor = c;
    151         } catch (Exception e) {
    152             Main.warn(e);
    153         }
    154     }
    155 
    156     public void cancelDrawing() {
    157         if (Main.map == null || Main.map.mapView == null)
    158             return;
    159         Main.map.statusLine.setHeading(-1);
    160         Main.map.statusLine.setAngle(-1);
    161         Main.map.mapView.repaint();
    162         updateStatusLine();
    163         parentPlugin.abortInput();
     17    protected OsmPrimitive getNearest(Point mousePos) {
     18        return Main.map.mapView.getNearestNodeOrWay(mousePos, OsmPrimitive::isUsable, false);
    16419    }
    16520}
  • applications/editors/josm/plugins/CommandLine/src/CommandLine/NodeAction.java

    r32779 r33026  
    22package CommandLine;
    33
    4 import java.awt.AWTEvent;
    5 import java.awt.Cursor;
    6 import java.awt.EventQueue;
    74import java.awt.Point;
    8 import java.awt.Toolkit;
    9 import java.awt.event.AWTEventListener;
    10 import java.awt.event.KeyEvent;
    11 import java.awt.event.MouseEvent;
    125
    136import org.openstreetmap.josm.Main;
    14 import org.openstreetmap.josm.actions.mapmode.MapMode;
    15 import org.openstreetmap.josm.data.osm.DataSet;
    167import org.openstreetmap.josm.data.osm.Node;
    178import org.openstreetmap.josm.data.osm.OsmPrimitive;
    189import org.openstreetmap.josm.gui.MapFrame;
    19 import org.openstreetmap.josm.tools.ImageProvider;
    2010
    21 public class NodeAction extends MapMode implements AWTEventListener {
    22     private final CommandLine parentPlugin;
    23     private final Cursor cursorNormal, cursorActive;
    24     private Cursor currentCursor;
    25     private Point mousePos;
    26     private Node nearestNode;
    27     private boolean isCtrlDown;
    28     // private Type type;
     11public class NodeAction extends AbstractOsmAction<Node> {
    2912
    3013    public NodeAction(MapFrame mapFrame, CommandLine parentPlugin) {
    31         super(null, "addsegment.png", null, mapFrame, ImageProvider.getCursor("normal", "selection"));
    32         this.parentPlugin = parentPlugin;
    33         cursorNormal = ImageProvider.getCursor("normal", "selection");
    34         cursorActive = ImageProvider.getCursor("normal", "joinnode");
    35         currentCursor = cursorNormal;
    36         nearestNode = null;
    37     }
    38 
    39     @Override public void enterMode() {
    40         super.enterMode();
    41         currentCursor = cursorNormal;
    42         Main.map.mapView.addMouseListener(this);
    43         Main.map.mapView.addMouseMotionListener(this);
    44         try {
    45             Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.KEY_EVENT_MASK);
    46         } catch (SecurityException ex) {
    47             Main.warn(ex);
    48         }
    49     }
    50 
    51     @Override public void exitMode() {
    52         super.exitMode();
    53         Main.map.mapView.removeMouseListener(this);
    54         Main.map.mapView.removeMouseMotionListener(this);
    55         try {
    56             Toolkit.getDefaultToolkit().removeAWTEventListener(this);
    57         } catch (SecurityException ex) {
    58             Main.warn(ex);
    59         }
     14        super(mapFrame, parentPlugin, "joinnode");
    6015    }
    6116
    6217    @Override
    63     public void mouseMoved(MouseEvent e) {
    64         if (!Main.map.mapView.isActiveLayerDrawable())
    65             return;
    66         processMouseEvent(e);
    67         updCursor();
    68         Main.map.mapView.repaint();
    69         super.mouseMoved(e);
    70     }
    71 
    72     @Override
    73     public void mousePressed(MouseEvent e) {
    74         if (!Main.map.mapView.isActiveLayerDrawable())
    75             return;
    76         processMouseEvent(e);
    77         if (nearestNode != null) {
    78             DataSet ds = Main.getLayerManager().getEditDataSet();
    79             if (isCtrlDown) {
    80                 ds.clearSelection(nearestNode);
    81                 Main.map.mapView.repaint();
    82             } else {
    83                 int maxInstances = parentPlugin.currentCommand.parameters.get(parentPlugin.currentCommand.currentParameterNum).maxInstances;
    84                 switch (maxInstances) {
    85                 case 0:
    86                     ds.addSelected(nearestNode);
    87                     Main.map.mapView.repaint();
    88                     break;
    89                 case 1:
    90                     ds.addSelected(nearestNode);
    91                     Main.map.mapView.repaint();
    92                     parentPlugin.loadParameter(nearestNode, true);
    93                     exitMode();
    94                     break;
    95                 default:
    96                     if (ds.getSelected().size() < maxInstances) {
    97                         ds.addSelected(nearestNode);
    98                         Main.map.mapView.repaint();
    99                     } else
    100                         parentPlugin.printHistory("Maximum instances is " + String.valueOf(maxInstances));
    101                 }
    102             }
    103         }
    104         super.mousePressed(e);
    105     }
    106 
    107     @Override
    108     public void eventDispatched(AWTEvent arg0) {
    109         if (!(arg0 instanceof KeyEvent))
    110             return;
    111         KeyEvent ev = (KeyEvent) arg0;
    112         isCtrlDown = (ev.getModifiersEx() & KeyEvent.CTRL_DOWN_MASK) != 0;
    113         if (ev.getKeyCode() == KeyEvent.VK_ESCAPE && ev.getID() == KeyEvent.KEY_PRESSED) {
    114             ev.consume();
    115             cancelDrawing();
    116         }
    117     }
    118 
    119     private void updCursor() {
    120         if (mousePos != null) {
    121             if (!Main.isDisplayingMapView())
    122                 return;
    123             nearestNode = Main.map.mapView.getNearestNode(mousePos, OsmPrimitive::isUsable);
    124             if (nearestNode != null) {
    125                 setCursor(cursorActive);
    126             } else {
    127                 setCursor(cursorNormal);
    128             }
    129         }
    130     }
    131 
    132     private void processMouseEvent(MouseEvent e) {
    133         if (e != null) {
    134             mousePos = e.getPoint();
    135         }
    136     }
    137 
    138     private void setCursor(final Cursor c) {
    139         if (currentCursor.equals(c))
    140             return;
    141         try {
    142             // We invoke this to prevent strange things from happening
    143             EventQueue.invokeLater(new Runnable() {
    144                 @Override
    145                 public void run() {
    146                     // Don't change cursor when mode has changed already
    147                     if (!(Main.map.mapMode instanceof NodeAction))
    148                         return;
    149                     Main.map.mapView.setCursor(c);
    150                 }
    151             });
    152             currentCursor = c;
    153         } catch (Exception e) {
    154             Main.warn(e);
    155         }
    156     }
    157 
    158     public void cancelDrawing() {
    159         if (Main.map == null || Main.map.mapView == null)
    160             return;
    161         Main.map.statusLine.setHeading(-1);
    162         Main.map.statusLine.setAngle(-1);
    163         Main.map.mapView.repaint();
    164         updateStatusLine();
    165         parentPlugin.abortInput();
     18    protected Node getNearest(Point mousePos) {
     19        return Main.map.mapView.getNearestNode(mousePos, OsmPrimitive::isUsable);
    16620    }
    16721}
  • applications/editors/josm/plugins/CommandLine/src/CommandLine/WayAction.java

    r32779 r33026  
    22package CommandLine;
    33
    4 import java.awt.AWTEvent;
    5 import java.awt.Cursor;
    6 import java.awt.EventQueue;
    74import java.awt.Point;
    8 import java.awt.Toolkit;
    9 import java.awt.event.AWTEventListener;
    10 import java.awt.event.KeyEvent;
    11 import java.awt.event.MouseEvent;
    125
    136import org.openstreetmap.josm.Main;
    14 import org.openstreetmap.josm.actions.mapmode.MapMode;
    15 import org.openstreetmap.josm.data.osm.DataSet;
    167import org.openstreetmap.josm.data.osm.OsmPrimitive;
    178import org.openstreetmap.josm.data.osm.Way;
    189import org.openstreetmap.josm.gui.MapFrame;
    19 import org.openstreetmap.josm.tools.ImageProvider;
    2010
    21 public class WayAction extends MapMode implements AWTEventListener {
    22     private final CommandLine parentPlugin;
    23     private final Cursor cursorNormal, cursorActive;
    24     private Cursor currentCursor;
    25     private Point mousePos;
    26     private Way nearestWay;
    27     private boolean isCtrlDown;
    28     // private Type type;
     11public class WayAction extends AbstractOsmAction<Way> {
    2912
    3013    public WayAction(MapFrame mapFrame, CommandLine parentPlugin) {
    31         super(null, "addsegment.png", null, mapFrame, ImageProvider.getCursor("normal", "selection"));
    32         this.parentPlugin = parentPlugin;
    33         /*
    34         this.type = type;
    35         switch (type) {
    36             case POINT:
    37                 cursorNormal = ImageProvider.getCursor("crosshair", null);
    38                 cursorActive = ImageProvider.getCursor("crosshair", "joinnode");
    39                 break;
    40             case NODE:
    41                 cursorNormal = ImageProvider.getCursor("normal", "selection");
    42                 cursorActive = ImageProvider.getCursor("normal", "joinnode");
    43                 break;
    44             case WAY:
    45          */
    46         cursorNormal = ImageProvider.getCursor("normal", "selection");
    47         cursorActive = ImageProvider.getCursor("normal", "joinway");
    48         /*
    49                 break;
    50             default:
    51                 cursorNormal = ImageProvider.getCursor("normal", "selection");
    52                 cursorActive = ImageProvider.getCursor("normal", null);
    53                 break;
    54         }
    55          */
    56         currentCursor = cursorNormal;
    57         nearestWay = null;
    58     }
    59 
    60     @Override public void enterMode() {
    61         super.enterMode();
    62         currentCursor = cursorNormal;
    63         Main.map.mapView.addMouseListener(this);
    64         Main.map.mapView.addMouseMotionListener(this);
    65         try {
    66             Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.KEY_EVENT_MASK);
    67         } catch (SecurityException ex) {
    68             Main.warn(ex);
    69         }
    70     }
    71 
    72     @Override public void exitMode() {
    73         super.exitMode();
    74         Main.map.mapView.removeMouseListener(this);
    75         Main.map.mapView.removeMouseMotionListener(this);
    76         try {
    77             Toolkit.getDefaultToolkit().removeAWTEventListener(this);
    78         } catch (SecurityException ex) {
    79             Main.warn(ex);
    80         }
     14        super(mapFrame, parentPlugin, "joinway");
    8115    }
    8216
    8317    @Override
    84     public void mouseMoved(MouseEvent e) {
    85         if (!Main.map.mapView.isActiveLayerDrawable())
    86             return;
    87         processMouseEvent(e);
    88         updCursor();
    89         Main.map.mapView.repaint();
    90         super.mouseMoved(e);
    91     }
    92 
    93     @Override
    94     public void mousePressed(MouseEvent e) {
    95         if (!Main.map.mapView.isActiveLayerDrawable())
    96             return;
    97         processMouseEvent(e);
    98         if (nearestWay != null) {
    99             DataSet ds = Main.getLayerManager().getEditDataSet();
    100             if (isCtrlDown) {
    101                 ds.clearSelection(nearestWay);
    102                 Main.map.mapView.repaint();
    103             } else {
    104                 int maxInstances = parentPlugin.currentCommand.parameters.get(parentPlugin.currentCommand.currentParameterNum).maxInstances;
    105                 switch (maxInstances) {
    106                 case 0:
    107                     ds.addSelected(nearestWay);
    108                     Main.map.mapView.repaint();
    109                     break;
    110                 case 1:
    111                     ds.addSelected(nearestWay);
    112                     Main.map.mapView.repaint();
    113                     parentPlugin.loadParameter(nearestWay, true);
    114                     exitMode();
    115                     break;
    116                 default:
    117                     if (ds.getSelected().size() < maxInstances) {
    118                         ds.addSelected(nearestWay);
    119                         Main.map.mapView.repaint();
    120                     } else
    121                         Main.info("Maximum instances!");
    122                 }
    123             }
    124         }
    125         super.mousePressed(e);
    126     }
    127 
    128     @Override
    129     public void eventDispatched(AWTEvent arg0) {
    130         if (!(arg0 instanceof KeyEvent))
    131             return;
    132         KeyEvent ev = (KeyEvent) arg0;
    133         isCtrlDown = (ev.getModifiersEx() & KeyEvent.CTRL_DOWN_MASK) != 0;
    134         if (ev.getKeyCode() == KeyEvent.VK_ESCAPE && ev.getID() == KeyEvent.KEY_PRESSED) {
    135             ev.consume();
    136             cancelDrawing();
    137         }
    138     }
    139 
    140     private void updCursor() {
    141         if (mousePos != null) {
    142             if (!Main.isDisplayingMapView())
    143                 return;
    144             nearestWay = Main.map.mapView.getNearestWay(mousePos, OsmPrimitive::isUsable);
    145             if (nearestWay != null) {
    146                 setCursor(cursorActive);
    147             } else {
    148                 setCursor(cursorNormal);
    149             }
    150         }
    151     }
    152 
    153     private void processMouseEvent(MouseEvent e) {
    154         if (e != null) {
    155             mousePos = e.getPoint();
    156         }
    157     }
    158 
    159     private void setCursor(final Cursor c) {
    160         if (currentCursor.equals(c))
    161             return;
    162         try {
    163             // We invoke this to prevent strange things from happening
    164             EventQueue.invokeLater(new Runnable() {
    165                 @Override
    166                 public void run() {
    167                     // Don't change cursor when mode has changed already
    168                     if (!(Main.map.mapMode instanceof WayAction))
    169                         return;
    170                     Main.map.mapView.setCursor(c);
    171                 }
    172             });
    173             currentCursor = c;
    174         } catch (Exception e) {
    175             Main.warn(e);
    176         }
    177     }
    178 
    179     public void cancelDrawing() {
    180         if (Main.map == null || Main.map.mapView == null)
    181             return;
    182         Main.map.statusLine.setHeading(-1);
    183         Main.map.statusLine.setAngle(-1);
    184         Main.map.mapView.repaint();
    185         updateStatusLine();
    186         parentPlugin.abortInput();
     18    protected Way getNearest(Point mousePos) {
     19        return Main.map.mapView.getNearestWay(mousePos, OsmPrimitive::isUsable);
    18720    }
    18821}
Note: See TracChangeset for help on using the changeset viewer.