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

Last change on this file since 11063 was 10446, checked in by Don-vip, 8 years ago

see #13001 - replace calls to Main.main.getCurrentDataSet() by Main.getLayerManager().getEditDataSet()

  • Property svn:eol-style set to native
File size: 6.7 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 AbstractAction() {
64 @Override
65 public void actionPerformed(ActionEvent e) {
66 EditRelationAction.launchEditor(getLastRelation());
67 }
68 }, shortcut);
69 }
70
71 /**
72 * Enables arrow button.
73 */
74 public void enableArrow() {
75 arrow.setVisible(getLastRelation() != null);
76 }
77
78 /**
79 * Returns the last relation.
80 * @return the last relation
81 */
82 public static Relation getLastRelation() {
83 List<Relation> recentRelations = getRecentRelationsOnActiveLayer();
84 if (recentRelations == null || recentRelations.isEmpty())
85 return null;
86 for (Relation relation: recentRelations) {
87 if (!isRelationListable(relation))
88 continue;
89 return relation;
90 }
91 return null;
92 }
93
94 /**
95 * Determines if the given relation is listable in last relations.
96 * @param relation relation
97 * @return {@code true} if relation is non null, not deleted, and in current dataset
98 */
99 public static boolean isRelationListable(Relation relation) {
100 return relation != null &&
101 !relation.isDeleted() &&
102 Main.getLayerManager().getEditDataSet().containsRelation(relation);
103 }
104
105 @Override
106 public void actionPerformed(ActionEvent e) {
107 RecentRelationsPopupMenu.launch(editButton, shortcut.getKeyStroke());
108 }
109
110 @Override
111 public void commandChanged(int queueSize, int redoSize) {
112 enableArrow();
113 }
114
115 @Override
116 public void layerAdded(LayerAddEvent e) {
117 enableArrow();
118 }
119
120 @Override
121 public void layerRemoving(LayerRemoveEvent e) {
122 enableArrow();
123 }
124
125 @Override
126 public void layerOrderChanged(LayerOrderChangeEvent e) {
127 enableArrow();
128 }
129
130 @Override
131 public void activeOrEditLayerChanged(ActiveLayerChangeEvent e) {
132 enableArrow();
133 }
134
135 /**
136 * Returns the list of recent relations on active layer.
137 * @return the list of recent relations on active layer
138 */
139 public static List<Relation> getRecentRelationsOnActiveLayer() {
140 if (!Main.isDisplayingMapView())
141 return Collections.emptyList();
142 Layer activeLayer = Main.getLayerManager().getActiveLayer();
143 if (!(activeLayer instanceof OsmDataLayer)) {
144 return Collections.emptyList();
145 } else {
146 return ((OsmDataLayer) activeLayer).getRecentRelations();
147 }
148 }
149
150 protected static class RecentRelationsPopupMenu extends JPopupMenu {
151 /**
152 * Constructs a new {@code RecentRelationsPopupMenu}.
153 * @param recentRelations list of recent relations
154 * @param keystroke key stroke for the first menu item
155 */
156 public RecentRelationsPopupMenu(List<Relation> recentRelations, KeyStroke keystroke) {
157 boolean first = true;
158 for (Relation relation: recentRelations) {
159 if (!isRelationListable(relation))
160 continue;
161 JMenuItem menuItem = new RecentRelationsMenuItem(relation);
162 if (first) {
163 menuItem.setAccelerator(keystroke);
164 first = false;
165 }
166 menuItem.setIcon(ImageProvider.getPadded(relation, ImageProvider.ImageSizes.MENU.getImageDimension()));
167 add(menuItem);
168 }
169 }
170
171 protected static void launch(Component parent, KeyStroke keystroke) {
172 Rectangle r = parent.getBounds();
173 new RecentRelationsPopupMenu(getRecentRelationsOnActiveLayer(), keystroke).show(parent, r.x, r.y + r.height);
174 }
175 }
176
177 /**
178 * A specialized {@link JMenuItem} for presenting one entry of the relation history
179 */
180 protected static class RecentRelationsMenuItem extends JMenuItem implements ActionListener {
181 protected final transient Relation relation;
182
183 public RecentRelationsMenuItem(Relation relation) {
184 super(relation.getDisplayName(DefaultNameFormatter.getInstance()));
185 this.relation = relation;
186 addActionListener(this);
187 }
188
189 @Override
190 public void actionPerformed(ActionEvent e) {
191 EditRelationAction.launchEditor(relation);
192 }
193 }
194
195}
Note: See TracBrowser for help on using the repository browser.