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

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

better handling of HelpAwareOptionPane.showOptionDialog in headless mode

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