mmb.menu.components.BoundCheckBoxMenuItem Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of multimachinebuilder Show documentation
Show all versions of multimachinebuilder Show documentation
Dependency for the MultiMachineBuilder, a voxel game about building an industrial empire in a finite world.
THIS RELEASE IS NOT PLAYABLE. To play the game, donwload from >ITCH.IO LINK HERE< or >GH releases link here<
/**
*
*/
package mmb.menu.components;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JCheckBoxMenuItem;
import it.unimi.dsi.fastutil.booleans.BooleanConsumer;
import mmb.NN;
import mmb.Nil;
import mmb.content.modular.gui.SafeCloseable;
import mmb.data.variables.ListenableBoolean;
/**
* @author oskar
*
*/
public class BoundCheckBoxMenuItem extends JCheckBoxMenuItem implements SafeCloseable {
private static final long serialVersionUID = 6007934685540436786L;
private boolean valueChangeUnderway = false;
@Override
public void setSelected(boolean arg0) {
if(valueChangeUnderway) return;
valueChangeUnderway = true;
super.setSelected(arg0);
if(bvar != null) bvar.setValue(isSelected());
valueChangeUnderway = false;
}
private transient ListenableBoolean bvar;
@NN private transient BooleanConsumer update = this::setSelected;
public void setVariable(@Nil ListenableBoolean var) {
if(var != null) setSelected(var.getValue());
if(bvar != null) bvar.remove(update);
bvar = var;
if(bvar != null) bvar.add(update);
}
private void initialize() {
setRolloverEnabled(false);
addChangeListener(e -> {
if(valueChangeUnderway) return;
valueChangeUnderway = true;
if(bvar != null) bvar.setValue(isSelected());
valueChangeUnderway = false;
});
}
public BoundCheckBoxMenuItem() {
super();
initialize();
}
public BoundCheckBoxMenuItem(Action a) {
super(a);
initialize();
}
public BoundCheckBoxMenuItem(Icon icon) {
super(icon);
initialize();
}
public BoundCheckBoxMenuItem(String text, boolean b) {
super(text, b);
initialize();
}
public BoundCheckBoxMenuItem(String text, Icon icon, boolean b) {
super(text, icon, b);
initialize();
}
public BoundCheckBoxMenuItem(String text, Icon icon) {
super(text, icon);
initialize();
}
public BoundCheckBoxMenuItem(String text) {
super(text);
initialize();
}
@Override
public void close() {
setVariable(null);
}
}