137 lines
2.8 KiB
Java
137 lines
2.8 KiB
Java
package org.hwo.ui;
|
|
|
|
import javax.swing.Action;
|
|
import javax.swing.BorderFactory;
|
|
import javax.swing.Icon;
|
|
import javax.swing.JButton;
|
|
import javax.swing.JLabel;
|
|
import javax.swing.JToggleButton;
|
|
import javax.swing.plaf.basic.BasicBorders;
|
|
|
|
import java.awt.Color;
|
|
import java.awt.Graphics;
|
|
import java.awt.event.ActionEvent;
|
|
import java.awt.event.ActionListener;
|
|
import java.awt.event.MouseAdapter;
|
|
import java.awt.event.MouseEvent;
|
|
import java.util.LinkedList;
|
|
|
|
import javax.swing.SwingConstants;
|
|
|
|
public class JColoredToggleButton extends JLabel {
|
|
|
|
/**
|
|
*
|
|
*/
|
|
private static final long serialVersionUID = 1L;
|
|
private Color colorOff = getBackground();
|
|
private Color colorOn = Color.ORANGE;
|
|
private Color textColorOff = Color.BLACK;
|
|
private Color textColorOn = Color.GREEN;
|
|
|
|
private boolean selected = false;
|
|
|
|
boolean mouseDown = false;
|
|
|
|
private LinkedList<ActionListener> actionListeners = new LinkedList<>();
|
|
|
|
public JColoredToggleButton() {
|
|
setHorizontalAlignment(SwingConstants.CENTER);
|
|
setBorder(BorderFactory.createRaisedSoftBevelBorder());
|
|
setOpaque(true);
|
|
setFocusable(true);
|
|
|
|
addMouseListener(new MouseAdapter() {
|
|
|
|
@Override
|
|
public void mousePressed(MouseEvent arg0) {
|
|
mouseDown = true;
|
|
}
|
|
@Override
|
|
public void mouseReleased(MouseEvent arg0) {
|
|
if (mouseDown) {
|
|
toggle();
|
|
}
|
|
}
|
|
@Override
|
|
public void mouseExited(MouseEvent arg0) {
|
|
mouseDown = false;
|
|
}
|
|
|
|
});
|
|
|
|
setSelected(false);
|
|
}
|
|
|
|
public void addActionListener(ActionListener listener) {
|
|
this.actionListeners.add(listener);
|
|
}
|
|
public void removeActionListener(ActionListener listener) {
|
|
this.actionListeners.remove(listener);
|
|
}
|
|
public void fireAction() {
|
|
for (ActionListener al: this.actionListeners) {
|
|
al.actionPerformed(new ActionEvent(this, 0, ""));
|
|
}
|
|
}
|
|
|
|
public void toggle() {
|
|
setSelected(!isSelected());
|
|
}
|
|
|
|
public boolean isSelected()
|
|
{
|
|
return this.selected;
|
|
}
|
|
public void setSelected(boolean selected){
|
|
if (this.selected != selected) {
|
|
this.selected = selected;
|
|
fireAction();
|
|
repaint();
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
public void paint(Graphics g) {
|
|
if (this.selected) {
|
|
setBackground(colorOn);
|
|
setForeground(textColorOn);
|
|
}
|
|
else {
|
|
setBackground(colorOff);
|
|
setForeground(textColorOff);
|
|
}
|
|
super.paint(g);
|
|
}
|
|
|
|
public Color getColorOff() {
|
|
return colorOff;
|
|
}
|
|
public void setColorOff(Color colorOff) {
|
|
this.colorOff = colorOff;
|
|
}
|
|
|
|
public Color getTextColorOff() {
|
|
return this.textColorOff;
|
|
}
|
|
public void setTextColorOff(Color textColorOff) {
|
|
this.textColorOff = textColorOff;
|
|
}
|
|
|
|
public Color getColorOn() {
|
|
return colorOn;
|
|
}
|
|
public void setColorOn(Color colorOn) {
|
|
this.colorOn = colorOn;
|
|
}
|
|
|
|
public Color getTextColorOn() {
|
|
return this.textColorOn;
|
|
}
|
|
public void setTextColorOn(Color textColorOn) {
|
|
this.textColorOn = textColorOn;
|
|
}
|
|
|
|
}
|