1. /*
  2. * @(#)BasicDesktopPaneUI.java 1.28 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 javax.swing.plaf.basic;
  8. import javax.swing.*;
  9. import javax.swing.plaf.*;
  10. import java.beans.*;
  11. import java.awt.event.*;
  12. import java.awt.Dimension;
  13. import java.awt.Insets;
  14. import java.awt.Graphics;
  15. import java.awt.*;
  16. import java.util.Vector;
  17. /**
  18. * Basic L&F for a desktop.
  19. *
  20. * @version 1.28 11/29/01
  21. * @author Steve Wilson
  22. */
  23. public class BasicDesktopPaneUI extends DesktopPaneUI
  24. {
  25. private static Dimension minSize = new Dimension(0,0);
  26. private static Dimension maxSize = new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
  27. protected JDesktopPane desktop;
  28. protected DesktopManager desktopManager;
  29. protected KeyStroke minimizeKey;
  30. protected KeyStroke maximizeKey;
  31. protected KeyStroke closeKey;
  32. protected KeyStroke navigateKey,navigateKey2;
  33. public static ComponentUI createUI(JComponent c) {
  34. return new BasicDesktopPaneUI();
  35. }
  36. public BasicDesktopPaneUI() {
  37. }
  38. public void installUI(JComponent c) {
  39. desktop = (JDesktopPane)c;
  40. installDefaults();
  41. installDesktopManager();
  42. installKeyboardActions();
  43. }
  44. public void uninstallUI(JComponent c) {
  45. uninstallKeyboardActions();
  46. uninstallDesktopManager();
  47. uninstallDefaults();
  48. desktop = null;
  49. }
  50. protected void installDefaults() {
  51. if (desktop.getBackground() == null ||
  52. desktop.getBackground() instanceof UIResource) {
  53. desktop.setBackground(UIManager.getColor("Desktop.background"));
  54. }
  55. }
  56. protected void uninstallDefaults() { }
  57. protected void installDesktopManager() {
  58. if(desktop.getDesktopManager() == null) {
  59. desktopManager = new DefaultDesktopManager();
  60. desktop.setDesktopManager(desktopManager);
  61. }
  62. }
  63. protected void uninstallDesktopManager() {
  64. if(desktop.getDesktopManager() == desktopManager) {
  65. desktop.setDesktopManager(null);
  66. }
  67. desktopManager = null;
  68. }
  69. protected void installKeyboardActions(){
  70. minimizeKey = KeyStroke.getKeyStroke(KeyEvent.VK_F9, KeyEvent.CTRL_MASK);
  71. maximizeKey = KeyStroke.getKeyStroke(KeyEvent.VK_F10, KeyEvent.CTRL_MASK);
  72. closeKey = KeyStroke.getKeyStroke(KeyEvent.VK_F4, KeyEvent.CTRL_MASK);
  73. navigateKey = KeyStroke.getKeyStroke(KeyEvent.VK_F6, KeyEvent.CTRL_MASK);
  74. navigateKey2 = KeyStroke.getKeyStroke(KeyEvent.VK_TAB, KeyEvent.CTRL_MASK);
  75. registerKeyboardActions();
  76. }
  77. protected void registerKeyboardActions(){
  78. // minimize
  79. desktop.registerKeyboardAction(new MinimizeAction(),
  80. minimizeKey,
  81. JComponent.WHEN_IN_FOCUSED_WINDOW);
  82. // maximize
  83. desktop.registerKeyboardAction(new MaximizeAction(),
  84. maximizeKey,
  85. JComponent.WHEN_IN_FOCUSED_WINDOW);
  86. // close
  87. desktop.registerKeyboardAction(new CloseAction(),
  88. closeKey,
  89. JComponent.WHEN_IN_FOCUSED_WINDOW);
  90. // navigate key
  91. desktop.registerKeyboardAction(new NavigateAction(),
  92. navigateKey2,
  93. JComponent.WHEN_IN_FOCUSED_WINDOW);
  94. desktop.registerKeyboardAction(new NavigateAction(),
  95. navigateKey,
  96. JComponent.WHEN_IN_FOCUSED_WINDOW);
  97. }
  98. protected void unregisterKeyboardActions(){
  99. desktop.unregisterKeyboardAction(minimizeKey);
  100. desktop.unregisterKeyboardAction(maximizeKey);
  101. desktop.unregisterKeyboardAction(closeKey);
  102. desktop.unregisterKeyboardAction(navigateKey);
  103. desktop.unregisterKeyboardAction(navigateKey2);
  104. minimizeKey = maximizeKey = closeKey = navigateKey = navigateKey2 = null;
  105. }
  106. protected void uninstallKeyboardActions(){
  107. unregisterKeyboardActions();
  108. }
  109. public void paint(Graphics g, JComponent c) {}
  110. public Dimension getPreferredSize(JComponent c) {return null;}
  111. public Dimension getMinimumSize(JComponent c) {
  112. return minSize;
  113. }
  114. public Dimension getMaximumSize(JComponent c){
  115. return maxSize;
  116. }
  117. protected class MinimizeAction extends AbstractAction {
  118. public void actionPerformed(ActionEvent e) {
  119. // get the active frame
  120. JInternalFrame f = desktop.getAllFrames()[0]; // current active frame
  121. if (f != null){
  122. if (f.isMaximizable()){
  123. try {
  124. f.setIcon(true);
  125. } catch(PropertyVetoException e0) { }
  126. }
  127. }
  128. }
  129. public boolean isEnabled() {
  130. return true;
  131. }
  132. }
  133. protected class MaximizeAction extends AbstractAction {
  134. public void actionPerformed(ActionEvent e) {
  135. JInternalFrame f = desktop.getAllFrames()[0]; // current active frame
  136. if (f != null){
  137. if (f.isMaximizable()){
  138. try {
  139. f.setMaximum(true);
  140. } catch(PropertyVetoException e0) { }
  141. }
  142. }
  143. }
  144. public boolean isEnabled() {
  145. return true;
  146. }
  147. }
  148. protected class CloseAction extends AbstractAction {
  149. public void actionPerformed(ActionEvent e) {
  150. // need to check for no JInternalFrame in the desktop
  151. JInternalFrame[] results = desktop.getAllFrames();
  152. JInternalFrame f = null;
  153. if (results.length > 0)
  154. f = results[0];
  155. if (f != null){
  156. if (f.isClosable()){
  157. try {
  158. f.setClosed(true);
  159. } catch(PropertyVetoException e0) { }
  160. }
  161. }
  162. }
  163. public boolean isEnabled() {
  164. return true;
  165. }
  166. }
  167. protected class NavigateAction extends AbstractAction {
  168. public void actionPerformed(ActionEvent e) {
  169. // very basic support now - we need this in DefaultDesktopManager
  170. // since we need to be in the loop of when Windows are activated
  171. // closed - etc - the getAllFrames schema is very limited
  172. JInternalFrame[] allFrames = desktop.getAllFrames();
  173. int i = allFrames.length;
  174. if (i >= 1){
  175. // basic Z-ordering in allFrame is kept from 0-n, where
  176. // 0 is the active, and 1 is the most recent active, etc.
  177. i--;
  178. }
  179. // icons always stay at the end - skip them for now
  180. while (allFrames[i].isIcon()){
  181. i--;
  182. if (i == 0)
  183. break;
  184. }
  185. desktopManager.activateFrame(allFrames[i]);
  186. try {
  187. allFrames[i].setSelected(true);
  188. } catch (PropertyVetoException e2){}
  189. }
  190. public boolean isEnabled() {
  191. return true;
  192. }
  193. }
  194. }