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-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.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.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
53  private final Collection<ButtonListListener> listeners = new CopyOnWriteArrayList<>();
54 
60  @NotNull
61  private final List<AbstractButton> selectedButtons = new ArrayList<>();
62 
66  @NotNull
67  private final ChangeListener changeListener = e -> {
68  final AbstractButton button = (AbstractButton) e.getSource();
69  if (button.isSelected()) {
70  selectButton(button);
71  } else if (!selectedButtons.isEmpty() && button == selectedButtons.get(0)) {
72  selectedButtons.remove(0);
74  if (!selectedButtons.isEmpty()) {
75  selectedButtons.get(0).setSelected(true);
76  }
77  }
78  };
79 
84  public ButtonList(@NotNull final Location location) {
85  buttons.setLayout(new BoxLayout(buttons, location.getAxis()));
86  }
87 
93  public void addButtonListListener(@NotNull final ButtonListListener listener) {
94  listeners.add(listener);
95  }
96 
101  public void addButton(@NotNull final AbstractButton button) {
102  if (button.isSelected()) {
103  throw new IllegalArgumentException("can't add a selected button");
104  }
105 
106  final String title = button.getText();
107  int index;
108  for (index = 0; index < buttons.getComponentCount(); index++) {
109  final Component tmp = buttons.getComponent(index);
110  if (tmp instanceof AbstractButton) {
111  final AbstractButton tmpButton = (AbstractButton) tmp;
112  final Comparable<String> tmpTitle = tmpButton.getText();
113  if (tmpTitle.compareTo(title) > 0) {
114  break;
115  }
116  }
117  }
118  buttons.add(button, index);
119  buttons.validate();
120  button.addChangeListener(changeListener);
121  }
122 
128  public void removeButton(@NotNull final AbstractButton button) {
129  button.removeChangeListener(changeListener);
130  buttons.remove(button);
131  buttons.validate();
132  final int index = selectedButtons.indexOf(button);
133  if (index != -1) {
134  selectedButtons.remove(index);
135  if (index == 0) {
137  if (!selectedButtons.isEmpty()) {
138  selectedButtons.get(0).setSelected(true);
139  }
140  }
141  }
142  }
143 
148  public void selectButton(@NotNull final AbstractButton button) {
149  if (selectedButtons.isEmpty() || button != selectedButtons.get(0)) {
150  final AbstractButton prevSelectedButton = getSelectedButton();
151  selectedButtons.remove(button);
152  selectedButtons.add(0, button);
153  fireSelectedButtonChanged(prevSelectedButton);
154  button.setSelected(true);
155  if (prevSelectedButton != null) {
156  prevSelectedButton.setSelected(false);
157  }
158  }
159  }
160 
165  private void fireSelectedButtonChanged(@Nullable final AbstractButton prevSelectedButton) {
166  final AbstractButton selectedButton = getSelectedButton();
167  for (final ButtonListListener listener : listeners) {
168  listener.selectedButtonChanged(prevSelectedButton, selectedButton);
169  }
170  }
171 
176  @Nullable
177  public AbstractButton getSelectedButton() {
178  return selectedButtons.isEmpty() ? null : selectedButtons.get(0);
179  }
180 
185  @NotNull
186  public Component getButtons() {
187  return buttons;
188  }
189 
194  public int getButtonCount() {
195  return buttons.getComponentCount();
196  }
197 
198 }
net.sf.gridarta.gui.utils.tabbedpanel.ButtonList.listeners
final Collection< ButtonListListener > listeners
The listeners to notify.
Definition: ButtonList.java:53
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.gui.utils.tabbedpanel.ButtonList.changeListener
final ChangeListener changeListener
The ChangeListener attached to all buttons.
Definition: ButtonList.java:67
net.sf
net.sf.gridarta.gui.utils.tabbedpanel.ButtonList.removeButton
void removeButton(@NotNull final AbstractButton button)
Removes a button.
Definition: ButtonList.java:128
net.sf.gridarta.gui.utils.tabbedpanel.ButtonList
A list of buttons where at most one button is active at any time.
Definition: ButtonList.java:41
net.sf.gridarta.gui.utils.tabbedpanel.ButtonList.addButton
void addButton(@NotNull final AbstractButton button)
Adds a button.
Definition: ButtonList.java:101
net.sf.gridarta.gui
Graphical User Interface of Gridarta.
net.sf.gridarta.gui.utils.tabbedpanel.ButtonList.buttons
final Container buttons
The Container that contains all buttons.
Definition: ButtonList.java:47
net
net.sf.gridarta.gui.utils.tabbedpanel.ButtonList.getButtonCount
int getButtonCount()
Returns the total number of buttons.
Definition: ButtonList.java:194
net.sf.gridarta.gui.utils.tabbedpanel.ButtonList.selectedButtons
final List< AbstractButton > selectedButtons
The currently selected buttons.
Definition: ButtonList.java:61
net.sf.gridarta.gui.utils.tabbedpanel.ButtonList.getSelectedButton
AbstractButton getSelectedButton()
Returns the currently selected button.
Definition: ButtonList.java:177
net.sf.gridarta.gui.utils.tabbedpanel.ButtonList.addButtonListListener
void addButtonListListener(@NotNull final ButtonListListener listener)
Adds a ButtonListListener to be notified.
Definition: ButtonList.java:93
net.sf.gridarta.gui.utils.borderpanel.Location
A location.
Definition: Location.java:33
net.sf.gridarta.gui.utils.tabbedpanel.ButtonListListener
Interface for listeners interested in ButtonList related events.
Definition: ButtonListListener.java:30
net.sf.gridarta.gui.utils.borderpanel
Definition: BorderPanel.java:20
net.sf.gridarta.gui.utils.tabbedpanel.ButtonList.getButtons
Component getButtons()
Returns the Container that contains all buttons.
Definition: ButtonList.java:186
net.sf.gridarta.gui.utils
Definition: AnimationComponent.java:20
net.sf.gridarta.gui.utils.tabbedpanel.ButtonList.ButtonList
ButtonList(@NotNull final Location location)
Creates a new instance.
Definition: ButtonList.java:84
net.sf.gridarta.gui.utils.tabbedpanel.ButtonList.fireSelectedButtonChanged
void fireSelectedButtonChanged(@Nullable final AbstractButton prevSelectedButton)
The selected button has changed.
Definition: ButtonList.java:165
net.sf.gridarta.gui.utils.tabbedpanel.ButtonList.selectButton
void selectButton(@NotNull final AbstractButton button)
Selects a button.
Definition: ButtonList.java:148