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

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

sonar fixes + javadoc

  • 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;
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 int ret = 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 return ret == 0;
98 }
99
100 protected void launchInfoBrowsersForSelectedPrimitivesAndNote() {
101 List<OsmPrimitive> primitivesToShow = new ArrayList<>();
102 if (getCurrentDataSet() != null) {
103 primitivesToShow.addAll(getCurrentDataSet().getAllSelected());
104 }
105
106 Note noteToShow = Main.isDisplayingMapView() ? Main.map.noteDialog.getSelectedNote() : null;
107
108 // filter out new primitives which are not yet uploaded to the server
109 //
110 Iterator<OsmPrimitive> it = primitivesToShow.iterator();
111 while (it.hasNext()) {
112 if (it.next().isNew()) {
113 it.remove();
114 }
115 }
116
117 if (primitivesToShow.isEmpty() && noteToShow == null) {
118 JOptionPane.showMessageDialog(
119 Main.parent,
120 tr("Please select at least one already uploaded node, way, or relation."),
121 tr("Warning"),
122 JOptionPane.WARNING_MESSAGE
123 );
124 return;
125 }
126
127 // don't launch more than 10 browser instances / browser windows
128 //
129 int max = Math.min(10, primitivesToShow.size());
130 if (primitivesToShow.size() > max && !confirmLaunchMultiple(primitivesToShow.size()))
131 return;
132 for (int i = 0; i < max; i++) {
133 launchInfoBrowser(primitivesToShow.get(i));
134 }
135
136 if (noteToShow != null) {
137 launchInfoBrowser(noteToShow);
138 }
139 }
140
141 protected final void launchInfoBrowser(Object o) {
142 String url = createInfoUrl(o);
143 if (url != null) {
144 String result = OpenBrowser.displayUrl(url);
145 if (result != null) {
146 Main.warn(result);
147 }
148 }
149 }
150
151 @Override
152 public void actionPerformed(ActionEvent e) {
153 launchInfoBrowsersForSelectedPrimitivesAndNote();
154 }
155
156 protected abstract String createInfoUrl(Object infoObject);
157
158 @Override
159 protected void updateEnabledState() {
160 setEnabled(getCurrentDataSet() != null && !getCurrentDataSet().getSelected().isEmpty());
161 }
162
163 @Override
164 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
165 setEnabled(selection != null && !selection.isEmpty());
166 }
167}
Note: See TracBrowser for help on using the repository browser.