Changeset 31104 in osm


Ignore:
Timestamp:
2015-04-27T11:14:22+02:00 (10 years ago)
Author:
upliner
Message:

BuildingsTools: use MapMode modifier handling and fix addresses under building bug

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/buildings_tools/src/buildings_tools/DrawBuildingAction.java

    r31095 r31104  
    66import static org.openstreetmap.josm.tools.I18n.tr;
    77
    8 import java.awt.AWTEvent;
    98import java.awt.BasicStroke;
    109import java.awt.Color;
     
    1514import java.awt.RenderingHints;
    1615import java.awt.Toolkit;
    17 import java.awt.event.AWTEventListener;
    1816import java.awt.event.KeyEvent;
    1917import java.awt.event.MouseEvent;
     
    2220import java.util.Collection;
    2321import java.util.LinkedList;
     22import java.util.Map;
     23import java.util.Map.Entry;
    2424
    2525import org.openstreetmap.josm.Main;
     
    3737import org.openstreetmap.josm.gui.layer.MapViewPaintable;
    3838import org.openstreetmap.josm.gui.layer.OsmDataLayer;
     39import org.openstreetmap.josm.gui.util.KeyPressReleaseListener;
     40import org.openstreetmap.josm.gui.util.ModifierListener;
    3941import org.openstreetmap.josm.tools.ImageProvider;
    4042import org.openstreetmap.josm.tools.Shortcut;
    4143
    4244@SuppressWarnings("serial")
    43 public class DrawBuildingAction extends MapMode implements MapViewPaintable, AWTEventListener, SelectionChangedListener {
     45public class DrawBuildingAction extends MapMode implements MapViewPaintable, SelectionChangedListener,
     46        KeyPressReleaseListener, ModifierListener {
    4447    private enum Mode {
    4548        None, Drawing, DrawingWidth, DrawingAngFix
     
    5457    private Mode nextMode = Mode.None;
    5558
    56     private final Color selectedColor;
     59    private Color selectedColor = Color.red;
    5760    private Point drawStartPos;
    5861    private Point mousePos;
    59     private boolean isCtrlDown;
    60     private boolean isShiftDown;
    61     private boolean isAltDown;
    6262
    6363    final Building building = new Building();
     
    7373        cursorJoinNode = ImageProvider.getCursor("crosshair", "joinnode");
    7474        currCursor = cursorCrosshair;
    75 
    76         selectedColor = Main.pref.getColor(marktr("selected"), Color.red);
    7775    }
    7876
     
    113111    private void showAddrDialog(Way w) {
    114112        AddressDialog dlg = new AddressDialog();
    115         if (!isAltDown) {
     113        if (!alt) {
    116114            dlg.showDialog();
    117115            if (dlg.getValue() != 1)
     
    134132            return;
    135133        }
     134        selectedColor = Main.pref.getColor(marktr("selected"), selectedColor);
    136135        currCursor = cursorCrosshair;
    137136        Main.map.mapView.addMouseListener(this);
    138137        Main.map.mapView.addMouseMotionListener(this);
    139138        Main.map.mapView.addTemporaryLayer(this);
     139        Main.map.keyDetector.addKeyListener(this);
     140        Main.map.keyDetector.addModifierListener(this);
    140141        DataSet.addSelectionListener(this);
    141142        updateSnap(getCurrentDataSet().getSelected());
    142         try {
    143             Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.KEY_EVENT_MASK);
    144         } catch (SecurityException ex) {
    145             Main.error(ex);
    146         }
    147143    }
    148144
     
    153149        Main.map.mapView.removeMouseMotionListener(this);
    154150        Main.map.mapView.removeTemporaryLayer(this);
     151        Main.map.keyDetector.removeKeyListener(this);
     152        Main.map.keyDetector.removeModifierListener(this);
    155153        DataSet.removeSelectionListener(this);
    156         try {
    157             Toolkit.getDefaultToolkit().removeAWTEventListener(this);
    158         } catch (SecurityException ex) {
    159             Main.error(ex);
    160         }
    161154        if (mode != Mode.None)
    162155            Main.map.mapView.repaint();
     
    176169
    177170    @Override
    178     public void eventDispatched(AWTEvent arg0) {
    179         if (!(arg0 instanceof KeyEvent))
    180             return;
    181         KeyEvent ev = (KeyEvent) arg0;
    182         int modifiers = ev.getModifiersEx();
    183         boolean isCtrlDown = (modifiers & KeyEvent.CTRL_DOWN_MASK) != 0;
    184         boolean isShiftDown = (modifiers & KeyEvent.SHIFT_DOWN_MASK) != 0;
    185         if (this.isCtrlDown != isCtrlDown || this.isShiftDown != isShiftDown) {
    186             this.isCtrlDown = isCtrlDown;
    187             this.isShiftDown = isShiftDown;
     171    public void modifiersChanged(int modifiers) {
     172        boolean oldCtrl = ctrl;
     173        boolean oldShift = shift;
     174        updateKeyModifiers(modifiers);
     175        if (ctrl != oldCtrl || shift != oldShift) {
    188176            processMouseEvent(null);
    189177            updCursor();
     
    191179                Main.map.mapView.repaint();
    192180        }
    193         isAltDown = (modifiers & KeyEvent.ALT_DOWN_MASK) != 0;
    194 
    195         if (ev.getKeyCode() == KeyEvent.VK_ESCAPE && ev.getID() == KeyEvent.KEY_PRESSED) {
    196             if (mode != Mode.None) {
    197                 ev.consume();
    198             }
     181    }
     182
     183    @Override
     184    public void doKeyPressed(KeyEvent e) {
     185        if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
     186            if (mode != Mode.None)
     187                e.consume();
    199188
    200189            cancelDrawing();
    201190        }
     191    }
     192
     193    @Override
     194    public void doKeyReleased(KeyEvent e) {
    202195    }
    203196
    204197    private EastNorth getEastNorth() {
    205198        Node n;
    206         if (isCtrlDown) {
     199        if (ctrl) {
    207200            n = null;
    208201        } else {
     
    217210
    218211    private boolean isRectDrawing() {
    219         return building.isRectDrawing() && (!isShiftDown || ToolSettings.isBBMode());
     212        return building.isRectDrawing() && (!shift || ToolSettings.isBBMode());
    220213    }
    221214
     
    224217        if (isRectDrawing()) {
    225218            building.setPlaceRect(p);
    226             return isShiftDown ? Mode.DrawingAngFix : Mode.None;
    227         } else {
    228             building.setPlace(p, ToolSettings.getWidth(), ToolSettings.getLenStep(), isShiftDown);
     219            return shift ? Mode.DrawingAngFix : Mode.None;
     220        } else {
     221            building.setPlace(p, ToolSettings.getWidth(), ToolSettings.getLenStep(), shift);
    229222            Main.map.statusLine.setDist(building.getLength());
    230223            this.nextMode = ToolSettings.getWidth() == 0 ? Mode.DrawingWidth : Mode.None;
     
    247240        if (e != null) {
    248241            mousePos = e.getPoint();
    249             isCtrlDown = e.isControlDown();
    250             isShiftDown = e.isShiftDown();
    251             isAltDown = e.isAltDown();
     242            updateKeyModifiers(e);
    252243        }
    253244        if (mode == Mode.None) {
     
    310301            Way w = building.create();
    311302            if (w != null) {
    312                 if (!isAltDown || ToolSettings.isUsingAddr())
    313                     w.setKeys(ToolSettings.getTags());
     303                if (!alt || ToolSettings.isUsingAddr())
     304                    for (Entry<String, String> kv : ToolSettings.getTags().entrySet())
     305                        w.put(kv.getKey(), kv.getValue());
    314306                if (ToolSettings.isUsingAddr())
    315307                    showAddrDialog(w);
    316308                if (ToolSettings.isAutoSelect()
    317                         && (Main.main.getCurrentDataSet().getSelected().isEmpty() || isShiftDown)) {
     309                        && (Main.main.getCurrentDataSet().getSelected().isEmpty() || shift)) {
    318310                    Main.main.getCurrentDataSet().setSelected(w);
    319311                }
     
    369361            return;
    370362        Node n = null;
    371         if (!isCtrlDown)
     363        if (!ctrl)
    372364            n = Main.map.mapView.getNearestNode(mousePos, OsmPrimitive.isUsablePredicate);
    373365        if (n != null) {
    374366            setCursor(cursorJoinNode);
    375367        } else {
    376             if (customCursor != null && (!isShiftDown || isRectDrawing()))
     368            if (customCursor != null && (!ctrl || isRectDrawing()))
    377369                setCursor(customCursor);
    378370            else
Note: See TracChangeset for help on using the changeset viewer.