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

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

Rework console output:

  • new log level "error"
  • Replace nearly all calls to system.out and system.err to Main.(error|warn|info|debug)
  • Remove some unnecessary debug output
  • Some messages are modified (removal of "Info", "Warning", "Error" from the message itself -> notable i18n impact but limited to console error messages not seen by the majority of users, so that's ok)
  • 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.io.OsmApi;
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 /**
36 * replies the base URL for browsing information about about a primitive
37 *
38 * @return the base URL, i.e. http://api.openstreetmap.org/browse
39 */
40 static public String getBaseBrowseUrl() {
41 String baseUrl = Main.pref.get("osm-server.url", OsmApi.DEFAULT_API_URL);
42 Pattern pattern = Pattern.compile("/api/?$");
43 String ret = pattern.matcher(baseUrl).replaceAll("/browse");
44 if (ret.equals(baseUrl)) {
45 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));
46 }
47 if (ret.startsWith("http://api.openstreetmap.org/")) {
48 ret = ret.substring("http://api.openstreetmap.org/".length());
49 ret = "http://www.openstreetmap.org/" + ret;
50 }
51 return ret;
52 }
53
54 /**
55 * replies the base URL for browsing information about a user
56 *
57 * @return the base URL, i.e. http://www.openstreetmap.org/user
58 */
59 static public String getBaseUserUrl() {
60 String baseUrl = Main.pref.get("osm-server.url", OsmApi.DEFAULT_API_URL);
61 Pattern pattern = Pattern.compile("/api/?$");
62 String ret = pattern.matcher(baseUrl).replaceAll("/user");
63 if (ret.equals(baseUrl)) {
64 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));
65 }
66 if (ret.startsWith("http://api.openstreetmap.org/")) {
67 ret = ret.substring("http://api.openstreetmap.org/".length());
68 ret = "http://www.openstreetmap.org/" + ret;
69 }
70 return ret;
71 }
72
73 protected void launchBrowser(URL url) {
74 OpenBrowser.displayUrl(
75 url.toString()
76 );
77 }
78
79 protected void launchBrowser(String url) {
80 OpenBrowser.displayUrl(
81 url
82 );
83 }
84
85 public static boolean confirmLaunchMultiple(int numBrowsers) {
86 String msg = tr(
87 "You are about to launch {0} browser windows.<br>"
88 + "This may both clutter your screen with browser windows<br>"
89 + "and take some time to finish.", numBrowsers);
90 msg = "<html>" + msg + "</html>";
91 ButtonSpec[] spec = new ButtonSpec[] {
92 new ButtonSpec(
93 tr("Continue"),
94 ImageProvider.get("ok"),
95 tr("Click to continue and to open {0} browsers", numBrowsers),
96 null // no specific help topic
97 ),
98 new ButtonSpec(
99 tr("Cancel"),
100 ImageProvider.get("cancel"),
101 tr("Click to abort launching external browsers"),
102 null // no specific help topic
103 )
104 };
105 int ret = HelpAwareOptionPane.showOptionDialog(
106 Main.parent,
107 msg,
108 tr("Warning"),
109 JOptionPane.WARNING_MESSAGE,
110 null,
111 spec,
112 spec[0],
113 HelpUtil.ht("/WarningMessages#ToManyBrowsersToOpen")
114 );
115 return ret == 0;
116 }
117
118 protected void launchInfoBrowsersForSelectedPrimitives() {
119 ArrayList<OsmPrimitive> primitivesToShow = new ArrayList<OsmPrimitive>(getCurrentDataSet().getAllSelected());
120
121 // filter out new primitives which are not yet uploaded to the server
122 //
123 Iterator<OsmPrimitive> it = primitivesToShow.iterator();
124 while(it.hasNext()) {
125 if (it.next().isNew()) {
126 it.remove();
127 }
128 }
129
130 if (primitivesToShow.isEmpty()) {
131 JOptionPane.showMessageDialog(
132 Main.parent,
133 tr("Please select at least one already uploaded node, way, or relation."),
134 tr("Warning"),
135 JOptionPane.WARNING_MESSAGE
136 );
137 return;
138 }
139
140 // don't launch more than 10 browser instances / browser windows
141 //
142 int max = Math.min(10, primitivesToShow.size());
143 if (primitivesToShow.size() > max && ! confirmLaunchMultiple(primitivesToShow.size()))
144 return;
145 for(int i = 0; i < max; i++) {
146 launchBrowser(createInfoUrl(primitivesToShow.get(i)));
147 }
148 }
149
150 @Override
151 public void actionPerformed(ActionEvent e) {
152 launchInfoBrowsersForSelectedPrimitives();
153 }
154
155 protected abstract String createInfoUrl(Object infoObject);
156
157 @Override
158 protected void updateEnabledState() {
159 setEnabled(getCurrentDataSet() != null && !getCurrentDataSet().getSelected().isEmpty());
160 }
161
162 @Override
163 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
164 setEnabled(selection != null && !selection.isEmpty());
165 }
166}
Note: See TracBrowser for help on using the repository browser.