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-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.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[] 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 = new ButtonListListener() {
57 
58  @Override
59  public void selectedButtonChanged(@Nullable final AbstractButton prevSelectedButton, @Nullable final AbstractButton selectedButton) {
60  final Tab prevTab = tabs.get(prevSelectedButton);
61  final Tab tab = tabs.get(selectedButton);
62  if (prevTab == tab) {
63  return;
64  }
65  buttonListsListener.tabChanged(prevTab, tab);
66  }
67 
68  };
69 
70  buttonList.addButtonListListener(buttonListListener);
71  }
72  }
73 
80  @NotNull
81  public DoubleButtonList addTab(@NotNull final Tab tab) {
82  final DoubleButtonList buttonList = buttonLists[tab.getLocation().ordinal()];
83  final AbstractButton button = tab.getButton();
84  buttonList.addButton(button, tab.isAlternativeLocation());
85  tabs.put(button, tab);
86  return buttonList;
87  }
88 
97  @NotNull
98  public DoubleButtonList[] moveTab(@NotNull final Tab tab, @NotNull final Location location) {
99  if (tabs.get(tab.getButton()) == null) {
100  throw new IllegalArgumentException();
101  }
102 
103  final DoubleButtonList oldButtonList = buttonLists[tab.getLocation().ordinal()];
104  final DoubleButtonList newButtonList = buttonLists[location.ordinal()];
105  final AbstractButton oldButton = tab.getButton();
106  oldButtonList.removeButton(oldButton, tab.isAlternativeLocation());
107  oldButton.setSelected(false);
108  tab.setLocation(location);
109  final AbstractButton newButton = tab.getButton();
110  newButtonList.addButton(newButton, tab.isAlternativeLocation());
111  tabs.remove(oldButton);
112  tabs.put(newButton, tab);
113  return new DoubleButtonList[] { oldButtonList, newButtonList, };
114  }
115 
116  @NotNull
117  public DoubleButtonList toggleTabSplitMode(@NotNull final Tab tab) {
118  final boolean alternativeLocation = !tab.isAlternativeLocation();
119  final DoubleButtonList buttonList = buttonLists[tab.getLocation().ordinal()];
120  final AbstractButton oldButton = tab.getButton();
121  buttonList.removeButton(oldButton, tab.isAlternativeLocation());
122  oldButton.setSelected(false);
123  tab.setAlternativeLocation(alternativeLocation);
124  final AbstractButton newButton = tab.getButton();
125  buttonList.addButton(newButton, tab.isAlternativeLocation());
126  tabs.remove(oldButton);
127  tabs.put(newButton, tab);
128  return buttonList;
129  }
130 
140  @Nullable
141  public Tab getActiveTab(@NotNull final Location location, final boolean alternativeLocation) {
142  final DoubleButtonList buttonList = buttonLists[location.ordinal()];
143  final AbstractButton selectedButton = buttonList.getSelectedButton(alternativeLocation);
144  if (selectedButton == null) {
145  return null;
146  }
147 
148  final Tab tab = tabs.get(selectedButton);
149  assert tab != null;
150  return tab;
151  }
152 
153 }
void addButton(@NotNull final AbstractButton button, final boolean alternativeLocation)
Adds a button.
void addButtonListListener(@NotNull final ButtonListListener listener)
Adds a ButtonListListener to be notified.
final DoubleButtonList [] buttonLists
The list of buttons for each Location.
Graphical User Interface of Gridarta.
Maintains a set of ButtonLists for Locations of Tabs.
A tab in a TabbedPanel component.
Definition: Tab.java:53
Base package of all Gridarta classes.
Interface for listeners interested in ButtonList related events.
A list of buttons divided into two parts.
DoubleButtonList addTab(@NotNull final Tab tab)
Adds a Tab to the button list associated with the tab&#39;s location.
Interface for listeners interested in ButtonLists related events.
DoubleButtonList [] moveTab(@NotNull final Tab tab, @NotNull final Location location)
Moves a Tab to a new location.
void removeButton(@NotNull final AbstractButton button, final boolean alternativeLocation)
Removes a button.
final Map< AbstractButton, Tab > tabs
Maps button instance to Tab instance.
AbstractButton getSelectedButton(final boolean alternativeLocation)
Returns the currently selected button.
Tab getActiveTab(@NotNull final Location location, final boolean alternativeLocation)
Returns the active Tab on a given Location of the main view.
ButtonLists(@NotNull final ButtonListsListener buttonListsListener)
Creates a new instance.
DoubleButtonList toggleTabSplitMode(@NotNull final Tab tab)