package net.xoetrope.swing;
import javax.swing.JTabbedPane;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.Hashtable;
import javax.swing.plaf.TabbedPaneUI;
import net.xoetrope.builder.XuiBuilder;
import net.xoetrope.xui.XAttributedComponent;
import net.xoetrope.xui.XProject;
import net.xoetrope.xui.XProjectManager;
import net.xoetrope.xui.helper.XuiUtilities;
/**
* A tab panel.
* <p> Copyright (c) Xoetrope Ltd., 2002-2004</p>
* <p> $Revision: 2.4 $</p>
* <p> License: see License.txt</p>
*/
public class XTabPanel extends JTabbedPane implements XAttributedComponent, MouseMotionListener
{
private Cursor handCursor, defaultCursor;
protected XProject currentProject = XProjectManager.getCurrentProject();
/**
* Create a new tab panel
*/
public XTabPanel()
{
defaultCursor = new Cursor( Cursor.DEFAULT_CURSOR );
handCursor = new Cursor( Cursor.HAND_CURSOR );
}
/**
* Add a new tab.
* The add method could not be overloaded so this method adds does the
* equivalent.
* @param comp The component to add
* @return the new component
*/
public Component add( Component comp )
{
if ( comp.getClass().getName().indexOf( "BasicTabbedPaneUI" ) >= 0 )
return super.add( comp );
String title = null;
try {
Hashtable attribs = XuiBuilder.getCurrentAttributes();
title = XuiUtilities.translate( currentProject, (String)attribs.get( "title" ));
}
catch ( Exception ex ) {
}
addTab( title, comp );
return comp;
}
/**
* Sets the hand cursor if the 'tabcursor' attribute is set in the component
* declaration XML.
* @param e the mouseevent triggering the call
*/
public void mouseMoved( MouseEvent e )
{
int tabcount = getTabCount();
int x = e.getX();
int y = e.getY();
boolean useHand = false;
for( int i = 0; i < tabcount; i++ ) {
TabbedPaneUI tpu = getUI();
Rectangle rect = tpu.getTabBounds( this, i );
if ( x > rect.x && x < rect.x + rect.width && y > rect.y && y < rect.y + rect.height )
useHand = true;
}
if ( useHand )
setCursor( handCursor );
else
setCursor( defaultCursor );
}
public void mouseDragged( MouseEvent e ){}
/**
* Set one or more attributes of the component.
* <UL>
* <LI>tabCursor true to change the cursor form a mouse move </LI>
* <LI>placement=int, set the placement of the tabs (top|bottom|left|right)</LI>
* </UL>
* @param attribName the name of the attribute
* @param attribValue the value of the attribute
* @return 0 for success, non zero for failure or to require some further
* action
*/
public int setAttribute( String attribName, Object attribValue )
{
String attribNameLwr = attribName.toLowerCase();
String attribValueStr = (String)attribValue;
String attribValueLwr = null;
if ( attribValue != null )
attribValueLwr = attribValueStr.toLowerCase();
if ( attribNameLwr.equals( "tabcursor" )) {
if ( attribValueLwr.equals( "true" ))
addMouseMotionListener( this );
}
else if ( attribNameLwr.equals( "placement" )) {
if ( attribValueLwr.equals( "top" ))
setTabPlacement( TOP );
else if ( attribValueLwr.equals( "bottom" ))
setTabPlacement( BOTTOM );
else if ( attribValueLwr.equals( "left" ))
setTabPlacement( LEFT );
else if ( attribValueLwr.equals( "right" ))
setTabPlacement( RIGHT );
}
else
return -1;
return 0;
}
}