components/visual-panels/core/src/java/util/com/oracle/solaris/vp/util/swing/ExtRootPane.java
changeset 827 0944d8c0158b
equal deleted inserted replaced
826:c6aad84d2493 827:0944d8c0158b
       
     1 /*
       
     2  * CDDL HEADER START
       
     3  *
       
     4  * The contents of this file are subject to the terms of the
       
     5  * Common Development and Distribution License (the "License").
       
     6  * You may not use this file except in compliance with the License.
       
     7  *
       
     8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
       
     9  * or http://www.opensolaris.org/os/licensing.
       
    10  * See the License for the specific language governing permissions
       
    11  * and limitations under the License.
       
    12  *
       
    13  * When distributing Covered Code, include this CDDL HEADER in each
       
    14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
       
    15  * If applicable, add the following below this CDDL HEADER, with the
       
    16  * fields enclosed by brackets "[]" replaced with your own identifying
       
    17  * information: Portions Copyright [yyyy] [name of copyright owner]
       
    18  *
       
    19  * CDDL HEADER END
       
    20  */
       
    21 
       
    22 /*
       
    23  * Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved.
       
    24  */
       
    25 
       
    26 package com.oracle.solaris.vp.util.swing;
       
    27 
       
    28 import java.awt.*;
       
    29 import javax.swing.*;
       
    30 
       
    31 /**
       
    32  * The {@code ExtRootPane} class, when {@link #validate validated}, can
       
    33  * optionally:
       
    34  * <ol>
       
    35  *   <li>
       
    36  *     {@link #setAutoResizeEnabled automatically} resize its enclosing {@code
       
    37  *     Window} to it's preferred size
       
    38  *   </li>
       
    39  *   <li>
       
    40  *     {@link #setAutoMinSizeEnabled automatically} set its enclosing {@code
       
    41  *     Window}'s minimum size to it's preferred size
       
    42  *   </li>
       
    43  * </ol>
       
    44  *
       
    45  * Additionally, the {@code ExtRootPane} takes any visible glass pane into
       
    46  * account when calculating its preferred size.
       
    47  *
       
    48  * @see         ExtDialog
       
    49  * @see         ExtFrame
       
    50  */
       
    51 @SuppressWarnings({"serial"})
       
    52 public class ExtRootPane extends JRootPane {
       
    53     //
       
    54     // Instance data
       
    55     //
       
    56 
       
    57     private boolean doingValidate;
       
    58     private boolean autoResizeEnabled = true;
       
    59     private boolean autoMinSizeEnabled = true;
       
    60 
       
    61     //
       
    62     // Component methods
       
    63     //
       
    64 
       
    65     @Override
       
    66     public void validate() {
       
    67 	if (!doingValidate && (autoResizeEnabled || autoMinSizeEnabled)) {
       
    68 	    doingValidate = true;
       
    69 	    try {
       
    70 		Window window = SwingUtilities.getWindowAncestor(this);
       
    71 		if (window != null) {
       
    72 		    if (autoResizeEnabled) {
       
    73 			window.pack();
       
    74 		    }
       
    75 		    if (autoMinSizeEnabled) {
       
    76 			window.setMinimumSize(window.getPreferredSize());
       
    77 		    }
       
    78 		}
       
    79 	    } finally {
       
    80 		doingValidate = false;
       
    81 	    }
       
    82 	}
       
    83 	super.validate();
       
    84     }
       
    85 
       
    86     @Override
       
    87     public Dimension getPreferredSize() {
       
    88         Dimension size = super.getPreferredSize();
       
    89 	if (isPreferredSizeSet()) {
       
    90 	    return size;
       
    91 	}
       
    92 
       
    93         Component glass = getGlassPane();
       
    94 
       
    95         // Resize for glass pane
       
    96         if (glass.isVisible()) {
       
    97             Dimension gSize = glass.getPreferredSize();
       
    98 	    if (gSize.width > size.width) {
       
    99 		size.width = gSize.width;
       
   100 	    }
       
   101 	    if (gSize.height > size.height) {
       
   102 		size.height = gSize.height;
       
   103 	    }
       
   104 	}
       
   105 
       
   106         return size;
       
   107     }
       
   108 
       
   109     //
       
   110     // ExtRootPane methods
       
   111     //
       
   112 
       
   113     /**
       
   114      * Gets whether auto-minimum size is enabled.
       
   115      */
       
   116     public boolean isAutoMinSizeEnabled() {
       
   117 	return autoMinSizeEnabled;
       
   118     }
       
   119 
       
   120     /**
       
   121      * Gets whether auto-resize is enabled.
       
   122      */
       
   123     public boolean isAutoResizeEnabled() {
       
   124 	return autoResizeEnabled;
       
   125     }
       
   126 
       
   127     /**
       
   128      * Sets whether auto-minimum size is enabled.
       
   129      */
       
   130     public void setAutoMinSizeEnabled(boolean autoMinSizeEnabled) {
       
   131 	this.autoMinSizeEnabled = autoMinSizeEnabled;
       
   132     }
       
   133 
       
   134     /**
       
   135      * Sets whether auto-resize is enabled.
       
   136      */
       
   137     public void setAutoResizeEnabled(boolean autoResizeEnabled) {
       
   138 	this.autoResizeEnabled = autoResizeEnabled;
       
   139     }
       
   140 }