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-2015 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.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;
38 
44 public class TristateCheckBox extends JCheckBox {
45 
49  private static final long serialVersionUID = 1L;
50 
55  @NotNull
56  private final ChangeListener enableListener = new ChangeListener() {
57 
58  @Override
59  public void stateChanged(final ChangeEvent e) {
60  setFocusable(getModel().isEnabled());
61  }
62 
63  };
64 
70  public TristateCheckBox(@NotNull final String text) {
71  this(text, null, TristateState.DESELECTED);
72  }
73 
80  private TristateCheckBox(@NotNull final String text, @Nullable final Icon icon, @NotNull final TristateState initialState) {
81  this(text, icon, new TristateButtonModel(initialState));
82  }
83 
90  private TristateCheckBox(@NotNull final String text, @Nullable final Icon icon, @NotNull final ButtonModel buttonModel) {
91  super(text, icon);
92  setModel(buttonModel);
93  //noinspection RefusedBequest
94  final MouseListener mouseAdapter = new MouseAdapter() {
95 
96  @Override
97  public void mousePressed(@NotNull final MouseEvent e) {
98  iterateState(e.getModifiers());
99  }
100 
101  };
102  addMouseListener(mouseAdapter);
103  final ActionMap actions = new ActionMapUIResource();
104  final Action action = new AbstractAction() {
105 
109  private static final long serialVersionUID = 1L;
110 
111  @Override
112  public void actionPerformed(@NotNull final ActionEvent e) {
113  iterateState(e.getModifiers());
114  }
115 
116  @NotNull
117  @Override
118  protected AbstractAction clone() {
119  try {
120  return (AbstractAction) super.clone();
121  } catch (final CloneNotSupportedException ex) {
122  throw new AssertionError(ex);
123  }
124  }
125 
126  };
127  actions.put("pressed", action);
128  actions.put("released", null);
129  //noinspection ThisEscapedInObjectConstruction
130  SwingUtilities.replaceUIActionMap(this, actions);
131  }
132 
133  @Override
134  public final void setModel(@NotNull final ButtonModel newModel) {
135  final ButtonModel oldModel = getModel();
136  if (oldModel instanceof TristateButtonModel) {
137  oldModel.removeChangeListener(enableListener);
138  }
139 
140  super.setModel(newModel);
141 
142  if (model instanceof TristateButtonModel) {
143  model.addChangeListener(enableListener);
144  }
145  }
146 
151  private void iterateState(final int modifiers) {
152  if (!getModel().isEnabled()) {
153  return;
154  }
155 
156  grabFocus();
157 
158  final TristateButtonModel buttonModel = getTristateModel();
159  buttonModel.setTristateState(nextState(buttonModel.getTristateState()));
160 
161  fireActionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, getText(), System.currentTimeMillis(), modifiers));
162  }
163 
169  @NotNull
170  protected TristateState nextState(@NotNull final TristateState state) {
171  return state.nextTristateState();
172  }
173 
178  @NotNull
180  return (TristateButtonModel) getModel();
181  }
182 
183 }
net.sf.gridarta.gui.utils.tristate.TristateCheckBox.TristateCheckBox
TristateCheckBox(@NotNull final String text)
Definition: TristateCheckBox.java:70
net.sf.gridarta.gui.utils.tristate.TristateButtonModel.getTristateState
TristateState getTristateState()
Definition: TristateButtonModel.java:96
net.sf.gridarta.gui.utils.tristate.TristateCheckBox.nextState
TristateState nextState(@NotNull final TristateState state)
Definition: TristateCheckBox.java:170
net.sf.gridarta.gui.utils.tristate.TristateCheckBox
Definition: TristateCheckBox.java:44
net.sf.gridarta.gui.utils.tristate.TristateCheckBox.setModel
final void setModel(@NotNull final ButtonModel newModel)
Definition: TristateCheckBox.java:134
net.sf.gridarta.gui.utils.tristate.TristateCheckBox.TristateCheckBox
TristateCheckBox(@NotNull final String text, @Nullable final Icon icon, @NotNull final ButtonModel buttonModel)
Definition: TristateCheckBox.java:90
net.sf.gridarta.gui.utils.tristate.TristateCheckBox.serialVersionUID
static final long serialVersionUID
Definition: TristateCheckBox.java:49
net.sf.gridarta.gui.utils.tristate.TristateCheckBox.iterateState
void iterateState(final int modifiers)
Definition: TristateCheckBox.java:151
net.sf.gridarta.gui.utils.tristate.TristateState.nextTristateState
nextTristateState
Definition: TristateState.java:36
net.sf.gridarta.gui.utils.tristate.TristateButtonModel.setTristateState
final void setTristateState(@NotNull final TristateState state)
Definition: TristateButtonModel.java:104
net.sf.gridarta.gui.utils.tristate.TristateState
Definition: TristateState.java:28
net.sf.gridarta.gui.utils.tristate.TristateCheckBox.enableListener
final ChangeListener enableListener
Definition: TristateCheckBox.java:56
net.sf.gridarta.gui.utils.tristate.TristateCheckBox.TristateCheckBox
TristateCheckBox(@NotNull final String text, @Nullable final Icon icon, @NotNull final TristateState initialState)
Definition: TristateCheckBox.java:80
net.sf.gridarta.gui.utils.tristate.TristateButtonModel
Definition: TristateButtonModel.java:31
net.sf.gridarta.gui.utils.tristate.TristateCheckBox.getTristateModel
TristateButtonModel getTristateModel()
Definition: TristateCheckBox.java:179