Gridarta Editor
ButtonLists.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.util.IdentityHashMap;
23 import java.util.Map;
24 import javax.swing.AbstractButton;
26 import org.jetbrains.annotations.NotNull;
27 import org.jetbrains.annotations.Nullable;
28 
34 public class ButtonLists {
35 
39  @NotNull
40  private final DoubleButtonList @NotNull [] buttonLists = new DoubleButtonList[Location.values().length];
41 
45  @NotNull
46  private final Map<AbstractButton, Tab> tabs = new IdentityHashMap<>();
47 
52  public ButtonLists(@NotNull final ButtonListsListener buttonListsListener) {
53  for (final Location location : Location.values()) {
54  final DoubleButtonList buttonList = new DoubleButtonList(location);
55  buttonLists[location.ordinal()] = buttonList;
56  final ButtonListListener buttonListListener = (prevSelectedButton, selectedButton) -> {
57  final Tab prevTab = tabs.get(prevSelectedButton);
58  final Tab tab = tabs.get(selectedButton);
59  if (prevTab == tab) {
60  return;
61  }
62  buttonListsListener.tabChanged(prevTab, tab);
63  };
64 
65  buttonList.addButtonListListener(buttonListListener);
66  }
67  }
68 
75  @NotNull
76  public DoubleButtonList addTab(@NotNull final Tab tab) {
77  final DoubleButtonList buttonList = buttonLists[tab.getLocation().ordinal()];
78  final AbstractButton button = tab.getButton();
79  buttonList.addButton(button, tab.isAlternativeLocation());
80  tabs.put(button, tab);
81  return buttonList;
82  }
83 
92  @NotNull
93  public DoubleButtonList @NotNull [] moveTab(@NotNull final Tab tab, @NotNull final Location location) {
94  if (tabs.get(tab.getButton()) == null) {
95  throw new IllegalArgumentException("can't move an undefined button: " + tab.getButton());
96  }
97 
98  final DoubleButtonList oldButtonList = buttonLists[tab.getLocation().ordinal()];
99  final DoubleButtonList newButtonList = buttonLists[location.ordinal()];
100  final AbstractButton oldButton = tab.getButton();
101  oldButtonList.removeButton(oldButton, tab.isAlternativeLocation());
102  oldButton.setSelected(false);
103  tab.setLocation(location);
104  final AbstractButton newButton = tab.getButton();
105  newButtonList.addButton(newButton, tab.isAlternativeLocation());
106  tabs.remove(oldButton);
107  tabs.put(newButton, tab);
108  return new DoubleButtonList[] { oldButtonList, newButtonList, };
109  }
110 
111  @NotNull
112  public DoubleButtonList toggleTabSplitMode(@NotNull final Tab tab) {
113  final boolean alternativeLocation = !tab.isAlternativeLocation();
114  final DoubleButtonList buttonList = buttonLists[tab.getLocation().ordinal()];
115  final AbstractButton oldButton = tab.getButton();
116  buttonList.removeButton(oldButton, tab.isAlternativeLocation());
117  oldButton.setSelected(false);
118  tab.setAlternativeLocation(alternativeLocation);
119  final AbstractButton newButton = tab.getButton();
120  buttonList.addButton(newButton, tab.isAlternativeLocation());
121  tabs.remove(oldButton);
122  tabs.put(newButton, tab);
123  return buttonList;
124  }
125 
134  @Nullable
135  public Tab getActiveTab(@NotNull final Location location, final boolean alternativeLocation) {
136  final DoubleButtonList buttonList = buttonLists[location.ordinal()];
137  final AbstractButton selectedButton = buttonList.getSelectedButton(alternativeLocation);
138  if (selectedButton == null) {
139  return null;
140  }
141 
142  final Tab tab = tabs.get(selectedButton);
143  assert tab != null;
144  return tab;
145  }
146 
147 }
net.sf.gridarta.gui.utils.tabbedpanel.DoubleButtonList.removeButton
void removeButton(@NotNull final AbstractButton button, final boolean alternativeLocation)
Removes a button.
Definition: DoubleButtonList.java:98
net.sf.gridarta.gui.utils.tabbedpanel.DoubleButtonList.addButtonListListener
void addButtonListListener(@NotNull final ButtonListListener listener)
Adds a ButtonListListener to be notified.
Definition: DoubleButtonList.java:75
net.sf.gridarta.gui.utils.tabbedpanel.ButtonLists.ButtonLists
ButtonLists(@NotNull final ButtonListsListener buttonListsListener)
Creates a new instance.
Definition: ButtonLists.java:52
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.gui.utils.tabbedpanel.DoubleButtonList
A list of buttons divided into two parts.
Definition: DoubleButtonList.java:36
net.sf.gridarta.gui.utils.tabbedpanel.ButtonLists.buttonLists
final DoubleButtonList[] buttonLists
The list of buttons for each Location.
Definition: ButtonLists.java:40
net.sf.gridarta.gui.utils.tabbedpanel.ButtonListsListener
Interface for listeners interested in ButtonLists related events.
Definition: ButtonListsListener.java:28
net.sf.gridarta.gui.utils.tabbedpanel.ButtonLists.getActiveTab
Tab getActiveTab(@NotNull final Location location, final boolean alternativeLocation)
Returns the active Tab on a given Location of the main view.
Definition: ButtonLists.java:135
net.sf
net.sf.gridarta.gui.utils.tabbedpanel.Tab
A tab in a TabbedPanel component.
Definition: Tab.java:54
net.sf.gridarta.gui
Graphical User Interface of Gridarta.
net.sf.gridarta.gui.utils.tabbedpanel.ButtonLists.moveTab
DoubleButtonList[] moveTab(@NotNull final Tab tab, @NotNull final Location location)
Moves a Tab to a new location.
Definition: ButtonLists.java:93
net
net.sf.gridarta.gui.utils.tabbedpanel.ButtonLists.addTab
DoubleButtonList addTab(@NotNull final Tab tab)
Adds a Tab to the button list associated with the tab's location.
Definition: ButtonLists.java:76
net.sf.gridarta.gui.utils.borderpanel.Location
A location.
Definition: Location.java:33
net.sf.gridarta.gui.utils.tabbedpanel.ButtonLists.toggleTabSplitMode
DoubleButtonList toggleTabSplitMode(@NotNull final Tab tab)
Definition: ButtonLists.java:112
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.ButtonLists.tabs
final Map< AbstractButton, Tab > tabs
Maps button instance to Tab instance.
Definition: ButtonLists.java:46
net.sf.gridarta.gui.utils.tabbedpanel.ButtonLists
Maintains a set of ButtonLists for Locations of Tabs.
Definition: ButtonLists.java:34
net.sf.gridarta.gui.utils.tabbedpanel.DoubleButtonList.addButton
void addButton(@NotNull final AbstractButton button, final boolean alternativeLocation)
Adds a button.
Definition: DoubleButtonList.java:86
net.sf.gridarta.gui.utils
Definition: AnimationComponent.java:20
net.sf.gridarta.gui.utils.tabbedpanel.DoubleButtonList.getSelectedButton
AbstractButton getSelectedButton(final boolean alternativeLocation)
Returns the currently selected button.
Definition: DoubleButtonList.java:122