source: josm/trunk/src/org/openstreetmap/josm/actions/relation/RecentRelationsAction.java@ 13664

Last change on this file since 13664 was 13278, checked in by stoecker, 6 years ago

see #15734 - allow shortcut parsing

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