Simplify navigation button code
Simplify code of the navigation button to also handle images and to extend the swing JButton instead of wrapping it in getButton method. Signed-off-by: Tomas Slusny <slusnucky@gmail.com>
This commit is contained in:
@@ -34,7 +34,6 @@ import java.time.Instant;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.inject.Inject;
|
||||
import javax.swing.ImageIcon;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.runelite.client.RuneLite;
|
||||
import net.runelite.client.account.AccountSession;
|
||||
@@ -73,18 +72,10 @@ public class AccountPlugin extends Plugin
|
||||
@Override
|
||||
protected void startUp() throws Exception
|
||||
{
|
||||
loginButton = new NavigationButton("Login");
|
||||
logoutButton = new NavigationButton("Logout");
|
||||
|
||||
ImageIcon icon = new ImageIcon(ImageIO.read(getClass().getResourceAsStream("login_icon.png")));
|
||||
loginButton.getButton().setIcon(icon);
|
||||
|
||||
icon = new ImageIcon(ImageIO.read(getClass().getResourceAsStream("logout_icon.png")));
|
||||
logoutButton.getButton().setIcon(icon);
|
||||
|
||||
loginButton.getButton().addActionListener(this::loginClick);
|
||||
logoutButton.getButton().addActionListener(this::logoutClick);
|
||||
|
||||
loginButton = new NavigationButton("Login", ImageIO.read(getClass().getResourceAsStream("login_icon.png")));
|
||||
logoutButton = new NavigationButton("Logout", ImageIO.read(getClass().getResourceAsStream("logout_icon.png")));
|
||||
loginButton.addActionListener(this::loginClick);
|
||||
logoutButton.addActionListener(this::logoutClick);
|
||||
ui.getPluginToolbar().addNavigation(loginButton);
|
||||
}
|
||||
|
||||
|
||||
@@ -26,7 +26,6 @@ package net.runelite.client.plugins.config;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.inject.Inject;
|
||||
import javax.swing.ImageIcon;
|
||||
import net.runelite.client.config.ConfigManager;
|
||||
import net.runelite.client.plugins.Plugin;
|
||||
import net.runelite.client.plugins.PluginDescriptor;
|
||||
@@ -49,19 +48,11 @@ public class ConfigPlugin extends Plugin
|
||||
@Override
|
||||
protected void startUp() throws Exception
|
||||
{
|
||||
navButton = new NavigationButton("Configuration", this::buildPanel);
|
||||
|
||||
ImageIcon icon = new ImageIcon(ImageIO.read(getClass().getResourceAsStream("config_icon.png")));
|
||||
navButton.getButton().setIcon(icon);
|
||||
navButton = new NavigationButton(
|
||||
"Configuration",
|
||||
ImageIO.read(getClass().getResourceAsStream("config_icon.png")),
|
||||
() -> new ConfigPanel(configManager));
|
||||
|
||||
ui.getPluginToolbar().addNavigation(navButton);
|
||||
}
|
||||
|
||||
private ConfigPanel buildPanel()
|
||||
{
|
||||
ConfigPanel panel = new ConfigPanel(configManager);
|
||||
panel.init();
|
||||
return panel;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -28,7 +28,6 @@ import com.google.inject.Binder;
|
||||
import java.awt.Font;
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.inject.Inject;
|
||||
import javax.swing.ImageIcon;
|
||||
import net.runelite.api.widgets.Widget;
|
||||
import net.runelite.client.plugins.Plugin;
|
||||
import net.runelite.client.plugins.PluginDescriptor;
|
||||
@@ -49,9 +48,6 @@ public class DevToolsPlugin extends Plugin
|
||||
@Inject
|
||||
DevToolsOverlay overlay;
|
||||
|
||||
private NavigationButton navButton;
|
||||
private DevToolsPanel panel;
|
||||
|
||||
private boolean togglePlayers;
|
||||
private boolean toggleNpcs;
|
||||
private boolean toggleGroundItems;
|
||||
@@ -76,11 +72,11 @@ public class DevToolsPlugin extends Plugin
|
||||
@Override
|
||||
protected void startUp() throws Exception
|
||||
{
|
||||
panel = injector.getInstance(DevToolsPanel.class);
|
||||
navButton = new NavigationButton("Developer Tools", () -> panel);
|
||||
|
||||
ImageIcon icon = new ImageIcon(ImageIO.read(getClass().getResourceAsStream("devtools_icon.png")));
|
||||
navButton.getButton().setIcon(icon);
|
||||
final DevToolsPanel panel = injector.getInstance(DevToolsPanel.class);
|
||||
final NavigationButton navButton = new NavigationButton(
|
||||
"Developer Tools",
|
||||
ImageIO.read(getClass().getResourceAsStream("devtools_icon.png")),
|
||||
() -> panel);
|
||||
|
||||
ui.getPluginToolbar().addNavigation(navButton);
|
||||
|
||||
|
||||
@@ -28,7 +28,6 @@ import com.google.common.eventbus.Subscribe;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.inject.Inject;
|
||||
import javax.swing.ImageIcon;
|
||||
import net.runelite.client.events.PlayerMenuOptionClicked;
|
||||
import net.runelite.client.menus.MenuManager;
|
||||
import net.runelite.client.plugins.Plugin;
|
||||
@@ -58,11 +57,11 @@ public class HiscorePlugin extends Plugin
|
||||
@Override
|
||||
protected void startUp() throws Exception
|
||||
{
|
||||
navButton = new NavigationButton("Hiscore", () -> hiscorePanel);
|
||||
hiscorePanel = injector.getInstance(HiscorePanel.class);
|
||||
|
||||
ImageIcon icon = new ImageIcon(ImageIO.read(getClass().getResourceAsStream("hiscore.gif")));
|
||||
navButton.getButton().setIcon(icon);
|
||||
navButton = new NavigationButton(
|
||||
"Hiscore",
|
||||
ImageIO.read(getClass().getResourceAsStream("hiscore.gif")),
|
||||
() -> hiscorePanel);
|
||||
|
||||
ui.getPluginToolbar().addNavigation(navButton);
|
||||
|
||||
|
||||
@@ -26,7 +26,6 @@ package net.runelite.client.plugins.xptracker;
|
||||
|
||||
import com.google.common.eventbus.Subscribe;
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.swing.ImageIcon;
|
||||
import net.runelite.api.Client;
|
||||
import net.runelite.api.GameState;
|
||||
import net.runelite.api.Skill;
|
||||
@@ -62,9 +61,12 @@ public class XpTrackerPlugin extends Plugin
|
||||
@Override
|
||||
protected void startUp() throws Exception
|
||||
{
|
||||
navButton = new NavigationButton("XP Tracker", () -> xpPanel);
|
||||
xpPanel = injector.getInstance(XpPanel.class);
|
||||
navButton.getButton().setIcon(new ImageIcon(ImageIO.read(getClass().getResourceAsStream("xp.png"))));
|
||||
navButton = new NavigationButton(
|
||||
"XP Tracker",
|
||||
ImageIO.read(getClass().getResourceAsStream("xp.png")),
|
||||
() -> xpPanel);
|
||||
|
||||
ui.getPluginToolbar().addNavigation(navButton);
|
||||
}
|
||||
|
||||
|
||||
@@ -24,40 +24,29 @@
|
||||
*/
|
||||
package net.runelite.client.ui;
|
||||
|
||||
import java.awt.Image;
|
||||
import java.util.function.Supplier;
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JButton;
|
||||
import lombok.Getter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
public class NavigationButton
|
||||
@Slf4j
|
||||
public class NavigationButton extends JButton
|
||||
{
|
||||
private final JButton button = new JButton();
|
||||
@Getter
|
||||
private final Supplier<PluginPanel> panelSupplier;
|
||||
|
||||
private String name;
|
||||
|
||||
public NavigationButton(String name)
|
||||
public NavigationButton(String name, Image icon)
|
||||
{
|
||||
this.name = name;
|
||||
this.panelSupplier = null;
|
||||
this(name, icon, null);
|
||||
}
|
||||
|
||||
public NavigationButton(String name, Supplier<PluginPanel> panelSupplier)
|
||||
public NavigationButton(String name, Image icon, Supplier<PluginPanel> panelSupplier)
|
||||
{
|
||||
this.name = name;
|
||||
super();
|
||||
setToolTipText(name);
|
||||
setIcon(new ImageIcon(icon));
|
||||
this.panelSupplier = panelSupplier;
|
||||
}
|
||||
|
||||
public JButton getButton()
|
||||
{
|
||||
return button;
|
||||
}
|
||||
|
||||
public Supplier<PluginPanel> getPanelSupplier()
|
||||
{
|
||||
return panelSupplier;
|
||||
}
|
||||
|
||||
public String getName()
|
||||
{
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,18 +52,18 @@ public class PluginToolbar extends JToolBar
|
||||
|
||||
public void addNavigation(NavigationButton button)
|
||||
{
|
||||
button.getButton().addActionListener((ae) -> onClick(button));
|
||||
button.getButton().setToolTipText(button.getName());
|
||||
button.addActionListener((ae) -> onClick(button));
|
||||
button.setToolTipText(button.getName());
|
||||
|
||||
buttons.add(button);
|
||||
add(button.getButton());
|
||||
add(button);
|
||||
revalidate();
|
||||
}
|
||||
|
||||
public void removeNavigation(NavigationButton button)
|
||||
{
|
||||
buttons.remove(button);
|
||||
remove(button.getButton());
|
||||
remove(button);
|
||||
revalidate();
|
||||
}
|
||||
|
||||
@@ -77,7 +77,7 @@ public class PluginToolbar extends JToolBar
|
||||
|
||||
if (current != null)
|
||||
{
|
||||
current.getButton().setSelected(false);
|
||||
current.setSelected(false);
|
||||
}
|
||||
|
||||
if (current == button)
|
||||
@@ -92,7 +92,7 @@ public class PluginToolbar extends JToolBar
|
||||
ui.expand(pluginPanel);
|
||||
|
||||
current = button;
|
||||
current.getButton().setSelected(true);
|
||||
current.setSelected(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user