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

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

see #16319 - scale properly all icons using ButtonSpec

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