com.magelang.tabsplitter
Class TabPanel

java.lang.Object
  |
  +--java.awt.Component
        |
        +--java.awt.Container
              |
              +--java.awt.Panel
                    |
                    +--com.magelang.tabsplitter.TabPanel
All Implemented Interfaces:
javax.accessibility.Accessible, java.awt.event.ActionListener, java.util.EventListener, java.awt.image.ImageObserver, java.awt.MenuContainer, java.awt.event.MouseListener, java.io.Serializable
Direct Known Subclasses:
TabSplitter

public class TabPanel
extends java.awt.Panel
implements java.awt.event.ActionListener, java.awt.event.MouseListener

TabPanel -- allows a user to select from several interface components by clicking a tab at the top of the panel. Note that this class is only dependent on the JDK; no other class libraries or files are necessary

Each contained component is represented by a tab at the top of the TabPanel, much like file folders in a file cabinet. When a tab is clicked, it becomes the "selected" tab and its associated component will be displayed.

There are two types of navigational aids provided with the TabPanel. If there are more tabs than can be displayed in the current window, two triangle buttons will appear. These buttons will scroll the set of tabs left and right.

There are also two buttons marked "+" and "-". These buttons move the user through each tab in succession.

To properly set up a tab panel, you need to do two things:

Note: It is extremely important that the user of this tab panel not try to directly use the layoutmanger (via getLayout() and setLayout() ). These two methods could not be overridden to prevent modification, as many GUI builders expect to use it. If you want to switch between tabs, use the "next()" and "previous()" methods provided by TabSplitter, not those of CardLayout.

Use this code at your own risk! MageLang Institute is not responsible for any damage caused directly or indirectly through use of this code.

SOFTWARE RIGHTS

TabSplitter, version 2.0, Scott Stanchfield, MageLang Institute

We reserve no legal rights to this code--it is fully in the public domain. An individual or company may do whatever they wish with source code distributed with it, including including the incorporation of it into commerical software.

However, this code cannot be sold as a standalone product.

We encourage users to develop software with this code. However, we do ask that credit is given to us for developing it By "credit", we mean that if you use these components or incorporate any source code into one of your programs (commercial product, research project, or otherwise) that you acknowledge this fact somewhere in the documentation, research report, etc... If you like these components and have developed a nice tool with the output, please mention that you developed it using these components. In addition, we ask that the headers remain intact in our source code. As long as these guidelines are kept, we expect to continue enhancing this system and expect to make other tools available as they are completed.

The MageLang Support Classes Gang:

See Also:
SplitterLayout, Serialized Form

Inner classes inherited from class java.awt.Panel
java.awt.Panel.AccessibleAWTPanel
 
Inner classes inherited from class java.awt.Container
java.awt.Container.AccessibleAWTContainer
 
Inner classes inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent
 
Field Summary
protected  java.util.Vector aTabSelectionListener
           
protected  java.awt.Polygon leftArrow
           
protected  boolean leftEnabled
           
protected  java.awt.Polygon rightArrow
           
protected  boolean rightEnabled
           
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
TabPanel()
          Constructor for the TabPanel
 
Method Summary
 void actionPerformed(java.awt.event.ActionEvent e)
          Handle the popup menu item selections
protected  void addImpl(java.awt.Component comp, java.lang.Object constraints, int index)
          Adds a component to the TabPanel
 void addTabSelectionListener(TabSelectionListener newListener)
          Add a listener who cares about tab selections
 java.lang.String[] determineTabText()
          Determines which text will be displayed in the tabs of the Tab panel.
protected  void determineVisible()
           
protected  void drawTabs(java.awt.Graphics g)
          Draws the tabs at the top of the tab panel
protected  void fireTabSelected(TabSelectionEvent e)
          let those who care know when a tab was selected
 java.awt.Color getBorderColor()
          Gets the borderColor property (java.awt.Color) value.
protected  java.lang.String getExplicitTabText(java.awt.Component c)
           
 int getFirstVisible()
          Get the number of the first-visible physical tab.
 java.awt.Insets getInsets()
          Returns the insets of the TabPanel.
protected  java.awt.PopupMenu getPopupMenu()
           
