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

Last change on this file since 7017 was 7005, checked in by Don-vip, 10 years ago

see #8465 - use diamond operator where applicable

  • Property svn:eol-style set to native
File size: 6.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;
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;
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. https://www.openstreetmap.org
40 */
41 public static 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("");
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 for (String prefix : new String[]{"http://api.openstreetmap.org/", "https://api.openstreetmap.org/"}) {
49 if (ret.startsWith(prefix)) {
50 ret = Main.getOSMWebsite() + "/" + ret.substring(prefix.length());
51 break;
52 }
53 }
54 return ret;
55 }
56
57 /**
58 * Replies the base URL for browsing information about a user.
59 *
60 * @return the base URL, i.e. https://www.openstreetmap.org/user
61 */
62 public static String getBaseUserUrl() {
63 String baseUrl = Main.pref.get("osm-server.url", OsmApi.DEFAULT_API_URL);
64 Pattern pattern = Pattern.compile("/api/?$");
65 String ret = pattern.matcher(baseUrl).replaceAll("/user");
66 if (ret.equals(baseUrl)) {
67 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));
68 }
69 for (String prefix : new String[]{"http://api.openstreetmap.org/", "https://api.openstreetmap.org/"}) {
70 if (ret.startsWith(prefix)) {
71 ret = Main.getOSMWebsite() + "/" + ret.substring(prefix.length());
72 break;
73 }
74 }
75 return ret;
76 }
77
78 public static boolean confirmLaunchMultiple(int numBrowsers) {
79 String msg = /* for correct i18n of plural forms - see #9110 */ trn(
80 "You are about to launch {0} browser window.<br>"
81 + "This may both clutter your screen with browser windows<br>"
82 + "and take some time to finish.",
83 "You are about to launch {0} browser windows.<br>"
84 + "This may both clutter your screen with browser windows<br>"
85 + "and take some time to finish.", numBrowsers, numBrowsers);
86 msg = "<html>" + msg + "</html>";
87 ButtonSpec[] spec = new ButtonSpec[] {
88 new ButtonSpec(
89 tr("Continue"),
90 ImageProvider.get("ok"),
91 trn("Click to continue and to open {0} browser", "Click to continue and to open {0} browsers", numBrowsers, numBrowsers),
92 null // no specific help topic
93 ),
94 new ButtonSpec(
95 tr("Cancel"),
96 ImageProvider.get("cancel"),
97 tr("Click to abort launching external browsers"),
98 null // no specific help topic
99 )
100 };
101 int ret = HelpAwareOptionPane.showOptionDialog(
102 Main.parent,
103 msg,
104 tr("Warning"),
105 JOptionPane.WARNING_MESSAGE,
106 null,
107 spec,
108 spec[0],
109 HelpUtil.ht("/WarningMessages#ToManyBrowsersToOpen")
110 );
111 return ret == 0;
112 }
113
114 protected void launchInfoBrowsersForSelectedPrimitives() {
115 List<OsmPrimitive> primitivesToShow = new ArrayList<>(getCurrentDataSet().getAllSelected());
116
117 // filter out new primitives which are not yet uploaded to the server
118 //
119 Iterator<OsmPrimitive> it = primitivesToShow.iterator();
120 while(it.hasNext()) {
121 if (it.next().isNew()) {
122 it.remove();
123 }
124 }
125
126 if (primitivesToShow.isEmpty()) {
127 JOptionPane.showMessageDialog(
128 Main.parent,
129 tr("Please select at least one already uploaded node, way, or relation."),
130 tr("Warning"),
131 JOptionPane.WARNING_MESSAGE
132 );
133 return;
134 }
135
136 // don't launch more than 10 browser instances / browser windows
137 //
138 int max = Math.min(10, primitivesToShow.size());
139 if (primitivesToShow.size() > max && ! confirmLaunchMultiple(primitivesToShow.size()))
140 return;
141 for(int i = 0; i < max; i++) {
142 OpenBrowser.displayUrl(createInfoUrl(primitivesToShow.get(i)));
143 }
144 }
145
146 @Override
147 public void actionPerformed(ActionEvent e) {
148 launchInfoBrowsersForSelectedPrimitives();
149 }
150
151 protected abstract String createInfoUrl(Object infoObject);
152
153 @Override
154 protected void updateEnabledState() {
155 setEnabled(getCurrentDataSet() != null && !getCurrentDataSet().getSelected().isEmpty());
156 }
157
158 @Override
159 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
160 setEnabled(selection != null && !selection.isEmpty());
161 }
162}
Note: See TracBrowser for help on using the repository browser.