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

Last change on this file since 11348 was 11348, checked in by Don-vip, 7 years ago

findbugs - fix some SIC_INNER_SHOULD_BE_STATIC_ANON

  • Property svn:eol-style set to native
File size: 6.8 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.SideButton;
24import org.openstreetmap.josm.gui.layer.Layer;
25import org.openstreetmap.josm.gui.layer.LayerManager.LayerAddEvent;
26import org.openstreetmap.josm.gui.layer.LayerManager.LayerChangeListener;
27import org.openstreetmap.josm.gui.layer.LayerManager.LayerOrderChangeEvent;
28import org.openstreetmap.josm.gui.layer.LayerManager.LayerRemoveEvent;
29import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeEvent;
30import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeListener;
31import org.openstreetmap.josm.gui.layer.OsmDataLayer;
32import org.openstreetmap.josm.gui.layer.OsmDataLayer.CommandQueueListener;
33import org.openstreetmap.josm.tools.ImageProvider;
34import org.openstreetmap.josm.tools.Shortcut;
35
36/**
37 * Action for accessing recent relations.
38 */
39public class RecentRelationsAction implements ActionListener, CommandQueueListener, LayerChangeListener, ActiveLayerChangeListener {
40
41 private final SideButton editButton;
42 private final BasicArrowButton arrow;
43 private final Shortcut shortcut;
44
45 /**
46 * Constructs a new <code>RecentRelationsAction</code>.
47 * @param editButton edit button
48 */
49 public RecentRelationsAction(SideButton editButton) {
50 this.editButton = editButton;
51 arrow = editButton.createArrow(this);
52 arrow.setToolTipText(tr("List of recent relations"));
53 Main.main.undoRedo.addCommandQueueListener(this);
54 Main.getLayerManager().addLayerChangeListener(this);
55 Main.getLayerManager().addActiveLayerChangeListener(this);
56 enableArrow();
57 shortcut = Shortcut.registerShortcut(
58 "relationeditor:editrecentrelation",
59 tr("Relation Editor: {0}", tr("Open recent relation")),
60 KeyEvent.VK_ESCAPE,
61 Shortcut.SHIFT
62 );
63 Main.registerActionShortcut(new LaunchEditorAction(), 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.getLayerManager().getEditDataSet().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 layerAdded(LayerAddEvent e) {
112 enableArrow();
113 }
114
115 @Override
116 public void layerRemoving(LayerRemoveEvent e) {
117 enableArrow();
118 }
119
120 @Override
121 public void layerOrderChanged(LayerOrderChangeEvent e) {
122 enableArrow();
123 }
124
125 @Override
126 public void activeOrEditLayerChanged(ActiveLayerChangeEvent e) {
127 enableArrow();
128 }
129
130 /**
131 * Returns the list of recent relations on active layer.
132 * @return the list of recent relations on active layer
133 */
134 public static List<Relation> getRecentRelationsOnActiveLayer() {
135 if (!Main.isDisplayingMapView())
136 return Collections.emptyList();
137 Layer activeLayer = Main.getLayerManager().getActiveLayer();
138 if (!(activeLayer instanceof OsmDataLayer)) {
139 return Collections.emptyList();
140 } else {
141 return ((OsmDataLayer) activeLayer).getRecentRelations();
142 }
143 }
144
145 protected static class LaunchEditorAction extends AbstractAction {
146 @Override
147 public void actionPerformed(ActionEvent e) {
148 EditRelationAction.launchEditor(getLastRelation());
149 }
150 }
151
152 protected static class RecentRelationsPopupMenu extends JPopupMenu {
153 /**
154 * Constructs a new {@code RecentRelationsPopupMenu}.
155 * @param recentRelations list of recent relations
156 * @param keystroke key stroke for the first menu item
157 */
158 public RecentRelationsPopupMenu(List<Relation> recentRelations, KeyStroke keystroke) {
159 boolean first = true;
160 for (Relation relation: recentRelations) {
161 if (!isRelationListable(relation))
162 continue;
163 JMenuItem menuItem = new RecentRelationsMenuItem(relation);
164 if (first) {
165 menuItem.setAccelerator(keystroke);
166 first = false;
167 }
168 menuItem.setIcon(ImageProvider.getPadded(relation, ImageProvider.ImageSizes.MENU.getImageDimension()));
169 add(menuItem);
170 }
171 }
172
173 protected static void launch(Component parent, KeyStroke keystroke) {
174 Rectangle r = parent.getBounds();
175 new RecentRelationsPopupMenu(getRecentRelationsOnActiveLayer(), keystroke).show(parent, r.x, r.y + r.height);
176 }
177 }
178
179 /**
180 * A specialized {@link JMenuItem} for presenting one entry of the relation history
181 */
182 protected static class RecentRelationsMenuItem extends JMenuItem implements ActionListener {
183 protected final transient Relation relation;
184
185 public RecentRelationsMenuItem(Relation relation) {
186 super(relation.getDisplayName(DefaultNameFormatter.getInstance()));
187 this.relation = relation;
188 addActionListener(this);
189 }
190
191 @Override
192 public void actionPerformed(ActionEvent e) {
193 EditRelationAction.launchEditor(relation);
194 }
195 }
196
197}
Note: See TracBrowser for help on using the repository browser.