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.Collection;
26 import java.util.List;
27 import java.util.concurrent.CopyOnWriteArrayList;
28 import javax.swing.AbstractButton;
29 import javax.swing.BoxLayout;
30 import javax.swing.JPanel;
31 import javax.swing.event.ChangeEvent;
32 import javax.swing.event.ChangeListener;
34 import org.jetbrains.annotations.NotNull;
35 import org.jetbrains.annotations.Nullable;
36 
42 public class ButtonList {
43 
47  @NotNull
48  private final Container buttons = new JPanel();
49 
53  @NotNull
54  private final Collection<ButtonListListener> listeners = new CopyOnWriteArrayList<>();
55 
61  @NotNull
62  private final List<AbstractButton> selectedButtons = new ArrayList<>();
63 
67  @NotNull
68  private final ChangeListener changeListener = new ChangeListener() {
69 
70  @Override
71  public void stateChanged(final ChangeEvent e) {
72  final AbstractButton button = (AbstractButton) e.getSource();
73  if (button.isSelected()) {
74  selectButton(button);
75  } else if (!selectedButtons.isEmpty() && button == selectedButtons.get(0)) {
76  selectedButtons.remove(0);
78  if (!selectedButtons.isEmpty()) {
79  selectedButtons.get(0).setSelected(true);
80  }
81  }
82  }
83 
84  };
85 
90  public ButtonList(@NotNull final Location location) {
91  buttons.setLayout(new BoxLayout(buttons, location.getAxis()));
92  }
93 
99  public void addButtonListListener(@NotNull final ButtonListListener listener) {
100  listeners.add(listener);
101  }
102 
107  public void addButton(@NotNull final AbstractButton button) {
108  if (button.isSelected()) {
109  throw new IllegalArgumentException("can't add a selected button");
110  }
111 
112  final String title = button.getText();
113  int index;
114  for (index = 0; index < buttons.getComponentCount(); index++) {
115  final Component tmp = buttons.getComponent(index);
116  if (tmp instanceof AbstractButton) {
117  final AbstractButton tmpButton = (AbstractButton) tmp;
118  final Comparable<String> tmpTitle = tmpButton.getText();
119  if (tmpTitle.compareTo(title) > 0) {
120  break;
121  }
122  }
123  }
124  buttons.add(button, index);
125  buttons.validate();
126  button.addChangeListener(changeListener);
127  }
128 
134  public void removeButton(@NotNull final AbstractButton button) {
135  button.removeChangeListener(changeListener);
136  buttons.remove(button);
137  buttons.validate();
138  final int index = selectedButtons.indexOf(button);
139  if (index != -1) {
140  selectedButtons.remove(index);
141  if (index == 0) {
143  if (!selectedButtons.isEmpty()) {
144  selectedButtons.get(0).setSelected(true);
145  }
146  }
147  }
148  }
149 
154  public void selectButton(@NotNull final AbstractButton button) {
155  if (selectedButtons.isEmpty() || button != selectedButtons.get(0)) {
156  final AbstractButton prevSelectedButton = getSelectedButton();
157  selectedButtons.remove(button);
158  selectedButtons.add(0, button);
159  fireSelectedButtonChanged(prevSelectedButton);
160  button.setSelected(true);
161  if (prevSelectedButton != null) {
162  prevSelectedButton.setSelected(false);
163  }
164  }
165  }
166 
171  private void fireSelectedButtonChanged(@Nullable final AbstractButton prevSelectedButton) {
172  final AbstractButton selectedButton = getSelectedButton();
173  for (final ButtonListListener listener : listeners) {
174  listener.selectedButtonChanged(prevSelectedButton, selectedButton);
175  }
176  }
177 
182  @Nullable
183  public AbstractButton getSelectedButton() {
184  return selectedButtons.isEmpty() ? null : selectedButtons.get(0);
185  }
186 
191  @NotNull
192  public Component getButtons() {
193  return buttons;
194  }
195 
200  public int getButtonCount() {
201  return buttons.getComponentCount();
202  }
203 
204 }
net.sf.gridarta.gui.utils.tabbedpanel.ButtonList.listeners
final Collection< ButtonListListener > listeners
Definition: ButtonList.java:54
net.sf.gridarta
net.sf.gridarta.gui.utils.tabbedpanel.ButtonList.changeListener
final ChangeListener changeListener
Definition: ButtonList.java:68
net.sf
net.sf.gridarta.gui.utils.tabbedpanel.ButtonList.removeButton
void removeButton(@NotNull final AbstractButton button)
Definition: ButtonList.java:134
net.sf.gridarta.gui.utils.tabbedpanel.ButtonList
Definition: ButtonList.java:42
net.sf.gridarta.gui.utils.tabbedpanel.ButtonList.addButton
void addButton(@NotNull final AbstractButton button)
Definition: ButtonList.java:107
net.sf.gridarta.gui
net.sf.gridarta.gui.utils.tabbedpanel.ButtonList.buttons
final Container buttons
Definition: ButtonList.java:48
net
net.sf.gridarta.gui.utils.tabbedpanel.ButtonList.getButtonCount
int getButtonCount()
Definition: ButtonList.java:200
net.sf.gridarta.gui.utils.tabbedpanel.ButtonList.selectedButtons
final List< AbstractButton > selectedButtons
Definition: ButtonList.java:62
net.sf.gridarta.gui.utils.tabbedpanel.ButtonList.getSelectedButton
AbstractButton getSelectedButton()
Definition: ButtonList.java:183
net.sf.gridarta.gui.utils.tabbedpanel.ButtonList.addButtonListListener
void addButtonListListener(@NotNull final ButtonListListener listener)
Definition: ButtonList.java:99
net.sf.gridarta.gui.utils.borderpanel.Location
Definition: Location.java:33
net.sf.gridarta.gui.utils.tabbedpanel.ButtonListListener
Definition: ButtonListListener.java:30
net.sf.gridarta.gui.utils.borderpanel
Definition: BorderPanel.java:20
net.sf.gridarta.gui.utils.tabbedpanel.ButtonList.getButtons
Component getButtons()
Definition: ButtonList.java:192
net.sf.gridarta.gui.utils
Definition: AnimationComponent.java:20
net.sf.gridarta.gui.utils.tabbedpanel.ButtonList.ButtonList
ButtonList(@NotNull final Location location)
Definition: ButtonList.java:90
net.sf.gridarta.gui.utils.tabbedpanel.ButtonList.fireSelectedButtonChanged
void fireSelectedButtonChanged(@Nullable final AbstractButton prevSelectedButton)
Definition: ButtonList.java:171
net.sf.gridarta.gui.utils.tabbedpanel.ButtonList.selectButton
void selectButton(@NotNull final AbstractButton button)
Definition: ButtonList.java:154