1. /*
  2. * @(#)WindowsDesktopManager.java 1.12 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.java.swing.plaf.windows;
  8. import javax.swing.DefaultDesktopManager;
  9. import javax.swing.JInternalFrame;
  10. import javax.swing.JLayeredPane;
  11. import java.awt.Component;
  12. import java.awt.Container;
  13. import java.awt.Dimension;
  14. import java.beans.PropertyVetoException;
  15. import java.util.Vector;
  16. /**
  17. * This class implements a DesktopManager which more closely follows
  18. * the MDI model than the DefaultDesktopManager. Unlike the
  19. * DefaultDesktopManager policy, MDI requires that the selected
  20. * and activated child frames are the same, and that that frame
  21. * always be the top-most window.
  22. * <p>
  23. * The maximized state is managed by the DesktopManager with MDI,
  24. * instead of just being a property of the individual child frame.
  25. * This means that if the currently selected window is maximized
  26. * and another window is selected, that new window will be maximized.
  27. *
  28. * @see javax.swing.DefaultDesktopManager
  29. * @version 1.12 11/29/01
  30. * @author Thomas Ball
  31. */
  32. public class WindowsDesktopManager extends DefaultDesktopManager
  33. implements java.io.Serializable {
  34. /* The frame which is currently selected/activated.
  35. * We store this value to enforce MDI's single-selection model.
  36. */
  37. JInternalFrame currentFrame;
  38. JInternalFrame initialFrame;
  39. /* The list of frames, sorted by order of creation.
  40. * This list is necessary because by default the order of
  41. * child frames in the JDesktopPane changes during frame
  42. * activation (the activated frame is moved to index 0).
  43. * We preserve the creation order so that "next" and "previous"
  44. * frame actions make sense.
  45. */
  46. Vector childFrames = new Vector(1);
  47. public void closeFrame(JInternalFrame f) {
  48. if(f == currentFrame){
  49. activateNextFrame();
  50. }
  51. childFrames.removeElement(f);
  52. super.closeFrame(f);
  53. }
  54. public void activateFrame(JInternalFrame f) {
  55. try {
  56. super.activateFrame(f);
  57. // If this is the first activation, add to child list.
  58. if (childFrames.indexOf(f) == -1) {
  59. childFrames.addElement(f);
  60. }
  61. if (currentFrame != null && f != currentFrame) {
  62. // If the current frame is maximized, transfer that
  63. // attribute to the frame being activated.
  64. if (currentFrame.isMaximum()) {
  65. currentFrame.setMaximum(false);
  66. f.setMaximum(true);
  67. }
  68. if (currentFrame.isSelected()) {
  69. currentFrame.setSelected(false);
  70. }
  71. }
  72. if (!f.isSelected()) {
  73. f.setSelected(true);
  74. }
  75. currentFrame = f;
  76. } catch (PropertyVetoException e) {}
  77. }
  78. private void switchFrame(boolean next) {
  79. if (currentFrame == null) {
  80. // initialize first frame we find
  81. if (initialFrame != null)
  82. activateFrame(initialFrame);
  83. return;
  84. }
  85. int count = childFrames.size();
  86. if (count <= 1) {
  87. // No other child frames.
  88. return;
  89. }
  90. int currentIndex = childFrames.indexOf(currentFrame);
  91. if (currentIndex == -1) {
  92. // should never happen...
  93. return;
  94. }
  95. int nextIndex;
  96. if (next) {
  97. nextIndex = currentIndex + 1;
  98. if (nextIndex == count) {
  99. nextIndex = 0;
  100. }
  101. } else {
  102. nextIndex = currentIndex - 1;
  103. if (nextIndex == -1) {
  104. nextIndex = count - 1;
  105. }
  106. }
  107. JInternalFrame f = (JInternalFrame)childFrames.elementAt(nextIndex);
  108. activateFrame(f);
  109. currentFrame = f;
  110. }
  111. /**
  112. * Activate the next child JInternalFrame, as determined by
  113. * the frames' Z-order. If there is only one child frame, it
  114. * remains activated. If there are no child frames, nothing
  115. * happens.
  116. */
  117. public void activateNextFrame() {
  118. switchFrame(true);
  119. }
  120. /** same as above but will activate a frame if none
  121. * have been selected
  122. */
  123. public void activateNextFrame(JInternalFrame f){
  124. initialFrame = f;
  125. switchFrame(true);
  126. }
  127. /**
  128. * Activate the previous child JInternalFrame, as determined by
  129. * the frames' Z-order. If there is only one child frame, it
  130. * remains activated. If there are no child frames, nothing
  131. * happens.
  132. */
  133. public void activatePreviousFrame() {
  134. switchFrame(false);
  135. }
  136. }