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

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

replace .get*Selected().isEmpty() by .selectionEmpty()

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