diff --git a/src/main/java/net/emustudio/emulib/runtime/interaction/ActionFromEvent.java b/src/main/java/net/emustudio/emulib/runtime/interaction/ActionFromEvent.java new file mode 100644 index 0000000..0ebea4d --- /dev/null +++ b/src/main/java/net/emustudio/emulib/runtime/interaction/ActionFromEvent.java @@ -0,0 +1,44 @@ +/* + * This file is part of emuLib. + * + * Copyright (C) 2006-2023 Peter Jakubčo + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package net.emustudio.emulib.runtime.interaction; + +import javax.swing.*; +import java.awt.event.ActionEvent; +import java.util.function.Consumer; + +import static net.emustudio.emulib.runtime.interaction.GuiUtils.loadIcon; + +public class ActionFromEvent extends AbstractAction { + private final Consumer action; + + public ActionFromEvent(Consumer action, String name, String iconResource, String tooltipText) { + super(name, loadIcon(iconResource)); + putValue(SHORT_DESCRIPTION, tooltipText); + this.action = action; + } + + public ActionFromEvent(Consumer action, String iconResource, String tooltipText) { + this(action, null, iconResource, tooltipText); + } + + @Override + public void actionPerformed(ActionEvent actionEvent) { + action.accept(actionEvent); + } +} diff --git a/src/main/java/net/emustudio/emulib/runtime/interaction/GuiUtils.java b/src/main/java/net/emustudio/emulib/runtime/interaction/GuiUtils.java index 1dd2b53..5595386 100644 --- a/src/main/java/net/emustudio/emulib/runtime/interaction/GuiUtils.java +++ b/src/main/java/net/emustudio/emulib/runtime/interaction/GuiUtils.java @@ -18,10 +18,12 @@ */ package net.emustudio.emulib.runtime.interaction; +import javax.swing.*; import java.awt.*; import java.awt.event.KeyListener; import java.awt.font.TextAttribute; import java.io.InputStream; +import java.net.URL; import java.util.HashMap; import java.util.Map; import java.util.Objects; @@ -69,7 +71,7 @@ public static void removeKeyListener(Component component, KeyListener listener) * @param path Resource path * @param size Default font size * @param resourceClass class where to look for resources - * @return loaded font, or Font.MONOSPACED if the font could not be loaded + * @return loaded font, or Font.MONOSPACED if the font could not be loaded */ public static Font loadFontResource(String path, Class resourceClass, int size) { Map attrs = new HashMap<>(); @@ -86,4 +88,15 @@ public static Font loadFontResource(String path, Class resourceClass, int siz return new Font(Font.MONOSPACED, Font.PLAIN, size); } } + + /** + * Loads an icon from a resource + * + * @param resource resource path + * @return loaded icon, or null if the icon could not be loaded + */ + public static ImageIcon loadIcon(String resource) { + URL url = GuiUtils.class.getResource(resource); + return url == null ? null : new ImageIcon(url); + } } diff --git a/src/main/java/net/emustudio/emulib/runtime/interaction/ToolbarButton.java b/src/main/java/net/emustudio/emulib/runtime/interaction/ToolbarButton.java index 72a04a8..89cc553 100644 --- a/src/main/java/net/emustudio/emulib/runtime/interaction/ToolbarButton.java +++ b/src/main/java/net/emustudio/emulib/runtime/interaction/ToolbarButton.java @@ -28,7 +28,7 @@ * Toolbar button - a button ready to add to a toolbar. * Properties: * - button text is hidden - * - tooltip is set from Action.getValue(SHORT_DESCRIPTION) by default + * - tooltip is set from Action.getValue(SHORT_DESCRIPTION) * - is not focusable * - icon is set from icon resource path * - button action is external @@ -36,22 +36,13 @@ @SuppressWarnings("unused") public class ToolbarButton extends JButton { - public ToolbarButton(Action action, String iconResource) { - super(action); - setHideActionText(true); - setIcon(new ImageIcon(getClass().getResource(iconResource))); - setToolTipText(String.valueOf(action.getValue(SHORT_DESCRIPTION))); - setFocusable(false); - } - - public ToolbarButton(Action action, String iconResource, String tooltipText) { - super(action); - setHideActionText(true); - setIcon(new ImageIcon(getClass().getResource(iconResource))); - setToolTipText(tooltipText); - setFocusable(false); - } - + /** + * Creates a new toolbar button. + *

+ * Tooltip text is set from action.getValue(SHORT_DESCRIPTION). + * + * @param action action to be performed when the button is pressed + */ public ToolbarButton(Action action) { super(action); setHideActionText(true); @@ -59,13 +50,14 @@ public ToolbarButton(Action action) { setFocusable(false); } + /** + * Creates a new toolbar button. + * + * @param action action to be performed when the button is pressed + * @param iconResource icon resource path + * @param tooltipText tooltip text + */ public ToolbarButton(Consumer action, String iconResource, String tooltipText) { - this(new AbstractAction() { - - @Override - public void actionPerformed(ActionEvent actionEvent) { - action.accept(actionEvent); - } - }, iconResource, tooltipText); + this(new ActionFromEvent(action, iconResource, tooltipText)); } } diff --git a/src/main/java/net/emustudio/emulib/runtime/interaction/ToolbarToggleButton.java b/src/main/java/net/emustudio/emulib/runtime/interaction/ToolbarToggleButton.java new file mode 100644 index 0000000..5cd3b7d --- /dev/null +++ b/src/main/java/net/emustudio/emulib/runtime/interaction/ToolbarToggleButton.java @@ -0,0 +1,63 @@ +/* + * This file is part of emuLib. + * + * Copyright (C) 2006-2023 Peter Jakubčo + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package net.emustudio.emulib.runtime.interaction; + +import javax.swing.*; +import java.awt.event.ActionEvent; +import java.util.function.Consumer; + +import static javax.swing.Action.SHORT_DESCRIPTION; + +/** + * Toolbar toggle button - a JToggleButton ready to add to a toolbar. + * Properties: + * - button text is hidden + * - tooltip is set from Action.getValue(SHORT_DESCRIPTION) by default + * - is not focusable + * - icon is set from icon resource path + * - button action is external + */ +@SuppressWarnings("unused") +public class ToolbarToggleButton extends JToggleButton { + + /** + * Creates a new toolbar toggle button. + *

+ * Tooltip text is set from action.getValue(SHORT_DESCRIPTION). + * + * @param action action to be performed when the button is pressed + */ + public ToolbarToggleButton(Action action) { + super(action); + setHideActionText(true); + setToolTipText(String.valueOf(action.getValue(SHORT_DESCRIPTION))); + setFocusable(false); + } + + /** + * Creates a new toolbar toggle button. + * + * @param action action to be performed when the button is pressed + * @param iconResource icon resource path + * @param tooltipText tooltip text + */ + public ToolbarToggleButton(Consumer action, String iconResource, String tooltipText) { + this(new ActionFromEvent(action, iconResource, tooltipText)); + } +}