Gridarta Editor
TristateCheckBox.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2023 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.gui.utils.tristate;
21 
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.ChangeListener;
34 import javax.swing.plaf.ActionMapUIResource;
35 import org.jetbrains.annotations.NotNull;
36 import org.jetbrains.annotations.Nullable;
37 
43 public class TristateCheckBox extends JCheckBox {
44 
48  private static final long serialVersionUID = 1L;
49 
54  @NotNull
55  private final ChangeListener enableListener = e -> setFocusable(getModel().isEnabled());
56 
62  public TristateCheckBox(@NotNull final String text) {
63  this(text, null, TristateState.DESELECTED);
64  }
65 
72  private TristateCheckBox(@NotNull final String text, @Nullable final Icon icon, @NotNull final TristateState initialState) {
73  this(text, icon, new TristateButtonModel(initialState));
74  }
75 
82  private TristateCheckBox(@NotNull final String text, @Nullable final Icon icon, @NotNull final ButtonModel buttonModel) {
83  super(text, icon);
84  setModel(buttonModel);
85  //noinspection RefusedBequest
86  final MouseListener mouseAdapter = new MouseAdapter() {
87 
88  @Override
89  public void mousePressed(@NotNull final MouseEvent e) {
90  iterateState(e.getModifiers());
91  }
92 
93  };
94  addMouseListener(mouseAdapter);
95  final ActionMap actions = new ActionMapUIResource();
96  final Action action = new AbstractAction() {
97 
101  private static final long serialVersionUID = 1L;
102 
103  @Override
104  public void actionPerformed(@NotNull final ActionEvent e) {
105  iterateState(e.getModifiers());
106  }
107 
108  @NotNull
109  @Override
110  protected AbstractAction clone() {
111  try {
112  return (AbstractAction) super.clone();
113  } catch (final CloneNotSupportedException ex) {
114  throw new AssertionError(ex);
115  }
116  }
117 
118  };
119  actions.put("pressed", action);
120  actions.put("released", null);
121  //noinspection ThisEscapedInObjectConstruction
122  SwingUtilities.replaceUIActionMap(this, actions);
123  }
124 
125  @Override
126  public final void setModel(@NotNull final ButtonModel newModel) {
127  final ButtonModel oldModel = getModel();
128  if (oldModel instanceof TristateButtonModel) {
129  oldModel.removeChangeListener(enableListener);
130  }
131 
132  super.setModel(newModel);
133 
134  if (model instanceof TristateButtonModel) {
135  model.addChangeListener(enableListener);
136  }
137  }
138 
143  private void iterateState(final int modifiers) {
144  if (!getModel().isEnabled()) {
145  return;
146  }
147 
148  grabFocus();
149 
150  final TristateButtonModel buttonModel = getTristateModel();
151  buttonModel.setTristateState(nextState(buttonModel.getTristateState()));
152 
153  fireActionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, getText(), System.currentTimeMillis(), modifiers));
154  }
155 
161  @NotNull
162  protected TristateState nextState(@NotNull final TristateState state) {
163  return state.nextTristateState();
164  }
165 
170  @NotNull
172  return (TristateButtonModel) getModel();
173  }
174 
175 }
net.sf.gridarta.gui.utils.tristate.TristateCheckBox.TristateCheckBox
TristateCheckBox(@NotNull final String text)
Creates a new instance without icon which is initially {}.
Definition: TristateCheckBox.java:62
net.sf.gridarta.gui.utils.tristate.TristateButtonModel.getTristateState
TristateState getTristateState()
Returns the internal state.
Definition: TristateButtonModel.java:96
net.sf.gridarta.gui.utils.tristate.TristateCheckBox.nextState
TristateState nextState(@NotNull final TristateState state)
Returns the next state.
Definition: TristateCheckBox.java:162
net.sf.gridarta.gui.utils.tristate.TristateCheckBox
A JCheckBox that supports three states: deselected, indeterminate, selected.
Definition: TristateCheckBox.java:43
net.sf.gridarta.gui.utils.tristate.TristateCheckBox.setModel
final void setModel(@NotNull final ButtonModel newModel)
Definition: TristateCheckBox.java:126
net.sf.gridarta.gui.utils.tristate.TristateCheckBox.TristateCheckBox
TristateCheckBox(@NotNull final String text, @Nullable final Icon icon, @NotNull final ButtonModel buttonModel)
Creates a new instance.
Definition: TristateCheckBox.java:82
net.sf.gridarta.gui.utils.tristate.TristateCheckBox.serialVersionUID
static final long serialVersionUID
The serial version UID.
Definition: TristateCheckBox.java:48
net.sf.gridarta.gui.utils.tristate.TristateCheckBox.iterateState
void iterateState(final int modifiers)
Increments the state.
Definition: TristateCheckBox.java:143
net.sf.gridarta.gui.utils.tristate.TristateState.nextTristateState
nextTristateState
The check box is not selected.
Definition: TristateState.java:36
net.sf.gridarta.gui.utils.tristate.TristateButtonModel.setTristateState
final void setTristateState(@NotNull final TristateState state)
Sets the state.
Definition: TristateButtonModel.java:104
net.sf.gridarta.gui.utils.tristate.TristateState
Possible states for TristateButtonModels.
Definition: TristateState.java:28
net.sf.gridarta.gui.utils.tristate.TristateCheckBox.enableListener
final ChangeListener enableListener
The ChangeListener on model changes to maintain correct focusability.
Definition: TristateCheckBox.java:55
net.sf.gridarta.gui.utils.tristate.TristateCheckBox.TristateCheckBox
TristateCheckBox(@NotNull final String text, @Nullable final Icon icon, @NotNull final TristateState initialState)
Creates a new instance.
Definition: TristateCheckBox.java:72
net.sf.gridarta.gui.utils.tristate.TristateButtonModel
A ToggleButtonModel supporting three states (deselected, indeterminate, selected).
Definition: TristateButtonModel.java:31
net.sf.gridarta.gui.utils.tristate.TristateCheckBox.getTristateModel
TristateButtonModel getTristateModel()
Returns the model as a TristateButtonModel.
Definition: TristateCheckBox.java:171