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

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

checkstyle: enable relevant whitespace checks and fix them

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