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

Last change on this file since 9722 was 9722, checked in by bastiK, 8 years ago

applied #12409 - arrow on edit relation button lists recent relations: fix (patch by kolesar)

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