Gridarta Editor
ButtonList.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.tabbedpanel;
21 
22 import java.awt.Component;
23 import java.awt.Container;
24 import java.util.ArrayList;
25 import java.util.List;
26 import javax.swing.AbstractButton;
27 import javax.swing.BoxLayout;
28 import javax.swing.JPanel;
29 import javax.swing.event.ChangeEvent;
30 import javax.swing.event.ChangeListener;
33 import org.jetbrains.annotations.NotNull;
34 import org.jetbrains.annotations.Nullable;
35 
41 public class ButtonList {
42 
46  @NotNull
47  private final Container buttons = new JPanel();
48 
52  @NotNull
54 
60  @NotNull
61  private final List<AbstractButton> selectedButtons = new ArrayList<>();
62 
66  @NotNull
67  private final ChangeListener changeListener = new ChangeListener() {
68 
69  @Override
70  public void stateChanged(final ChangeEvent e) {
71  final AbstractButton button = (AbstractButton) e.getSource();
72  if (button.isSelected()) {
73  selectButton(button);
74  } else if (!selectedButtons.isEmpty() && button == selectedButtons.get(0)) {
75  selectedButtons.remove(0);
77  if (!selectedButtons.isEmpty()) {
78  selectedButtons.get(0).setSelected(true);
79  }
80  }
81  }
82 
83  };
84 
89  public ButtonList(@NotNull final Location location) {
90  buttons.setLayout(new BoxLayout(buttons, location.getAxis()));
91  }
92 
98  public void addButtonListListener(@NotNull final ButtonListListener listener) {
99  listeners.add(listener);
100  }
101 
106  public void addButton(@NotNull final AbstractButton button) {
107  if (button.isSelected()) {
108  throw new IllegalArgumentException();
109  }
110 
111  final String title = button.getText();
112  int index;
113  for (index = 0; index < buttons.getComponentCount(); index++) {
114  final Component tmp = buttons.getComponent(index);
115  if (tmp instanceof AbstractButton) {
116  final AbstractButton tmpButton = (AbstractButton) tmp;
117  final Comparable<String> tmpTitle = tmpButton.getText();
118  if (tmpTitle.compareTo(title) > 0) {
119  break;
120  }
121  }
122  }
123  buttons.add(button, index);
124  buttons.validate();
125  button.addChangeListener(changeListener);
126  }
127 
133  public void removeButton(@NotNull final AbstractButton button) {
134  button.removeChangeListener(changeListener);
135  buttons.remove(button);
136  buttons.validate();
137  final int index = selectedButtons.indexOf(button);
138  if (index != -1) {
139  selectedButtons.remove(index);
140  if (index == 0) {
142  if (!selectedButtons.isEmpty()) {
143  selectedButtons.get(0).setSelected(true);
144  }
145  }
146  }
147  }
148 
153  public void selectButton(@NotNull final AbstractButton button) {
154  if (selectedButtons.isEmpty() || button != selectedButtons.get(0)) {
155  final AbstractButton prevSelectedButton = getSelectedButton();
156  selectedButtons.remove(button);
157  selectedButtons.add(0, button);
158  fireSelectedButtonChanged(prevSelectedButton);
159  button.setSelected(true);
160  if (prevSelectedButton != null) {
161  prevSelectedButton.setSelected(false);
162  }
163  }
164  }
165 
170  private void fireSelectedButtonChanged(@Nullable final AbstractButton prevSelectedButton) {
171  final AbstractButton selectedButton = getSelectedButton();
172  for (final ButtonListListener listener : listeners.getListeners()) {
173  listener.selectedButtonChanged(prevSelectedButton, selectedButton);
174  }
175  }
176 
181  @Nullable
182  public AbstractButton getSelectedButton() {
183  return selectedButtons.isEmpty() ? null : selectedButtons.get(0);
184  }
185 
190  @NotNull
191  public Component getButtons() {
192  return buttons;
193  }
194 
199  public int getButtonCount() {
200  return buttons.getComponentCount();
201  }
202 
203 }
final ChangeListener changeListener
The ChangeListener attached to all buttons.
Definition: ButtonList.java:67
Graphical User Interface of Gridarta.
T [] getListeners()
Returns an array of all the listeners.
final EventListenerList2< ButtonListListener > listeners
The listeners to notify.
Definition: ButtonList.java:53
Component getButtons()
Returns the Container that contains all buttons.
int getButtonCount()
Returns the total number of buttons.
A list of buttons where at most one button is active at any time.
Definition: ButtonList.java:41
Base package of all Gridarta classes.
Interface for listeners interested in ButtonList related events.
AbstractButton getSelectedButton()
Returns the currently selected button.
void selectButton(@NotNull final AbstractButton button)
Selects a button.
void add(@NotNull final T listener)
Adds a listener.
final Container buttons
The Container that contains all buttons.
Definition: ButtonList.java:47
void addButton(@NotNull final AbstractButton button)
Adds a button.
ButtonList(@NotNull final Location location)
Creates a new instance.
Definition: ButtonList.java:89
void fireSelectedButtonChanged(@Nullable final AbstractButton prevSelectedButton)
The selected button has changed.
Type-safe version of EventListenerList.
void removeButton(@NotNull final AbstractButton button)
Removes a button.
final List< AbstractButton > selectedButtons
The currently selected buttons.
Definition: ButtonList.java:61
void addButtonListListener(@NotNull final ButtonListListener listener)
Adds a ButtonListListener to be notified.
Definition: ButtonList.java:98