source: josm/trunk/src/org/openstreetmap/josm/actions/AbstractInfoAction.java@ 13952

Last change on this file since 13952 was 13926, checked in by Don-vip, 6 years ago

add AbstractOsmDataLayer, MainLayerManager.getActiveData, Main.getInProgressISelection

  • 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;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5import static org.openstreetmap.josm.tools.I18n.trn;
6
7import java.awt.event.ActionEvent;
8import java.util.ArrayList;
9import java.util.Collection;
10import java.util.List;
11
12import javax.swing.JOptionPane;
13
14import org.openstreetmap.josm.Main;
15import org.openstreetmap.josm.data.notes.Note;
16import org.openstreetmap.josm.data.osm.AbstractPrimitive;
17import org.openstreetmap.josm.data.osm.DataSet;
18import org.openstreetmap.josm.data.osm.OsmData;
19import org.openstreetmap.josm.data.osm.OsmPrimitive;
20import org.openstreetmap.josm.gui.HelpAwareOptionPane;
21import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;
22import org.openstreetmap.josm.gui.MainApplication;
23import org.openstreetmap.josm.gui.help.HelpUtil;
24import org.openstreetmap.josm.tools.ImageProvider;
25import org.openstreetmap.josm.tools.Logging;
26import org.openstreetmap.josm.tools.OpenBrowser;
27import org.openstreetmap.josm.tools.Shortcut;
28
29/**
30 * Abstract base class for info actions, opening an URL describing a particular object.
31 * @since 1697
32 */
33public abstract class AbstractInfoAction extends JosmAction {
34
35 /**
36 * Constructs a new {@code AbstractInfoAction}.
37 * @param installAdapters false, if you don't want to install layer changed and selection changed adapters
38 */
39 public AbstractInfoAction(boolean installAdapters) {
40 super(installAdapters);
41 }
42
43 /**
44 * Constructs a new {@code AbstractInfoAction}.
45 * @param name the action's text as displayed on the menu (if it is added to a menu)
46 * @param iconName the filename of the icon to use
47 * @param tooltip a longer description of the action that will be displayed in the tooltip. Please note
48 * that html is not supported for menu actions on some platforms.
49 * @param shortcut a ready-created shortcut object or null if you don't want a shortcut. But you always
50 * do want a shortcut, remember you can always register it with group=none, so you
51 * won't be assigned a shortcut unless the user configures one. If you pass null here,
52 * the user CANNOT configure a shortcut for your action.
53 * @param register register this action for the toolbar preferences?
54 * @param toolbarId identifier for the toolbar preferences. The iconName is used, if this parameter is null
55 * @param installAdapters false, if you don't want to install layer changed and selection changed adapters
56 */
57 public AbstractInfoAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean register,
58 String toolbarId, boolean installAdapters) {
59 super(name, iconName, tooltip, shortcut, register, toolbarId, installAdapters);
60 }
61
62 /**
63 * Asks user confirmation before launching a large number of browser windows.
64 * @param numBrowsers the number of browser windows to open
65 * @return {@code true} if the user confirms, {@code false} otherwise
66 */
67 public static boolean confirmLaunchMultiple(int numBrowsers) {
68 String msg = /* for correct i18n of plural forms - see #9110 */ trn(
69 "You are about to launch {0} browser window.<br>"
70 + "This may both clutter your screen with browser windows<br>"
71 + "and take some time to finish.",
72 "You are about to launch {0} browser windows.<br>"
73 + "This may both clutter your screen with browser windows<br>"
74 + "and take some time to finish.", numBrowsers, numBrowsers);
75 ButtonSpec[] spec = new ButtonSpec[] {
76 new ButtonSpec(
77 tr("Continue"),
78 new ImageProvider("ok"),
79 trn("Click to continue and to open {0} browser", "Click to continue and to open {0} browsers",
80 numBrowsers, numBrowsers),
81 null // no specific help topic
82 ),
83 new ButtonSpec(
84 tr("Cancel"),
85 new ImageProvider("cancel"),
86 tr("Click to abort launching external browsers"),
87 null // no specific help topic
88 )
89 };
90 return 0 == HelpAwareOptionPane.showOptionDialog(
91 Main.parent,
92 new StringBuilder(msg).insert(0, "<html>").append("</html>").toString(),
93 tr("Warning"),
94 JOptionPane.WARNING_MESSAGE,
95 null,
96 spec,
97 spec[0],
98 HelpUtil.ht("/WarningMessages#ToManyBrowsersToOpen")
99 );
100 }
101
102 protected void launchInfoBrowsersForSelectedPrimitivesAndNote() {
103 List<OsmPrimitive> primitivesToShow = new ArrayList<>();
104 DataSet ds = getLayerManager().getActiveDataSet();
105 if (ds != null) {
106 primitivesToShow.addAll(ds.getAllSelected());
107 }
108
109 Note noteToShow = MainApplication.isDisplayingMapView() ? MainApplication.getMap().noteDialog.getSelectedNote() : null;
110
111 // filter out new primitives which are not yet uploaded to the server
112 //
113 primitivesToShow.removeIf(AbstractPrimitive::isNew);
114
115 if (primitivesToShow.isEmpty() && noteToShow == null) {
116 JOptionPane.showMessageDialog(
117 Main.parent,
118 tr("Please select at least one already uploaded node, way, or relation."),
119 tr("Warning"),
120 JOptionPane.WARNING_MESSAGE
121 );
122 return;
123 }
124
125 // don't launch more than 10 browser instances / browser windows
126 //
127 int max = Math.min(10, primitivesToShow.size());
128 if (primitivesToShow.size() > max && !confirmLaunchMultiple(primitivesToShow.size()))
129 return;
130 for (int i = 0; i < max; i++) {
131 launchInfoBrowser(primitivesToShow.get(i));
132 }
133
134 if (noteToShow != null) {
135 launchInfoBrowser(noteToShow);
136 }
137 }
138
139 protected final void launchInfoBrowser(Object o) {
140 String url = createInfoUrl(o);
141 if (url != null) {
142 String result = OpenBrowser.displayUrl(url);
143 if (result != null) {
144 Logging.warn(result);
145 }
146 }
147 }
148
149 @Override
150 public void actionPerformed(ActionEvent e) {
151 launchInfoBrowsersForSelectedPrimitivesAndNote();
152 }
153
154 protected abstract String createInfoUrl(Object infoObject);
155
156 @Override
157 protected void updateEnabledState() {
158 OsmData<?, ?, ?, ?> ds = getLayerManager().getActiveData();
159 setEnabled(ds != null && !ds.selectionEmpty());
160 }
161
162 @Override
163 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
164 setEnabled(selection != null && !selection.isEmpty());
165 }
166}
Note: See TracBrowser for help on using the repository browser.