Skip to content

Commit

Permalink
Merge pull request #91 from emustudio/feature-78
Browse files Browse the repository at this point in the history
Feature 76
  • Loading branch information
vbmacher committed Sep 3, 2023
2 parents aa06e83 + 1611328 commit 8db6e0f
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/
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<ActionEvent> action;

public ActionFromEvent(Consumer<ActionEvent> action, String name, String iconResource, String tooltipText) {
super(name, loadIcon(iconResource));
putValue(SHORT_DESCRIPTION, tooltipText);
this.action = action;
}

public ActionFromEvent(Consumer<ActionEvent> action, String iconResource, String tooltipText) {
this(action, null, iconResource, tooltipText);
}

@Override
public void actionPerformed(ActionEvent actionEvent) {
action.accept(actionEvent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@
*/
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;

import static java.lang.StackWalker.Option.RETAIN_CLASS_REFERENCE;

@SuppressWarnings("unused")
public class GuiUtils {

Expand Down Expand Up @@ -69,7 +73,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 <code>Font.MONOSPACED</code> if the font could not be loaded
*/
public static Font loadFontResource(String path, Class<?> resourceClass, int size) {
Map<TextAttribute, Object> attrs = new HashMap<>();
Expand All @@ -86,4 +90,17 @@ 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) {
// emuLib is loaded at the system level, so it does not see resources of a plugin
Class<?> klass = StackWalker.getInstance(RETAIN_CLASS_REFERENCE).getCallerClass();
URL url = (klass == null ? GuiUtils.class : klass).getResource(resource);
return url == null ? null : new ImageIcon(url);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,49 +23,62 @@
import java.util.function.Consumer;

import static javax.swing.Action.SHORT_DESCRIPTION;
import static javax.swing.Action.SMALL_ICON;
import static net.emustudio.emulib.runtime.interaction.GuiUtils.loadIcon;

/**
* 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
*/
@SuppressWarnings("unused")
public class ToolbarButton extends JButton {

public ToolbarButton(Action action, String iconResource) {
/**
* Creates a new toolbar button.
* <p>
* Tooltip text is set from <code>action.getValue(SHORT_DESCRIPTION)</code>.
*
* @param action action to be performed when the button is pressed
*/
public ToolbarButton(Action action) {
super(action);
setHideActionText(true);
setIcon(new ImageIcon(getClass().getResource(iconResource)));
setToolTipText(String.valueOf(action.getValue(SHORT_DESCRIPTION)));
setFocusable(false);
}

/**
* Creates a new toolbar button.
* <p>
* Tooltip text is set to <code>Action.SHORT_DESCRIPTION</code>.
* Icon is set to <code>Action.SMALL_ICON</code>.
*
* @param action action to be performed when the button is pressed
* @param iconResource icon resource path
* @param tooltipText tooltip text
*/
public ToolbarButton(Action action, String iconResource, String tooltipText) {
super(action);
action.putValue(SHORT_DESCRIPTION, tooltipText);
action.putValue(SMALL_ICON, loadIcon(iconResource));
setHideActionText(true);
setIcon(new ImageIcon(getClass().getResource(iconResource)));
setToolTipText(tooltipText);
setFocusable(false);
}

public ToolbarButton(Action action) {
super(action);
setHideActionText(true);
setToolTipText(String.valueOf(action.getValue(SHORT_DESCRIPTION)));
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<ActionEvent> 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));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*/
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;
import static javax.swing.Action.SMALL_ICON;
import static net.emustudio.emulib.runtime.interaction.GuiUtils.loadIcon;

/**
* 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.
* <p>
* Tooltip text is set from <code>action.getValue(SHORT_DESCRIPTION)</code>.
*
* @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.
* <p>
* Tooltip text is set to <code>Action.SHORT_DESCRIPTION</code>.
* Icon is set to <code>Action.SMALL_ICON</code>.
*
* @param action action to be performed when the button is pressed
* @param iconResource icon resource path
* @param tooltipText tooltip text
*/
public ToolbarToggleButton(Action action, String iconResource, String tooltipText) {
super(action);
action.putValue(SHORT_DESCRIPTION, tooltipText);
action.putValue(SMALL_ICON, loadIcon(iconResource));
setHideActionText(true);
setToolTipText(tooltipText);
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<ActionEvent> action, String iconResource, String tooltipText) {
this(new ActionFromEvent(action, iconResource, tooltipText));
}
}

0 comments on commit 8db6e0f

Please sign in to comment.