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

Last change on this file since 8486 was 8475, checked in by Don-vip, 9 years ago

fix #11219 - Open notes in browser with Ctrl-Shift-I: "Advanced info (web)"

  • Property svn:eol-style set to native
File size: 5.0 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
25public abstract class AbstractInfoAction extends JosmAction {
26
27 public AbstractInfoAction(boolean installAdapters) {
28 super(installAdapters);
29 }
30
31 public AbstractInfoAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean register, String toolbarId, boolean installAdapters) {
32 super(name, iconName, tooltip, shortcut, register, toolbarId, installAdapters);
33 }
34
35 public static boolean confirmLaunchMultiple(int numBrowsers) {
36 String msg = /* for correct i18n of plural forms - see #9110 */ trn(
37 "You are about to launch {0} browser window.<br>"
38 + "This may both clutter your screen with browser windows<br>"
39 + "and take some time to finish.",
40 "You are about to launch {0} browser windows.<br>"
41 + "This may both clutter your screen with browser windows<br>"
42 + "and take some time to finish.", numBrowsers, numBrowsers);
43 msg = "<html>" + msg + "</html>";
44 ButtonSpec[] spec = new ButtonSpec[] {
45 new ButtonSpec(
46 tr("Continue"),
47 ImageProvider.get("ok"),
48 trn("Click to continue and to open {0} browser", "Click to continue and to open {0} browsers", numBrowsers, numBrowsers),
49 null // no specific help topic
50 ),
51 new ButtonSpec(
52 tr("Cancel"),
53 ImageProvider.get("cancel"),
54 tr("Click to abort launching external browsers"),
55 null // no specific help topic
56 )
57 };
58 int ret = HelpAwareOptionPane.showOptionDialog(
59 Main.parent,
60 msg,
61 tr("Warning"),
62 JOptionPane.WARNING_MESSAGE,
63 null,
64 spec,
65 spec[0],
66 HelpUtil.ht("/WarningMessages#ToManyBrowsersToOpen")
67 );
68 return ret == 0;
69 }
70
71 protected void launchInfoBrowsersForSelectedPrimitivesAndNote() {
72 List<OsmPrimitive> primitivesToShow = new ArrayList<>();
73 if (getCurrentDataSet() != null) {
74 primitivesToShow.addAll(getCurrentDataSet().getAllSelected());
75 }
76
77 Note noteToShow = Main.isDisplayingMapView() ? Main.map.noteDialog.getSelectedNote() : null;
78
79 // filter out new primitives which are not yet uploaded to the server
80 //
81 Iterator<OsmPrimitive> it = primitivesToShow.iterator();
82 while(it.hasNext()) {
83 if (it.next().isNew()) {
84 it.remove();
85 }
86 }
87
88 if (primitivesToShow.isEmpty() && noteToShow == null) {
89 JOptionPane.showMessageDialog(
90 Main.parent,
91 tr("Please select at least one already uploaded node, way, or relation."),
92 tr("Warning"),
93 JOptionPane.WARNING_MESSAGE
94 );
95 return;
96 }
97
98 // don't launch more than 10 browser instances / browser windows
99 //
100 int max = Math.min(10, primitivesToShow.size());
101 if (primitivesToShow.size() > max && !confirmLaunchMultiple(primitivesToShow.size()))
102 return;
103 for (int i = 0; i < max; i++) {
104 launchInfoBrowser(primitivesToShow.get(i));
105 }
106
107 if (noteToShow != null) {
108 launchInfoBrowser(noteToShow);
109 }
110 }
111
112 protected final void launchInfoBrowser(Object o) {
113 String url = createInfoUrl(o);
114 if (url != null) {
115 String result = OpenBrowser.displayUrl(url);
116 if (result != null) {
117 Main.warn(result);
118 }
119 }
120 }
121
122 @Override
123 public void actionPerformed(ActionEvent e) {
124 launchInfoBrowsersForSelectedPrimitivesAndNote();
125 }
126
127 protected abstract String createInfoUrl(Object infoObject);
128
129 @Override
130 protected void updateEnabledState() {
131 setEnabled(getCurrentDataSet() != null && !getCurrentDataSet().getSelected().isEmpty());
132 }
133
134 @Override
135 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
136 setEnabled(selection != null && !selection.isEmpty());
137 }
138}
Note: See TracBrowser for help on using the repository browser.