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