From 3ab89564e81be0e0ddcd8b00a55e52c7b7558a75 Mon Sep 17 00:00:00 2001 From: Taylor Smock Date: Tue, 9 Jul 2024 14:44:48 -0600 Subject: [PATCH] Add shortcuts to actions in 'Image info' dialog Signed-off-by: Taylor Smock --- .../imageinfo/AddTagToPrimitiveAction.java | 40 ++++++++++--------- .../gui/imageinfo/ClipboardAction.java | 30 ++++++++++---- .../gui/imageinfo/ImageInfoPanel.java | 7 ++-- .../gui/imageinfo/MapillaryAction.java | 19 +++++++++ .../gui/imageinfo/WebLinkAction.java | 18 ++++++--- 5 files changed, 80 insertions(+), 34 deletions(-) create mode 100644 src/main/java/org/openstreetmap/josm/plugins/mapillary/gui/imageinfo/MapillaryAction.java diff --git a/src/main/java/org/openstreetmap/josm/plugins/mapillary/gui/imageinfo/AddTagToPrimitiveAction.java b/src/main/java/org/openstreetmap/josm/plugins/mapillary/gui/imageinfo/AddTagToPrimitiveAction.java index 3c93d44ee..b8ff50be9 100644 --- a/src/main/java/org/openstreetmap/josm/plugins/mapillary/gui/imageinfo/AddTagToPrimitiveAction.java +++ b/src/main/java/org/openstreetmap/josm/plugins/mapillary/gui/imageinfo/AddTagToPrimitiveAction.java @@ -1,9 +1,12 @@ // License: GPL. For details, see LICENSE file. package org.openstreetmap.josm.plugins.mapillary.gui.imageinfo; +import static org.openstreetmap.josm.tools.I18n.tr; + import java.awt.event.ActionEvent; +import java.awt.event.KeyEvent; +import java.io.Serial; -import javax.swing.AbstractAction; import javax.swing.JOptionPane; import org.openstreetmap.josm.command.ChangePropertyCommand; @@ -14,11 +17,13 @@ import org.openstreetmap.josm.gui.MainApplication; import org.openstreetmap.josm.gui.layer.geoimage.ImageViewerDialog; import org.openstreetmap.josm.plugins.mapillary.gui.layer.geoimage.MapillaryImageEntry; -import org.openstreetmap.josm.tools.I18n; -import org.openstreetmap.josm.tools.ImageProvider; -import org.openstreetmap.josm.tools.ImageProvider.ImageSizes; +import org.openstreetmap.josm.tools.Shortcut; -public class AddTagToPrimitiveAction extends AbstractAction { +/** + * Add the mapillary tag to a primitive + */ +public class AddTagToPrimitiveAction extends MapillaryAction { + @Serial private static final long serialVersionUID = 4834918715956633953L; private Tag tag; @@ -30,7 +35,10 @@ public class AddTagToPrimitiveAction extends AbstractAction { * @param name The name to use */ public AddTagToPrimitiveAction(final String name) { - super(name, ImageProvider.get("dialogs/add", ImageSizes.SMALLICON)); + super(name, "dialogs/add", tr("Add the mapillary source tag to the selected primitive"), + Shortcut.registerShortcut("mapillary:add_tag_to_primitive", tr("Mapillary: Add Tag to Primitive"), + KeyEvent.CHAR_UNDEFINED, Shortcut.NONE), + false, "mapillary:add_tag_to_primitive", false); this.updateEnabled(); } @@ -63,21 +71,17 @@ public void actionPerformed(ActionEvent e) { if (target != null && tag != null) { int conflictResolution = JOptionPane.YES_OPTION; if (target.hasKey(tag.getKey()) && !target.hasTag(tag.getKey(), tag.getValue())) { - conflictResolution = JOptionPane.showConfirmDialog(MainApplication.getMainFrame(), - "" - + I18n.tr( - "A tag with key {0} is already present on the selected OSM object.", tag.getKey()) - + "
" - + I18n.tr( - "Do you really want to replace the current value {0} with the new value {1}?", - target.get(tag.getKey()), tag.getValue()) - + "", - I18n.tr("Tag conflict"), JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE); + conflictResolution = JOptionPane.showConfirmDialog(MainApplication.getMainFrame(), "" + + tr("A tag with key {0} is already present on the selected OSM object.", tag.getKey()) + + "
" + + tr("Do you really want to replace the current value {0} with the new value {1}?", + target.get(tag.getKey()), tag.getValue()) + + "", tr("Tag conflict"), JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE); } if (JOptionPane.YES_OPTION == conflictResolution) { - if (target instanceof OsmPrimitive) { + if (target instanceof OsmPrimitive primitive) { UndoRedoHandler.getInstance() - .add(new ChangePropertyCommand((OsmPrimitive) target, tag.getKey(), tag.getValue())); + .add(new ChangePropertyCommand(primitive, tag.getKey(), tag.getValue())); } else { target.put(tag); target.setModified(true); diff --git a/src/main/java/org/openstreetmap/josm/plugins/mapillary/gui/imageinfo/ClipboardAction.java b/src/main/java/org/openstreetmap/josm/plugins/mapillary/gui/imageinfo/ClipboardAction.java index e399667c9..cb05cb898 100644 --- a/src/main/java/org/openstreetmap/josm/plugins/mapillary/gui/imageinfo/ClipboardAction.java +++ b/src/main/java/org/openstreetmap/josm/plugins/mapillary/gui/imageinfo/ClipboardAction.java @@ -1,16 +1,18 @@ // License: GPL. For details, see LICENSE file. package org.openstreetmap.josm.plugins.mapillary.gui.imageinfo; +import static org.openstreetmap.josm.tools.I18n.tr; + import java.awt.Color; import java.awt.Component; import java.awt.FlowLayout; -import java.awt.Toolkit; import java.awt.datatransfer.Transferable; import java.awt.event.ActionEvent; +import java.awt.event.KeyEvent; +import java.io.Serial; import java.util.Timer; import java.util.TimerTask; -import javax.swing.AbstractAction; import javax.swing.Action; import javax.swing.JComponent; import javax.swing.JLabel; @@ -18,12 +20,16 @@ import javax.swing.Popup; import javax.swing.PopupFactory; +import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils; import org.openstreetmap.josm.gui.util.GuiHelper; import org.openstreetmap.josm.plugins.mapillary.utils.MapillaryColorScheme; -import org.openstreetmap.josm.tools.ImageProvider; -import org.openstreetmap.josm.tools.ImageProvider.ImageSizes; +import org.openstreetmap.josm.tools.Shortcut; -public class ClipboardAction extends AbstractAction { +/** + * An action for copying items to the clipboard + */ +public class ClipboardAction extends MapillaryAction { + @Serial private static final long serialVersionUID = 3323536079627210533L; /** * The duration in milliseconds for which the popup will be shown @@ -48,8 +54,18 @@ public class ClipboardAction extends AbstractAction { */ private Transferable contents; + /** + * Create a new action for clipboards + * + * @param name The name of the action (use {@link org.openstreetmap.josm.tools.I18n#marktr(String)}) + * @param successMessage The message for success + * @param contents The initial contents to use + */ public ClipboardAction(final String name, final String successMessage, final Transferable contents) { - super(name, ImageProvider.get("copy", ImageSizes.SMALLICON)); + super(tr(name), "copy", tr("Copy {0} to clipboard", tr(name)), + Shortcut.registerShortcut("mapillary:copy_to_clipboard_" + name.replace(' ', '_'), + tr("Mapillary: {0}", tr(name)), KeyEvent.CHAR_UNDEFINED, Shortcut.NONE), + false, "mapillary:copy_to_clipboard_" + name.replace(' ', '_'), false); this.contents = contents; // Init popup @@ -88,7 +104,7 @@ public void setPopupParent(Component popupParent) { @Override public void actionPerformed(ActionEvent e) { if (contents != null) { - Toolkit.getDefaultToolkit().getSystemClipboard().setContents(contents, null); + ClipboardUtils.copy(contents); if (popupParent != null && lastCopyTime + POPUP_DURATION < System.currentTimeMillis()) { final PopupFactory popupFactory = new PopupFactory(); final Popup popup = popupFactory.getPopup(popupParent, popupContent, diff --git a/src/main/java/org/openstreetmap/josm/plugins/mapillary/gui/imageinfo/ImageInfoPanel.java b/src/main/java/org/openstreetmap/josm/plugins/mapillary/gui/imageinfo/ImageInfoPanel.java index 7d4989d7a..0302ec6de 100644 --- a/src/main/java/org/openstreetmap/josm/plugins/mapillary/gui/imageinfo/ImageInfoPanel.java +++ b/src/main/java/org/openstreetmap/josm/plugins/mapillary/gui/imageinfo/ImageInfoPanel.java @@ -1,6 +1,7 @@ // License: GPL. For details, see LICENSE file. package org.openstreetmap.josm.plugins.mapillary.gui.imageinfo; +import static org.openstreetmap.josm.tools.I18n.marktr; import static org.openstreetmap.josm.tools.I18n.tr; import java.awt.Font; @@ -119,13 +120,13 @@ private ImageInfoPanel() { imgKeyValue = new HtmlPanel(); - imgLinkAction = new WebLinkAction(tr("View in browser"), null); + imgLinkAction = new WebLinkAction(marktr("View in browser"), null); - copyImgUrlAction = new ClipboardAction(tr("Copy URL"), tr("Copied URL to clipboard …"), null); + copyImgUrlAction = new ClipboardAction(marktr("Copy URL"), tr("Copied URL to clipboard …"), null); final var copyUrlButton = new MapillaryButton(copyImgUrlAction, true); copyImgUrlAction.setPopupParent(copyUrlButton); - copyImgKeyAction = new ClipboardAction(tr("Copy key"), tr("Copied key to clipboard …"), null); + copyImgKeyAction = new ClipboardAction(marktr("Copy key"), tr("Copied key to clipboard …"), null); final var copyKeyButton = new MapillaryButton(copyImgKeyAction, true); copyImgKeyAction.setPopupParent(copyKeyButton); diff --git a/src/main/java/org/openstreetmap/josm/plugins/mapillary/gui/imageinfo/MapillaryAction.java b/src/main/java/org/openstreetmap/josm/plugins/mapillary/gui/imageinfo/MapillaryAction.java new file mode 100644 index 000000000..b6b02b73a --- /dev/null +++ b/src/main/java/org/openstreetmap/josm/plugins/mapillary/gui/imageinfo/MapillaryAction.java @@ -0,0 +1,19 @@ +// License: GPL. For details, see LICENSE file. +package org.openstreetmap.josm.plugins.mapillary.gui.imageinfo; + +import javax.swing.Action; + +import org.openstreetmap.josm.actions.JosmAction; +import org.openstreetmap.josm.tools.Shortcut; + +/** + * A common class for Mapillary actions + */ +abstract class MapillaryAction extends JosmAction { + MapillaryAction(String name, String icon, String tooltip, Shortcut shortcut, boolean registerInToolbar, + String toolbarId, boolean installAdapters) { + super(name, icon, tooltip, shortcut, registerInToolbar, toolbarId, installAdapters); + // We don't need the large icon, and it messes with spacing in buttons + this.putValue(Action.LARGE_ICON_KEY, null); + } +} diff --git a/src/main/java/org/openstreetmap/josm/plugins/mapillary/gui/imageinfo/WebLinkAction.java b/src/main/java/org/openstreetmap/josm/plugins/mapillary/gui/imageinfo/WebLinkAction.java index b61e24027..4211d133a 100644 --- a/src/main/java/org/openstreetmap/josm/plugins/mapillary/gui/imageinfo/WebLinkAction.java +++ b/src/main/java/org/openstreetmap/josm/plugins/mapillary/gui/imageinfo/WebLinkAction.java @@ -1,24 +1,27 @@ // License: GPL. For details, see LICENSE file. package org.openstreetmap.josm.plugins.mapillary.gui.imageinfo; +import static org.openstreetmap.josm.tools.I18n.tr; + import java.awt.event.ActionEvent; +import java.awt.event.KeyEvent; +import java.io.Serial; import java.net.URI; import java.net.URISyntaxException; -import javax.swing.AbstractAction; import javax.swing.JOptionPane; import org.openstreetmap.josm.gui.Notification; import org.openstreetmap.josm.tools.I18n; -import org.openstreetmap.josm.tools.ImageProvider; -import org.openstreetmap.josm.tools.ImageProvider.ImageSizes; import org.openstreetmap.josm.tools.Logging; import org.openstreetmap.josm.tools.OpenBrowser; +import org.openstreetmap.josm.tools.Shortcut; /** * An action to open web links */ -public class WebLinkAction extends AbstractAction { +public class WebLinkAction extends MapillaryAction { + @Serial private static final long serialVersionUID = 2397830510179013823L; private URI uri; @@ -26,11 +29,14 @@ public class WebLinkAction extends AbstractAction { /** * Create a new action * - * @param name The name to show users + * @param name The name to show users (use {@link I18n#marktr(String)}) * @param uri The original URI to open */ public WebLinkAction(final String name, final URI uri) { - super(name, ImageProvider.get("link", ImageSizes.SMALLICON)); + super(tr(name), "link", tr("Open in browser"), + Shortcut.registerShortcut("mapillary:open_in_browser_" + name.replace(' ', '_'), + tr("Mapillary: Open {0} in browser", tr(name)), KeyEvent.CHAR_UNDEFINED, Shortcut.NONE), + false, "mapillary:open_in_browser_" + name.replace(' ', '_'), false); setURI(uri); }