1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.plugins.turnrestrictions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.event.ActionEvent;
|
---|
7 | import java.awt.event.KeyEvent;
|
---|
8 | import java.util.Collection;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
11 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
12 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
13 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
14 | import org.openstreetmap.josm.plugins.turnrestrictions.editor.TurnRestrictionEditor;
|
---|
15 | import org.openstreetmap.josm.plugins.turnrestrictions.editor.TurnRestrictionEditorManager;
|
---|
16 | import org.openstreetmap.josm.plugins.turnrestrictions.editor.TurnRestrictionSelectionPopupPanel;
|
---|
17 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * This action is triggered by a global shortcut (default is Shift-Ctrl-T on windows).
|
---|
21 | * Depending on the current selection it either launches an editor for a new turn
|
---|
22 | * restriction or a popup component from which one can choose a turn restriction to
|
---|
23 | * edit.
|
---|
24 | *
|
---|
25 | */
|
---|
26 | public class CreateOrEditTurnRestrictionAction extends JosmAction {
|
---|
27 | /** the singleton instance of this action */
|
---|
28 | private static CreateOrEditTurnRestrictionAction instance;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Replies the unique instance of this action
|
---|
32 | */
|
---|
33 | public static CreateOrEditTurnRestrictionAction getInstance() {
|
---|
34 | if (instance == null) {
|
---|
35 | instance = new CreateOrEditTurnRestrictionAction();
|
---|
36 | }
|
---|
37 | return instance;
|
---|
38 | }
|
---|
39 |
|
---|
40 | protected CreateOrEditTurnRestrictionAction() {
|
---|
41 | super(
|
---|
42 | tr("Create/Edit turn restriction..."),
|
---|
43 | null,
|
---|
44 | tr("Create or edit a turn restriction."),
|
---|
45 | Shortcut.registerShortcut("tools:turnrestriction", tr("Create or edit a turn restriction."),
|
---|
46 | KeyEvent.VK_2, Shortcut.ALT_SHIFT),
|
---|
47 | false
|
---|
48 | );
|
---|
49 | }
|
---|
50 |
|
---|
51 | @Override
|
---|
52 | public void actionPerformed(ActionEvent e) {
|
---|
53 | OsmDataLayer layer = getLayerManager().getEditLayer();
|
---|
54 | if (layer == null) return;
|
---|
55 | Collection<Relation> trs = TurnRestrictionSelectionPopupPanel.getTurnRestrictionsParticipatingIn(layer.data.getSelected());
|
---|
56 | if (trs.isEmpty()) {
|
---|
57 | // current selection isn't participating in turn restrictions. Launch
|
---|
58 | // an editor for a new turn restriction
|
---|
59 | //
|
---|
60 | Relation tr = new TurnRestrictionBuilder().buildFromSelection(layer);
|
---|
61 | TurnRestrictionEditor editor = new TurnRestrictionEditor(MainApplication.getMap().mapView, layer, tr);
|
---|
62 | TurnRestrictionEditorManager.getInstance().positionOnScreen(editor);
|
---|
63 | TurnRestrictionEditorManager.getInstance().register(layer, tr, editor);
|
---|
64 | editor.setVisible(true);
|
---|
65 | } else {
|
---|
66 | // let the user choose whether he wants to create a new turn restriction or
|
---|
67 | // edit one of the turn restrictions participating in the current selection
|
---|
68 | TurnRestrictionSelectionPopupPanel pnl = new TurnRestrictionSelectionPopupPanel(layer);
|
---|
69 | pnl.launch();
|
---|
70 | }
|
---|
71 | }
|
---|
72 | }
|
---|