001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.actions; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005import static org.openstreetmap.josm.tools.I18n.trn; 006 007import java.awt.event.ActionEvent; 008import java.util.ArrayList; 009import java.util.Collection; 010import java.util.Iterator; 011import java.util.List; 012 013import javax.swing.JOptionPane; 014 015import org.openstreetmap.josm.Main; 016import org.openstreetmap.josm.data.notes.Note; 017import org.openstreetmap.josm.data.osm.OsmPrimitive; 018import org.openstreetmap.josm.gui.HelpAwareOptionPane; 019import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec; 020import org.openstreetmap.josm.gui.help.HelpUtil; 021import org.openstreetmap.josm.tools.ImageProvider; 022import org.openstreetmap.josm.tools.OpenBrowser; 023import org.openstreetmap.josm.tools.Shortcut; 024 025/** 026 * Abstract base class for info actions, opening an URL describing a particular object. 027 * @since 1697 028 */ 029public abstract class AbstractInfoAction extends JosmAction { 030 031 /** 032 * Constructs a new {@code AbstractInfoAction}. 033 * @param installAdapters false, if you don't want to install layer changed and selection changed adapters 034 */ 035 public AbstractInfoAction(boolean installAdapters) { 036 super(installAdapters); 037 } 038 039 /** 040 * Constructs a new {@code AbstractInfoAction}. 041 * @param name the action's text as displayed on the menu (if it is added to a menu) 042 * @param iconName the filename of the icon to use 043 * @param tooltip a longer description of the action that will be displayed in the tooltip. Please note 044 * that html is not supported for menu actions on some platforms. 045 * @param shortcut a ready-created shortcut object or null if you don't want a shortcut. But you always 046 * do want a shortcut, remember you can always register it with group=none, so you 047 * won't be assigned a shortcut unless the user configures one. If you pass null here, 048 * the user CANNOT configure a shortcut for your action. 049 * @param register register this action for the toolbar preferences? 050 * @param toolbarId identifier for the toolbar preferences. The iconName is used, if this parameter is null 051 * @param installAdapters false, if you don't want to install layer changed and selection changed adapters 052 */ 053 public AbstractInfoAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean register, 054 String toolbarId, boolean installAdapters) { 055 super(name, iconName, tooltip, shortcut, register, toolbarId, installAdapters); 056 } 057 058 /** 059 * Asks user confirmation before launching a large number of browser windows. 060 * @param numBrowsers the number of browser windows to open 061 * @return {@code true} if the user confirms, {@code false} otherwise 062 */ 063 public static boolean confirmLaunchMultiple(int numBrowsers) { 064 String msg = /* for correct i18n of plural forms - see #9110 */ trn( 065 "You are about to launch {0} browser window.<br>" 066 + "This may both clutter your screen with browser windows<br>" 067 + "and take some time to finish.", 068 "You are about to launch {0} browser windows.<br>" 069 + "This may both clutter your screen with browser windows<br>" 070 + "and take some time to finish.", numBrowsers, numBrowsers); 071 msg = "<html>" + msg + "</html>"; 072 ButtonSpec[] spec = new ButtonSpec[] { 073 new ButtonSpec( 074 tr("Continue"), 075 ImageProvider.get("ok"), 076 trn("Click to continue and to open {0} browser", "Click to continue and to open {0} browsers", 077 numBrowsers, numBrowsers), 078 null // no specific help topic 079 ), 080 new ButtonSpec( 081 tr("Cancel"), 082 ImageProvider.get("cancel"), 083 tr("Click to abort launching external browsers"), 084 null // no specific help topic 085 ) 086 }; 087 int ret = HelpAwareOptionPane.showOptionDialog( 088 Main.parent, 089 msg, 090 tr("Warning"), 091 JOptionPane.WARNING_MESSAGE, 092 null, 093 spec, 094 spec[0], 095 HelpUtil.ht("/WarningMessages#ToManyBrowsersToOpen") 096 ); 097 return ret == 0; 098 } 099 100 protected void launchInfoBrowsersForSelectedPrimitivesAndNote() { 101 List<OsmPrimitive> primitivesToShow = new ArrayList<>(); 102 if (getCurrentDataSet() != null) { 103 primitivesToShow.addAll(getCurrentDataSet().getAllSelected()); 104 } 105 106 Note noteToShow = Main.isDisplayingMapView() ? Main.map.noteDialog.getSelectedNote() : null; 107 108 // filter out new primitives which are not yet uploaded to the server 109 // 110 Iterator<OsmPrimitive> it = primitivesToShow.iterator(); 111 while (it.hasNext()) { 112 if (it.next().isNew()) { 113 it.remove(); 114 } 115 } 116 117 if (primitivesToShow.isEmpty() && noteToShow == null) { 118 JOptionPane.showMessageDialog( 119 Main.parent, 120 tr("Please select at least one already uploaded node, way, or relation."), 121 tr("Warning"), 122 JOptionPane.WARNING_MESSAGE 123 ); 124 return; 125 } 126 127 // don't launch more than 10 browser instances / browser windows 128 // 129 int max = Math.min(10, primitivesToShow.size()); 130 if (primitivesToShow.size() > max && !confirmLaunchMultiple(primitivesToShow.size())) 131 return; 132 for (int i = 0; i < max; i++) { 133 launchInfoBrowser(primitivesToShow.get(i)); 134 } 135 136 if (noteToShow != null) { 137 launchInfoBrowser(noteToShow); 138 } 139 } 140 141 protected final void launchInfoBrowser(Object o) { 142 String url = createInfoUrl(o); 143 if (url != null) { 144 String result = OpenBrowser.displayUrl(url); 145 if (result != null) { 146 Main.warn(result); 147 } 148 } 149 } 150 151 @Override 152 public void actionPerformed(ActionEvent e) { 153 launchInfoBrowsersForSelectedPrimitivesAndNote(); 154 } 155 156 protected abstract String createInfoUrl(Object infoObject); 157 158 @Override 159 protected void updateEnabledState() { 160 setEnabled(getCurrentDataSet() != null && !getCurrentDataSet().getSelected().isEmpty()); 161 } 162 163 @Override 164 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) { 165 setEnabled(selection != null && !selection.isEmpty()); 166 } 167}