TabControlpublic class TabControl extends Panel implements ComponentListener, MouseListener, KeyListener, FocusListener
Fields Summary |
---|
public static final int | ALIGN_TOP | public static final int | ALIGN_LEFT | private Panel | panelPageContainer | private CardLayout | layoutCard | private int | nCurrentPage | private Button | buttonFocus | private boolean | boolFocus | private int | nAlignment | private int | MARGIN_PAGE_VERT | private int | MARGIN_PAGE_HORZ | private String | strPageToShowAfterPaint | private Cursor | cursorNormal | private Cursor | cursorWait | private Vector | vectorTabs | private int | nTabHeightMax | private int | nTabWidthMax | private int | nRowCount |
Constructors Summary |
---|
public TabControl()
this ( ALIGN_TOP );
| public TabControl(int nAlignment)
super ();
this.nAlignment = nAlignment;
try {
init();
}
catch (Exception e) {
e.printStackTrace();
}
|
Methods Summary |
---|
public int | addPage(java.awt.Panel panelPage, java.lang.String strTitle)
int nIndex;
nIndex = addPage ( panelPage, strTitle, null );
return ( nIndex );
| public int | addPage(java.awt.Panel panelPage, java.lang.String strTitle, java.awt.Image image)
int nIndex;
Dimension dim;
TabField tabField;
nIndex = vectorTabs.size ();
tabField = new TabField ( this, panelPage, strTitle, image );
vectorTabs.addElement ( tabField );
panelPageContainer.add ( panelPage, strTitle );
if ( nIndex == 0 ) {
nCurrentPage = 0;
layoutCard.show ( panelPageContainer, strTitle );
}
tabField.calculateTabDimension ( getFontMetrics(getFont()) );
nTabHeightMax = Math.max ( tabField.dim.height, nTabHeightMax );
nTabWidthMax = Math.max ( tabField.dim.width, nTabWidthMax );
recalculateTabs ();
repaint ();
return ( nIndex );
| public void | componentHidden(java.awt.event.ComponentEvent event)
| public void | componentMoved(java.awt.event.ComponentEvent event)
| public void | componentResized(java.awt.event.ComponentEvent event)
recalculateTabs ();
this.doLayout ();
panelPageContainer.validate ();
| public void | componentShown(java.awt.event.ComponentEvent event)
| private void | drawDot(java.awt.Graphics graphics, int nX, int nY)
if ( (nX + nY) % 2 == 0 )
graphics.drawLine ( nX, nY, nX, nY );
| private void | drawDottedLine(java.awt.Graphics graphics, int nX1, int nY1, int nX2, int nY2)
int nX, nY;
double dDiv;
if ( nX1 == nX2 && nY1 == nY2 ) {
drawDot ( graphics, nX1, nY1 );
return;
}
if ( nX1 > nX2 ) {
nX = nX1;
nX1 = nX2;
nX2 = nX;
}
if ( nY1 > nY2 ) {
nY = nY1;
nY1 = nY2;
nY2 = nY;
}
if ( nX2 - nX1 > nY2 - nY1 ) {
dDiv = (double)(nY2 - nY1) / (nX2 - nX1);
for ( nX = nX1; nX <= nX2; nX++ ) {
nY = (int)Math.rint ( nY1 + (nX - nX1) * dDiv );
drawDot ( graphics, nX, nY );
}
}
else {
dDiv = (nX2 - nX1) / (nY2 - nY1);
for ( nY = nY1; nY <= nY2; nY++ ) {
nX = (int)Math.rint ( nX1 + (nY - nY1) * dDiv );
drawDot ( graphics, nX, nY );
}
}
| private void | drawDottedRectangle(java.awt.Graphics graphics, int nX, int nY, int nWidth, int nHeight)
drawDottedLine ( graphics, nX, nY, nX + nWidth - 1, nY );
drawDottedLine ( graphics, nX + nWidth - 1, nY, nX + nWidth - 1, nY + nHeight - 1 );
drawDottedLine ( graphics, nX + nWidth - 1, nY + nHeight - 1, nX, nY + nHeight - 1 );
drawDottedLine ( graphics, nX, nY + nHeight - 1, nX, nY );
| private int | findPage(java.awt.Panel panelPage)
int i;
TabField tabField;
i = vectorTabs.size() - 1;
while ( i >= 0 ) {
tabField = (TabField) vectorTabs.elementAt ( i );
if ( tabField.panelPage == panelPage )
break;
i--;
}
return ( i );
| public void | focusGained(java.awt.event.FocusEvent event)
if ( boolFocus == true )
return;
boolFocus = true;
repaint ();
| public void | focusLost(java.awt.event.FocusEvent event)
if ( boolFocus == false )
return;
boolFocus = false;
repaint ();
| public java.awt.Insets | getInsets()
Insets insets;
insets = super.getInsets ();
if ( nAlignment == ALIGN_LEFT )
insets = new Insets ( insets.top + MARGIN_PAGE_VERT,
insets.left + nRowCount * nTabWidthMax - 2 + MARGIN_PAGE_HORZ,
insets.bottom + MARGIN_PAGE_VERT,
insets.right + MARGIN_PAGE_HORZ );
else // if ( nAlignment == ALIGN_TOP )
insets = new Insets ( insets.top + nRowCount * nTabHeightMax - 2 + MARGIN_PAGE_VERT,
insets.left + MARGIN_PAGE_HORZ,
insets.bottom + MARGIN_PAGE_VERT,
insets.right + MARGIN_PAGE_HORZ );
return ( insets );
| public java.awt.Dimension | getPreferredSize()
int i;
TabField tabField;
Dimension dim;
int nRowWidth;
dim = super.getPreferredSize ();
if ( nAlignment == ALIGN_LEFT )
dim.height = Math.max ( dim.height, nTabHeightMax * vectorTabs.size() + 1 );
else { // if ( nAlignment == ALIGN_TOP )
nRowWidth = 0;
for ( i = 0; i < vectorTabs.size(); i++ ) {
tabField = (TabField) vectorTabs.elementAt ( i );
nRowWidth += tabField.dim.width;
}
dim.width = Math.max ( dim.width, nRowWidth + 1 );
}
return ( dim );
| private void | init()
Panel panel;
Font font;
Font fontOld;
this.setLayout ( new BorderLayout() );
this.addComponentListener ( this );
this.addMouseListener ( this );
buttonFocus = new Button ( "Focus" );
this.add ( buttonFocus );
buttonFocus.addKeyListener ( this );
buttonFocus.addFocusListener ( this );
panelPageContainer = new Panel ( layoutCard );
this.add ( panelPageContainer, BorderLayout.CENTER );
fontOld = panelPageContainer.getFont ();
if ( fontOld == null ) {
fontOld = new Font ( "Dialog", Font.PLAIN, 12 );
}
font = new Font ( "Dialog", Font.PLAIN, 12 );
this.setFont ( font );
panelPageContainer.setFont ( fontOld );
this.setBackground ( TabField.COLOR_BG );
panelPageContainer.setBackground ( TabField.COLOR_BG );
| public void | keyPressed(java.awt.event.KeyEvent event)
int nKeyCode;
int nIndex;
TabField tabField;
nIndex = nCurrentPage;
nKeyCode = event.getKeyCode ();
if ( nKeyCode == KeyEvent.VK_DOWN || nKeyCode == KeyEvent.VK_RIGHT ) {
nIndex++;
}
else if ( nKeyCode == KeyEvent.VK_UP || nKeyCode == KeyEvent.VK_LEFT ) {
nIndex--;
}
if ( nIndex >= vectorTabs.size() )
nIndex = vectorTabs.size() - 1;
if ( nIndex < 0 )
nIndex = 0;
if ( nCurrentPage != nIndex ) {
nCurrentPage = nIndex;
tabField = (TabField) vectorTabs.elementAt ( nIndex );
strPageToShowAfterPaint = tabField.strTitle;
this.setCursor ( cursorWait );
repaint ();
}
| public void | keyReleased(java.awt.event.KeyEvent event)
| public void | keyTyped(java.awt.event.KeyEvent event)
| public void | mouseClicked(java.awt.event.MouseEvent event)
| public void | mouseEntered(java.awt.event.MouseEvent event)
| public void | mouseExited(java.awt.event.MouseEvent event)
| public void | mousePressed(java.awt.event.MouseEvent event)
int i;
int nTabCount;
TabField tabField;
int x, y;
x = event.getX ();
y = event.getY ();
nTabCount = vectorTabs.size ();
for ( i = 0; i < nTabCount; i++ ) {
tabField = (TabField) vectorTabs.elementAt ( i );
if ( tabField.rect.contains (x,y) ) {
buttonFocus.requestFocus ();
nCurrentPage = i;
strPageToShowAfterPaint = tabField.strTitle;
this.setCursor ( cursorWait );
repaint ();
break;
}
}
| public void | mouseReleased(java.awt.event.MouseEvent event)
| public void | paint(java.awt.Graphics graphics)
int i;
int nSize;
Rectangle rect;
TabField tabField;
Rectangle rectClient;
Rectangle rectString;
String stringTabName;
Font fontNormal;
Font fontBold;
FontMetrics fontMetrics;
int nX, nY;
int nWidth, nHeight;
super.paint ( graphics );
rectClient = this.getBounds ();
rectClient.x = 0;
rectClient.y = 0;
rect = new Rectangle ( rectClient );
if ( nAlignment == ALIGN_LEFT ) {
rect.x += nTabWidthMax * nRowCount - 2;
rect.width -= nTabWidthMax * nRowCount - 2;
}
else { // if ( nAlignment == ALIGN_TOP )
rect.y += nTabHeightMax * nRowCount - 2;
rect.height -= nTabHeightMax * nRowCount - 2;
}
rect.width--;
rect.height--;
graphics.setColor ( TabField.COLOR_SHADOW_BOTTOM );
graphics.drawRect ( rect.x, rect.y, rect.width, rect.height );
graphics.setColor ( TabField.COLOR_SHADOW_TOP );
graphics.drawLine ( rect.x + 1, rect.y + 1, rect.x + 1, rect.y + rect.height - 2 );
graphics.drawLine ( rect.x + 1, rect.y + 1, rect.x + rect.width - 2, rect.y + 1 );
fontNormal = this.getFont ();
// fontBold = new Font ( fontNormal.getName(), Font.BOLD, fontNormal.getSize() );
fontBold = fontNormal;
nSize = vectorTabs.size ();
for ( i = nSize - 1; i >= 0; i-- ) {
tabField = (TabField) vectorTabs.elementAt ( i );
if ( i == nCurrentPage ) {
if ( nAlignment == ALIGN_LEFT )
tabField.drawCurrentTabLeft ( graphics );
else // if ( nAlignment == ALIGN_TOP )
tabField.drawCurrentTabTop ( graphics );
}
else {
if ( nAlignment == ALIGN_LEFT )
tabField.drawTabLeft ( graphics );
else // if ( nAlignment == ALIGN_TOP )
tabField.drawTabTop ( graphics );
}
graphics.setColor ( this.getForeground() );
rectString = new Rectangle ( tabField.rect );
if ( nAlignment == ALIGN_LEFT )
rectString.width = nTabWidthMax;
else // if ( nAlignment == ALIGN_TOP )
rectString.height = nTabHeightMax;
if ( tabField.image != null ) {
nWidth = tabField.image.getWidth ( this );
rectString.x += nWidth + tabField.MARGIN_TAB_HORZ;
rectString.width -= nWidth + tabField.MARGIN_TAB_HORZ;
}
rectString.y++;
if ( i == nCurrentPage ) {
graphics.setFont ( fontBold );
fontMetrics = graphics.getFontMetrics ( fontBold );
if ( boolFocus == true ) {
nWidth = fontMetrics.stringWidth ( tabField.strTitle ) + 6;
nHeight = fontMetrics.getHeight () + 1;
nX = rectString.x + (rectString.width - nWidth) / 2;
nY = rectString.y + (nTabHeightMax - 2 - nHeight) / 2 + 1;
drawDottedRectangle ( graphics, nX, nY, nWidth, nHeight );
}
}
else {
graphics.setFont ( fontNormal );
fontMetrics = graphics.getFontMetrics ( fontNormal );
}
nX = rectString.x + (rectString.width - fontMetrics.stringWidth(tabField.strTitle)) / 2;
nY = rectString.y + rectString.height - (rectString.height - fontMetrics.getHeight()) / 2 - fontMetrics.getMaxDescent ();
nY--;
if ( i != nCurrentPage ) {
nX++;
nY++;
}
graphics.drawString ( tabField.strTitle, nX, nY );
if ( tabField.image != null ) {
nHeight = tabField.image.getHeight ( this );
nX = tabField.rect.x + tabField.MARGIN_TAB_HORZ;
nY = tabField.rect.y + (nTabHeightMax - nHeight) / 2;
if ( i != nCurrentPage ) {
nX++;
nY++;
}
graphics.drawImage ( tabField.image, nX, nY, this );
}
}
if ( strPageToShowAfterPaint != null ) {
layoutCard.show ( panelPageContainer, strPageToShowAfterPaint );
strPageToShowAfterPaint = null;
this.setCursor ( cursorNormal );
}
| private void | recalculateTabs()
int i, j;
int nTabCount;
int nRowSize = 1;
int nRowIndex;
Rectangle rectClient;
int nOffsetX;
int nOffsetY;
int nRowWidth;
TabField tabField;
rectClient = this.getBounds ();
rectClient.x = 0;
rectClient.y = 0;
if ( rectClient.width < 1 || rectClient.height < 1 )
return;
nTabCount = vectorTabs.size ();
if ( nAlignment == ALIGN_LEFT ) {
nRowSize = rectClient.height / nTabHeightMax;
nRowCount = (nTabCount + nRowSize - 1) / nRowSize;
nOffsetX = nRowCount * nTabWidthMax;
nOffsetY = 0;
for ( i = 0; i < nTabCount; i++ ) {
if ( (i % nRowSize) == 0 ) {
nOffsetX -= nTabWidthMax;
nOffsetY = 0;
}
tabField = (TabField) vectorTabs.elementAt ( i );
tabField.rect.x = nOffsetX;
tabField.rect.y = nOffsetY;
tabField.rect.width = nTabWidthMax * (i / nRowSize + 1);
tabField.rect.height = nTabHeightMax;
tabField.nRowIndex = i / nRowSize;
if ( tabField.nRowIndex > 0 )
tabField.rect.width -= 2;
nOffsetY += nTabHeightMax;
}
}
else { // if ( nAlignment == ALIGN_TOP )
nRowCount = 1;
nRowWidth = 0;
for ( i = 0; i < nTabCount; i++ ) {
tabField = (TabField) vectorTabs.elementAt ( i );
if ( nRowWidth + tabField.dim.width > rectClient.width ) {
nRowWidth = 0;
nRowCount++;
}
nRowWidth += tabField.dim.width;
}
nOffsetX = 0;
nOffsetY = nRowCount * nTabHeightMax;
nRowIndex = 0;
j = 0;
for ( i = 0; i < nTabCount; i++ ) {
if ( i == j ) { // start of the row
nOffsetX = 0;
nOffsetY -= nTabHeightMax;
nRowWidth = 0;
for ( j = i; j < nTabCount; j++ ) {
tabField = (TabField) vectorTabs.elementAt ( j );
if ( j > i && nRowWidth + tabField.dim.width > rectClient.width )
break;
nRowWidth += tabField.dim.width;
tabField.nRowIndex = nRowIndex;
}
nRowSize = j - i;
nRowIndex++;
}
tabField = (TabField) vectorTabs.elementAt ( i );
tabField.rect.x = nOffsetX;
tabField.rect.y = nOffsetY;
tabField.rect.width = tabField.dim.width;
if ( nRowCount > 1 && nRowIndex < nRowCount ) {
tabField.rect.width += (rectClient.width - nRowWidth - 1) / nRowSize;
tabField.rect.width += (j - i > (rectClient.width - nRowWidth - 1) % nRowSize) ? 0 : 1;
}
tabField.rect.height = nTabHeightMax * nRowIndex;
if ( tabField.nRowIndex > 0 )
tabField.rect.height -= 2;
nOffsetX += tabField.rect.width;
}
}
repaint();
| public int | setPageImage(java.awt.Panel panelPage, java.awt.Image imageTab)
int i;
int nCount;
TabField tabField;
int nIndex;
Dimension dim;
nIndex = findPage ( panelPage );
if ( nIndex < 0 || nIndex >= vectorTabs.size() )
return ( nIndex );
tabField = (TabField) vectorTabs.elementAt ( nIndex );
if ( tabField.image == imageTab )
return ( nIndex );
tabField.image = imageTab;
nTabHeightMax = 1;
nTabWidthMax = 1;
nCount = vectorTabs.size ();
for ( i = 0; i < nCount; i++ ) {
tabField = (TabField) vectorTabs.elementAt ( i );
tabField.calculateTabDimension ( getFontMetrics(getFont()) );
nTabHeightMax = Math.max ( tabField.dim.height, nTabHeightMax );
nTabWidthMax = Math.max ( tabField.dim.width, nTabWidthMax );
}
recalculateTabs ();
repaint ();
return ( nIndex );
| public void | update(java.awt.Graphics g)
Rectangle rectClient;
Image image;
Graphics graphics;
rectClient = this.getBounds ();
// Safeguard to prevent an exception on 8-bit displays
if (rectClient.width < 1 || rectClient.height < 1)
return;
image = createImage ( rectClient.width, rectClient.height );
if ( image != null )
graphics = image.getGraphics ();
else
graphics = g;
paint ( graphics );
if ( image != null )
g.drawImage ( image, 0, 0, this );
|
|