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.Main; 015import org.openstreetmap.josm.data.notes.Note; 016import org.openstreetmap.josm.data.osm.AbstractPrimitive; 017import org.openstreetmap.josm.data.osm.DataSet; 018import org.openstreetmap.josm.data.osm.OsmPrimitive; 019import org.openstreetmap.josm.gui.HelpAwareOptionPane; 020import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec; 021import org.openstreetmap.josm.gui.help.HelpUtil; 022import org.openstreetmap.josm.tools.ImageProvider; 023import org.openstreetmap.josm.tools.OpenBrowser; 024import org.openstreetmap.josm.tools.Shortcut; 025 026/** 027 * Abstract base class for info actions, opening an URL describing a particular object. 028 * @since 1697 029 */ 030public abstract class AbstractInfoAction extends JosmAction { 031 032 /** 033 * Constructs a new {@code AbstractInfoAction}. 034 * @param installAdapters false, if you don't want to install layer changed and selection changed adapters 035 */ 036 public AbstractInfoAction(boolean installAdapters) { 037 super(installAdapters); 038 } 039 040 /** 041 * Constructs a new {@code AbstractInfoAction}. 042 * @param name the action's text as displayed on the menu (if it is added to a menu) 043 * @param iconName the filename of the icon to use 044 * @param tooltip a longer description of the action that will be displayed in the tooltip. Please note 045 * that html is not supported for menu actions on some platforms. 046 * @param shortcut a ready-created shortcut object or null if you don't want a shortcut. But you always 047 * do want a shortcut, remember you can always register it with group=none, so you 048 * won't be assigned a shortcut unless the user configures one. If you pass null here, 049 * the user CANNOT configure a shortcut for your action. 050 * @param register register this action for the toolbar preferences? 051 * @param toolbarId identifier for the toolbar preferences. The iconName is used, if this parameter is null 052 * @param installAdapters false, if you don't want to install layer changed and selection changed adapters 053 */ 054 public AbstractInfoAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean register, 055 String toolbarId, boolean installAdapters) { 056 super(name, iconName, tooltip, shortcut, register, toolbarId, installAdapters); 057 } 058 059 /** 060 * Asks user confirmation before launching a large number of browser windows. 061 * @param numBrowsers the number of browser windows to open 062 * @return {@code true} if the user confirms, {@code false} otherwise 063 */ 064 public static boolean confirmLaunchMultiple(int numBrowsers) { 065 String msg = /* for correct i18n of plural forms - see #9110 */ trn( 066 "You are about to launch {0} browser window.<br>" 067 + "This may both clutter your screen with browser windows<br>" 068 + "and take some time to finish.", 069 "You are about to launch {0} browser windows.<br>" 070 + "This may both clutter your screen with browser windows<br>" 071 + "and take some time to finish.", numBrowsers, numBrowsers); 072 msg = "<html>" + msg + "</html>"; 073 ButtonSpec[] spec = new ButtonSpec[] { 074 new ButtonSpec( 075 tr("Continue"), 076 ImageProvider.get("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 ImageProvider.get("cancel"), 084 tr("Click to abort launching external browsers"), 085 null // no specific help topic 086 ) 087 }; 088 return 0 == HelpAwareOptionPane.showOptionDialog( 089 Main.parent, 090 msg, 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<OsmPrimitive> primitivesToShow = new ArrayList<>(); 102 DataSet ds = getLayerManager().getEditDataSet(); 103 if (ds != null) { 104 primitivesToShow.addAll(ds.getAllSelected()); 105 } 106 107 Note noteToShow = Main.isDisplayingMapView() ? Main.map.noteDialog.getSelectedNote() : null; 108 109 // filter out new primitives which are not yet uploaded to the server 110 // 111 primitivesToShow.removeIf(AbstractPrimitive::isNew); 112 113 if (primitivesToShow.isEmpty() && noteToShow == null) { 114 JOptionPane.showMessageDialog( 115 Main.parent, 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 Main.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 DataSet ds = getLayerManager().getEditDataSet(); 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}