Skip to content

Commit

Permalink
[#76] Fixed loading icon
Browse files Browse the repository at this point in the history
  • Loading branch information
vbmacher committed Sep 3, 2023
1 parent c9976c4 commit 1611328
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
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 @@ -96,7 +98,9 @@ public static Font loadFontResource(String path, Class<?> resourceClass, int siz
* @return loaded icon, or null if the icon could not be loaded
*/
public static ImageIcon loadIcon(String resource) {
URL url = GuiUtils.class.getResource(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,6 +23,8 @@
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.
Expand Down Expand Up @@ -50,6 +52,25 @@ public ToolbarButton(Action action) {
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);
setToolTipText(tooltipText);
setFocusable(false);
}

/**
* Creates a new toolbar button.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
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.
Expand Down Expand Up @@ -50,6 +52,26 @@ public ToolbarToggleButton(Action action) {
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.
*
Expand Down

0 comments on commit 1611328

Please sign in to comment.