| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.actions.mapmode;
|
|---|
| 3 |
|
|---|
| 4 | import java.awt.Cursor;
|
|---|
| 5 | import java.awt.event.ActionEvent;
|
|---|
| 6 | import java.awt.event.InputEvent;
|
|---|
| 7 | import java.awt.event.MouseEvent;
|
|---|
| 8 | import java.awt.event.MouseListener;
|
|---|
| 9 | import java.awt.event.MouseMotionListener;
|
|---|
| 10 |
|
|---|
| 11 | import org.openstreetmap.josm.Main;
|
|---|
| 12 | import org.openstreetmap.josm.actions.JosmAction;
|
|---|
| 13 | import org.openstreetmap.josm.gui.MapFrame;
|
|---|
| 14 | import org.openstreetmap.josm.gui.layer.Layer;
|
|---|
| 15 | import org.openstreetmap.josm.tools.ImageProvider;
|
|---|
| 16 | import org.openstreetmap.josm.tools.Shortcut;
|
|---|
| 17 | import org.openstreetmap.josm.data.Preferences.PreferenceChangeEvent;
|
|---|
| 18 | import org.openstreetmap.josm.data.Preferences.PreferenceChangedListener;
|
|---|
| 19 |
|
|---|
| 20 | /**
|
|---|
| 21 | * A class implementing MapMode is able to be selected as an mode for map editing.
|
|---|
| 22 | * As example scrolling the map is a MapMode, connecting Nodes to new Ways is another.
|
|---|
| 23 | *
|
|---|
| 24 | * MapModes should register/deregister all necessary listeners on the map's view control.
|
|---|
| 25 | */
|
|---|
| 26 | public abstract class MapMode extends JosmAction implements MouseListener, MouseMotionListener, PreferenceChangedListener {
|
|---|
| 27 | protected final Cursor cursor;
|
|---|
| 28 | protected boolean ctrl;
|
|---|
| 29 | protected boolean alt;
|
|---|
| 30 | protected boolean shift;
|
|---|
| 31 |
|
|---|
| 32 | /**
|
|---|
| 33 | * Constructor for mapmodes without a menu
|
|---|
| 34 | * @param name the action's text
|
|---|
| 35 | * @param iconName icon filename in {@code mapmode} directory
|
|---|
| 36 | * @param tooltip a longer description of the action that will be displayed in the tooltip.
|
|---|
| 37 | * @param shortcut a ready-created shortcut object or null if you don't want a shortcut.
|
|---|
| 38 | * @param mapFrame unused but kept for plugin compatibility. Can be {@code null}
|
|---|
| 39 | * @param cursor cursor displayed when map mode is active
|
|---|
| 40 | */
|
|---|
| 41 | public MapMode(String name, String iconName, String tooltip, Shortcut shortcut, MapFrame mapFrame, Cursor cursor) {
|
|---|
| 42 | super(name, "mapmode/"+iconName, tooltip, shortcut, false);
|
|---|
| 43 | this.cursor = cursor;
|
|---|
| 44 | putValue("active", Boolean.FALSE);
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | /**
|
|---|
| 48 | * Constructor for mapmodes with a menu (no shortcut will be registered)
|
|---|
| 49 | * @param name the action's text
|
|---|
| 50 | * @param iconName icon filename in {@code mapmode} directory
|
|---|
| 51 | * @param tooltip a longer description of the action that will be displayed in the tooltip.
|
|---|
| 52 | * @param mapFrame unused but kept for plugin compatibility. Can be {@code null}
|
|---|
| 53 | * @param cursor cursor displayed when map mode is active
|
|---|
| 54 | */
|
|---|
| 55 | public MapMode(String name, String iconName, String tooltip, MapFrame mapFrame, Cursor cursor) {
|
|---|
| 56 | putValue(NAME, name);
|
|---|
| 57 | new ImageProvider("mapmode", iconName).getResource().attachImageIcon(this);
|
|---|
| 58 | putValue(SHORT_DESCRIPTION, tooltip);
|
|---|
| 59 | this.cursor = cursor;
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | /**
|
|---|
| 63 | * Makes this map mode active.
|
|---|
| 64 | */
|
|---|
| 65 | public void enterMode() {
|
|---|
| 66 | putValue("active", Boolean.TRUE);
|
|---|
| 67 | Main.pref.addPreferenceChangeListener(this);
|
|---|
| 68 | readPreferences();
|
|---|
| 69 | Main.map.mapView.setNewCursor(cursor, this);
|
|---|
| 70 | updateStatusLine();
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | /**
|
|---|
| 74 | * Makes this map mode inactive.
|
|---|
| 75 | */
|
|---|
| 76 | public void exitMode() {
|
|---|
| 77 | putValue("active", Boolean.FALSE);
|
|---|
| 78 | Main.pref.removePreferenceChangeListener(this);
|
|---|
| 79 | Main.map.mapView.resetCursor(this);
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | protected void updateStatusLine() {
|
|---|
| 83 | Main.map.statusLine.setHelpText(getModeHelpText());
|
|---|
| 84 | Main.map.statusLine.repaint();
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | public String getModeHelpText() {
|
|---|
| 88 | return "";
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | protected void readPreferences() {}
|
|---|
| 92 |
|
|---|
| 93 | /**
|
|---|
| 94 | * Call selectMapMode(this) on the parent mapFrame.
|
|---|
| 95 | */
|
|---|
| 96 | @Override
|
|---|
| 97 | public void actionPerformed(ActionEvent e) {
|
|---|
| 98 | if (Main.isDisplayingMapView()) {
|
|---|
| 99 | Main.map.selectMapMode(this);
|
|---|
| 100 | }
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | /**
|
|---|
| 104 | * Determines if layer {@code l} is supported by this map mode.
|
|---|
| 105 | * By default, all tools will work with all layers.
|
|---|
| 106 | * Can be overwritten to require a special type of layer
|
|---|
| 107 | * @param l layer
|
|---|
| 108 | * @return {@code true} if the layer is supported by this map mode
|
|---|
| 109 | */
|
|---|
| 110 | public boolean layerIsSupported(Layer l) {
|
|---|
| 111 | return l != null;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | protected void updateKeyModifiers(InputEvent e) {
|
|---|
| 115 | updateKeyModifiers(e.getModifiers());
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | protected void updateKeyModifiers(MouseEvent e) {
|
|---|
| 119 | updateKeyModifiers(e.getModifiers());
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | protected void updateKeyModifiers(int modifiers) {
|
|---|
| 123 | ctrl = (modifiers & ActionEvent.CTRL_MASK) != 0;
|
|---|
| 124 | alt = (modifiers & (ActionEvent.ALT_MASK | InputEvent.ALT_GRAPH_MASK)) != 0;
|
|---|
| 125 | shift = (modifiers & ActionEvent.SHIFT_MASK) != 0;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | protected void requestFocusInMapView() {
|
|---|
| 129 | if (isEnabled()) {
|
|---|
| 130 | // request focus in order to enable the expected keyboard shortcuts (see #8710)
|
|---|
| 131 | Main.map.mapView.requestFocus();
|
|---|
| 132 | }
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | @Override
|
|---|
| 136 | public void mouseReleased(MouseEvent e) {
|
|---|
| 137 | requestFocusInMapView();
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | @Override
|
|---|
| 141 | public void mouseExited(MouseEvent e) {
|
|---|
| 142 | // Do nothing
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | @Override
|
|---|
| 146 | public void mousePressed(MouseEvent e) {
|
|---|
| 147 | requestFocusInMapView();
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | @Override
|
|---|
| 151 | public void mouseClicked(MouseEvent e) {
|
|---|
| 152 | // Do nothing
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | @Override
|
|---|
| 156 | public void mouseEntered(MouseEvent e) {
|
|---|
| 157 | // Do nothing
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | @Override
|
|---|
| 161 | public void mouseMoved(MouseEvent e) {
|
|---|
| 162 | // Do nothing
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | @Override
|
|---|
| 166 | public void mouseDragged(MouseEvent e) {
|
|---|
| 167 | // Do nothing
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | @Override
|
|---|
| 171 | public void preferenceChanged(PreferenceChangeEvent e) {
|
|---|
| 172 | readPreferences();
|
|---|
| 173 | }
|
|---|
| 174 | }
|
|---|