protected  int getPosition(java.lang.Object comp)
          Return the position of the component in the tabsplitter (its physical tab number)
 java.lang.String getSelectedName()
          Get the tab text of the currently-selected component
 int getSelectedTabNum()
          Get the number of the currently-selected tab
 java.awt.Color getTabBackground()
          Gets the color that's behind the tabs
 java.awt.Color getTabColor()
          Deprecated. the new tabColors property should be used instead.
 java.awt.Color[] getTabColors()
          Get the colors used to draw the tabs.
 java.awt.Color getTabColors(int index)
          get a specific tab color.
 java.lang.String[] getTabText()
          Gets the explicitly-set tab text array.
 java.lang.String getTabText(int index)
          Gets a specific string from the explicitly-set tab text array.
 java.lang.Object getVisibleComponent()
          Get the component that is currently visible
 int[] getVisibleComponentNum()
          Return an array containing the selected tab number.
protected  void mergeOrShow(int n)
          A convenience method so TabSplitter can add merge capability
 void mouseClicked(java.awt.event.MouseEvent e)
          Dummy method for the MouseListener interface...
 void mouseEntered(java.awt.event.MouseEvent e)
          Dummy method for the MouseListener interface...
 void mouseExited(java.awt.event.MouseEvent e)
          Dummy method for the MouseListener interface...
 void mousePressed(java.awt.event.MouseEvent e)
          handle the mouse button being pressed -- check for right mouse click to bring up popup menu
 void mouseReleased(java.awt.event.MouseEvent e)
          When the mouse is released, check for tab selection
 void next()
          Move to next tab, wrapping around to first tab if necessary.
 void paint(java.awt.Graphics g)
          Paints the tab panel image (tabs, border, background)
 void previous()
          Move to previous tab, wrapping around to last tab if necessary.
 void remove(java.awt.Component comp)
          Remove a component from the container.
 void remove(int index)
          Remove a component from the container.
 void removeAll()
          Remove all components from the container.
 void removeTabSelectionListener(TabSelectionListener newListener)
          Remove a TabSelectionListener.
protected  void selectTab(java.awt.event.MouseEvent e)
          Check to see if the user clicked on a tab or one of the tab panel navigation buttons.
 void setBorderColor(java.awt.Color borderColor)
          Sets the borderColor property (java.awt.Color) value.
protected  void setExplicitTabText(java.awt.Component c, java.lang.String text)
           
 void setFirstVisible(int value)
          Explicitly sets which tab is the first to be visible
 void setFont(java.awt.Font f)
          set the font to use on the tabs
 void setSelectedTabNum(int num)
          Explicitly pick the selected tab by number.
 void setTabBackground(java.awt.Color color)
          set the color to use behind the tabs
 void setTabColor(java.awt.Color color)
          Deprecated. the new tabColors property should be used instead.
 void setTabColors(java.awt.Color[] tabColors)
          Sets the colors to use when drawing the tabs.
 void setTabColors(int index, java.awt.Color tabColor)
          Sets the colors to use when drawing the tabs.
 void setTabText(int index, java.lang.String tabText)
          Sets the explicit tab text to use when drawing the tabs.
 void setTabText(java.lang.String[] tabText)
          Sets the explicit tab text to use when drawing the tabs.
protected  void setupTabPolygons()
          Creates a Polygon object for each tab.
 void shiftLeft()
          Shifts the row of tabs one position to the left
 void shiftRight()
          Shifts the row of tabs one position to the right
 void show(java.awt.Component comp)
          Select a tab by component
 void show(int n)
          Select a tab by number
 void show(java.lang.String tabName)
          Select a tab by text
 void showPhysicalTab(int n)
          Select a tab by number
protected  void showPopup(java.awt.event.MouseEvent e)
          bring up the popup menu for the tabsplitter
protected  boolean tabContains(int num, int x, int y)
          Check to see if the numbered tab contains an x,y location.
 void update(java.awt.Graphics g)
          Overridden to get rid of screen erase between drawings
 
