Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
3f1df20e1This file is pinned to the selected release. Download the original to open it in your design tool.
package com.marginallyclever.convenience;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
/**
* A button with multiple sets of icons, texts and optionally foreground colors
*/
public class ToggleButtonIcon extends JButton {
private static final Logger logger = LoggerFactory.getLogger(ToggleButtonIcon.class);
private final List<String> translations = new ArrayList<>();
private final List<ImageIcon> icons = new ArrayList<>();
private final List<Color> foregroundColors = new ArrayList<>();
public ToggleButtonIcon(Item... items) {
for (Item item : items) {
translations.add(item.translation);
icons.add(createImageIcon(item.icon));
foregroundColors.add(item.foregroundColor);
}
updateButton(0);
}
public void updateButton(int position) {
setIcon(icons.get(position));
if (foregroundColors != null) {
setForeground(foregroundColors.get(position));
}
setText(translations.get(position));
}
/**
* @return an ImageIcon, or null if the path was invalid.
*/
private static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = ToggleButtonIcon.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
logger.warn("Failed to load icon {}", path);
return null;
}
}
public static class Item {
private final String translation;
private final String icon;
private final Color foregroundColor;
public Item(String translation, String icon, Color foregroundColor) {
this.translation = translation;
this.icon = icon;
this.foregroundColor = foregroundColor;
}
}
}