Gridarta Editor
DirectionComponent.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;
21 
22 import java.awt.Component;
23 import java.awt.GridBagConstraints;
24 import java.awt.GridBagLayout;
25 import java.awt.Insets;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.HashMap;
29 import java.util.Map;
30 import javax.swing.AbstractButton;
31 import javax.swing.ButtonGroup;
32 import javax.swing.JPanel;
33 import javax.swing.JToggleButton;
34 import net.sf.japi.swing.action.ActionBuilder;
35 import net.sf.japi.swing.action.ActionBuilderFactory;
36 import net.sf.japi.swing.action.ActionMethod;
37 import org.jetbrains.annotations.NotNull;
38 import org.jetbrains.annotations.Nullable;
39 
48 public abstract class DirectionComponent extends JPanel {
49 
53  private static final long serialVersionUID = 1L;
54 
58  @NotNull
59  private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
60 
64  @NotNull
65  private static final Insets EMPTY_INSETS = new Insets(0, 0, 0, 0);
66 
70  @NotNull
71  private final ButtonGroup directionButtonGroup = new ButtonGroup();
72 
76  @NotNull
77  private final Collection<JToggleButton> directionButtons = new ArrayList<>();
78 
82  @NotNull
83  private final Map<Integer, JToggleButton> directions = new HashMap<>(9);
84 
88  private boolean enableButtons;
89 
94  protected DirectionComponent(final boolean includeDefault) {
95  super(new GridBagLayout());
96  final GridBagConstraints gbc = new GridBagConstraints();
97  gbc.fill = GridBagConstraints.BOTH;
98  gbc.anchor = GridBagConstraints.CENTER;
99  gbc.insets = new Insets(1, 1, 1, 1);
100  createButton(7, gbc, 0, 0);
101  createButton(8, gbc, 1, 0);
102  createButton(1, gbc, 2, 0);
103  createButton(6, gbc, 0, 1);
104  createButton(0, gbc, 1, 1);
105  createButton(2, gbc, 2, 1);
106  createButton(5, gbc, 0, 2);
107  createButton(4, gbc, 1, 2);
108  createButton(3, gbc, 2, 2);
109  if (includeDefault) {
110  gbc.gridwidth = 3;
111  createButton(null, gbc, 0, 3);
112  }
113  updateEnabled(true);
114  updateDirection(0);
115  }
116 
121  protected final void updateEnabled(final boolean enableButtons) {
122  this.enableButtons = enableButtons;
123  updateEnabled();
124  }
125 
130  public final void updateDirection(@Nullable final Integer direction) {
131  final AbstractButton selectedButton = directions.get(direction);
132  if (selectedButton != null) {
133  selectedButton.setSelected(true);
134  }
135  }
136 
144  private void createButton(@Nullable final Integer direction, @NotNull final GridBagConstraints gbc, final int x, final int y) {
145  final JToggleButton button = new JToggleButton(ACTION_BUILDER.createAction(false, "direction" + direction, this));
146  directionButtonGroup.add(button);
147  button.setFocusable(false);
148  //button.setEnabled(false);
149  button.setMargin(EMPTY_INSETS);
150  gbc.gridx = x;
151  gbc.gridy = y;
152  add(button, gbc);
153  directionButtons.add(button);
154  button.setSelected(true);
155  directions.put(direction, button);
156  }
157 
161  @ActionMethod
162  public void direction0() {
163  direction(0);
164  }
165 
169  @ActionMethod
170  public void direction1() {
171  direction(1);
172  }
173 
177  @ActionMethod
178  public void direction2() {
179  direction(2);
180  }
181 
185  @ActionMethod
186  public void direction3() {
187  direction(3);
188  }
189 
193  @ActionMethod
194  public void direction4() {
195  direction(4);
196  }
197 
201  @ActionMethod
202  public void direction5() {
203  direction(5);
204  }
205 
209  @ActionMethod
210  public void direction6() {
211  direction(6);
212  }
213 
217  @ActionMethod
218  public void direction7() {
219  direction(7);
220  }
221 
225  @ActionMethod
226  public void direction8() {
227  direction(8);
228  }
229 
233  @ActionMethod
234  public void directionnull() {
235  direction(null);
236  }
237 
242  protected abstract void direction(@Nullable Integer direction);
243 
244  @Override
245  public void setEnabled(final boolean enabled) {
246  super.setEnabled(enabled);
247  updateEnabled();
248  }
249 
253  private void updateEnabled() {
254  final boolean enabled = enableButtons && isEnabled();
255  for (final Component button : directionButtons) {
256  button.setEnabled(enabled);
257  }
258  }
259 
260 }
static final Insets EMPTY_INSETS
Empty Insets.
final void updateDirection(@Nullable final Integer direction)
Sets the selected direction.
DirectionComponent(final boolean includeDefault)
Creates a new instance.
static final ActionBuilder ACTION_BUILDER
The ActionBuilder instance.
void createButton(@Nullable final Integer direction, @NotNull final GridBagConstraints gbc, final int x, final int y)
Creates a direction button.
void direction3()
Action method for direction.
final Map< Integer, JToggleButton > directions
Maps direction to button.
static final long serialVersionUID
The serial version UID.
void direction7()
Action method for direction.
void direction2()
Action method for direction.
boolean enableButtons
Whether the buttons are currently enabled.
void direction1()
Action method for direction.
A GUI component for selecting a direction.
void directionnull()
Action method for default direction.
final void updateEnabled(final boolean enableButtons)
Enables/disables the direction buttons for a given archetype.
void direction6()
Action method for direction.
abstract void direction(@Nullable Integer direction)
Called whenever a direction button has been selected.
void direction8()
Action method for direction.
void updateEnabled()
Enables/disables the buttons.
final Collection< JToggleButton > directionButtons
The buttons in directionButtonGroup.
void direction4()
Action method for direction.
void direction0()
Action method for direction.
final ButtonGroup directionButtonGroup
The ButtonGroup for the direction buttons.
void direction5()
Action method for direction.