20 package net.sf.gridarta.gui.utils.tabbedpanel;
22 import java.awt.Component;
23 import java.awt.Dimension;
24 import java.awt.Insets;
25 import java.awt.event.MouseAdapter;
26 import java.awt.event.MouseEvent;
27 import java.awt.event.MouseListener;
28 import java.util.MissingResourceException;
29 import java.util.prefs.Preferences;
30 import javax.swing.AbstractButton;
31 import javax.swing.Action;
32 import javax.swing.JComponent;
33 import javax.swing.JMenu;
34 import javax.swing.JPopupMenu;
40 import net.
sf.japi.swing.action.ActionBuilder;
41 import net.
sf.japi.swing.action.ActionBuilderFactory;
42 import net.
sf.japi.swing.action.ToggleAction;
43 import org.jetbrains.annotations.NotNull;
44 import org.jetbrains.annotations.Nullable;
58 private static final int SPACE = 6;
82 private static final String
TAB_LOCATION =
"MainWindow.tab_location.";
94 private static final ActionBuilder
ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder(
"net.sf.gridarta");
135 private final JPopupMenu
popupMenu = ACTION_BUILDER.createPopupMenu(
false,
"tabButtonMenu");
198 public void mousePressed(@NotNull
final MouseEvent e) {
203 public void mouseReleased(@NotNull
final MouseEvent e) {
211 private void checkPopup(@NotNull
final MouseEvent e) {
212 if (e.isPopupTrigger()) {
213 getPopupMenu().show(e.getComponent(), e.getX(), e.getY());
229 public Tab(@NotNull
final String ident, @NotNull
final JComponent component, @NotNull
final Location defaultLocation,
final boolean alternativeLocation,
final int index,
final boolean defaultOpen) {
232 final Dimension preferredSize = component.getPreferredSize();
233 final int defaultWidth = PREFERENCES.getInt(TAB_WIDTH_PREFIX + ident, preferredSize.width);
234 final int defaultHeight = PREFERENCES.getInt(TAB_HEIGHT_PREFIX + ident, preferredSize.height);
235 final Dimension minimumSize = component.getMinimumSize();
236 width = Math.max(defaultWidth, minimumSize.width);
237 height = Math.max(defaultHeight, minimumSize.height);
238 open = PREFERENCES.getBoolean(TAB_OPEN_PREFIX + ident, defaultOpen);
240 final String locationString = PREFERENCES.get(TAB_LOCATION + ident, defaultLocation.toString());
242 location =
Location.valueOf(locationString);
243 }
catch (
final IllegalArgumentException ignored) {
244 location = defaultLocation;
246 this.alternativeLocation = PREFERENCES.getBoolean(TAB_ALT_LOCATION + ident, alternativeLocation);
251 throw new MissingResourceException(
"missing action ",
MenuUtils.class.getName(),
"tabButtonMoveTo");
262 final int acceleratorIndex = index < 10 ?
index : -1;
264 final AbstractButton result = location.
createButton((String) action.getValue(Action.NAME));
265 result.setMargin(location.
isTopOrBottom() ?
new Insets(0, SPACE, 0, SPACE) :
new Insets(SPACE, 0, SPACE, 0));
266 if (acceleratorIndex != -1) {
267 result.setMnemonic((
int)
'0' + index);
269 result.addMouseListener(mouseListener);
278 listeners.
add(listener);
286 listeners.
remove(listener);
349 PREFERENCES.putBoolean(TAB_ALT_LOCATION + ident, alternativeLocation);
350 if (splitModeAction != null) {
351 splitModeAction.setSelected(alternativeLocation);
369 if (this.location == location) {
375 PREFERENCES.put(TAB_LOCATION + ident, location.toString());
395 PREFERENCES.putInt(TAB_HEIGHT_PREFIX + ident, size);
398 PREFERENCES.putInt(TAB_WIDTH_PREFIX + ident, size);
415 if (this.open == open) {
420 PREFERENCES.putBoolean(TAB_OPEN_PREFIX + ident, open);
437 if (this.severity == severity) {
443 listener.severityChanged(severity);
static final String TAB_OPEN_PREFIX
The key used to store the open status of a tab.
JMenu getMoveToMenu()
Returns the "Move To" menu within the button's context menu.
static final String TAB_WIDTH_PREFIX
The key used to store the preferred width of a tab.
Graphical User Interface of Gridarta.
T [] getListeners()
Returns an array of all the listeners.
final JPopupMenu popupMenu
The button's context menu.
createButton
The location on the top side.
Interface for listeners interested in Tab related events.
final JMenu moveToMenu
The "Move To" menu within popupMenu.
A tab in a TabbedPanel component.
final TabButtonAction action
The button's action.
Location location
The tab's location.
Base package of all Gridarta classes.
static final Preferences PREFERENCES
The Preferences.
Location getLocation()
Returns the tab's location.
void addTabListener(@NotNull final TabListener listener)
Adds a TabListener to be notified.
void setSplitModeAction(@Nullable final ToggleAction splitModeAction)
Sets the ToggleAction to update if the "split mode" state changes.
boolean open
The tab's open status.
static final int SPACE
Additional space before and after the buttons' text.
void removeTabListener(@NotNull final TabListener listener)
Removes a TabListener to be notified.
Component getComponent()
Returns the Component that is shown when this tab is active.
void setLocation(@NotNull final Location location)
Sets the tab's location.
abstract boolean isTopOrBottom()
Returns whether this location is TOP or BOTTOM.
void remove(@NotNull final T listener)
Removes a listener.
static final String TAB_HEIGHT_PREFIX
The key used to store the preferred height of a tab.
final String ident
The tab's identification string.
void setSize(final int size)
Sets the tab's size.
int width
The tab's width when in locations Location#LEFT or Location#RIGHT.
Interface used as preferences location.
int height
The tab's height when in locations Location#TOP or Location#BOTTOM.
void add(@NotNull final T listener)
Adds a listener.
final MouseListener mouseListener
The MouseListener attached to button to open the context menu.
static final ActionBuilder ACTION_BUILDER
The ActionBuilder.
boolean alternativeLocation
Whether the tab is shown in the alternative location (.
static final String TAB_LOCATION
The key used to store the current location of a tab.
int getSize()
Returns the tab's size.
AbstractButton button
The AbstractButton for showing or hiding the JComponent.
Severity levels for colors of tabs.
void setAlternativeLocation(final boolean alternativeLocation)
Sets whether the button is shown in the alternative location.
Severity severity
The tab's Severity.
Type-safe version of EventListenerList.
JPopupMenu getPopupMenu()
Returns the button's context menu.
final EventListenerList2< TabListener > listeners
The registered TabListeners to notify.
AbstractButton getButton()
Returns the AbstractButton for showing or hiding the component.
AbstractButton createButton()
Creates a button for this tab.
boolean isOpen()
Returns the tab's open status.
DEFAULT
The tab contents are unchanged from defaults.
boolean isAlternativeLocation()
Returns whether the button is shown in the alternative location.
Tab(@NotNull final String ident, @NotNull final JComponent component, @NotNull final Location defaultLocation, final boolean alternativeLocation, final int index, final boolean defaultOpen)
Creates a new instance.
void setSeverity(@NotNull final Severity severity)
Sets the tab's Severity.
ToggleAction splitModeAction
The ToggleAction to update if the "split mode" state changes.
final int index
The tab's index for ordering.
static final String TAB_ALT_LOCATION
The key used to store the current location of a tab.
final JComponent component
The JComponent that is shown when this tab is active.
void setOpen(final boolean open)
Sets the tab's open status.
Severity getSeverity()
Returns the tab's Severity.