Methods inherited from class java.awt.Panel
addNotify, getAccessibleContext
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getLayout, getListeners, getMaximumSize, getMinimumSize, getPreferredSize, insets, invalidate, isAncestorOf, layout, list, list, locate, minimumSize, paintComponents, paramString, preferredSize, print, printComponents, processContainerEvent, processEvent, removeContainerListener, removeNotify, setLayout, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addPropertyChangeListener, addPropertyChangeListener, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, disable, disableEvents, dispatchEvent, enable, enable, enableEvents, enableInputMethods, firePropertyChange, getBackground, getBounds, getBounds, getColorModel, getComponentOrientation, getCursor, getDropTarget, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getInputContext, getInputMethodRequests, getLocale, getLocation, getLocation, getLocationOnScreen, getName, getParent, getPeer, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isDisplayable, isDoubleBuffered, isEnabled, isFocusTraversable, isLightweight, isOpaque, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, printAll, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setForeground, setLocale, setLocation, setLocation, setName, setSize, setSize, setVisible, show, show, size, toString, transferFocus
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

leftArrow

protected transient java.awt.Polygon leftArrow

rightArrow

protected transient java.awt.Polygon rightArrow

leftEnabled

protected boolean leftEnabled

rightEnabled

protected boolean rightEnabled

aTabSelectionListener

protected transient java.util.Vector aTabSelectionListener
Constructor Detail

TabPanel

public TabPanel()
Constructor for the TabPanel
Method Detail

actionPerformed

public void actionPerformed(java.awt.event.ActionEvent e)
Handle the popup menu item selections
Specified by:
actionPerformed in interface java.awt.event.ActionListener
Parameters:
e - (java.awt.event.ActionEvent) -- the event that was fired to us

addImpl

protected void addImpl(java.awt.Component comp,
                       java.lang.Object constraints,
                       int index)
Adds a component to the TabPanel
Overrides:
addImpl in class java.awt.Container
Parameters:
comp - (Component) -- the component to be added
constraints - (Object) -- constraints on the component
index - (int) -- at which position will the component be added (-1 means at end)

addTabSelectionListener

public void addTabSelectionListener(TabSelectionListener newListener)
Add a listener who cares about tab selections
Parameters:
newListener - (TabSelectionListener) -- the component who cares...

determineTabText

public java.lang.String[] determineTabText()
Determines which text will be displayed in the tabs of the Tab panel. The text for each tab is determined as follows:
     If an explicit text were passed to add(), that text is used
       (note if the text happens to have the same value as that component's
        getName() call, it is not considered an explicit text)
     else if the component implements TabNamedComponent
       call its getTabName() method to determine the text
     else if the tabText[] propery was set and the
       text for that component is non-null
       use the tabText[tab number]
     else
       call getName() as a "last resort"
  

Note that this method should be used to figure out the tab text, not getTabText(), as getTabText() only gets the tabText[] property.

Returns:
An array of Strings that will appear on the tabs.
See Also:
getTabText(), setTabText(java.lang.String[])

determineVisible

protected void determineVisible()

drawTabs

protected void drawTabs(java.awt.Graphics g)
Draws the tabs at the top of the tab panel
Parameters:
g - (Graphics) -- the graphics context into which tabs are drawn

fireTabSelected

protected void fireTabSelected(TabSelectionEvent e)
let those who care know when a tab was selected
Parameters:
e - (TabSelectionEvent) -- the event to pass to the listeners
See Also:
addTabSelectionListener(com.magelang.tabsplitter.TabSelectionListener), removeTabSelectionListener(com.magelang.tabsplitter.TabSelectionListener)

getBorderColor

public java.awt.Color getBorderColor()
Gets the borderColor property (java.awt.Color) value. The borderColor is the color to paint behind the tabs and around the edge of the panel display area.
Returns:
The borderColor property value.
See Also:
setBorderColor(java.awt.Color)

getExplicitTabText

protected java.lang.String getExplicitTabText(java.awt.Component c)

getFirstVisible

public int getFirstVisible()
Get the number of the first-visible physical tab.
Returns:
The number of the tab that is visible.
See Also:
setFirstVisible(int)

getInsets

public java.awt.Insets getInsets()
Returns the insets of the TabPanel. The insets indicate the size of the border of the container.
Overrides:
getInsets in class java.awt.Container
See Also:
LayoutManager

getPopupMenu

protected java.awt.PopupMenu getPopupMenu()

getPosition

protected int getPosition(java.lang.Object comp)
Return the position of the component in the tabsplitter (its physical tab number)
Parameters:
comp - (Component) -- the component to search for.
Returns:
The position of the component (-1 means not found)

getSelectedName

public java.lang.String getSelectedName()
Get the tab text of the currently-selected component
Returns:
A String containing the currently-selected tab's text

