001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.actions; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.awt.event.ActionEvent; 007import java.util.ArrayList; 008import java.util.Collection; 009import java.util.List; 010 011import javax.swing.JOptionPane; 012 013import org.openstreetmap.josm.data.notes.Note; 014import org.openstreetmap.josm.data.osm.IPrimitive; 015import org.openstreetmap.josm.data.osm.OsmData; 016import org.openstreetmap.josm.data.osm.OsmPrimitive; 017import org.openstreetmap.josm.gui.MainApplication; 018import org.openstreetmap.josm.tools.Logging; 019import org.openstreetmap.josm.tools.OpenBrowser; 020import org.openstreetmap.josm.tools.Shortcut; 021 022/** 023 * Abstract base class for info actions, opening an URL describing a particular object. 024 * @since 1697 025 */ 026public abstract class AbstractInfoAction extends JosmAction { 027 028 /** 029 * Constructs a new {@code AbstractInfoAction}. 030 * @param installAdapters false, if you don't want to install layer changed and selection changed adapters 031 */ 032 public AbstractInfoAction(boolean installAdapters) { 033 super(installAdapters); 034 } 035 036 /** 037 * Constructs a new {@code AbstractInfoAction}. 038 * @param name the action's text as displayed on the menu (if it is added to a menu) 039 * @param iconName the filename of the icon to use 040 * @param tooltip a longer description of the action that will be displayed in the tooltip. Please note 041 * that html is not supported for menu actions on some platforms. 042 * @param shortcut a ready-created shortcut object or null if you don't want a shortcut. But you always 043 * do want a shortcut, remember you can always register it with group=none, so you 044 * won't be assigned a shortcut unless the user configures one. If you pass null here, 045 * the user CANNOT configure a shortcut for your action. 046 * @param register register this action for the toolbar preferences? 047 * @param toolbarId identifier for the toolbar preferences. The iconName is used, if this parameter is null 048 * @param installAdapters false, if you don't want to install layer changed and selection changed adapters 049 */ 050 public AbstractInfoAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean register, 051 String toolbarId, boolean installAdapters) { 052 super(name, iconName, tooltip, shortcut, register, toolbarId, installAdapters); 053 } 054 055 /** 056 * Asks user confirmation before launching a large number of browser windows. 057 * @param numBrowsers the number of browser windows to open 058 * @return {@code true} if the user confirms, {@code false} otherwise 059 * @deprecated use {@link OpenBrowserAction#confirmLaunchMultiple(int)} 060 */ 061 @Deprecated 062 public static boolean confirmLaunchMultiple(int numBrowsers) { 063 return OpenBrowserAction.confirmLaunchMultiple(numBrowsers); 064 } 065 066 protected void launchInfoBrowsersForSelectedPrimitivesAndNote() { 067 List<IPrimitive> primitivesToShow = new ArrayList<>(); 068 OsmData<?, ?, ?, ?> ds = getLayerManager().getActiveData(); 069 if (ds != null) { 070 primitivesToShow.addAll(ds.getAllSelected()); 071 } 072 073 Note noteToShow = MainApplication.isDisplayingMapView() ? MainApplication.getMap().noteDialog.getSelectedNote() : null; 074 075 // filter out new primitives which are not yet uploaded to the server 076 // 077 primitivesToShow.removeIf(IPrimitive::isNew); 078 079 if (primitivesToShow.isEmpty() && noteToShow == null) { 080 JOptionPane.showMessageDialog( 081 MainApplication.getMainFrame(), 082 tr("Please select at least one already uploaded node, way, or relation."), 083 tr("Warning"), 084 JOptionPane.WARNING_MESSAGE 085 ); 086 return; 087 } 088 089 // don't launch more than 10 browser instances / browser windows 090 // 091 int max = Math.min(10, primitivesToShow.size()); 092 if (primitivesToShow.size() > max && !OpenBrowserAction.confirmLaunchMultiple(primitivesToShow.size())) 093 return; 094 for (int i = 0; i < max; i++) { 095 launchInfoBrowser(primitivesToShow.get(i)); 096 } 097 098 if (noteToShow != null) { 099 launchInfoBrowser(noteToShow); 100 } 101 } 102 103 protected final void launchInfoBrowser(Object o) { 104 String url = createInfoUrl(o); 105 if (url != null) { 106 String result = OpenBrowser.displayUrl(url); 107 if (result != null) { 108 Logging.warn(result); 109 } 110 } 111 } 112 113 @Override 114 public void actionPerformed(ActionEvent e) { 115 launchInfoBrowsersForSelectedPrimitivesAndNote(); 116 } 117 118 protected abstract String createInfoUrl(Object infoObject); 119 120 @Override 121 protected void updateEnabledState() { 122 OsmData<?, ?, ?, ?> ds = getLayerManager().getActiveData(); 123 setEnabled(ds != null && !ds.selectionEmpty()); 124 } 125 126 @Override 127 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) { 128 setEnabled(selection != null && !selection.isEmpty()); 129 } 130}