1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
6 |
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.util.ArrayList;
|
---|
9 | import java.util.Collection;
|
---|
10 | import java.util.Iterator;
|
---|
11 | import java.util.List;
|
---|
12 | import java.util.regex.Pattern;
|
---|
13 |
|
---|
14 | import javax.swing.JOptionPane;
|
---|
15 |
|
---|
16 | import org.openstreetmap.josm.Main;
|
---|
17 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
18 | import org.openstreetmap.josm.gui.HelpAwareOptionPane;
|
---|
19 | import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;
|
---|
20 | import org.openstreetmap.josm.gui.help.HelpUtil;
|
---|
21 | import org.openstreetmap.josm.io.OsmApi;
|
---|
22 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
23 | import org.openstreetmap.josm.tools.OpenBrowser;
|
---|
24 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
25 |
|
---|
26 | public 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 = Main.OSM_WEBSITE + "/" + 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 = Main.OSM_WEBSITE + "/" + ret;
|
---|
70 | }
|
---|
71 | return ret;
|
---|
72 | }
|
---|
73 |
|
---|
74 | public static boolean confirmLaunchMultiple(int numBrowsers) {
|
---|
75 | String msg = /* for correct i18n of plural forms - see #9110 */ trn(
|
---|
76 | "You are about to launch {0} browser window.<br>"
|
---|
77 | + "This may both clutter your screen with browser windows<br>"
|
---|
78 | + "and take some time to finish.",
|
---|
79 | "You are about to launch {0} browser windows.<br>"
|
---|
80 | + "This may both clutter your screen with browser windows<br>"
|
---|
81 | + "and take some time to finish.", numBrowsers, numBrowsers);
|
---|
82 | msg = "<html>" + msg + "</html>";
|
---|
83 | ButtonSpec[] spec = new ButtonSpec[] {
|
---|
84 | new ButtonSpec(
|
---|
85 | tr("Continue"),
|
---|
86 | ImageProvider.get("ok"),
|
---|
87 | trn("Click to continue and to open {0} browser", "Click to continue and to open {0} browsers", numBrowsers, numBrowsers),
|
---|
88 | null // no specific help topic
|
---|
89 | ),
|
---|
90 | new ButtonSpec(
|
---|
91 | tr("Cancel"),
|
---|
92 | ImageProvider.get("cancel"),
|
---|
93 | tr("Click to abort launching external browsers"),
|
---|
94 | null // no specific help topic
|
---|
95 | )
|
---|
96 | };
|
---|
97 | int ret = HelpAwareOptionPane.showOptionDialog(
|
---|
98 | Main.parent,
|
---|
99 | msg,
|
---|
100 | tr("Warning"),
|
---|
101 | JOptionPane.WARNING_MESSAGE,
|
---|
102 | null,
|
---|
103 | spec,
|
---|
104 | spec[0],
|
---|
105 | HelpUtil.ht("/WarningMessages#ToManyBrowsersToOpen")
|
---|
106 | );
|
---|
107 | return ret == 0;
|
---|
108 | }
|
---|
109 |
|
---|
110 | protected void launchInfoBrowsersForSelectedPrimitives() {
|
---|
111 | List<OsmPrimitive> primitivesToShow = new ArrayList<OsmPrimitive>(getCurrentDataSet().getAllSelected());
|
---|
112 |
|
---|
113 | // filter out new primitives which are not yet uploaded to the server
|
---|
114 | //
|
---|
115 | Iterator<OsmPrimitive> it = primitivesToShow.iterator();
|
---|
116 | while(it.hasNext()) {
|
---|
117 | if (it.next().isNew()) {
|
---|
118 | it.remove();
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | if (primitivesToShow.isEmpty()) {
|
---|
123 | JOptionPane.showMessageDialog(
|
---|
124 | Main.parent,
|
---|
125 | tr("Please select at least one already uploaded node, way, or relation."),
|
---|
126 | tr("Warning"),
|
---|
127 | JOptionPane.WARNING_MESSAGE
|
---|
128 | );
|
---|
129 | return;
|
---|
130 | }
|
---|
131 |
|
---|
132 | // don't launch more than 10 browser instances / browser windows
|
---|
133 | //
|
---|
134 | int max = Math.min(10, primitivesToShow.size());
|
---|
135 | if (primitivesToShow.size() > max && ! confirmLaunchMultiple(primitivesToShow.size()))
|
---|
136 | return;
|
---|
137 | for(int i = 0; i < max; i++) {
|
---|
138 | OpenBrowser.displayUrl(createInfoUrl(primitivesToShow.get(i)));
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | @Override
|
---|
143 | public void actionPerformed(ActionEvent e) {
|
---|
144 | launchInfoBrowsersForSelectedPrimitives();
|
---|
145 | }
|
---|
146 |
|
---|
147 | protected abstract String createInfoUrl(Object infoObject);
|
---|
148 |
|
---|
149 | @Override
|
---|
150 | protected void updateEnabledState() {
|
---|
151 | setEnabled(getCurrentDataSet() != null && !getCurrentDataSet().getSelected().isEmpty());
|
---|
152 | }
|
---|
153 |
|
---|
154 | @Override
|
---|
155 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
156 | setEnabled(selection != null && !selection.isEmpty());
|
---|
157 | }
|
---|
158 | }
|
---|