getSelectedTabNum

public int getSelectedTabNum()
Get the number of the currently-selected tab
Returns:
an int for the currently-selected tab
See Also:
setSelectedTabNum(int)

getTabBackground

public java.awt.Color getTabBackground()
Gets the color that's behind the tabs
Returns:
The color behind the tabs
See Also:
setTabBackground(java.awt.Color)

getTabColor

public java.awt.Color getTabColor()
Deprecated. the new tabColors property should be used instead.

Get the color to use when drawing the tabs
See Also:
getTabColors()

getTabColors

public java.awt.Color[] getTabColors()
Get the colors used to draw the tabs. These colors are cycled through all tabs that are painted.
Returns:
The tabColors property value.
See Also:
setTabColors(java.awt.Color[])

getTabColors

public java.awt.Color getTabColors(int index)
get a specific tab color.
Parameters:
index - The index value into the property array.
Returns:
The tabColor property value.
See Also:
getTabColors(), setTabColors(java.awt.Color[])

getTabText

public java.lang.String[] getTabText()
Gets the explicitly-set tab text array. Note that this method should not be used to see the text displayed on the tabs; instead, determineTabText() should be used.
Returns:
The tabText property value.
See Also:
setTabText(java.lang.String[]), determineTabText()

getTabText

public java.lang.String getTabText(int index)
Gets a specific string from the explicitly-set tab text array. Note that this method should not be used to see the text displayed on the tabs; instead, determineTabText() should be used.
Returns:
The tabText property value.
See Also:
setTabText(java.lang.String[]), determineTabText()

getVisibleComponent

public java.lang.Object getVisibleComponent()
Get the component that is currently visible
Returns:
the component that is on the currently-selected tab (note that this method returns an Object because its subclass returns an array of visible comps)

getVisibleComponentNum

