121 lines
2.6 KiB
Java
121 lines
2.6 KiB
Java
package org.hwo.pulscounter.ui;
|
|
|
|
import javax.swing.JCheckBox;
|
|
import javax.swing.JMenuItem;
|
|
import javax.swing.JPanel;
|
|
import javax.swing.JPopupMenu;
|
|
import javax.swing.event.ChangeEvent;
|
|
import javax.swing.event.ChangeListener;
|
|
|
|
import java.awt.FlowLayout;
|
|
import java.awt.MenuItem;
|
|
import java.awt.event.ActionEvent;
|
|
import java.awt.event.ActionListener;
|
|
import java.util.LinkedList;
|
|
import java.util.List;
|
|
|
|
public class JBitSetter extends JPanel {
|
|
|
|
/**
|
|
* Create the panel.
|
|
*/
|
|
|
|
JCheckBox[] checkboxes;
|
|
|
|
JPopupMenu contextMenu;
|
|
|
|
List<ActionListener> actionListeners;
|
|
|
|
|
|
public JBitSetter() {
|
|
setLayout(new FlowLayout(FlowLayout.CENTER, 0, 5));
|
|
|
|
this.checkboxes = new JCheckBox[32];
|
|
this.actionListeners = new LinkedList<ActionListener>();
|
|
|
|
for (int i=0;i<32;i++){
|
|
this.checkboxes[i] = new JCheckBox();
|
|
// this.checkboxes[i].setText(String.format("%d", i));
|
|
this.checkboxes[i].setInheritsPopupMenu(true);
|
|
this.checkboxes[i].setToolTipText(String.format("%d", i));
|
|
/* this.checkboxes[i].addChangeListener(new ChangeListener() {
|
|
|
|
@Override
|
|
public void stateChanged(ChangeEvent e) {
|
|
fireAction();
|
|
}
|
|
});
|
|
*/
|
|
this.checkboxes[i].addActionListener(new ActionListener() {
|
|
|
|
@Override
|
|
public void actionPerformed(ActionEvent e) {
|
|
fireAction();
|
|
}
|
|
});
|
|
|
|
this.add(this.checkboxes[i]);
|
|
}
|
|
|
|
contextMenu = new JPopupMenu();
|
|
JMenuItem mi = new JMenuItem("Keins");
|
|
mi.addActionListener(new ActionListener() {
|
|
|
|
@Override
|
|
public void actionPerformed(ActionEvent e) {
|
|
for (int i=0;i<32;i++){
|
|
checkboxes[i].setSelected(false);
|
|
}
|
|
fireAction();
|
|
}
|
|
});
|
|
contextMenu.add(mi);
|
|
mi = new JMenuItem("Alle");
|
|
mi.addActionListener(new ActionListener() {
|
|
|
|
@Override
|
|
public void actionPerformed(ActionEvent e) {
|
|
for (int i=0;i<32;i++){
|
|
checkboxes[i].setSelected(true);
|
|
}
|
|
fireAction();
|
|
}
|
|
});
|
|
contextMenu.add(mi);
|
|
|
|
setComponentPopupMenu(contextMenu);
|
|
|
|
invalidate();
|
|
}
|
|
|
|
public void addActionListener(ActionListener listener){
|
|
this.actionListeners.add(listener);
|
|
}
|
|
public void removeActionListener(ActionListener listener){
|
|
this.actionListeners.remove(listener);
|
|
}
|
|
|
|
protected void fireAction(){
|
|
for (ActionListener listener: this.actionListeners){
|
|
listener.actionPerformed(new ActionEvent(this, 0, ""));
|
|
}
|
|
}
|
|
|
|
public void setValue(Integer value){
|
|
for (int i=0;i<32;i++){
|
|
this.checkboxes[i].setSelected( (value & (1<<i))!=0 );
|
|
}
|
|
}
|
|
|
|
public Integer getValue(){
|
|
Integer n = 0;
|
|
for (int i=0;i<32;i++){
|
|
if (this.checkboxes[i].isSelected())
|
|
n |= 1<<i;
|
|
}
|
|
return n;
|
|
}
|
|
|
|
|
|
}
|