20 package net.sf.gridarta.gui.utils.tristate;
22 import java.awt.event.ActionEvent;
23 import java.awt.event.MouseAdapter;
24 import java.awt.event.MouseEvent;
25 import java.awt.event.MouseListener;
26 import javax.swing.AbstractAction;
27 import javax.swing.Action;
28 import javax.swing.ActionMap;
29 import javax.swing.ButtonModel;
30 import javax.swing.Icon;
31 import javax.swing.JCheckBox;
32 import javax.swing.SwingUtilities;
33 import javax.swing.event.ChangeEvent;
34 import javax.swing.event.ChangeListener;
35 import javax.swing.plaf.ActionMapUIResource;
36 import org.jetbrains.annotations.NotNull;
37 import org.jetbrains.annotations.Nullable;
59 public void stateChanged(
final ChangeEvent e) {
60 setFocusable(getModel().isEnabled());
90 private TristateCheckBox(@NotNull
final String text, @Nullable
final Icon icon, @NotNull
final ButtonModel buttonModel) {
94 final MouseListener mouseAdapter =
new MouseAdapter() {
97 public void mousePressed(@NotNull
final MouseEvent e) {
102 addMouseListener(mouseAdapter);
103 final ActionMap actions =
new ActionMapUIResource();
104 final Action action =
new AbstractAction() {
109 private static final long serialVersionUID = 1L;
112 public void actionPerformed(@NotNull
final ActionEvent e) {
118 protected Object clone() {
120 return super.clone();
121 }
catch (
final CloneNotSupportedException ex) {
122 throw new AssertionError(ex);
127 actions.put(
"pressed", action);
128 actions.put(
"released", null);
130 SwingUtilities.replaceUIActionMap(
this, actions);
134 public final void setModel(@NotNull
final ButtonModel newModel) {
135 final ButtonModel oldModel = getModel();
137 oldModel.removeChangeListener(enableListener);
140 super.setModel(newModel);
142 if (model instanceof TristateButtonModel) {
143 model.addChangeListener(enableListener);
152 if (!getModel().isEnabled()) {
161 fireActionPerformed(
new ActionEvent(
this, ActionEvent.ACTION_PERFORMED, getText(), System.currentTimeMillis(), modifiers));
final ChangeListener enableListener
The ChangeListener on model changes to maintain correct focusability.
TristateState nextState(@NotNull final TristateState state)
Returns the next state.
TristateCheckBox(@NotNull final String text, @Nullable final Icon icon, @NotNull final TristateState initialState)
Creates a new instance.
TristateButtonModel getTristateModel()
Returns the model as a TristateButtonModel.
TristateCheckBox(@NotNull final String text)
Creates a new instance without icon which is initially TristateState#DESELECTED.
A JCheckBox that supports three states: deselected, indeterminate, selected.
void iterateState(final int modifiers)
Increments the state.
Possible states for TristateButtonModels.
final void setModel(@NotNull final ButtonModel newModel)
static final long serialVersionUID
The serial version UID.
nextTristateState
The check box is not selected.
TristateCheckBox(@NotNull final String text, @Nullable final Icon icon, @NotNull final ButtonModel buttonModel)
Creates a new instance.