1 7 8 package javax.swing.plaf.basic; 9 10 import javax.swing.*; 11 import javax.swing.plaf.*; 12 13 import java.beans.*; 14 15 import java.awt.event.*; 16 import java.awt.Dimension ; 17 import java.awt.Insets ; 18 import java.awt.Graphics ; 19 import java.awt.KeyboardFocusManager ; 20 import java.awt.*; 21 import java.util.Vector ; 22 import sun.swing.DefaultLookup; 23 import sun.swing.UIAction; 24 import sun.awt.AppContext; 25 26 32 public class BasicDesktopPaneUI extends DesktopPaneUI { 33 private static final Actions SHARED_ACTION = new Actions(); 35 private static final Object FRAMES_CACHE_KEY = 36 new StringBuilder ("BASIC_DESKTOP_PANE_UI.FRAMES_CACHE"); 37 38 private static Dimension minSize = new Dimension (0,0); 39 private static Dimension maxSize = new Dimension (Integer.MAX_VALUE, 40 Integer.MAX_VALUE); 41 private Handler handler; 42 private PropertyChangeListener pcl; 43 44 protected JDesktopPane desktop; 45 protected DesktopManager desktopManager; 46 47 55 @Deprecated 56 protected KeyStroke minimizeKey; 57 65 @Deprecated 66 protected KeyStroke maximizeKey; 67 75 @Deprecated 76 protected KeyStroke closeKey; 77 85 @Deprecated 86 protected KeyStroke navigateKey; 87 95 @Deprecated 96 protected KeyStroke navigateKey2; 97 98 public static ComponentUI createUI(JComponent c) { 99 return new BasicDesktopPaneUI (); 100 } 101 102 public BasicDesktopPaneUI() { 103 } 104 105 private static Vector getCurrentFramesCache() { 106 synchronized (BasicDesktopPaneUI .class) { 107 AppContext appContext = AppContext.getAppContext(); 108 Vector framesCache = (Vector ) appContext.get(FRAMES_CACHE_KEY); 109 if(framesCache == null) { 110 framesCache = new Vector (); 111 appContext.put(FRAMES_CACHE_KEY,framesCache); 112 } 113 return framesCache; 114 } 115 } 116 117 public void installUI(JComponent c) { 118 desktop = (JDesktopPane)c; 119 installDefaults(); 120 installDesktopManager(); 121 installListeners(); 122 installKeyboardActions(); 123 } 124 125 public void uninstallUI(JComponent c) { 126 uninstallKeyboardActions(); 127 uninstallListeners(); 128 uninstallDesktopManager(); 129 uninstallDefaults(); 130 desktop = null; 131 handler = null; 132 } 133 134 protected void installDefaults() { 135 if (desktop.getBackground() == null || 136 desktop.getBackground() instanceof UIResource) { 137 desktop.setBackground(UIManager.getColor("Desktop.background")); 138 } 139 LookAndFeel.installProperty(desktop, "opaque", Boolean.TRUE); 140 } 141 142 protected void uninstallDefaults() { } 143 144 152 protected void installListeners() { 153 pcl = createPropertyChangeListener(); 154 desktop.addPropertyChangeListener(pcl); 155 } 156 157 165 protected void uninstallListeners() { 166 desktop.removePropertyChangeListener(pcl); 167 pcl = null; 168 } 169 170 protected void installDesktopManager() { 171 desktopManager = desktop.getDesktopManager(); 172 if(desktopManager == null) { 173 desktopManager = new BasicDesktopManager(); 174 desktop.setDesktopManager(desktopManager); 175 } 176 } 177 178 protected void uninstallDesktopManager() { 179 if(desktop.getDesktopManager() instanceof UIResource) { 180 desktop.setDesktopManager(null); 181 } 182 desktopManager = null; 183 } 184 185 protected void installKeyboardActions(){ 186 InputMap inputMap = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); 187 if (inputMap != null) { 188 SwingUtilities.replaceUIInputMap(desktop, 189 JComponent.WHEN_IN_FOCUSED_WINDOW, inputMap); 190 } 191 inputMap = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); 192 if (inputMap != null) { 193 SwingUtilities.replaceUIInputMap(desktop, 194 JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, 195 inputMap); 196 } 197 198 LazyActionMap.installLazyActionMap(desktop, BasicDesktopPaneUI .class, 199 "DesktopPane.actionMap"); 200 registerKeyboardActions(); 201 } 202 203 protected void registerKeyboardActions(){ 204 } 205 206 protected void unregisterKeyboardActions(){ 207 } 208 209 InputMap getInputMap(int condition) { 210 if (condition == JComponent.WHEN_IN_FOCUSED_WINDOW) { 211 return createInputMap(condition); 212 } 213 else if (condition == JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) { 214 return (InputMap)DefaultLookup.get(desktop, this, 215 "Desktop.ancestorInputMap"); 216 } 217 return null; 218 } 219 220 InputMap createInputMap(int condition) { 221 if (condition == JComponent.WHEN_IN_FOCUSED_WINDOW) { 222 Object [] bindings = (Object [])DefaultLookup.get(desktop, 223 this, "Desktop.windowBindings"); 224 225 if (bindings != null) { 226 return LookAndFeel.makeComponentInputMap(desktop, bindings); 227 } 228 } 229 return null; 230 } 231 232 static void loadActionMap(LazyActionMap map) { 233 map.put(new Actions(Actions.RESTORE)); 234 map.put(new Actions(Actions.CLOSE)); 235 map.put(new Actions(Actions.MOVE)); 236 map.put(new Actions(Actions.RESIZE)); 237 map.put(new Actions(Actions.LEFT)); 238 map.put(new Actions(Actions.SHRINK_LEFT)); 239 map.put(new Actions(Actions.RIGHT)); 240 map.put(new Actions(Actions.SHRINK_RIGHT)); 241 map.put(new Actions(Actions.UP)); 242 map.put(new Actions(Actions.SHRINK_UP)); 243 map.put(new Actions(Actions.DOWN)); 244 map.put(new Actions(Actions.SHRINK_DOWN)); 245 map.put(new Actions(Actions.ESCAPE)); 246 map.put(new Actions(Actions.MINIMIZE)); 247 map.put(new Actions(Actions.MAXIMIZE)); 248 map.put(new Actions(Actions.NEXT_FRAME)); 249 map.put(new Actions(Actions.PREVIOUS_FRAME)); 250 map.put(new Actions(Actions.NAVIGATE_NEXT)); 251 map.put(new Actions(Actions.NAVIGATE_PREVIOUS)); 252 } 253 254 protected void uninstallKeyboardActions(){ 255 unregisterKeyboardActions(); 256 SwingUtilities.replaceUIInputMap(desktop, JComponent. 257 WHEN_IN_FOCUSED_WINDOW, null); 258 SwingUtilities.replaceUIInputMap(desktop, JComponent. 259 WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, null); 260 SwingUtilities.replaceUIActionMap(desktop, null); 261 } 262 263 public void paint(Graphics g, JComponent c) {} 264 265 public Dimension getPreferredSize(JComponent c) {return null;} 266 267 public Dimension getMinimumSize(JComponent c) { 268 return minSize; 269 } 270 public Dimension getMaximumSize(JComponent c){ 271 return maxSize; 272 } 273 274 282 protected PropertyChangeListener createPropertyChangeListener() { 283 return getHandler(); 284 } 285 286 private Handler getHandler() { 287 if (handler == null) { 288 handler = new Handler(); 289 } 290 return handler; 291 } 292 293 private class Handler implements PropertyChangeListener { 294 public void propertyChange(PropertyChangeEvent evt) { 295 String propertyName = evt.getPropertyName(); 296 297 if ("desktopManager" == propertyName) { 298 installDesktopManager(); 299 } 300 } 301 } 302 303 306 private class BasicDesktopManager extends DefaultDesktopManager 307 implements UIResource { 308 } 309 310 private static class Actions extends UIAction { 311 private static String CLOSE = "close"; 312 private static String ESCAPE = "escape"; 313 private static String MAXIMIZE = "maximize"; 314 private static String MINIMIZE = "minimize"; 315 private static String MOVE = "move"; 316 private static String RESIZE = "resize"; 317 private static String RESTORE = "restore"; 318 private static String LEFT = "left"; 319 private static String RIGHT = "right"; 320 private static String UP = "up"; 321 private static String DOWN = "down"; 322 private static String SHRINK_LEFT = "shrinkLeft"; 323 private static String SHRINK_RIGHT = "shrinkRight"; 324 private static String SHRINK_UP = "shrinkUp"; 325 private static String SHRINK_DOWN = "shrinkDown"; 326 private static String NEXT_FRAME = "selectNextFrame"; 327 private static String PREVIOUS_FRAME = "selectPreviousFrame"; 328 private static String NAVIGATE_NEXT = "navigateNext"; 329 private static String NAVIGATE_PREVIOUS = "navigatePrevious"; 330 private final int MOVE_RESIZE_INCREMENT = 10; 331 private static boolean moving = false; 332 private static boolean resizing = false; 333 private static JInternalFrame sourceFrame = null; 334 private static Component focusOwner = null; 335 336 Actions() { 337 super(null); 338 } 339 340 Actions(String name) { 341 super(name); 342 } 343 344 public void actionPerformed(ActionEvent e) { 345 JDesktopPane dp = (JDesktopPane)e.getSource(); 346 String key = getName(); 347 348 if (CLOSE == key || MAXIMIZE == key || MINIMIZE == key || 349 RESTORE == key) { 350 setState(dp, key); 351 } 352 else if (ESCAPE == key) { 353 if (sourceFrame == dp.getSelectedFrame() && 354 focusOwner != null) { 355 focusOwner.requestFocus(); 356 } 357 moving = false; 358 resizing = false; 359 sourceFrame = null; 360 focusOwner = null; 361 } 362 else if (MOVE == key || RESIZE == key) { 363 sourceFrame = dp.getSelectedFrame(); 364 if (sourceFrame == null) { 365 return; 366 } 367 moving = (key == MOVE) ? true : false; 368 resizing = (key == RESIZE) ? true : false; 369 370 focusOwner = KeyboardFocusManager. 371 getCurrentKeyboardFocusManager().getFocusOwner(); 372 if (!SwingUtilities.isDescendingFrom(focusOwner, sourceFrame)) { 373 focusOwner = null; 374 } 375 sourceFrame.requestFocus(); 376 } 377 else if (LEFT == key || 378 RIGHT == key || 379 UP == key || 380 DOWN == key || 381 SHRINK_RIGHT == key || 382 SHRINK_LEFT == key || 383 SHRINK_UP == key || 384 SHRINK_DOWN == key) { 385 JInternalFrame c = dp.getSelectedFrame(); 386 if (sourceFrame == null || c != sourceFrame || 387 KeyboardFocusManager. 388 getCurrentKeyboardFocusManager().getFocusOwner() != 389 sourceFrame) { 390 return; 391 } 392 Dimension size = c.getSize(); 393 Dimension minSize = c.getMinimumSize(); 394 Point loc = c.getLocation(); 395 if (LEFT == key) { 396 if (moving) { 397 c.setLocation(loc.x - MOVE_RESIZE_INCREMENT, loc.y); 398 } else if (resizing) { 399 c.setLocation(loc.x - MOVE_RESIZE_INCREMENT, loc.y); 400 c.setSize(size.width + MOVE_RESIZE_INCREMENT, 401 size.height); 402 } 403 } else if (RIGHT == key) { 404 if (moving) { 405 c.setLocation(loc.x + MOVE_RESIZE_INCREMENT, loc.y); 406 } else if (resizing) { 407 c.setLocation(loc.x, loc.y); 408 c.setSize(size.width + MOVE_RESIZE_INCREMENT, 409 size.height); 410 } 411 } else if (UP == key) { 412 if (moving) { 413 c.setLocation(loc.x, loc.y - MOVE_RESIZE_INCREMENT); 414 } else if (resizing) { 415 c.setLocation(loc.x, loc.y - MOVE_RESIZE_INCREMENT); 416 c.setSize(size.width, 417 size.height + MOVE_RESIZE_INCREMENT); 418 } 419 } else if (DOWN == key) { 420 if (moving) { 421 c.setLocation(loc.x, loc.y + MOVE_RESIZE_INCREMENT); 422 } else if (resizing) { 423 c.setLocation(loc.x, loc.y); 424 c.setSize(size.width, 425 size.height + MOVE_RESIZE_INCREMENT); 426 } 427 } else if (SHRINK_LEFT == key && resizing) { 428 if (minSize.width < (size.width - MOVE_RESIZE_INCREMENT)) { 429 c.setLocation(loc.x, loc.y); 430 c.setSize(size.width - MOVE_RESIZE_INCREMENT, 431 size.height); 432 } else { 433 c.setSize(minSize.width, size.height); 434 } 435 } else if (SHRINK_RIGHT == key && resizing) { 436 if (minSize.width < (size.width - MOVE_RESIZE_INCREMENT)) { 437 c.setLocation(loc.x + MOVE_RESIZE_INCREMENT, loc.y); 438 c.setSize(size.width - MOVE_RESIZE_INCREMENT, 439 size.height); 440 } else { 441 c.setLocation(loc.x - minSize.width + size.width, 442 loc.y); 443 c.setSize(minSize.width, size.height); 444 } 445 } else if (SHRINK_UP == key && resizing) { 446 if (minSize.height < 447 (size.height - MOVE_RESIZE_INCREMENT)) { 448 c.setLocation(loc.x, loc.y); 449 c.setSize(size.width, 450 size.height - MOVE_RESIZE_INCREMENT); 451 } else { 452 c.setSize(size.width, minSize.height); 453 } 454 } else if (SHRINK_DOWN == key && resizing) { 455 if (minSize.height < 456 (size.height - MOVE_RESIZE_INCREMENT)) { 457 c.setLocation(loc.x, loc.y + MOVE_RESIZE_INCREMENT); 458 c.setSize(size.width, 459 size.height - MOVE_RESIZE_INCREMENT); 460 } else { 461 c.setLocation(loc.x, 462 loc.y - minSize.height + size.height); 463 c.setSize(size.width, minSize.height); 464 } 465 } 466 } 467 else if (NEXT_FRAME == key || PREVIOUS_FRAME == key) { 468 selectFrame(dp, (key == NEXT_FRAME) ? true : false); 469 } 470 else if (NAVIGATE_NEXT == key || 471 NAVIGATE_PREVIOUS == key) { 472 boolean moveForward = true; 473 if (NAVIGATE_PREVIOUS == key) { 474 moveForward = false; 475 } 476 Container cycleRoot = dp.getFocusCycleRootAncestor(); 477 478 if (cycleRoot != null) { 479 FocusTraversalPolicy policy = 480 cycleRoot.getFocusTraversalPolicy(); 481 if (policy != null && policy instanceof 482 SortingFocusTraversalPolicy) { 483 SortingFocusTraversalPolicy sPolicy = 484 (SortingFocusTraversalPolicy)policy; 485 boolean idc = sPolicy.getImplicitDownCycleTraversal(); 486 try { 487 sPolicy.setImplicitDownCycleTraversal(false); 488 if (moveForward) { 489 KeyboardFocusManager. 490 getCurrentKeyboardFocusManager(). 491 focusNextComponent(dp); 492 } else { 493 KeyboardFocusManager. 494 getCurrentKeyboardFocusManager(). 495 focusPreviousComponent(dp); 496 } 497 } finally { 498 sPolicy.setImplicitDownCycleTraversal(idc); 499 } 500 } 501 } 502 } 503 } 504 505 private void selectFrame(JDesktopPane dp, boolean forward) { 506 Vector framesCache = getCurrentFramesCache(); 507 if (forward) { 508 int i = 0; 510 verifyFramesCache(dp); 511 if (framesCache.size() == 0) { 512 return; 513 } 514 515 JInternalFrame f = dp.getSelectedFrame(); 516 517 if (f != null) { 518 i = framesCache.indexOf(f); 519 } 520 if (i == -1) { 521 522 i = framesCache.indexOf(f.getDesktopIcon()); 523 if (i == -1) { 524 525 return; 526 } 527 } 528 if (++i == framesCache.size()) { 529 530 i = 0; 531 } 532 JComponent c = (JComponent) framesCache.elementAt(i); 533 if (c instanceof JInternalFrame) { 534 try { 535 ((JInternalFrame)c).setSelected(true); 536 dp.getDesktopManager().activateFrame((JInternalFrame)c); 537 } catch (PropertyVetoException pve) {} 538 } else { 539 540 if (!(c instanceof JInternalFrame.JDesktopIcon)){ 541 542 return; 543 } 544 try { 545 ((JInternalFrame)((JInternalFrame.JDesktopIcon)c). 546 getInternalFrame()).setSelected(true); 547 dp.getDesktopManager().activateFrame( 548 ((JInternalFrame.JDesktopIcon)c). 549 getInternalFrame()); 550 } catch (PropertyVetoException pve) {} 551 } 552 } else { 553 int i = 0; 555 verifyFramesCache(dp); 556 if (framesCache.size() == 0) { 557 return; 558 } 559 JInternalFrame f = dp.getSelectedFrame(); 560 if (f != null) { 561 i = framesCache.indexOf(f); 562 } 563 if (i == -1) { 564 565 i = framesCache.indexOf(f.getDesktopIcon()); 566 if (i == -1) { 567 568 return; 569 } 570 } 571 if (--i == -1) { 572 573 i = framesCache.size() - 1; 574 } 575 JComponent c = (JComponent) framesCache.elementAt(i); 576 if (c instanceof JInternalFrame) { 577 try { 578 ((JInternalFrame)c).setSelected(true); 579 } catch (PropertyVetoException pve) {} 580 } else { 581 582 if (!(c instanceof JInternalFrame.JDesktopIcon)) { 583 584 return; 585 } 586 try { 587 ((JInternalFrame)((JInternalFrame.JDesktopIcon)c). 588 getInternalFrame()).setSelected(true); 589 } catch (PropertyVetoException pve) {} 590 } 591 } 592 } 593 594 private void setState(JDesktopPane dp, String state) { 595 if (state == CLOSE) { 596 JInternalFrame f = dp.getSelectedFrame(); 597 if (f == null) { 598 return; 599 } 600 f.doDefaultCloseAction(); 601 } else if (state == MAXIMIZE) { 602 JInternalFrame f = dp.getSelectedFrame(); 604 if (f == null) { 605 return; 606 } 607 if (!f.isMaximum()) { 608 if (f.isIcon()) { 609 try { 610 f.setIcon(false); 611 f.setMaximum(true); 612 } catch (PropertyVetoException pve) {} 613 } else { 614 try { 615 f.setMaximum(true); 616 } catch (PropertyVetoException pve) { 617 } 618 } 619 } 620 } else if (state == MINIMIZE) { 621 JInternalFrame f = dp.getSelectedFrame(); 623 if (f == null) { 624 return; 625 } 626 if (!f.isIcon()) { 627 try { 628 f.setIcon(true); 629 } catch (PropertyVetoException pve) { 630 } 631 } 632 } else if (state == RESTORE) { 633 JInternalFrame f = dp.getSelectedFrame(); 635 if (f == null) { 636 return; 637 } 638 try { 639 if (f.isIcon()) { 640 f.setIcon(false); 641 } else if (f.isMaximum()) { 642 f.setMaximum(false); 643 } 644 f.setSelected(true); 645 } catch (PropertyVetoException pve) { 646 } 647 } 648 } 649 650 651 public boolean isEnabled(Object sender) { 652 if (sender instanceof JDesktopPane) { 653 JDesktopPane dp = (JDesktopPane)sender; 654 JInternalFrame iFrame = dp.getSelectedFrame(); 655 if (iFrame == null) { 656 return false; 657 } 658 String action = getName(); 659 if (action == Actions.CLOSE) { 660 return iFrame.isClosable(); 661 } else if (action == Actions.MINIMIZE) { 662 return iFrame.isIconifiable(); 663 } else if (action == Actions.MAXIMIZE) { 664 return iFrame.isMaximizable(); 665 } 666 return true; 667 } 668 return false; 669 } 670 } 671 672 673 676 protected class OpenAction extends AbstractAction { 677 public void actionPerformed(ActionEvent evt) { 678 JDesktopPane dp = (JDesktopPane)evt.getSource(); 679 SHARED_ACTION.setState(dp, Actions.RESTORE); 680 } 681 682 public boolean isEnabled() { 683 return true; 684 } 685 } 686 687 690 protected class CloseAction extends AbstractAction { 691 public void actionPerformed(ActionEvent evt) { 692 JDesktopPane dp = (JDesktopPane)evt.getSource(); 693 SHARED_ACTION.setState(dp, Actions.CLOSE); 694 } 695 696 public boolean isEnabled() { 697 JInternalFrame iFrame = desktop.getSelectedFrame(); 698 if (iFrame != null) { 699 return iFrame.isClosable(); 700 } 701 return false; 702 } 703 } 704 705 708 protected class MinimizeAction extends AbstractAction { 709 public void actionPerformed(ActionEvent evt) { 710 JDesktopPane dp = (JDesktopPane)evt.getSource(); 711 SHARED_ACTION.setState(dp, Actions.MINIMIZE); 712 } 713 714 public boolean isEnabled() { 715 JInternalFrame iFrame = desktop.getSelectedFrame(); 716 if (iFrame != null) { 717 return iFrame.isIconifiable(); 718 } 719 return false; 720 } 721 } 722 723 726 protected class MaximizeAction extends AbstractAction { 727 public void actionPerformed(ActionEvent evt) { 728 JDesktopPane dp = (JDesktopPane)evt.getSource(); 729 SHARED_ACTION.setState(dp, Actions.MAXIMIZE); 730 } 731 732 public boolean isEnabled() { 733 JInternalFrame iFrame = desktop.getSelectedFrame(); 734 if (iFrame != null) { 735 return iFrame.isMaximizable(); 736 } 737 return false; 738 } 739 } 740 741 744 protected class NavigateAction extends AbstractAction { 745 public void actionPerformed(ActionEvent evt) { 746 JDesktopPane dp = (JDesktopPane)evt.getSource(); 747 SHARED_ACTION.selectFrame(dp, true); 748 } 749 750 public boolean isEnabled() { 751 return true; 752 } 753 } 754 755 private static void verifyFramesCache(JDesktopPane dp) { 756 Vector framesCache = getCurrentFramesCache(); 757 boolean framesHaveClosed = false; 760 int len = framesCache.size(); 761 for (int i = 0; i < len; i++) { 762 JComponent c = 763 (JComponent)framesCache.elementAt(i); 764 if (c instanceof JInternalFrame) { 765 JInternalFrame f = (JInternalFrame)c; 766 if (f.isClosed()) { 767 framesHaveClosed = true; 768 break; 769 } 770 } 771 else if (c instanceof JInternalFrame.JDesktopIcon) { 772 JInternalFrame.JDesktopIcon icon = 773 (JInternalFrame.JDesktopIcon)c; 774 JInternalFrame f = (JInternalFrame)icon.getInternalFrame(); 775 if (f.isClosed()) { 776 framesHaveClosed = true; 777 break; 778 } 779 } 780 } 781 JInternalFrame [] allFrames = dp.getAllFrames(); 782 if (framesHaveClosed || allFrames.length != framesCache.size()) { 783 framesCache.clear(); 785 int low = dp.lowestLayer(); 786 int high = dp.highestLayer(); 787 for (int i = high; i >= low; i--) { 788 Component [] comp = dp.getComponentsInLayer(i); 789 if (comp.length > 0) { 790 for (int j = 0; j < comp.length; j++) { 791 framesCache.addElement(comp[j]); 792 } 793 } 794 } 795 } 796 } 797 } 798 | Popular Tags |