Move IconTextField common icons to IconTextField.Icon class

To unify the access to the icons that are anyway residing near
IconTextField, move the declarations to IconTextField.Icon class.

Signed-off-by: Tomas Slusny <slusnucky@gmail.com>
This commit is contained in:
Tomas Slusny
2018-09-24 13:44:18 +02:00
parent c8ab330a34
commit 7426babd3f
4 changed files with 91 additions and 74 deletions

View File

@@ -96,7 +96,6 @@ public class ConfigPanel extends PluginPanel
private static final int OFFSET = 6;
private static final ImageIcon BACK_ICON;
private static final ImageIcon BACK_ICON_HOVER;
private static final ImageIcon SEARCH;
private static final String RUNELITE_GROUP_NAME = RuneLiteConfig.class.getAnnotation(ConfigGroup.class).value();
private static final String PINNED_PLUGINS_CONFIG_KEY = "pinnedPlugins";
@@ -109,9 +108,9 @@ public class ConfigPanel extends PluginPanel
private final ScheduledExecutorService executorService;
private final RuneLiteConfig runeLiteConfig;
private final ChatColorConfig chatColorConfig;
private final IconTextField searchBar = new IconTextField();
private final List<PluginListItem> pluginList = new ArrayList<>();
private final IconTextField searchBar = new IconTextField();
private final JPanel topPanel;
private final JPanel mainPanel;
private final JScrollPane scrollPane;
@@ -124,7 +123,6 @@ public class ConfigPanel extends PluginPanel
final BufferedImage backIcon = ImageUtil.getResourceStreamFromClass(ConfigPanel.class, "config_back_icon.png");
BACK_ICON = new ImageIcon(backIcon);
BACK_ICON_HOVER = new ImageIcon(ImageUtil.alphaOffset(backIcon, -100));
SEARCH = new ImageIcon(ImageUtil.getResourceStreamFromClass(IconTextField.class, "search.png"));
}
ConfigPanel(PluginManager pluginManager, ConfigManager configManager, ScheduledExecutorService executorService,
@@ -137,7 +135,7 @@ public class ConfigPanel extends PluginPanel
this.runeLiteConfig = runeLiteConfig;
this.chatColorConfig = chatColorConfig;
searchBar.setIcon(SEARCH);
searchBar.setIcon(IconTextField.Icon.SEARCH);
searchBar.setPreferredSize(new Dimension(PluginPanel.PANEL_WIDTH - 20, 30));
searchBar.setBackground(ColorScheme.DARKER_GRAY_COLOR);
searchBar.setHoverBackgroundColor(ColorScheme.DARK_GRAY_HOVER_COLOR);

View File

@@ -36,7 +36,6 @@ import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ScheduledExecutorService;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
@@ -50,7 +49,6 @@ import net.runelite.client.game.ItemManager;
import net.runelite.client.ui.ColorScheme;
import net.runelite.client.ui.components.IconTextField;
import net.runelite.client.ui.components.PluginErrorPanel;
import net.runelite.client.util.ImageUtil;
import net.runelite.client.util.RunnableExceptionLogger;
import net.runelite.http.api.item.Item;
import net.runelite.http.api.item.SearchResult;
@@ -65,10 +63,6 @@ class GrandExchangeSearchPanel extends JPanel
private static final String ERROR_PANEL = "ERROR_PANEL";
private static final String RESULTS_PANEL = "RESULTS_PANEL";
private static final ImageIcon SEARCH_ICON;
private static final ImageIcon LOADING_ICON;
private static final ImageIcon ERROR_ICON;
private final GridBagConstraints constraints = new GridBagConstraints();
private final CardLayout cardLayout = new CardLayout();
@@ -76,7 +70,7 @@ class GrandExchangeSearchPanel extends JPanel
private final ItemManager itemManager;
private final ScheduledExecutorService executor;
private final IconTextField searchBox = new IconTextField();
private final IconTextField searchBar = new IconTextField();
/* The main container, this holds the search bar and the center panel */
private final JPanel container = new JPanel();
@@ -98,13 +92,6 @@ class GrandExchangeSearchPanel extends JPanel
@Setter
private Map<Integer, Integer> itemGELimits = Collections.emptyMap();
static
{
SEARCH_ICON = new ImageIcon(ImageUtil.alphaOffset(ImageUtil.grayscaleOffset(ImageUtil.getResourceStreamFromClass(IconTextField.class, "search.png"), 0f), 1.75f));
LOADING_ICON = new ImageIcon(IconTextField.class.getResource("loading_spinner.gif"));
ERROR_ICON = new ImageIcon(ImageUtil.getResourceStreamFromClass(IconTextField.class, "error.png"));
}
GrandExchangeSearchPanel(ClientThread clientThread, ItemManager itemManager, ScheduledExecutorService executor)
{
this.clientThread = clientThread;
@@ -113,7 +100,7 @@ class GrandExchangeSearchPanel extends JPanel
init();
}
void init()
private void init()
{
setLayout(new BorderLayout());
setBackground(ColorScheme.DARK_GRAY_COLOR);
@@ -122,11 +109,11 @@ class GrandExchangeSearchPanel extends JPanel
container.setBorder(new EmptyBorder(10, 10, 10, 10));
container.setBackground(ColorScheme.DARK_GRAY_COLOR);
searchBox.setPreferredSize(new Dimension(100, 30));
searchBox.setBackground(ColorScheme.MEDIUM_GRAY_COLOR);
searchBox.setHoverBackgroundColor(ColorScheme.MEDIUM_GRAY_COLOR.brighter());
searchBox.setIcon(SEARCH_ICON);
searchBox.addActionListener(e -> executor.execute(RunnableExceptionLogger.wrap(() -> priceLookup(false))));
searchBar.setIcon(IconTextField.Icon.SEARCH);
searchBar.setPreferredSize(new Dimension(100, 30));
searchBar.setBackground(ColorScheme.MEDIUM_GRAY_COLOR);
searchBar.setHoverBackgroundColor(ColorScheme.MEDIUM_GRAY_COLOR.brighter());
searchBar.addActionListener(e -> executor.execute(RunnableExceptionLogger.wrap(() -> priceLookup(false))));
searchItemsPanel.setLayout(new GridBagLayout());
searchItemsPanel.setBackground(ColorScheme.DARK_GRAY_COLOR);
@@ -160,7 +147,7 @@ class GrandExchangeSearchPanel extends JPanel
cardLayout.show(centerPanel, ERROR_PANEL);
container.add(searchBox, BorderLayout.NORTH);
container.add(searchBar, BorderLayout.NORTH);
container.add(centerPanel, BorderLayout.CENTER);
add(container, BorderLayout.CENTER);
@@ -168,13 +155,13 @@ class GrandExchangeSearchPanel extends JPanel
void priceLookup(String item)
{
searchBox.setText(item);
searchBar.setText(item);
executor.execute(RunnableExceptionLogger.wrap(() -> priceLookup(true)));
}
private void priceLookup(boolean exactMatch)
{
String lookup = searchBox.getText();
String lookup = searchBar.getText();
if (Strings.isNullOrEmpty(lookup))
{
@@ -185,9 +172,9 @@ class GrandExchangeSearchPanel extends JPanel
// Input is not empty, add searching label
searchItemsPanel.removeAll();
searchBox.setBackground(ColorScheme.MEDIUM_GRAY_COLOR);
searchBox.setEditable(false);
searchBox.setIcon(LOADING_ICON);
searchBar.setBackground(ColorScheme.MEDIUM_GRAY_COLOR);
searchBar.setEditable(false);
searchBar.setIcon(IconTextField.Icon.LOADING);
SearchResult result;
@@ -198,8 +185,8 @@ class GrandExchangeSearchPanel extends JPanel
catch (Exception ex) // handle com.google.common.cache.CacheLoader$InvalidCacheLoadException
{
log.warn("Unable to search for item {}", lookup, ex);
searchBox.setIcon(ERROR_ICON);
searchBox.setEditable(true);
searchBar.setIcon(IconTextField.Icon.ERROR);
searchBar.setEditable(true);
errorPanel.setContent("Error fetching results", "An error occurred while trying to fetch item data, please try again later.");
cardLayout.show(centerPanel, ERROR_PANEL);
return;
@@ -242,7 +229,7 @@ class GrandExchangeSearchPanel extends JPanel
}
else
{
searchBox.setIcon(ERROR_ICON);
searchBar.setIcon(IconTextField.Icon.ERROR);
errorPanel.setContent("No results found.", "No items were found with that name, please try again.");
cardLayout.show(centerPanel, ERROR_PANEL);
}
@@ -271,7 +258,7 @@ class GrandExchangeSearchPanel extends JPanel
{
searchItemsPanel.add(panel, constraints);
}
constraints.gridy++;
}
@@ -280,12 +267,12 @@ class GrandExchangeSearchPanel extends JPanel
{
searchItemsPanel.requestFocusInWindow();
}
searchBox.setEditable(true);
searchBar.setEditable(true);
// Remove searching label after search is complete
if (!itemsList.isEmpty())
{
searchBox.setIcon(SEARCH_ICON);
searchBar.setIcon(IconTextField.Icon.SEARCH);
}
});
}

