components/visual-panels/core/src/java/vpanels/panel/com/oracle/solaris/vp/panel/swing/control/SwingNavigator.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.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 
       
    34 @SuppressWarnings({"serial"})
       
    35 public class SwingNavigator extends Navigator {
       
    36     //
       
    37     // Constructors
       
    38     //
       
    39 
       
    40     public SwingNavigator() {
       
    41 	// Resize windows on each navigation
       
    42 	addNavigationListener(new NavigationWindowResizer());
       
    43 
       
    44 	// Display a dialog on error
       
    45 	addNavigationListener(new SwingNavigationErrorHandler());
       
    46     }
       
    47 
       
    48     //
       
    49     // SwingNavigator methods
       
    50     //
       
    51 
       
    52     public Window getFirstWindow() {
       
    53 	return getFirstWindow(this);
       
    54     }
       
    55 
       
    56     public Window getLastWindow() {
       
    57 	return getLastWindow(this);
       
    58     }
       
    59 
       
    60     public List<Window> getWindows() {
       
    61 	return getWindows(this);
       
    62     }
       
    63 
       
    64     //
       
    65     // Static methods
       
    66     //
       
    67 
       
    68     /**
       
    69      * Searches forward through the navigation stack for a {@link SwingControl}
       
    70      * with a {@code Component} in a {@code Window}, then returns that {@code
       
    71      * Window}.
       
    72      */
       
    73     public static Window getFirstWindow(Navigator navigator) {
       
    74 	for (Control control : navigator.getPath()) {
       
    75 	    if (control instanceof SwingControl) {
       
    76 		Component comp = ((SwingControl)control).getComponent();
       
    77 		if (comp != null) {
       
    78 		    Window window = SwingUtilities.getWindowAncestor(comp);
       
    79 		    if (window != null) {
       
    80 			return window;
       
    81 		    }
       
    82 		}
       
    83 	    }
       
    84 	}
       
    85 	return null;
       
    86     }
       
    87 
       
    88     /**
       
    89      * Searches backward through the navigation stack for a {@link SwingControl}
       
    90      * with a {@code Component} in a {@code Window}, then returns that {@code
       
    91      * Window}.
       
    92      */
       
    93     public static Window getLastWindow(Navigator navigator) {
       
    94 	List<Control> controls = navigator.getPath();
       
    95 	for (int i = controls.size() - 1; i >= 0; i--) {
       
    96 	    Control control = controls.get(i);
       
    97 	    if (control instanceof SwingControl) {
       
    98 		Component comp = ((SwingControl)control).getComponent();
       
    99 		if (comp != null) {
       
   100 		    Window window = SwingUtilities.getWindowAncestor(comp);
       
   101 		    if (window != null) {
       
   102 			return window;
       
   103 		    }
       
   104 		}
       
   105 	    }
       
   106 	}
       
   107 	return null;
       
   108     }
       
   109 
       
   110     /**
       
   111      * Searches forward through the navigation stack for all {@link
       
   112      * SwingControl}s with a {@code Component} in a {@code Window}, then returns
       
   113      * a {@code List} of those {@code Window}s.
       
   114      */
       
   115     public static List<Window> getWindows(Navigator navigator) {
       
   116 	List<Window> windows = new LinkedList<Window>();
       
   117 	for (Control control : navigator.getPath()) {
       
   118 	    if (control instanceof SwingControl) {
       
   119 		Component comp = ((SwingControl)control).getComponent();
       
   120 		if (comp != null) {
       
   121 		    Window window = SwingUtilities.getWindowAncestor(comp);
       
   122 		    if (window != null && !windows.contains(window)) {
       
   123 			windows.add(window);
       
   124 		    }
       
   125 		}
       
   126 	    }
       
   127 	}
       
   128 	return windows;
       
   129     }
       
   130 }