1 7 8 9 package javax.swing; 10 11 import java.awt.*; 12 import java.beans.PropertyVetoException ; 13 import java.beans.PropertyChangeEvent ; 14 import javax.swing.border.Border ; 15 import java.awt.event.ComponentListener ; 16 import java.awt.event.ComponentAdapter ; 17 import java.awt.event.ComponentEvent ; 18 19 34 public class DefaultDesktopManager implements DesktopManager , java.io.Serializable { 35 final static String HAS_BEEN_ICONIFIED_PROPERTY = "wasIconOnce"; 36 37 final static int DEFAULT_DRAG_MODE = 0; 38 final static int OUTLINE_DRAG_MODE = 1; 39 final static int FASTER_DRAG_MODE = 2; 40 41 int dragMode = DEFAULT_DRAG_MODE; 42 43 private transient Rectangle currentBounds = null; 44 private transient Graphics desktopGraphics = null; 45 private transient Rectangle desktopBounds = null; 46 private transient Rectangle[] floatingItems = {}; 47 48 49 53 public void openFrame(JInternalFrame f) { 54 if(f.getDesktopIcon().getParent() != null) { 55 f.getDesktopIcon().getParent().add(f); 56 removeIconFor(f); 57 } 58 } 59 60 65 public void closeFrame(JInternalFrame f) { 66 boolean findNext = f.isSelected(); 67 Container c = f.getParent(); 68 if (findNext) 69 try { f.setSelected(false); } catch (PropertyVetoException e2) { } 70 if(c != null) { 71 c.remove(f); 72 c.repaint(f.getX(), f.getY(), f.getWidth(), f.getHeight()); 73 } 74 removeIconFor(f); 75 if(f.getNormalBounds() != null) 76 f.setNormalBounds(null); 77 if(wasIcon(f)) 78 setWasIcon(f, null); 79 if (findNext) activateNextFrame(c); 80 } 81 82 86 public void maximizeFrame(JInternalFrame f) { 87 if (f.isIcon()) { 88 try { 89 f.setIcon(false); 92 } catch (PropertyVetoException e2) { 93 } 94 } else { 95 f.setNormalBounds(f.getBounds()); 96 Rectangle desktopBounds = f.getParent().getBounds(); 97 setBoundsForFrame(f, 0, 0, 98 desktopBounds.width, desktopBounds.height); 99 } 100 101 try { 103 f.setSelected(true); 104 } catch (PropertyVetoException e2) { 105 } 106 } 107 108 113 public void minimizeFrame(JInternalFrame f) { 114 if (f.isIcon()) { 116 iconifyFrame(f); 117 return; 118 } 119 120 if ((f.getNormalBounds()) != null) { 121 Rectangle r = f.getNormalBounds(); 122 f.setNormalBounds(null); 123 try { f.setSelected(true); } catch (PropertyVetoException e2) { } 124 setBoundsForFrame(f, r.x, r.y, r.width, r.height); 125 } 126 } 127 128 133 public void iconifyFrame(JInternalFrame f) { 134 JInternalFrame.JDesktopIcon desktopIcon; 135 Container c = f.getParent(); 136 JDesktopPane d = f.getDesktopPane(); 137 boolean findNext = f.isSelected(); 138 139 desktopIcon = f.getDesktopIcon(); 140 if(!wasIcon(f)) { 141 Rectangle r = getBoundsForIconOf(f); 142 desktopIcon.setBounds(r.x, r.y, r.width, r.height); 143 setWasIcon(f, Boolean.TRUE); 144 } 145 146 if (c == null) { 147 return; 148 } 149 150 if (c instanceof JLayeredPane ) { 151 JLayeredPane lp = (JLayeredPane )c; 152 int layer = lp.getLayer(f); 153 lp.putLayer(desktopIcon, layer); 154 } 155 156 if (!f.isMaximum()) { 160 f.setNormalBounds(f.getBounds()); 161 } 162 c.remove(f); 163 c.add(desktopIcon); 164 c.repaint(f.getX(), f.getY(), f.getWidth(), f.getHeight()); 165 try { 166 f.setSelected(false); 167 } catch (PropertyVetoException e2) { 168 } 169 170 if (findNext) { 172 activateNextFrame(c); 173 } 174 } 175 176 void activateNextFrame(Container c) { 177 int i; 178 JInternalFrame nextFrame = null; 179 if (c == null) return; 180 for (i = 0; i < c.getComponentCount(); i++) { 181 if (c.getComponent(i) instanceof JInternalFrame ) { 182 nextFrame = (JInternalFrame ) c.getComponent(i); 183 break; 184 } 185 } 186 if (nextFrame != null) { 187 try { nextFrame.setSelected(true); } 188 catch (PropertyVetoException e2) { } 189 nextFrame.moveToFront(); 190 } 191 192 } 193 194 199 public void deiconifyFrame(JInternalFrame f) { 200 JInternalFrame.JDesktopIcon desktopIcon = f.getDesktopIcon(); 201 Container c = desktopIcon.getParent(); 202 if (c != null) { 203 c.add(f); 204 if (f.isMaximum()) { 207 Rectangle desktopBounds = c.getBounds(); 208 if (f.getWidth() != desktopBounds.width || 209 f.getHeight() != desktopBounds.height) { 210 setBoundsForFrame(f, 0, 0, 211 desktopBounds.width, desktopBounds.height); 212 } 213 } 214 removeIconFor(f); 215 if (f.isSelected()) { 216 f.moveToFront(); 217 } else { 218 try { 219 f.setSelected(true); 220 } catch (PropertyVetoException e2) { 221 } 222 } 223 } 224 } 225 226 232 public void activateFrame(JInternalFrame f) { 233 Container p = f.getParent(); 234 Component[] c; 235 JDesktopPane d = f.getDesktopPane(); 236 JInternalFrame currentlyActiveFrame = 237 (d == null) ? null : d.getSelectedFrame(); 238 if(p == null) { 240 p = f.getDesktopIcon().getParent(); 242 if(p == null) 243 return; 244 } 245 if (currentlyActiveFrame == null){ 247 if (d != null) { d.setSelectedFrame(f);} 248 } else if (currentlyActiveFrame != f) { 249 if (currentlyActiveFrame.isSelected()) { 252 try { 253 currentlyActiveFrame.setSelected(false); 254 } 255 catch(PropertyVetoException e2) {} 256 } 257 if (d != null) { d.setSelectedFrame(f);} 258 } 259 f.moveToFront(); 260 } 261 262 public void deactivateFrame(JInternalFrame f) { 264 JDesktopPane d = f.getDesktopPane(); 265 JInternalFrame currentlyActiveFrame = 266 (d == null) ? null : d.getSelectedFrame(); 267 if (currentlyActiveFrame == f) 268 d.setSelectedFrame(null); 269 } 270 271 public void beginDraggingFrame(JComponent f) { 273 setupDragMode(f); 274 275 if (dragMode == FASTER_DRAG_MODE) { 276 floatingItems = findFloatingItems(f); 277 currentBounds = f.getBounds(); 278 desktopBounds = f.getParent().getBounds(); 279 desktopBounds.x = 0; 280 desktopBounds.y = 0; 281 desktopGraphics = f.getParent().getGraphics(); 282 ((JInternalFrame )f).isDragging = true; 283 } 284 285 } 286 287 private void setupDragMode(JComponent f) { 288 JDesktopPane p = getDesktopPane(f); 289 if (p != null) { 290 String mode = (String )p.getClientProperty("JDesktopPane.dragMode"); 291 if (mode != null && mode.equals("outline")) { 292 dragMode = OUTLINE_DRAG_MODE; 293 } else if (mode != null && mode.equals("faster") 294 && f instanceof JInternalFrame 295 && ((JInternalFrame )f).isOpaque()) { 296 dragMode = FASTER_DRAG_MODE; 297 } else { 298 if (p.getDragMode() == JDesktopPane.OUTLINE_DRAG_MODE ) { 299 dragMode = OUTLINE_DRAG_MODE; 300 } else if ( p.getDragMode() == JDesktopPane.LIVE_DRAG_MODE 301 && f instanceof JInternalFrame 302 && ((JInternalFrame )f).isOpaque()) { 303 dragMode = FASTER_DRAG_MODE; 304 } else { 305 dragMode = DEFAULT_DRAG_MODE; 306 } 307 } 308 } 309 } 310 311 private transient Point currentLoc = null; 312 313 319 public void dragFrame(JComponent f, int newX, int newY) { 320 321 if (dragMode == OUTLINE_DRAG_MODE) { 322 JDesktopPane desktopPane = getDesktopPane(f); 323 if (desktopPane != null){ 324 Graphics g = desktopPane.getGraphics(); 325 326 g.setXORMode(Color.white); 327 if (currentLoc != null) { 328 g.drawRect(currentLoc.x, currentLoc.y, 329 f.getWidth()-1, f.getHeight()-1); 330 } 331 g.drawRect( newX, newY, f.getWidth()-1, f.getHeight()-1); 332 currentLoc = new Point (newX, newY); 333 g.dispose(); 334 } 335 } else if (dragMode == FASTER_DRAG_MODE) { 336 dragFrameFaster(f, newX, newY); 337 } else { 338 setBoundsForFrame(f, newX, newY, f.getWidth(), f.getHeight()); 339 } 340 } 341 342 public void endDraggingFrame(JComponent f) { 344 if ( dragMode == OUTLINE_DRAG_MODE && currentLoc != null) { 345 setBoundsForFrame(f, currentLoc.x, currentLoc.y, f.getWidth(), f.getHeight() ); 346 currentLoc = null; 347 } else if (dragMode == FASTER_DRAG_MODE) { 348 currentBounds = null; 349 if (desktopGraphics != null) { 350 desktopGraphics.dispose(); 351 desktopGraphics = null; 352 } 353 desktopBounds = null; 354 ((JInternalFrame )f).isDragging = false; 355 } 356 } 357 358 public void beginResizingFrame(JComponent f, int direction) { 360 setupDragMode(f); 361 } 362 363 371 public void resizeFrame(JComponent f, int newX, int newY, int newWidth, int newHeight) { 372 373 if ( dragMode == DEFAULT_DRAG_MODE || dragMode == FASTER_DRAG_MODE ) { 374 setBoundsForFrame(f, newX, newY, newWidth, newHeight); 375 } else { 376 JDesktopPane desktopPane = getDesktopPane(f); 377 if (desktopPane != null){ 378 Graphics g = desktopPane.getGraphics(); 379 380 g.setXORMode(Color.white); 381 if (currentBounds != null) { 382 g.drawRect( currentBounds.x, currentBounds.y, currentBounds.width-1, currentBounds.height-1); 383 } 384 g.drawRect( newX, newY, newWidth-1, newHeight-1); 385 currentBounds = new Rectangle (newX, newY, newWidth, newHeight); 386 g.setPaintMode(); 387 g.dispose(); 388 } 389 } 390 391 } 392 393 public void endResizingFrame(JComponent f) { 395 if ( dragMode == OUTLINE_DRAG_MODE && currentBounds != null) { 396 setBoundsForFrame(f, currentBounds.x, currentBounds.y, currentBounds.width, currentBounds.height ); 397 currentBounds = null; 398 } 399 } 400 401 402 403 public void setBoundsForFrame(JComponent f, int newX, int newY, int newWidth, int newHeight) { 404 boolean didResize = (f.getWidth() != newWidth || f.getHeight() != newHeight); 405 f.setBounds(newX, newY, newWidth, newHeight); 406 if(didResize) { 407 f.validate(); 408 } 409 } 410 411 412 protected void removeIconFor(JInternalFrame f) { 413 JInternalFrame.JDesktopIcon di = f.getDesktopIcon(); 414 Container c = di.getParent(); 415 if(c != null) { 416 c.remove(di); 417 c.repaint(di.getX(), di.getY(), di.getWidth(), di.getHeight()); 418 } 419 } 420 421 424 425 protected Rectangle getBoundsForIconOf(JInternalFrame f) { 426 430 JInternalFrame.JDesktopIcon icon = f.getDesktopIcon(); 431 Dimension prefSize = icon.getPreferredSize(); 432 436 Container c = f.getParent(); 437 if (c == null) { 438 c = f.getDesktopIcon().getParent(); 439 } 440 441 if (c == null) { 442 443 return new Rectangle(0, 0, prefSize.width, prefSize.height); 444 } 445 446 Rectangle parentBounds = c.getBounds(); 447 Component [] components = c.getComponents(); 448 449 450 455 Rectangle availableRectangle = null; 456 JInternalFrame.JDesktopIcon currentIcon = null; 457 458 int x = 0; 459 int y = parentBounds.height - prefSize.height; 460 int w = prefSize.width; 461 int h = prefSize.height; 462 463 boolean found = false; 464 465 while (!found) { 466 467 availableRectangle = new Rectangle(x,y,w,h); 468 469 found = true; 470 471 for ( int i=0; i<components.length; i++ ) { 472 473 477 if ( components[i] instanceof JInternalFrame ) { 478 currentIcon = ((JInternalFrame )components[i]).getDesktopIcon(); 479 } 480 else if ( components[i] instanceof JInternalFrame.JDesktopIcon ){ 481 currentIcon = (JInternalFrame.JDesktopIcon )components[i]; 482 } else 483 488 continue; 489 490 494 if ( !currentIcon.equals(icon) ) { 495 if ( availableRectangle.intersects(currentIcon.getBounds()) ) { 496 found = false; 497 break; 498 } 499 } 500 } 501 502 if (currentIcon == null) 503 506 return availableRectangle; 507 508 x += currentIcon.getBounds().width; 509 510 if ( x + w > parentBounds.width ) { 511 x = 0; 512 y -= h; 513 } 514 } 515 516 return(availableRectangle); 517 } 518 519 524 protected void setPreviousBounds(JInternalFrame f, Rectangle r) { 525 f.setNormalBounds(r); 526 } 527 528 534 protected Rectangle getPreviousBounds(JInternalFrame f) { 535 return f.getNormalBounds(); 536 } 537 538 542 protected void setWasIcon(JInternalFrame f, Boolean value) { 543 if (value != null) { 544 f.putClientProperty(HAS_BEEN_ICONIFIED_PROPERTY, value); 545 } 546 } 547 548 557 protected boolean wasIcon(JInternalFrame f) { 558 return (f.getClientProperty(HAS_BEEN_ICONIFIED_PROPERTY) == Boolean.TRUE); 559 } 560 561 562 JDesktopPane getDesktopPane( JComponent frame ) { 563 JDesktopPane pane = null; 564 Component c = frame.getParent(); 565 566 while ( pane == null ) { 568 if ( c instanceof JDesktopPane ) { 569 pane = (JDesktopPane )c; 570 } 571 else if ( c == null ) { 572 break; 573 } 574 else { 575 c = c.getParent(); 576 } 577 } 578 579 return pane; 580 } 581 582 583 585 private void dragFrameFaster(JComponent f, int newX, int newY) { 586 587 Rectangle previousBounds = new Rectangle(currentBounds.x, 588 currentBounds.y, 589 currentBounds.width, 590 currentBounds.height); 591 592 currentBounds.x = newX; 594 currentBounds.y = newY; 595 596 emergencyCleanup(f); 597 598 boolean floaterCollision = isFloaterCollision(previousBounds, currentBounds); 599 600 Rectangle visBounds = previousBounds.intersection(desktopBounds); 602 604 605 607 if(!floaterCollision) { 608 desktopGraphics.copyArea(visBounds.x, 611 visBounds.y, 612 visBounds.width, 613 visBounds.height, 614 newX - previousBounds.x, 615 newY - previousBounds.y); 616 } 617 618 JComponent parent = (JComponent )f.getParent(); 619 620 f.setBounds(currentBounds); 621 622 if(floaterCollision) { 623 ((JInternalFrame )f).isDragging = false; 626 parent.paintImmediately(currentBounds); 627 ((JInternalFrame )f).isDragging = true; 628 } 629 630 RepaintManager currentManager = RepaintManager.currentManager(f); 632 633 currentManager.markCompletelyClean(parent); 634 currentManager.markCompletelyClean(f); 635 636 Rectangle[] dirtyRects = null; 640 if ( previousBounds.intersects(currentBounds) ) { 641 dirtyRects = SwingUtilities.computeDifference(previousBounds, currentBounds); 642 } else { 643 dirtyRects = new Rectangle[1]; 644 dirtyRects[0] = previousBounds; 645 }; 647 648 for (int i = 0; i < dirtyRects.length; i++) { 650 parent.paintImmediately(dirtyRects[i]); 651 } 652 653 if ( !(visBounds.equals(previousBounds)) ) { 655 dirtyRects = SwingUtilities.computeDifference(previousBounds, desktopBounds); 656 for (int i = 0; i < dirtyRects.length; i++) { 657 dirtyRects[i].x += newX - previousBounds.x; 658 dirtyRects[i].y += newY - previousBounds.y; 659 ((JInternalFrame )f).isDragging = false; 660 661 parent.paintImmediately(dirtyRects[i]); 662 ((JInternalFrame )f).isDragging = true; 663 664 } 666 667 } 668 } 669 670 private boolean isFloaterCollision(Rectangle moveFrom, Rectangle moveTo) { 671 if (floatingItems.length == 0) { 672 return false; 674 } 675 676 for (int i = 0; i < floatingItems.length; i++) { 677 boolean intersectsFrom = moveFrom.intersects(floatingItems[i]); 678 if (intersectsFrom) { 679 return true; 680 } 681 boolean intersectsTo = moveTo.intersects(floatingItems[i]); 682 if (intersectsTo) { 683 return true; 684 } 685 } 686 687 return false; 688 } 689 690 private Rectangle[] findFloatingItems(JComponent f) { 691 Container desktop = f.getParent(); 692 Component[] children = desktop.getComponents(); 693 int i = 0; 694 for (i = 0; i < children.length; i++) { 695 if (children[i] == f) { 696 break; 697 } 698 } 699 Rectangle[] floaters = new Rectangle[i]; 701 for (i = 0; i < floaters.length; i++) { 702 floaters[i] = children[i].getBounds(); 703 } 704 705 return floaters; 706 } 707 708 714 private void emergencyCleanup(final JComponent f) { 715 716 if ( ((JInternalFrame )f).danger ) { 717 718 SwingUtilities.invokeLater( new Runnable (){ 719 public void run(){ 720 721 ((JInternalFrame )f).isDragging = false; 722 f.paintImmediately(0,0, 723 f.getWidth(), 724 f.getHeight()); 725 726 ((JInternalFrame )f).isDragging = true; 728 }}); 730 731 ((JInternalFrame )f).danger = false; 732 } 733 734 } 735 736 737 } 738 739 | Popular Tags |