1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions.relation;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Component;
|
---|
7 | import java.awt.Rectangle;
|
---|
8 | import java.awt.event.ActionEvent;
|
---|
9 | import java.awt.event.ActionListener;
|
---|
10 | import java.awt.event.KeyEvent;
|
---|
11 | import java.util.Collections;
|
---|
12 | import java.util.List;
|
---|
13 |
|
---|
14 | import javax.swing.AbstractAction;
|
---|
15 | import javax.swing.JMenuItem;
|
---|
16 | import javax.swing.JPopupMenu;
|
---|
17 | import javax.swing.KeyStroke;
|
---|
18 | import javax.swing.plaf.basic.BasicArrowButton;
|
---|
19 |
|
---|
20 | import org.openstreetmap.josm.Main;
|
---|
21 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
22 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
23 | import org.openstreetmap.josm.gui.DefaultNameFormatter;
|
---|
24 | import org.openstreetmap.josm.gui.SideButton;
|
---|
25 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
26 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
27 | import org.openstreetmap.josm.gui.layer.OsmDataLayer.CommandQueueListener;
|
---|
28 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
29 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Action for accessing recent relations.
|
---|
33 | * @since 9668
|
---|
34 | */
|
---|
35 | public class RecentRelationsAction extends JosmAction implements CommandQueueListener {
|
---|
36 |
|
---|
37 | private final SideButton editButton;
|
---|
38 | private final BasicArrowButton arrow;
|
---|
39 | private final Shortcut shortcut;
|
---|
40 | private final LaunchEditorAction launchAction;
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Constructs a new <code>RecentRelationsAction</code>.
|
---|
44 | * @param editButton edit button
|
---|
45 | */
|
---|
46 | public RecentRelationsAction(SideButton editButton) {
|
---|
47 | super(RecentRelationsAction.class.getName(), null, null, null, false, true);
|
---|
48 | this.editButton = editButton;
|
---|
49 | arrow = editButton.createArrow(this);
|
---|
50 | arrow.setToolTipText(tr("List of recent relations"));
|
---|
51 | Main.main.undoRedo.addCommandQueueListener(this);
|
---|
52 | enableArrow();
|
---|
53 | shortcut = Shortcut.registerShortcut(
|
---|
54 | "relationeditor:editrecentrelation",
|
---|
55 | tr("Relation Editor: {0}", tr("Open recent relation")),
|
---|
56 | KeyEvent.VK_ESCAPE,
|
---|
57 | Shortcut.SHIFT
|
---|
58 | );
|
---|
59 | launchAction = new LaunchEditorAction();
|
---|
60 | Main.registerActionShortcut(launchAction, shortcut);
|
---|
61 | }
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Enables arrow button.
|
---|
65 | */
|
---|
66 | public void enableArrow() {
|
---|
67 | if (arrow != null) {
|
---|
68 | arrow.setVisible(getLastRelation() != null);
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Returns the last relation.
|
---|
74 | * @return the last relation
|
---|
75 | */
|
---|
76 | public static Relation getLastRelation() {
|
---|
77 | List<Relation> recentRelations = getRecentRelationsOnActiveLayer();
|
---|
78 | if (recentRelations == null || recentRelations.isEmpty())
|
---|
79 | return null;
|
---|
80 | for (Relation relation: recentRelations) {
|
---|
81 | if (!isRelationListable(relation))
|
---|
82 | continue;
|
---|
83 | return relation;
|
---|
84 | }
|
---|
85 | return null;
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Determines if the given relation is listable in last relations.
|
---|
90 | * @param relation relation
|
---|
91 | * @return {@code true} if relation is non null, not deleted, and in current dataset
|
---|
92 | */
|
---|
93 | public static boolean isRelationListable(Relation relation) {
|
---|
94 | return relation != null &&
|
---|
95 | !relation.isDeleted() &&
|
---|
96 | Main.getLayerManager().getEditDataSet().containsRelation(relation);
|
---|
97 | }
|
---|
98 |
|
---|
99 | @Override
|
---|
100 | public void actionPerformed(ActionEvent e) {
|
---|
101 | RecentRelationsPopupMenu.launch(editButton, shortcut.getKeyStroke());
|
---|
102 | }
|
---|
103 |
|
---|
104 | @Override
|
---|
105 | public void commandChanged(int queueSize, int redoSize) {
|
---|
106 | enableArrow();
|
---|
107 | }
|
---|
108 |
|
---|
109 | @Override
|
---|
110 | protected void updateEnabledState() {
|
---|
111 | enableArrow();
|
---|
112 | }
|
---|
113 |
|
---|
114 | @Override
|
---|
115 | public void destroy() {
|
---|
116 | Main.unregisterActionShortcut(launchAction, shortcut);
|
---|
117 | super.destroy();
|
---|
118 | }
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * Returns the list of recent relations on active layer.
|
---|
122 | * @return the list of recent relations on active layer
|
---|
123 | */
|
---|
124 | public static List<Relation> getRecentRelationsOnActiveLayer() {
|
---|
125 | if (!Main.isDisplayingMapView())
|
---|
126 | return Collections.emptyList();
|
---|
127 | Layer activeLayer = Main.getLayerManager().getActiveLayer();
|
---|
128 | if (!(activeLayer instanceof OsmDataLayer)) {
|
---|
129 | return Collections.emptyList();
|
---|
130 | } else {
|
---|
131 | return ((OsmDataLayer) activeLayer).getRecentRelations();
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | static class LaunchEditorAction extends AbstractAction {
|
---|
136 | @Override
|
---|
137 | public void actionPerformed(ActionEvent e) {
|
---|
138 | EditRelationAction.launchEditor(getLastRelation());
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | static class RecentRelationsPopupMenu extends JPopupMenu {
|
---|
143 | /**
|
---|
144 | * Constructs a new {@code RecentRelationsPopupMenu}.
|
---|
145 | * @param recentRelations list of recent relations
|
---|
146 | * @param keystroke key stroke for the first menu item
|
---|
147 | */
|
---|
148 | RecentRelationsPopupMenu(List<Relation> recentRelations, KeyStroke keystroke) {
|
---|
149 | boolean first = true;
|
---|
150 | for (Relation relation: recentRelations) {
|
---|
151 | if (!isRelationListable(relation))
|
---|
152 | continue;
|
---|
153 | JMenuItem menuItem = new RecentRelationsMenuItem(relation);
|
---|
154 | if (first) {
|
---|
155 | menuItem.setAccelerator(keystroke);
|
---|
156 | first = false;
|
---|
157 | }
|
---|
158 | menuItem.setIcon(ImageProvider.getPadded(relation, ImageProvider.ImageSizes.MENU.getImageDimension()));
|
---|
159 | add(menuItem);
|
---|
160 | }
|
---|
161 | }
|
---|
162 |
|
---|
163 | static void launch(Component parent, KeyStroke keystroke) {
|
---|
164 | Rectangle r = parent.getBounds();
|
---|
165 | new RecentRelationsPopupMenu(getRecentRelationsOnActiveLayer(), keystroke).show(parent, r.x, r.y + r.height);
|
---|
166 | }
|
---|
167 | }
|
---|
168 |
|
---|
169 | /**
|
---|
170 | * A specialized {@link JMenuItem} for presenting one entry of the relation history
|
---|
171 | */
|
---|
172 | static class RecentRelationsMenuItem extends JMenuItem implements ActionListener {
|
---|
173 | private final transient Relation relation;
|
---|
174 |
|
---|
175 | RecentRelationsMenuItem(Relation relation) {
|
---|
176 | super(relation.getDisplayName(DefaultNameFormatter.getInstance()));
|
---|
177 | this.relation = relation;
|
---|
178 | addActionListener(this);
|
---|
179 | }
|
---|
180 |
|
---|
181 | @Override
|
---|
182 | public void actionPerformed(ActionEvent e) {
|
---|
183 | EditRelationAction.launchEditor(relation);
|
---|
184 | }
|
---|
185 | }
|
---|
186 | }
|
---|