// // TabBar class // part of the set of documents known as Java no sugar. // Copyright (c) 1996,1997 Sunil Gupta, sunil@magnetic.demon.co.uk // placed into the public domain by the author // import java.awt.*; import java.util.Vector; import TabEntry; public class TabBar extends Canvas { //################################################################## // //################################################################## public static final int SQUARE = 0; public static final int TOP_BEVELLED = 1; public static final int BOTTOM_BEVELLED = 3; public static final int CASCADED = 2; public static final int HORIZONTAL = 0; public static final int VERTICAL = 1; //################################################################## // //################################################################## public int vspace = 5; //pixels public int hspace = 10; //pixels public int font_height = -1; public int fixed_width = -1; public int stagger = 2; private Vector items = new Vector(); private int style = TOP_BEVELLED; private int orientation = HORIZONTAL; private boolean width_is_fixed = true; private boolean tab_pressed = false; private int active_index = 0; private long button_down_time=-1; private Color active_tab_colour = Color.yellow; private Color active_text_colour = Color.black; private Color inactive_tab_colour = Color.lightGray; private Color inactive_text_colour = Color.gray; //################################################################## // //################################################################## public TabBar( String string, char delim) { Vector tokens; String token; int index; tokens = tokenise_string(string, delim); for (index=0; index < tokens.size(); index++) { token = (String) tokens.elementAt(index); items.addElement( new TabEntry(token, index) ); } } public TabBar( TabEntry entries[]) { int index; for (index=0; index < entries.length; index++) items.addElement( entries[index]); } //################################################################## // //################################################################## //****************************************************************** // anything to do with graphics initialisation gets done in the addNotify public void addNotify() { super.addNotify(); set_item_widths(); resize(this.preferredSize()); } //****************************************************************** public Dimension minimumSize() { int w,h,i; TabEntry entry; w = 0; h = 0; if (orientation == HORIZONTAL) { h = font_height + 2*vspace; if (width_is_fixed) w = items.size() * (fixed_width + 2*hspace); else for (i =0; i < items.size(); i++) { entry = (TabEntry) items.elementAt(i); w += (2*hspace) +entry.width; } } else { w = font_height + (2*hspace); for (i =0; i < items.size(); i++) { entry = (TabEntry) items.elementAt(i); h += (2*vspace) + font_height* entry.caption.length(); } } return ( new Dimension (w,h)); } //****************************************************************** public Dimension preferredSize() { return minimumSize(); } //****************************************************************** public void paint (Graphics g) { Dimension size = size(); switch (style) { case SQUARE: draw_square_tabs(g); break; case TOP_BEVELLED: draw_bevelled_tabs(g,true); break; case BOTTOM_BEVELLED: draw_bevelled_tabs(g,false); break; case CASCADED: draw_cascaded_tabs(g); break; default: g.drawLine(1,1,size.width, size.height); break; } } //****************************************************************** public boolean handleEvent(Event e) { long time_diff; switch (e.id) { case Event.MOUSE_DOWN: set_active_tab(e.x, e.y); tab_pressed = true; repaint(); return true; case Event.MOUSE_UP: postEvent(new Event(this,Event.ACTION_EVENT,this)); tab_pressed = false; repaint(); return true; default: return super.handleEvent(e); } } //################################################################## // //################################################################## public void set_fixed_size( boolean flag) { if (flag != width_is_fixed) { width_is_fixed = flag; repaint(); } } //****************************************************************** public int getActiveTabID() { return active_index; } //****************************************************************** public String getActiveTabName() { TabEntry entry; int i; for (i =0; i < items.size(); i++) { entry = (TabEntry) items.elementAt(i); if (entry.ID == active_index) return entry.caption; } return null; } //****************************************************************** public void set_style( int style) { if (style != this.style) { this.style = style; repaint(); } } //################################################################## // //################################################################## private void draw_square_tabs(Graphics g) { int i, dx= 0; TabEntry entry; Color bg_colour, fg_colour; Dimension size=size(); boolean raised = true; int item_width; for ( i=0; i <items.size(); i++) { raised = true; entry = (TabEntry) items.elementAt(i); if (active_index == entry.ID) { bg_colour = active_tab_colour; fg_colour = active_text_colour; if (tab_pressed) raised = false; } else { bg_colour = inactive_tab_colour; fg_colour = inactive_text_colour; } if (width_is_fixed) item_width = fixed_width; else item_width = entry.width; g.setColor(bg_colour); g.fill3DRect(dx,0, item_width +(2*hspace), size.height, raised); g.setColor(fg_colour); g.drawString( entry.caption, dx+hspace, font_height + vspace); dx += (2*hspace) + item_width; } } //****************************************************************** // draw these backwards as they overlp from left to right private void draw_bevelled_tabs(Graphics g, boolean on_top) { int i,x; TabEntry entry; Color bg_colour, fg_colour; Dimension size=size(); int item_width, total_width; int y1,y2; Polygon poly; //-----------find total width-------------------------------- total_width= 0; if (width_is_fixed) total_width = items.size() *(fixed_width + 2*hspace); else for ( i=items.size(); i>0; i--) { entry = (TabEntry) items.elementAt(i-1); total_width += entry.width + (2*hspace); } if (on_top) { y1=size.height-1; y2=0; } else { y2=size.height-1; y1=0; } //-----------draw backwards----------------------------------- x= total_width; for ( i=items.size(); i>0; i--) { entry = (TabEntry) items.elementAt(i-1); if (width_is_fixed) item_width = fixed_width; else item_width = entry.width; x -= (2*hspace) + item_width; //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - if (active_index == entry.ID) { bg_colour = active_tab_colour; fg_colour = active_text_colour; } else { bg_colour = inactive_tab_colour; fg_colour = inactive_text_colour; } //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - g.setColor(bg_colour); poly = new Polygon(); poly.addPoint( x-hspace, y1); poly.addPoint( x, y2); poly.addPoint( x+item_width+hspace,y2); poly.addPoint( x+(2*hspace)+item_width, y1); g.fillPolygon(poly); g.setColor(Color.black); g.drawPolygon(poly); g.setColor(fg_colour); g.drawString( entry.caption, x+hspace, font_height + vspace); } } //****************************************************************** private void draw_cascaded_tabs(Graphics g) { } //################################################################## // //################################################################## private Vector tokenise_string(String string, char delim) { int start,end; Vector tokens = new Vector(); String substring; start = 0; end = string.indexOf(delim, start); while (end != -1) { if (end > start) { tokens.addElement( string.substring(start, end)); } start = end+1; end = string.indexOf(delim, start); } tokens.addElement( string.substring(start, string.length())); return tokens; } //****************************************************************** private void set_item_widths() { TabEntry entry; Graphics g; FontMetrics metrics; int i, item_width, max_width=-1; g = getGraphics(); metrics = g.getFontMetrics(); //-------get individual string widths---------------- for ( i=0 ; i<items.size() ; i++) { entry = (TabEntry) items.elementAt(i); item_width = metrics.stringWidth( entry.caption); entry.width = item_width; if (item_width > max_width) max_width = item_width; } //-------get overall font height-------------------- font_height = metrics.getHeight(); fixed_width = max_width; } //****************************************************************** private void set_active_tab(int x, int y) { int i, dx=0, end_x = x, width; TabEntry entry; for ( i=0 ; i<items.size() ; i++) { entry = (TabEntry) items.elementAt(i); if (width_is_fixed) width = fixed_width; else width = entry.width; end_x = dx+(2*hspace)+width; if ( (x > dx) && (x < end_x) ) { active_index = entry.ID; break; } dx = end_x; } } }
file: /Techref/language/java/nosugar/custom/tabs/code/TabBar.java, 9KB, , updated: 1997/1/27 20:18, local time: 2024/11/19 14:36,
18.117.230.165:LOG IN
|
©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions? <A HREF="http://massmind.org/techref/language/java/nosugar/custom/tabs/code/TabBar.java"> language java nosugar custom tabs code TabBar</A> |
Did you find what you needed? |
Welcome to massmind.org! |
Welcome to massmind.org! |
.