1 15 16 package org.chateverywhere; 17 import java.applet.*; 18 import java.awt.*; 19 import java.util.*; 20 21 42 public class TabPanel extends Panel 43 { 44 45 public int margin = 3; 47 Font tabFont; FontMetrics metric; 49 int nCards = 0; Vector names = new Vector(10,10); int pos[], width[]; int selected = 0; int offset = 0; int tabH; int tabN = 12; int tabLeft[][] = new int[2][tabN]; int tabRight[][] = new int[2][tabN]; 58 Image offscreen=null; 59 60 61 public TabPanel() 62 { 63 setLayout(new CardLayout()); 64 setTabFont(new Font("Helvetica",Font.BOLD,12)); 65 } 66 67 68 int findComponent(Component c) 69 { for (int i=0; i<nCards; i++) 71 if (getComponent(i) == c) 72 return i; 73 74 return -1; 75 } 76 77 78 81 82 83 public Component add(String name, Component component) 84 { 85 name = name.intern(); 86 super.add(name,component); 88 if (!names.contains(name)) { names.addElement(name); nCards++; 91 if(isShowing()) { computeTabs(); repaint(); 93 } 94 } 95 96 return component; 97 } 98 99 100 101 102 public void remove(Component component) 103 { 104 int i = findComponent(component); 105 106 super.remove(component); names.removeElementAt(i); nCards--; 109 if (i < selected) { 110 setSelected(selected-1,true); 111 } else if ((i == selected) && (nCards > 0)) { setSelected(selected % nCards,true); 113 } 114 115 if (isShowing()) { computeTabs(); 117 repaint(); 118 } 119 } 120 121 122 public void remove(String name) 123 { 124 int i = names.indexOf(name.intern()); 125 126 if (i != -1) 127 remove(getComponent(i)); 128 } 129 130 131 public void removeAll() 132 { 133 super.removeAll(); 134 names.removeAllElements(); 135 repaint(); 136 } 137 138 141 void setSelected(int i, boolean force) 142 { 143 if (force || ((i != selected) && (i >= 0) && (i < nCards))) { 144 if (nCards > 0) { 145 selected = i % nCards; 146 } 147 148 ((CardLayout) getLayout()).show(this, (String ) names.elementAt(i)); 149 repaint(); 150 Component c = getComponent(i); 151 c.postEvent(new Event(c,Event.WINDOW_EXPOSE,this)); 152 } 153 } 154 155 156 public void first() { setSelected(0,false); } 157 158 159 public void last() { setSelected(nCards-1,false); } 160 161 162 public void next() { setSelected((selected+1) % nCards,false);} 163 164 165 public void previous() { setSelected((selected-1+nCards) % nCards,false); } 166 167 168 public void show(String name) { 169 setSelected(names.indexOf(name.intern()),false); } 170 171 172 public void show(Component component) 173 { 174 setSelected(findComponent(component),false); 175 } 176 177 int cardAt(int x, int y) 178 { 179 if (y <= tabH) { x += offset; 181 182 for(int i = 0; i < nCards; i++) 183 if ((pos[i]<=x) && (x<pos[i+1])) 184 return i; 185 } 186 return -1; 187 } 188 189 194 public String documentCard(String name) 195 { 196 return "Select tab card " + name; 197 } 198 199 Applet applet = null; 200 201 202 public boolean handleEvent(Event e) 203 { 204 if (e.id == Event.MOUSE_DOWN) { 205 int i = cardAt(e.x,e.y); 206 207 if (i != -1) { 208 setSelected(i,false); 209 return true; 210 } 211 } else if (e.id == Event.MOUSE_MOVE) { 212 if (applet == null) { 213 Component c = getParent(); 214 215 while (c != null) { 216 if (c instanceof Applet) 217 applet = (Applet) c; 218 219 c = c.getParent(); 220 } 221 } 222 223 if (applet != null) { 224 int i=cardAt(e.x,e.y); 225 226 if (i != -1) 227 applet.showStatus(documentCard((String ) names.elementAt(i))); 228 } 229 } else if (e.id == Event.MOUSE_EXIT) { 230 if (applet != null) 231 applet.showStatus(""); 232 } 233 234 return super.handleEvent(e); 235 } 236 237 239 240 241 public Insets insets() 242 { 243 return new Insets(tabH+margin,margin,margin,margin); 244 } 245 246 249 public void setTabFont(Font font) 250 { 251 tabFont = font; 252 metric = getFontMetrics(font); 253 int r = (metric.getHeight()+1)/2; 254 tabH = 2*r; 255 256 int c,s,nn = (tabN-2)/2; 258 double a; 259 260 for(int i=0; i<=nn; i++) { 261 a = Math.PI*i/2/nn; 262 c = (int)(r*Math.cos(a)); 263 s = (int)(r*Math.sin(a)); 264 tabLeft[0][i] = s; 265 tabLeft[1][i] = r + c; 266 tabLeft[0][i+nn]= tabH -c; 267 tabLeft[1][i+nn]= r - s; 268 } 269 270 tabLeft[0][2*nn+1] = tabH; 271 tabLeft[1][2*nn+1] = tabH; 272 273 for(int i=0; i< tabN; i++) { 274 tabRight[0][i] = -tabLeft[0][i]; 275 tabRight[1][i] = tabLeft[1][i]; 276 } 277 } 278 279 void computeTabs() 280 { if ((pos == null) || (pos.length <= nCards)) { 282 width = new int[nCards+1]; 283 pos = new int[nCards+1]; 284 } 286 int x = tabH/2; 287 for(int i=0; i<nCards; i++) { pos[i] = x; 289 width[i] = tabH + metric.stringWidth((String ) names.elementAt(i)); 290 x += width[i]; 291 } 292 293 pos[nCards] = x; 294 int w = size().width; 295 if ((offscreen==null) || (offscreen.getHeight(this)<tabH) 296 || (offscreen.getWidth(this)<w)) { 297 offscreen=createImage(w,tabH); 298 } 299 } 300 301 302 public void layout() 303 { 304 super.layout(); 305 computeTabs(); 306 } 308 310 311 void paintTabEdge(Graphics g, int x, int edges[][]) 312 { 313 g.translate(x,0); 314 g.setColor(getBackground()); 315 g.fillPolygon(edges[0],edges[1],tabN); 316 g.setColor(getForeground()); 317 318 for(int i=0; i<tabN-2;i++) 319 g.drawLine(edges[0][i],edges[1][i],edges[0][i+1],edges[1][i+1]); 320 321 g.translate(-x,0); 322 } 323 324 void paintTab(Graphics g, int x, int p) 325 { 326 int r = tabH/2, w = width[p]; 327 328 paintTabEdge(g,x-r,tabLeft); 329 paintTabEdge(g,x+w+r,tabRight); 330 g.setColor(getBackground()); 333 g.fillRect(x+r,0,w-tabH,tabH); 334 g.setColor(getForeground()); 335 g.drawLine(x+r,0,x+w-r,0); 336 g.setFont(tabFont); 337 g.drawString((String ) names.elementAt(p),x+r,tabH-metric.getDescent()); 338 } 339 340 342 public void update(Graphics g) 343 { 344 paint(g); 345 } 346 347 348 public void paint(Graphics gg) 349 { 350 Dimension sz = size(); 351 Graphics g = offscreen.getGraphics(); 352 int x,w = sz.width-1, h = sz.height-1, r = tabH/2; 353 int j, s = selected; 354 int shadow = 4; int nShadows = 3; Color backback= getParent().getBackground(); 359 360 g.setColor(getParent().getBackground()); 362 g.fillRect(0,0,w+1,tabH); 363 g.setColor(getForeground()); 364 365 if (nCards == 0) 366 g.drawLine(0,tabH,w,tabH); 367 else { 368 int offmax = pos[s] - r - Math.min(nShadows,s)*shadow, 370 offmin = pos[s+1] - w + r + Math.min(nCards-s,nShadows)*shadow; 371 372 if ((offset < offmin) || (offset > offmax)) 373 offset = Math.min(Math.max(0,(offmin+offmax)/2),pos[nCards]+r-w); 374 375 for(j = 0, x = offset+r;(j < s) && (pos[j] <= x); j++); 378 if (j > 0) { 379 x = 0; 380 381 for(int i=Math.max(0,j-nShadows); i<j-1; i++, x+=shadow) 382 paintTabEdge(g,x,tabLeft); 383 384 paintTab(g,x+r,j-1); 385 } 386 387 for(int i = j; i < s; i++) { 388 paintTab(g,pos[i]-offset,i); 389 } 390 391 for(j = nCards-1, x = offset+w-r; (j > s) && (pos[j+1] >= x); j--); 393 394 if (j < nCards-1) { 395 x = w; 396 397 for(int i=Math.min(nCards-1,j+nShadows); i>j+1; i--, x-=shadow) 398 paintTabEdge(g,x,tabRight); 399 400 paintTab(g,x-r-width[j+1],j+1); 401 } 402 403 for(int i = j; i > s; i--) { 404 paintTab(g,pos[i]-offset,i); 405 } 406 407 g.clearRect(pos[s]-r-offset+2,tabH-1,width[s]+tabH-1,1); 409 paintTab(g,pos[s]-offset,s); 410 g.drawLine(0,tabH-1,pos[s]-r-offset+1,tabH-1); 412 g.drawLine(pos[s+1]+r-offset-1,tabH-1,w,tabH-1); 413 } 414 415 gg.drawImage(offscreen,0,0,this); 416 gg.drawLine(w,tabH,w,h); 417 gg.drawLine(w,h,0,h); 418 gg.drawLine(0,h,0,tabH); 419 } 420 } 421 422 | Popular Tags |