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

Last change on this file since 6320 was 6316, checked in by Don-vip, 11 years ago

Sonar/FindBugs - Loose coupling

  • Property svn:eol-style set to native
File size: 6.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;
5
6import java.awt.event.ActionEvent;
7import java.net.URL;
8import java.util.ArrayList;
9import java.util.Collection;
10import java.util.Iterator;
11import java.util.List;
12import java.util.regex.Pattern;
13
14import javax.swing.JOptionPane;
15
16import org.openstreetmap.josm.Main;
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.io.OsmApi;
22import org.openstreetmap.josm.tools.ImageProvider;
23import org.openstreetmap.josm.tools.OpenBrowser;
24import org.openstreetmap.josm.tools.Shortcut;
25
26public abstract class AbstractInfoAction extends JosmAction {
27
28 public AbstractInfoAction(boolean installAdapters) {
29 super(installAdapters);
30 }
31
32 public AbstractInfoAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean register, String toolbarId, boolean installAdapters) {
33 super(name, iconName, tooltip, shortcut, register, toolbarId, installAdapters);
34 }
35
36 /**
37 * replies the base URL for browsing information about about a primitive
38 *
39 * @return the base URL, i.e. http://api.openstreetmap.org/browse
40 */
41 static public String getBaseBrowseUrl() {
42 String baseUrl = Main.pref.get("osm-server.url", OsmApi.DEFAULT_API_URL);
43 Pattern pattern = Pattern.compile("/api/?$");
44 String ret = pattern.matcher(baseUrl).replaceAll("/browse");
45 if (ret.equals(baseUrl)) {
46 Main.warn(tr("Unexpected format of API base URL. Redirection to info or history page for OSM object will probably fail. API base URL is: ''{0}''",baseUrl));
47 }
48 if (ret.startsWith("http://api.openstreetmap.org/")) {
49 ret = ret.substring("http://api.openstreetmap.org/".length());
50 ret = "http://www.openstreetmap.org/" + ret;
51 }
52 return ret;
53 }
54
55 /**
56 * replies the base URL for browsing information about a user
57 *
58 * @return the base URL, i.e. http://www.openstreetmap.org/user
59 */
60 static public String getBaseUserUrl() {
61 String baseUrl = Main.pref.get("osm-server.url", OsmApi.DEFAULT_API_URL);
62 Pattern pattern = Pattern.compile("/api/?$");
63 String ret = pattern.matcher(baseUrl).replaceAll("/user");
64 if (ret.equals(baseUrl)) {
65 Main.warn(tr("Unexpected format of API base URL. Redirection to user page for OSM user will probably fail. API base URL is: ''{0}''",baseUrl));
66 }
67 if (ret.startsWith("http://api.openstreetmap.org/")) {
68 ret = ret.substring("http://api.openstreetmap.org/".length());
69 ret = "http://www.openstreetmap.org/" + ret;
70 }
71 return ret;
72 }
73
74 protected void launchBrowser(URL url) {
75 OpenBrowser.displayUrl(
76 url.toString()
77 );
78 }
79
80 protected void launchBrowser(String url) {
81 OpenBrowser.displayUrl(
82 url
83 );
84 }
85
86 public static boolean confirmLaunchMultiple(int numBrowsers) {
87 String msg = tr(
88 "You are about to launch {0} browser windows.<br>"
89 + "This may both clutter your screen with browser windows<br>"
90 + "and take some time to finish.", numBrowsers);
91 msg = "<html>" + msg + "</html>";
92 ButtonSpec[] spec = new ButtonSpec[] {
93 new ButtonSpec(
94 tr("Continue"),
95 ImageProvider.get("ok"),
96 tr("Click to continue and to open {0} browsers", numBrowsers),
97 null // no specific help topic
98 ),
99 new ButtonSpec(
100 tr("Cancel"),
101 ImageProvider.get("cancel"),
102 tr("Click to abort launching external browsers"),
103 null // no specific help topic
104 )
105 };
106 int ret = HelpAwareOptionPane.showOptionDialog(
107 Main.parent,
108 msg,
109 tr("Warning"),
110 JOptionPane.WARNING_MESSAGE,
111 null,
112 spec,
113 spec[0],
114 HelpUtil.ht("/WarningMessages#ToManyBrowsersToOpen")
115 );
116 return ret == 0;
117 }
118
119 protected void launchInfoBrowsersForSelectedPrimitives() {
120 List<OsmPrimitive> primitivesToShow = new ArrayList<OsmPrimitive>(getCurrentDataSet().getAllSelected());
121
122 // filter out new primitives which are not yet uploaded to the server
123 //
124 Iterator<OsmPrimitive> it = primitivesToShow.iterator();
125 while(it.hasNext()) {
126 if (it.next().isNew()) {
127 it.remove();
128 }
129 }
130
131 if (primitivesToShow.isEmpty()) {
132 JOptionPane.showMessageDialog(
133 Main.parent,
134 tr("Please select at least one already uploaded node, way, or relation."),
135 tr("Warning"),
136 JOptionPane.WARNING_MESSAGE
137 );
138 return;
139 }
140
141 // don't launch more than 10 browser instances / browser windows
142 //
143 int max = Math.min(10, primitivesToShow.size());
144 if (primitivesToShow.size() > max && ! confirmLaunchMultiple(primitivesToShow.size()))
145 return;
146 for(int i = 0; i < max; i++) {
147 launchBrowser(createInfoUrl(primitivesToShow.get(i)));
148 }
149 }
150
151 @Override
152 public void actionPerformed(ActionEvent e) {
153 launchInfoBrowsersForSelectedPrimitives();
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.