source: josm/trunk/src/org/openstreetmap/josm/actions/OpenBrowserAction.java@ 17335

Last change on this file since 17335 was 16839, checked in by simon04, 4 years ago

fix #19622 - Tag2Link: show icons based on presets/styles

File size: 4.6 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.io.Serializable;
9import java.util.ArrayList;
10import java.util.List;
11
12import javax.swing.AbstractAction;
13import javax.swing.JOptionPane;
14
15import org.openstreetmap.josm.gui.HelpAwareOptionPane;
16import org.openstreetmap.josm.gui.MainApplication;
17import org.openstreetmap.josm.gui.help.HelpUtil;
18import org.openstreetmap.josm.spi.preferences.Config;
19import org.openstreetmap.josm.tools.ImageProvider;
20import org.openstreetmap.josm.tools.ImageResource;
21import org.openstreetmap.josm.tools.OpenBrowser;
22import org.openstreetmap.josm.tools.Utils;
23
24/**
25 * Action to open browser on given URL.
26 * @see OpenBrowser
27 * @since 15706
28 */
29public class OpenBrowserAction extends AbstractAction {
30
31 private final List<String> urls = new ArrayList<>();
32 private final String originalName;
33
34 /**
35 * Constructs a new {@link OpenBrowserAction}.
36 * @param name the name of this action
37 * @param url the URL to launch
38 */
39 public OpenBrowserAction(String name, String url) {
40 this(name, url, null);
41 }
42
43 /**
44 * Constructs a new {@link OpenBrowserAction}.
45 * @param name the name of this action
46 * @param url the URL to launch
47 * @param icon the action icon
48 * @since 16839
49 */
50 public OpenBrowserAction(String name, String url, ImageResource icon) {
51 if (icon == null) {
52 new ImageProvider("help/internet").getResource().attachImageIcon(this, true);
53 } else {
54 icon.attachImageIcon(this, true);
55 }
56 this.urls.add(url);
57 this.originalName = name;
58 updateNameAndDescription();
59 }
60
61 /**
62 * Adds an additional URL to be launched.
63 * @param url the URL to launch
64 */
65 public void addUrl(String url) {
66 urls.add(url);
67 updateNameAndDescription();
68 }
69
70 private void updateNameAndDescription() {
71 final Serializable countString = urls.size() > 1 ? tr(" ({0})", urls.size()) : "";
72 putValue(NAME, originalName + countString);
73 putValue(SHORT_DESCRIPTION, Utils.shortenString(tr("Open {0}", String.join(", ", urls)), 256));
74
75 }
76
77 @Override
78 public void actionPerformed(ActionEvent e) {
79 final int size = urls.size();
80 if (size > Config.getPref().getInt("warn.open.maxbrowser", 10) && !confirmLaunchMultiple(size)) {
81 return;
82 }
83 for (String url : urls) {
84 OpenBrowser.displayUrl(url);
85 }
86 }
87
88 /**
89 * Asks user confirmation before launching a large number of browser windows.
90 * @param numBrowsers the number of browser windows to open
91 * @return {@code true} if the user confirms, {@code false} otherwise
92 */
93 public static boolean confirmLaunchMultiple(int numBrowsers) {
94 String msg = /* for correct i18n of plural forms - see #9110 */ trn(
95 "You are about to launch {0} browser window.<br>"
96 + "This may both clutter your screen with browser windows<br>"
97 + "and take some time to finish.",
98 "You are about to launch {0} browser windows.<br>"
99 + "This may both clutter your screen with browser windows<br>"
100 + "and take some time to finish.", numBrowsers, numBrowsers);
101 HelpAwareOptionPane.ButtonSpec[] spec = {
102 new HelpAwareOptionPane.ButtonSpec(
103 tr("Continue"),
104 new ImageProvider("ok"),
105 trn("Click to continue and to open {0} browser", "Click to continue and to open {0} browsers",
106 numBrowsers, numBrowsers),
107 null // no specific help topic
108 ),
109 new HelpAwareOptionPane.ButtonSpec(
110 tr("Cancel"),
111 new ImageProvider("cancel"),
112 tr("Click to abort launching external browsers"),
113 null // no specific help topic
114 )
115 };
116 return 0 == HelpAwareOptionPane.showOptionDialog(
117 MainApplication.getMainFrame(),
118 new StringBuilder(msg).insert(0, "<html>").append("</html>").toString(),
119 tr("Warning"),
120 JOptionPane.WARNING_MESSAGE,
121 null,
122 spec,
123 spec[0],
124 HelpUtil.ht("/WarningMessages#ToManyBrowsersToOpen")
125 );
126 }
127}
Note: See TracBrowser for help on using the repository browser.