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

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

see #15229 - deprecate Main.parent and Main itself

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