public int[] getVisibleComponentNum()
Return an array containing the selected tab number. (It's done this way to provide a consistent interface for TabSplitter.) If you are using TabPanel and just want the int, use getSelectedTabNum()
Returns:
an array containing the selected tab number.

mergeOrShow

protected void mergeOrShow(int n)
A convenience method so TabSplitter can add merge capability
Parameters:
n - (int) -- the target tab or a merge or tab to show

mouseClicked

public void mouseClicked(java.awt.event.MouseEvent e)
Dummy method for the MouseListener interface...
Specified by:
mouseClicked in interface java.awt.event.MouseListener

mouseEntered

public void mouseEntered(java.awt.event.MouseEvent e)
Dummy method for the MouseListener interface...
Specified by:
mouseEntered in interface java.awt.event.MouseListener

mouseExited

public void mouseExited(java.awt.event.MouseEvent e)
Dummy method for the MouseListener interface...
Specified by:
mouseExited in interface java.awt.event.MouseListener

mousePressed

public void mousePressed(java.awt.event.MouseEvent e)
handle the mouse button being pressed -- check for right mouse click to bring up popup menu
Specified by:
mousePressed in interface java.awt.event.MouseListener

mouseReleased

public void mouseReleased(java.awt.event.MouseEvent e)
When the mouse is released, check for tab selection
Specified by:
mouseReleased in interface java.awt.event.MouseListener
Parameters:
e - (MouseEvent)

next

public void next()
Move to next tab, wrapping around to first tab if necessary.

paint

public void paint(java.awt.Graphics g)
Paints the tab panel image (tabs, border, background)
Overrides:
paint in class java.awt.Container
Parameters:
g - (Graphics) The graphics context into which the tab panel is painted

previous

public void previous()
Move to previous tab, wrapping around to last tab if necessary.

remove

public void remove(int index)
Remove a component from the container.
Overrides:
remove in class java.awt.Container
Parameters:
index - (int) Which component to remove.

remove

public void remove(java.awt.Component comp)
Remove a component from the container.
Overrides:
remove in class java.awt.Container
Parameters:
comp - (Component) Which component to remove.

removeAll

public void removeAll()
Remove all components from the container. This method is overridden to force a repaint in design mode.
Overrides:
removeAll in class java.awt.Container

removeTabSelectionListener

public void removeTabSelectionListener(TabSelectionListener newListener)
Remove a TabSelectionListener.
Parameters:
newListener - com.magelang.tabsplitter.TabSelectionListener
See Also:
addTabSelectionListener(com.magelang.tabsplitter.TabSelectionListener)

selectTab

protected void selectTab(java.awt.event.MouseEvent e)
Check to see if the user clicked on a tab or one of the tab panel navigation buttons.
Parameters:
e - (java.awt.event.MouseEvent) The mouse click.

setBorderColor

public void setBorderColor(java.awt.Color borderColor)
Sets the borderColor property (java.awt.Color) value. The borderColor is the color to paint behind the tabs and around the edge of the panel display area.
Parameters:
borderColor - The new value for the property.
See Also:
getBorderColor()

setExplicitTabText

protected void setExplicitTabText(java.awt.Component c,
                                  java.lang.String text)

setFirstVisible

public void setFirstVisible(int value)
Explicitly sets which tab is the first to be visible
Parameters:
value - (int) the number for the first tab

setFont

public void setFont(java.awt.Font f)
set the font to use on the tabs
Overrides:
setFont in class java.awt.Container
Parameters:
f - (Font) -- the font to use when writing tab text

setSelectedTabNum

public void setSelectedTabNum(int num)
Explicitly pick the selected tab by number. Note that the number refers to the current tabs visible on the screen
Parameters:
num - int -- the tab number to select
See Also:
getSelectedTabNum()

setTabBackground

public void setTabBackground(java.awt.Color color)
set the color to use behind the tabs
Parameters:
color - (Color) the color to draw behind the tabs

setTabColor

public void setTabColor(java.awt.Color color)
Deprecated. the new tabColors property should be used instead.

Set the color to use when drawing the tabs
See Also:
setTabColors(java.awt.Color[])

setTabColors

public void setTabColors(java.awt.Color[] tabColors)
Sets the colors to use when drawing the tabs. This an an array of colors that will be cycled through, ie, if there are more tabs than colors, the next tab will reuse the first color.
Parameters:
tabColors - The new value for the property.
See Also:
getTabColors()

setTabColors

public void setTabColors(int index,
                         java.awt.Color tabColor)
Sets the colors to use when drawing the tabs. This an an array of colors that will be cycled through, ie, if there are more tabs than colors, the next tab will reuse the first color.
Parameters:
tabColors - The new value for the property.
See Also:
getTabColors()

setTabText

public void setTabText(java.lang.String[] tabText)
Sets the explicit tab text to use when drawing the tabs.
Parameters:
tabText - The new value for the property.
See Also:
getTabText(), determineTabText()

setTabText

public void setTabText(int index,
                       java.lang.String tabText)
Sets the explicit tab text to use when drawing the tabs.
Parameters:
index - (int) the specific text to set
tabText - The new value for the property.
See Also:
getTabText(), determineTabText()

setupTabPolygons

protected void setupTabPolygons()
Creates a Polygon object for each tab. This polygon object is used to draw the tab and to test if the user has clicked the mouse within that tab. The navigation buttons are also set up in this method.
Parameters:
g - (java.awt.Graphics) The graphics context into which the tabs will be drawn

shiftLeft

public void shiftLeft()
Shifts the row of tabs one position to the left

shiftRight

public void shiftRight()
Shifts the row of tabs one position to the right

show

public void show(int n)
Select a tab by number
Parameters:
n - (int) the number of the tab to select

show

public void show(java.awt.Component comp)
Select a tab by component
Parameters:
comp - (Component) the main component of the tab to select

show

public void show(java.lang.String tabName)
Select a tab by text
Parameters:
tabName - (String) the text of the tab to be selected

showPhysicalTab

public void showPhysicalTab(int n)
Select a tab by number
Parameters:
n - (int) the number of the tab to select

showPopup

protected void showPopup(java.awt.event.MouseEvent e)
bring up the popup menu for the tabsplitter
Parameters:
e - (MouseEvent) tells where to bring it up

tabContains

protected boolean tabContains(int num,
                              int x,
                              int y)
Check to see if the numbered tab contains an x,y location. Used to determine if the mouse was clicked in a given tab
Parameters:
num - (int) number of the tab to check
x - (int) x-coord to check
y - (int) y-coord to check
Returns:
true if the (x,y) point is in the tab; false otherwise

update

public void update(java.awt.Graphics g)
Overridden to get rid of screen erase between drawings
Overrides:
update in class java.awt.Container