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