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

Last change on this file since 4175 was 3995, checked in by mjulius, 13 years ago

fix #3590 - Primitives or objects: Pick one

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