1 7 package javax.swing; 8 9 import java.awt.Component ; 10 import java.util.ArrayList ; 11 import java.util.Hashtable ; 12 import java.awt.Color ; 13 import java.awt.Graphics ; 14 import java.awt.Rectangle ; 15 16 import javax.accessibility.*; 17 18 134 public class JLayeredPane extends JComponent implements Accessible { 135 137 public final static Integer DEFAULT_LAYER = new Integer (0); 138 139 public final static Integer PALETTE_LAYER = new Integer (100); 140 141 public final static Integer MODAL_LAYER = new Integer (200); 142 143 public final static Integer POPUP_LAYER = new Integer (300); 144 145 public final static Integer DRAG_LAYER = new Integer (400); 146 152 public final static Integer FRAME_CONTENT_LAYER = new Integer (-30000); 153 154 155 public final static String LAYER_PROPERTY = "layeredContainerLayer"; 156 private Hashtable <Component ,Integer > componentToLayer; 158 private boolean optimizedDrawingPossible = true; 159 160 161 165 public JLayeredPane() { 166 setLayout(null); 167 } 168 169 private void validateOptimizedDrawing() { 170 boolean layeredComponentFound = false; 171 synchronized(getTreeLock()) { 172 Integer layer = null; 173 174 for (Component c : getComponents()) { 175 layer = null; 176 if(c instanceof JInternalFrame || (c instanceof JComponent && 177 (layer = (Integer )((JComponent )c).getClientProperty( 178 LAYER_PROPERTY)) != null)) { 179 if(layer != null && layer.equals(FRAME_CONTENT_LAYER)) 180 continue; 181 layeredComponentFound = true; 182 break; 183 } 184 } 185 } 186 187 if(layeredComponentFound) 188 optimizedDrawingPossible = false; 189 else 190 optimizedDrawingPossible = true; 191 } 192 193 protected void addImpl(Component comp, Object constraints, int index) { 194 int layer = DEFAULT_LAYER.intValue(); 195 int pos; 196 197 if(constraints instanceof Integer ) { 198 layer = ((Integer )constraints).intValue(); 199 setLayer(comp, layer); 200 } else 201 layer = getLayer(comp); 202 203 pos = insertIndexForLayer(layer, index); 204 super.addImpl(comp, constraints, pos); 205 comp.validate(); 206 comp.repaint(); 207 validateOptimizedDrawing(); 208 } 209 210 217 public void remove(int index) { 218 Component c = getComponent(index); 219 super.remove(index); 220 if (c != null && !(c instanceof JComponent )) { 221 getComponentToLayer().remove(c); 222 } 223 validateOptimizedDrawing(); 224 } 225 226 231 public void removeAll() { 232 Component [] children = getComponents(); 233 Hashtable cToL = getComponentToLayer(); 234 for (int counter = children.length - 1; counter >= 0; counter--) { 235 Component c = children[counter]; 236 if (c != null && !(c instanceof JComponent )) { 237 cToL.remove(c); 238 } 239 } 240 super.removeAll(); 241 } 242 243 250 public boolean isOptimizedDrawingEnabled() { 251 return optimizedDrawingPossible; 252 } 253 254 255 267 public static void putLayer(JComponent c, int layer) { 268 Integer layerObj; 270 271 layerObj = new Integer (layer); 272 c.putClientProperty(LAYER_PROPERTY, layerObj); 273 } 274 275 282 public static int getLayer(JComponent c) { 283 Integer i; 284 if((i = (Integer )c.getClientProperty(LAYER_PROPERTY)) != null) 285 return i.intValue(); 286 return DEFAULT_LAYER.intValue(); 287 } 288 289 301 public static JLayeredPane getLayeredPaneAbove(Component c) { 302 if(c == null) return null; 303 304 Component parent = c.getParent(); 305 while(parent != null && !(parent instanceof JLayeredPane )) 306 parent = parent.getParent(); 307 return (JLayeredPane )parent; 308 } 309 310 318 public void setLayer(Component c, int layer) { 319 setLayer(c, layer, -1); 320 } 321 322 332 public void setLayer(Component c, int layer, int position) { 333 Integer layerObj; 334 layerObj = getObjectForLayer(layer); 335 336 if(layer == getLayer(c) && position == getPosition(c)) { 337 repaint(c.getBounds()); 338 return; 339 } 340 341 if(c instanceof JComponent ) 343 ((JComponent )c).putClientProperty(LAYER_PROPERTY, layerObj); 344 else 345 getComponentToLayer().put((Component )c, layerObj); 346 347 if(c.getParent() == null || c.getParent() != this) { 348 repaint(c.getBounds()); 349 return; 350 } 351 352 int index = insertIndexForLayer(c, layer, position); 353 354 setComponentZOrder(c, index); 355 repaint(c.getBounds()); 356 } 357 358 364 public int getLayer(Component c) { 365 Integer i; 366 if(c instanceof JComponent ) 367 i = (Integer )((JComponent )c).getClientProperty(LAYER_PROPERTY); 368 else 369 i = (Integer )getComponentToLayer().get((Component )c); 370 371 if(i == null) 372 return DEFAULT_LAYER.intValue(); 373 return i.intValue(); 374 } 375 376 385 public int getIndexOf(Component c) { 386 int i, count; 387 388 count = getComponentCount(); 389 for(i = 0; i < count; i++) { 390 if(c == getComponent(i)) 391 return i; 392 } 393 return -1; 394 } 395 402 public void moveToFront(Component c) { 403 setPosition(c, 0); 404 } 405 406 413 public void moveToBack(Component c) { 414 setPosition(c, -1); 415 } 416 417 431 public void setPosition(Component c, int position) { 432 setLayer(c, getLayer(c), position); 433 } 434 435 445 public int getPosition(Component c) { 446 int i, count, startLayer, curLayer, startLocation, pos = 0; 447 448 count = getComponentCount(); 449 startLocation = getIndexOf(c); 450 451 if(startLocation == -1) 452 return -1; 453 454 startLayer = getLayer(c); 455 for(i = startLocation - 1; i >= 0; i--) { 456 curLayer = getLayer(getComponent(i)); 457 if(curLayer == startLayer) 458 pos++; 459 else 460 return pos; 461 } 462 return pos; 463 } 464 465 471 public int highestLayer() { 472 if(getComponentCount() > 0) 473 return getLayer(getComponent(0)); 474 return 0; 475 } 476 477 483 public int lowestLayer() { 484 int count = getComponentCount(); 485 if(count > 0) 486 return getLayer(getComponent(count-1)); 487 return 0; 488 } 489 490 496 public int getComponentCountInLayer(int layer) { 497 int i, count, curLayer; 498 int layerCount = 0; 499 500 count = getComponentCount(); 501 for(i = 0; i < count; i++) { 502 curLayer = getLayer(getComponent(i)); 503 if(curLayer == layer) { 504 layerCount++; 505 } else if(layerCount > 0 || curLayer < layer) { 507 break; 508 } 509 } 510 511 return layerCount; 512 } 513 514 520 public Component [] getComponentsInLayer(int layer) { 521 int i, count, curLayer; 522 int layerCount = 0; 523 Component [] results; 524 525 results = new Component [getComponentCountInLayer(layer)]; 526 count = getComponentCount(); 527 for(i = 0; i < count; i++) { 528 curLayer = getLayer(getComponent(i)); 529 if(curLayer == layer) { 530 results[layerCount++] = getComponent(i); 531 } else if(layerCount > 0 || curLayer < layer) { 533 break; 534 } 535 } 536 537 return results; 538 } 539 540 545 public void paint(Graphics g) { 546 if(isOpaque()) { 547 Rectangle r = g.getClipBounds(); 548 Color c = getBackground(); 549 if(c == null) 550 c = Color.lightGray; 551 g.setColor(c); 552 if (r != null) { 553 g.fillRect(r.x, r.y, r.width, r.height); 554 } 555 else { 556 g.fillRect(0, 0, getWidth(), getHeight()); 557 } 558 } 559 super.paint(g); 560 } 561 562 566 571 protected Hashtable <Component ,Integer > getComponentToLayer() { 572 if(componentToLayer == null) 573 componentToLayer = new Hashtable <Component ,Integer >(4); 574 return componentToLayer; 575 } 576 577 583 protected Integer getObjectForLayer(int layer) { 584 Integer layerObj; 585 switch(layer) { 586 case 0: 587 layerObj = DEFAULT_LAYER; 588 break; 589 case 100: 590 layerObj = PALETTE_LAYER; 591 break; 592 case 200: 593 layerObj = MODAL_LAYER; 594 break; 595 case 300: 596 layerObj = POPUP_LAYER; 597 break; 598 case 400: 599 layerObj = DRAG_LAYER; 600 break; 601 default: 602 layerObj = new Integer (layer); 603 } 604 return layerObj; 605 } 606 607 617 protected int insertIndexForLayer(int layer, int position) { 618 return insertIndexForLayer(null, layer, position); 619 } 620 621 634 private int insertIndexForLayer(Component comp, int layer, int position) { 635 int i, count, curLayer; 636 int layerStart = -1; 637 int layerEnd = -1; 638 int componentCount = getComponentCount(); 639 640 ArrayList <Component > compList = 641 new ArrayList <Component >(componentCount); 642 for (int index = 0; index < componentCount; index++) { 643 if (getComponent(index) != comp) { 644 compList.add(getComponent(index)); 645 } 646 } 647 648 count = compList.size(); 649 for (i = 0; i < count; i++) { 650 curLayer = getLayer(compList.get(i)); 651 if (layerStart == -1 && curLayer == layer) { 652 layerStart = i; 653 } 654 if (curLayer < layer) { 655 if (i == 0) { 656 layerStart = 0; 659 layerEnd = 0; 660 } else { 661 layerEnd = i; 662 } 663 break; 664 } 665 } 666 667 if (layerStart == -1 && layerEnd == -1) 671 return count; 672 673 if (layerStart != -1 && layerEnd == -1) 675 layerEnd = count; 676 677 if (layerEnd != -1 && layerStart == -1) 678 layerStart = layerEnd; 679 680 if (position == -1) 682 return layerEnd; 683 684 if (position > -1 && layerStart + position <= layerEnd) 687 return layerStart + position; 688 689 return layerEnd; 691 } 692 693 702 protected String paramString() { 703 String optimizedDrawingPossibleString = (optimizedDrawingPossible ? 704 "true" : "false"); 705 706 return super.paramString() + 707 ",optimizedDrawingPossible=" + optimizedDrawingPossibleString; 708 } 709 710 714 723 public AccessibleContext getAccessibleContext() { 724 if (accessibleContext == null) { 725 accessibleContext = new AccessibleJLayeredPane(); 726 } 727 return accessibleContext; 728 } 729 730 745 protected class AccessibleJLayeredPane extends AccessibleJComponent { 746 747 754 public AccessibleRole getAccessibleRole() { 755 return AccessibleRole.LAYERED_PANE; 756 } 757 } 758 } 759 760 761 | Popular Tags |