components/visual-panels/core/src/java/vpanels/panel/com/oracle/solaris/vp/panel/swing/control/NavigationWindowResizer.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) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
       
    24  */
       
    25 
       
    26 package com.oracle.solaris.vp.panel.swing.control;
       
    27 
       
    28 import java.awt.*;
       
    29 import java.util.*;
       
    30 import java.util.List;
       
    31 import javax.swing.SwingUtilities;
       
    32 import com.oracle.solaris.vp.panel.common.control.*;
       
    33 import com.oracle.solaris.vp.util.swing.*;
       
    34 
       
    35 public class NavigationWindowResizer implements NavigationListener {
       
    36     //
       
    37     // NavigationListener methods
       
    38     //
       
    39 
       
    40     @Override
       
    41     public void navigationStarted(NavigationStartEvent e) {
       
    42     }
       
    43 
       
    44     @Override
       
    45     public void navigationStopped(NavigationStopEvent e) {
       
    46 	final List<Control> controls = getControls(e);
       
    47 
       
    48 	GUIUtil.invokeAndWait(
       
    49 	    new Runnable() {
       
    50 		@Override
       
    51 		public void run() {
       
    52 		    resizeWindows(controls);
       
    53 		}
       
    54 	    });
       
    55     }
       
    56 
       
    57     //
       
    58     // Private methods
       
    59     //
       
    60 
       
    61     private List<Control> getControls(NavigationStopEvent e) {
       
    62 	// Get the Controls to examine (all of the started Controls, or, if none
       
    63 	// were started, the top of the Control stack (if non-null))
       
    64 	List<Control> controls = e.getStarted();
       
    65 
       
    66 	if (controls.isEmpty()) {
       
    67 	    Control current = e.getSource().getCurrentControl();
       
    68 	    if (current != null) {
       
    69 		controls = new ArrayList<Control>(1);
       
    70 		controls.add(current);
       
    71 	    }
       
    72 	}
       
    73 
       
    74 	return controls;
       
    75     }
       
    76 
       
    77     private Set<Window> getWindows(List<Control> controls) {
       
    78 	Set<Window> windows = new HashSet<Window>();
       
    79 
       
    80 	for (Control control : controls) {
       
    81 	    if (control instanceof SwingControl) {
       
    82 		Component comp = ((SwingControl)control).getComponent();
       
    83 		if (comp != null) {
       
    84 		    Window window = SwingUtilities.getWindowAncestor(comp);
       
    85 		    if (window != null) {
       
    86 			windows.add(window);
       
    87 		    }
       
    88 		}
       
    89 	    }
       
    90 	}
       
    91 
       
    92 	return windows;
       
    93     }
       
    94 
       
    95     private void resizeWindows(List<Control> controls) {
       
    96 	for (Window window : getWindows(controls)) {
       
    97 	    Dimension cur = window.getSize();
       
    98 	    Dimension pref = window.getPreferredSize();
       
    99 
       
   100 	    // Grow window if necessary, do not shrink
       
   101 	    Dimension d = new Dimension(Math.max(cur.width, pref.width),
       
   102 		Math.max(cur.height, pref.height));
       
   103 
       
   104 	    Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
       
   105 
       
   106 	    // Don't exceed screen size
       
   107 	    d = new Dimension(Math.min(d.width, screen.width),
       
   108 		Math.min(d.height, screen.height));
       
   109 
       
   110 	    // Set size -- animate if visible
       
   111 	    if (window.isVisible()) {
       
   112 		new ResizeAnimator(window, d);
       
   113 	    } else {
       
   114 		window.setSize(d);
       
   115 	    }
       
   116 	}
       
   117     }
       
   118 }