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

Last change on this file since 1847 was 1847, checked in by Gubaer, 15 years ago

replaced calls to JOptionPane.* by calls to OptionPaneUtil.*

File size: 3.3 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.util.ArrayList;
8import java.util.Iterator;
9import java.util.regex.Pattern;
10
11import javax.swing.JOptionPane;
12
13import org.openstreetmap.josm.Main;
14import org.openstreetmap.josm.data.osm.OsmPrimitive;
15import org.openstreetmap.josm.gui.OptionPaneUtil;
16import org.openstreetmap.josm.tools.OpenBrowser;
17import org.openstreetmap.josm.tools.Shortcut;
18
19public abstract class AbstractInfoAction extends JosmAction {
20
21 public AbstractInfoAction() {
22 super();
23 }
24
25 public AbstractInfoAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean register) {
26 super(name, iconName, tooltip, shortcut, register);
27 }
28
29 /**
30 * replies the base URL for browsing information about about a primitive
31 *
32 * @return the base URL, i.e. http://api.openstreetmap.org/browse
33 */
34 protected String getBaseURL() {
35 String baseUrl = Main.pref.get("osm-server.url", "http://api.openstreetmap.org/api");
36 Pattern pattern = Pattern.compile("/api/?$");
37 String ret = pattern.matcher(baseUrl).replaceAll("/browse");
38 if (ret.equals(baseUrl)) {
39 System.out.println(tr("WARNING: unexpected format of API base URL. Redirection to history page for OSM primitive will probably fail. API base URL is: ''{0}''",baseUrl));
40 }
41 if (ret.startsWith("http://api.openstreetmap.org/")) {
42 ret = ret.substring("http://api.openstreetmap.org/".length());
43 ret = "http://www.openstreetmap.org/" + ret;
44 }
45 return ret;
46 }
47
48 protected void launchBrowser() {
49 ArrayList<OsmPrimitive> primitivesToShow = new ArrayList<OsmPrimitive>(getCurrentDataSet().getSelected());
50
51 // filter out new primitives which are not yet uploaded to the server
52 //
53 Iterator<OsmPrimitive> it = primitivesToShow.iterator();
54 while(it.hasNext()) {
55 if (it.next().id == 0) {
56 it.remove();
57 }
58 }
59
60 if (primitivesToShow.isEmpty()) {
61 OptionPaneUtil.showMessageDialog(
62 Main.parent,
63 tr("Please select at least one already uploaded node, way, or relation."),
64 tr("Warning"),
65 JOptionPane.WARNING_MESSAGE
66 );
67 return;
68 }
69
70 // don't launch more than 10 browser instances / browser windows
71 //
72 int max = Math.min(10, primitivesToShow.size());
73 if (max < primitivesToShow.size()) {
74 System.out.println(tr("WARNING: launching browser windows for the first {0} of {1} selected primitives only", 10, primitivesToShow.size()));
75 }
76 for(int i = 0; i < max; i++) {
77 OpenBrowser.displayUrl(
78 createInfoUrl(primitivesToShow.get(i))
79 );
80 }
81 }
82
83 public void actionPerformed(ActionEvent e) {
84 launchBrowser();
85 }
86
87 protected abstract String createInfoUrl(OsmPrimitive primitive);
88
89 @Override
90 protected void updateEnabledState() {
91 setEnabled(getCurrentDataSet() != null && !getCurrentDataSet().getSelected().isEmpty());
92 }
93}
Note: See TracBrowser for help on using the repository browser.