Gridarta Editor
TabbedPanel.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.BorderLayout;
23 import java.awt.Component;
24 import java.awt.Container;
25 import java.util.IdentityHashMap;
26 import java.util.Map;
27 import java.util.prefs.Preferences;
28 import javax.swing.JMenu;
29 import javax.swing.JPopupMenu;
30 import net.sf.gridarta.MainControl;
35 import net.sf.japi.swing.action.ActionBuilder;
36 import net.sf.japi.swing.action.ActionBuilderFactory;
37 import net.sf.japi.swing.action.ToggleAction;
38 import org.jetbrains.annotations.NotNull;
39 import org.jetbrains.annotations.Nullable;
40 
47 public class TabbedPanel extends Container {
48 
52  @NotNull
53  private static final String TAB_PREFIX = "MainWindow.tab";
54 
58  private static final long serialVersionUID = 1L;
59 
63  @NotNull
64  private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
65 
69  @NotNull
70  private static final Preferences PREFERENCES = Preferences.userNodeForPackage(MainControl.class);
71 
76  @NotNull
77  private final BorderPanel borderPanel;
78 
82  @NotNull
83  private final Map<Component, Tab> openTabs = new IdentityHashMap<>();
84 
88  @NotNull
90 
91  @Override
92  public void tabChanged(@Nullable final Tab prevTab, @Nullable final Tab tab) {
93  if (prevTab != null) {
94  close(prevTab);
95  }
96  if (tab != null) {
97  open(tab);
98  }
99  }
100 
101  };
102 
106  @NotNull
107  private final ButtonLists buttonLists = new ButtonLists(buttonListsListener);
108 
113  public TabbedPanel(@NotNull final Component centerComponent) {
114  final BorderPanelListener borderPanelListener = new BorderPanelListener() {
115 
116  @Override
117  public void sizeChanged(@NotNull final Component component, final int size) {
118  final Tab tab = openTabs.get(component);
119  if (tab != null) {
120  tab.setSize(size);
121  }
122  }
123 
124  @Override
125  public void size2Changed(@NotNull final Location location, final int size2) {
126  PREFERENCES.putInt(TAB_PREFIX + location.getName() + ".position", size2);
127  }
128 
129  @Override
130  public int getSize2(final Location location) {
131  return PREFERENCES.getInt(TAB_PREFIX + location.getName() + ".position", -1);
132  }
133 
134  };
135  borderPanel = new BorderPanel(centerComponent, borderPanelListener);
136  setLayout(new BorderLayout());
137  add(borderPanel, BorderLayout.CENTER);
138  }
139 
144  public void addTab(@NotNull final Tab tab) {
145  final DoubleButtonList buttonList = buttonLists.addTab(tab);
146  fillContextMenu(tab, true);
147  tabAdded(tab, buttonList, tab.isOpen());
148  }
149 
159  @Nullable
160  public Tab getActiveTab(@NotNull final Location location, final boolean alternativeLocation) {
161  return buttonLists.getActiveTab(location, alternativeLocation);
162  }
163 
169  public void moveTab(@NotNull final Tab tab, @NotNull final Location location) {
170  if (tab.getLocation() == location) {
171  return;
172  }
173 
174  final boolean open = tab.isOpen();
175  final DoubleButtonList[] result = buttonLists.moveTab(tab, location);
176  final DoubleButtonList oldButtonList = result[0];
177  final DoubleButtonList newButtonList = result[1];
178 
179  fillContextMenu(tab, false);
180  tabRemoved(oldButtonList);
181  tabAdded(tab, newButtonList, open);
182  }
183 
189  public void setTabSplitMode(@NotNull final Tab tab, final boolean splitMode) {
190  if (tab.isAlternativeLocation() == splitMode) {
191  return;
192  }
193 
194  final boolean open = tab.isOpen();
195  final DoubleButtonList buttonList = buttonLists.toggleTabSplitMode(tab);
196  if (open) {
197  buttonList.selectButton(tab.getButton(), tab.isAlternativeLocation());
198  }
199  }
200 
208  private void tabAdded(@NotNull final Tab tab, @NotNull final DoubleButtonList buttonList, final boolean open) {
209  if (buttonList.getButtonCount() == 1) {
210  add(buttonList.getButtons(), tab.getLocation().getBorderLocation());
211  validate();
212  }
213 
214  if (open) {
215  buttonList.selectButton(tab.getButton(), tab.isAlternativeLocation());
216  }
217  }
218 
224  private void tabRemoved(@NotNull final DoubleButtonList buttonList) {
225  if (buttonList.getButtonCount() == 0) {
226  remove(buttonList.getButtons());
227  validate();
228  }
229  }
230 
236  private void fillContextMenu(@NotNull final Tab tab, final boolean initialize) {
237  final MoveToActions moveToActions = new MoveToActions(tab, this);
238  if (initialize) {
239  final JPopupMenu popupMenu = tab.getPopupMenu();
240  final ToggleAction splitModeAction = (ToggleAction) ACTION_BUILDER.createToggle(false, "tabSplitMode", moveToActions);
241  splitModeAction.setSelected(tab.isAlternativeLocation());
242  popupMenu.insert(splitModeAction.createCheckBoxMenuItem(), 0);
243  tab.setSplitModeAction(splitModeAction);
244  }
245 
246  final JMenu moveToMenu = tab.getMoveToMenu();
247  MenuUtils.removeAll(moveToMenu);
248  final Location location = tab.getLocation();
249  for (final Location thisLocation : Location.values()) {
250  if (thisLocation != location) {
251  moveToMenu.add(ACTION_BUILDER.createAction(true, "tabButtonMoveTo" + thisLocation.getName(), moveToActions));
252  }
253  }
254  }
255 
260  private void open(@NotNull final Tab tab) {
261  openTabs.put(tab.getComponent(), tab);
262  borderPanel.setComponent(tab.getLocation(), tab.getComponent(), tab.isAlternativeLocation(), tab.getSize());
263  tab.setOpen(true);
264  }
265 
270  private void close(@NotNull final Tab tab) {
271  closeInt(tab);
272  tab.setOpen(false);
273  }
274 
279  private void closeInt(final Tab tab) {
280  borderPanel.unsetComponent(tab.getLocation(), tab.isAlternativeLocation());
281  openTabs.remove(tab.getComponent());
282  }
283 
284 }
Defines ActionMethods to move tab locations.
static final ActionBuilder ACTION_BUILDER
The ActionBuilder.
void addTab(@NotNull final Tab tab)
Adds a tab.
abstract String getName()
Returns a name for building resource keys.
void setTabSplitMode(@NotNull final Tab tab, final boolean splitMode)
Toggles split mode for the given tab.
final ButtonListsListener buttonListsListener
The ButtonListsListener attached to buttonLists.
Graphical User Interface of Gridarta.
Maintains a set of ButtonLists for Locations of Tabs.
void selectButton(@NotNull final AbstractButton button, final boolean alternativeLocation)
Selects a button.
A tab in a TabbedPanel component.
Definition: Tab.java:53
final BorderPanel borderPanel
The BorderPanel.
void setComponent(@NotNull final Location location, @NotNull final Component component, final boolean alternativeLocation, final int size)
Sets the optional Component for a location.
void tabAdded(@NotNull final Tab tab, @NotNull final DoubleButtonList buttonList, final boolean open)
Called whenever a Tab has been added to a ButtonList.
static void removeAll(@NotNull final JMenu menu)
Remove all menu entries.
Definition: MenuUtils.java:72
final ButtonLists buttonLists
The list of buttons for each Location.
Base package of all Gridarta classes.
Location getLocation()
Returns the tab&#39;s location.
Definition: Tab.java:360
void fillContextMenu(@NotNull final Tab tab, final boolean initialize)
Fills in context popup menu entries for a tab in a given location.
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.
Utility class implementing menu related functions.
Definition: MenuUtils.java:39
Component getComponent()
Returns the Component that is shown when this tab is active.
Definition: Tab.java:294
void close(@NotNull final Tab tab)
Closes a Tab.
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 setSize(final int size)
Sets the tab&#39;s size.
Definition: Tab.java:392
static final long serialVersionUID
The serial version UID.
Interface used as preferences location.
A Component that always displays another component and optionally a number of tabs around it...
Tab getActiveTab(@NotNull final Location location, final boolean alternativeLocation)
Returns the active Tab on a given Location of the main view.
void open(@NotNull final Tab tab)
Opens a Tab.
void moveTab(@NotNull final Tab tab, @NotNull final Location location)
Moves the tab to the given location.
final Map< Component, Tab > openTabs
The Tabs currently shown in borderPanel.
static final String TAB_PREFIX
The key used to store the preferred height of a tab.
A Component that permanently displays another Component and optionally displays more components on th...
Interface for listeners interested in BorderPanel related events.
static final Preferences PREFERENCES
The Preferences.
boolean isAlternativeLocation()
Returns whether the button is shown in the alternative location.
Definition: Tab.java:338
void closeInt(final Tab tab)
Closes a Tab but does not update the tab&#39;s open status.
Tab getActiveTab(@NotNull final Location location, final boolean alternativeLocation)
Returns the active Tab on a given Location of the main view.
void tabRemoved(@NotNull final DoubleButtonList buttonList)
Called whenever a Tab has been added from a ButtonList.
TabbedPanel(@NotNull final Component centerComponent)
Creates a new instance.
void unsetComponent(@NotNull final Location location, final boolean alternativeLocation)
Unsets the optional Component for a location.
DoubleButtonList toggleTabSplitMode(@NotNull final Tab tab)