View File

@@ -99,10 +99,6 @@ public class HiscorePanel extends PluginPanel
/* The maximum allowed username length in runescape accounts */
private static final int MAX_USERNAME_LENGTH = 12;
private static final ImageIcon SEARCH_ICON;
private static final ImageIcon LOADING_ICON;
private static final ImageIcon ERROR_ICON;
/**
* Real skills, ordered in the way they should be displayed in the panel.
*/
@@ -125,7 +121,8 @@ public class HiscorePanel extends PluginPanel
private Client client;
private final HiscoreConfig config;
private final IconTextField input;
private final IconTextField searchBar;
private final List<JLabel> skillLabels = new ArrayList<>();
@@ -144,13 +141,6 @@ public class HiscorePanel extends PluginPanel
/* Used to prevent users from switching endpoint tabs while the results are loading */
private boolean loading = false;
static
{
SEARCH_ICON = new ImageIcon(ImageUtil.getResourceStreamFromClass(IconTextField.class, "search.png"));
LOADING_ICON = new ImageIcon(IconTextField.class.getResource("loading_spinner_darker.gif"));
ERROR_ICON = new ImageIcon(ImageUtil.getResourceStreamFromClass(IconTextField.class, "error.png"));
}
@Inject
public HiscorePanel(HiscoreConfig config)
{
@@ -173,13 +163,14 @@ public class HiscorePanel extends PluginPanel
c.weighty = 0;
c.insets = new Insets(0, 0, 10, 0);
input = new IconTextField();
input.setMinimumSize(new Dimension(0, 30));
input.setBackground(ColorScheme.DARKER_GRAY_COLOR);
input.setHoverBackgroundColor(ColorScheme.DARK_GRAY_HOVER_COLOR);
input.setIcon(SEARCH_ICON);
input.addActionListener(e -> executor.execute(RunnableExceptionLogger.wrap(this::lookup)));
input.addMouseListener(new MouseAdapter()
searchBar = new IconTextField();
searchBar.setIcon(IconTextField.Icon.SEARCH);
searchBar.setPreferredSize(new Dimension(PluginPanel.PANEL_WIDTH - 20, 30));
searchBar.setBackground(ColorScheme.DARKER_GRAY_COLOR);
searchBar.setHoverBackgroundColor(ColorScheme.DARK_GRAY_HOVER_COLOR);
searchBar.setMinimumSize(new Dimension(0, 30));
searchBar.addActionListener(e -> executor.execute(RunnableExceptionLogger.wrap(this::lookup)));
searchBar.addMouseListener(new MouseAdapter()
{
@Override
public void mouseClicked(MouseEvent e)
@@ -202,7 +193,7 @@ public class HiscorePanel extends PluginPanel
}
});
add(input, c);
add(searchBar, c);
c.gridy++;
tabGroup = new MaterialTabGroup();
@@ -296,7 +287,7 @@ public class HiscorePanel extends PluginPanel
public void onActivate()
{
super.onActivate();
input.requestFocusInWindow();
searchBar.requestFocusInWindow();
}
/* Builds a JPanel displaying an icon and level/number associated with it */
@@ -337,14 +328,14 @@ public class HiscorePanel extends PluginPanel
public void lookup(String username)
{
input.setText(username);
searchBar.setText(username);
resetEndpoints();
lookup();
}
private void lookup()
{
String lookup = input.getText();
String lookup = searchBar.getText();
lookup = sanitize(lookup);
@@ -356,13 +347,13 @@ public class HiscorePanel extends PluginPanel
/* Runescape usernames can't be longer than 12 characters long */
if (lookup.length() > MAX_USERNAME_LENGTH)
{
input.setIcon(ERROR_ICON);
searchBar.setIcon(IconTextField.Icon.ERROR);
loading = false;
return;
}
input.setEditable(false);
input.setIcon(LOADING_ICON);
searchBar.setEditable(false);
searchBar.setIcon(IconTextField.Icon.LOADING_DARKER);
loading = true;
for (JLabel label : skillLabels)
@@ -385,23 +376,23 @@ public class HiscorePanel extends PluginPanel
catch (IOException ex)
{
log.warn("Error fetching Hiscore data " + ex.getMessage());
input.setIcon(ERROR_ICON);
input.setEditable(true);
searchBar.setIcon(IconTextField.Icon.ERROR);
searchBar.setEditable(true);
loading = false;
return;
}
if (result == null)
{
input.setIcon(ERROR_ICON);
input.setEditable(true);
searchBar.setIcon(IconTextField.Icon.ERROR);
searchBar.setEditable(true);
loading = false;
return;
}
//successful player search
input.setIcon(SEARCH_ICON);
input.setEditable(true);
searchBar.setIcon(IconTextField.Icon.SEARCH);
searchBar.setEditable(true);
loading = false;
int index = 0;
@@ -448,12 +439,12 @@ public class HiscorePanel extends PluginPanel
void addInputKeyListener(KeyListener l)
{
this.input.addKeyListener(l);
this.searchBar.addKeyListener(l);
}
void removeInputKeyListener(KeyListener l)
{
this.input.removeKeyListener(l);
this.searchBar.removeKeyListener(l);
}
/*
@@ -638,4 +629,4 @@ public class HiscorePanel extends PluginPanel
// Select the first tab (NORMAL hiscores)
tabGroup.select(tabGroup.getTab(0));
}
}
}

View File

@@ -30,10 +30,12 @@ import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.function.Consumer;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
@@ -43,6 +45,8 @@ import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Document;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import net.runelite.client.ui.ColorScheme;
import net.runelite.client.ui.FontManager;
@@ -113,7 +117,7 @@ public class IconTextField extends JPanel
clearButton.setVisible(false);
// ActionListener for keyboard use (via Tab -> Space)
clearButton.addActionListener((l) -> setText(null));
clearButton.addActionListener(evt -> setText(null));
// MouseListener for hover and click events
clearButton.addMouseListener(new MouseAdapter()
@@ -170,9 +174,10 @@ public class IconTextField extends JPanel
textField.addActionListener(actionListener);
}
public void setIcon(ImageIcon icon)
public void setIcon(Icon icon)
{
iconWrapperLabel.setIcon(icon);
final ImageIcon imageIcon = new ImageIcon(this.getClass().getResource(icon.getFile()));
iconWrapperLabel.setIcon(imageIcon);
}
public String getText()
@@ -217,6 +222,30 @@ public class IconTextField extends JPanel
textField.addKeyListener(keyListener);
}
public void addKeyListener(Consumer<KeyEvent> keyEventConsumer)
{
addKeyListener(new net.runelite.client.input.KeyListener()
{
@Override
public void keyTyped(KeyEvent e)
{
keyEventConsumer.accept(e);
}
@Override
public void keyPressed(KeyEvent e)
{
keyEventConsumer.accept(e);
}
@Override
public void keyReleased(KeyEvent e)
{
keyEventConsumer.accept(e);
}
});
}
@Override
public void removeKeyListener(KeyListener keyListener)
{
@@ -244,4 +273,16 @@ public class IconTextField extends JPanel
return textField.getDocument();
}
@Getter
@RequiredArgsConstructor
public enum Icon
{
SEARCH("search.png"),
LOADING("loading_spinner.gif"),
LOADING_DARKER("loading_spinner_darker.gif"),
ERROR("error.png");
private final String file;
}
}