components/visual-panels/core/src/java/vpanels/panel/com/oracle/solaris/vp/panel/swing/view/AddressPanel.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.view;
       
    27 
       
    28 import java.awt.*;
       
    29 import java.awt.event.*;
       
    30 import java.util.*;
       
    31 import javax.swing.*;
       
    32 import com.oracle.solaris.vp.panel.common.control.*;
       
    33 import com.oracle.solaris.vp.util.misc.ObjectUtil;
       
    34 import com.oracle.solaris.vp.util.swing.*;
       
    35 
       
    36 @SuppressWarnings({"serial"})
       
    37 public class AddressPanel extends JPanel {
       
    38     //
       
    39     // Static data
       
    40     //
       
    41 
       
    42     private static final String CARD_BREADCRUMBS = "breadcrumbs";
       
    43     private static final String CARD_ADDRFIELD = "addrfield";
       
    44     private static final int RESERVED_CLICKABLE_WHITESPACE_WIDTH = 50;
       
    45 
       
    46     //
       
    47     // Instance data
       
    48     //
       
    49 
       
    50     private Navigator navigator;
       
    51     private ControlBreadCrumbs breadCrumbs;
       
    52     private JTextField textField;
       
    53     private JTextField bgField;
       
    54     private String address;
       
    55 
       
    56     private MouseListener showAddressListener =
       
    57 	new MouseAdapter() {
       
    58 	    @Override
       
    59 	    public void mouseClicked(MouseEvent e) {
       
    60 		showTextField(true);
       
    61 	    }
       
    62 	};
       
    63 
       
    64     private NavigationListener navListener =
       
    65 	new NavigationListener() {
       
    66 	    @Override
       
    67 	    public void navigationStarted(NavigationStartEvent e) {
       
    68 	    }
       
    69 
       
    70 	    @Override
       
    71 	    public void navigationStopped(final NavigationStopEvent e) {
       
    72 		GUIUtil.invokeAndWait(
       
    73 		    new Runnable() {
       
    74 			@Override
       
    75 			public void run() {
       
    76 			    // getPath is safe here because invokeAndWait is
       
    77 			    // (presumably) being run by the navigation thread
       
    78 			    setAddress(e.getSource().getPath());
       
    79 			}
       
    80 		    });
       
    81 	    }
       
    82 	};
       
    83 
       
    84     //
       
    85     // Constructors
       
    86     //
       
    87 
       
    88     public AddressPanel() {
       
    89 	createTextField();
       
    90 	createBreadCrumbs();
       
    91 
       
    92 	bgField = new JTextField();
       
    93 	bgField.setEditable(false);
       
    94 
       
    95 	// Automatically show and transfer focus to the real text field
       
    96 	bgField.addFocusListener(
       
    97 	    new FocusListener() {
       
    98 		@Override
       
    99 		public void focusGained(FocusEvent e) {
       
   100 		    if (!e.isTemporary()) {
       
   101 			showTextField(true);
       
   102 		    }
       
   103 		}
       
   104 
       
   105 		@Override
       
   106 		public void focusLost(FocusEvent e) {
       
   107 		}
       
   108 	    });
       
   109 
       
   110 	// Reserve some whitespace following the bread crumbs in the layout, so
       
   111 	// that there is always something the user can click on to switch to
       
   112 	// address view, even when the AddressPanel is shrunk.
       
   113 	Spacer whiteSpace = new Spacer(RESERVED_CLICKABLE_WHITESPACE_WIDTH, 1);
       
   114 	whiteSpace.addMouseListener(showAddressListener);
       
   115 
       
   116 	JPanel breadCrumbPanel = new JPanel(new BorderLayout());
       
   117 	breadCrumbPanel.setOpaque(false);
       
   118 	breadCrumbPanel.add(breadCrumbs, BorderLayout.CENTER);
       
   119 	breadCrumbPanel.add(whiteSpace, BorderLayout.EAST);
       
   120 
       
   121 	OverlayStackPanel sPanel = new OverlayStackPanel();
       
   122 	sPanel.push(bgField);
       
   123 	sPanel.push(breadCrumbPanel);
       
   124 
       
   125 	CardLayout cards = new CardLayout();
       
   126 	setLayout(cards);
       
   127 	add(sPanel, CARD_BREADCRUMBS);
       
   128 	add(textField, CARD_ADDRFIELD);
       
   129     }
       
   130 
       
   131     public AddressPanel(Navigator navigator) {
       
   132 	this();
       
   133 	setNavigator(navigator);
       
   134     }
       
   135 
       
   136     //
       
   137     // AddressPanel methods
       
   138     //
       
   139 
       
   140     public ControlBreadCrumbs getBreadCrumbs() {
       
   141 	return breadCrumbs;
       
   142     }
       
   143 
       
   144     public Navigator getNavigator() {
       
   145 	return navigator;
       
   146     }
       
   147 
       
   148     public JTextField getTextField() {
       
   149 	return textField;
       
   150     }
       
   151 
       
   152     protected boolean isTextFieldChanged() {
       
   153 	return textField.getText().equals(address);
       
   154     }
       
   155 
       
   156     public void setAddress(Collection<Control> path) {
       
   157 	// If the breadcrumbs have focus when they are cleared, the focus will
       
   158 	// be given to the faux text field, which automatically shows the real
       
   159 	// text field and then forwards the focus to it (see its FocusListener).
       
   160 	// To avoid this, make it temporarily unfocusable.
       
   161 	bgField.setFocusable(false);
       
   162 	breadCrumbs.setAddress(path);
       
   163 	bgField.setFocusable(true);
       
   164 
       
   165 	address = path == null ? null : Navigator.getPathString(path);
       
   166 	textField.setText(address);
       
   167     }
       
   168 
       
   169     public void setNavigator(Navigator navigator) {
       
   170 	Navigator oldNav = getNavigator();
       
   171 	if (!ObjectUtil.equals(oldNav, navigator)) {
       
   172 	    if (oldNav != null) {
       
   173 		oldNav.removeNavigationListener(navListener);
       
   174 	    }
       
   175 
       
   176 	    if (navigator != null) {
       
   177 		navigator.addNavigationListener(navListener);
       
   178 	    }
       
   179 
       
   180 	    this.navigator = navigator;
       
   181 	}
       
   182     }
       
   183 
       
   184     protected void showBreadCrumbs(boolean requestFocus) {
       
   185 	((CardLayout)getLayout()).show(this, CARD_BREADCRUMBS);
       
   186 
       
   187 	if (requestFocus) {
       
   188 	    FocusTraversalPolicy policy = breadCrumbs.
       
   189 		getFocusCycleRootAncestor().getFocusTraversalPolicy();
       
   190 
       
   191 	    if (policy != null) {
       
   192 		Component last = policy.getLastComponent(breadCrumbs);
       
   193 		if (last != null) {
       
   194 		    last.requestFocusInWindow();
       
   195 		}
       
   196 	    }
       
   197 	}
       
   198     }
       
   199 
       
   200     protected void showTextField(boolean requestFocus) {
       
   201 	((CardLayout)getLayout()).show(this, CARD_ADDRFIELD);
       
   202 	textField.setText(address);
       
   203 
       
   204 	if (requestFocus) {
       
   205 	    textField.selectAll();
       
   206 	    textField.requestFocusInWindow();
       
   207 	}
       
   208     }
       
   209 
       
   210     //
       
   211     // Private methods
       
   212     //
       
   213 
       
   214     private void createTextField() {
       
   215 	textField = new JTextField(GUIUtil.getTextFieldWidth());
       
   216 	textField.setVisible(false);
       
   217 
       
   218 	// Remove shit-TAB as a backward focus traversal key
       
   219 	KeyStroke shiftTab = KeyStroke.getKeyStroke(
       
   220 	    KeyEvent.VK_TAB, InputEvent.SHIFT_DOWN_MASK);
       
   221 
       
   222 	Set<AWTKeyStroke> backKeys = new HashSet<AWTKeyStroke>(
       
   223 	    textField.getFocusTraversalKeys(
       
   224 	    KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS));
       
   225 
       
   226 	backKeys.remove(shiftTab);
       
   227 
       
   228 	textField.setFocusTraversalKeys(
       
   229 	    KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, backKeys);
       
   230 
       
   231 	// Make shift-TAB a) switch to breadcrumbs view if the text has not been
       
   232 	// changed by the user, or b) follow normal backwards focus traversal
       
   233 	// otherwise
       
   234 	Action shiftTabAction =
       
   235 	    new AbstractAction() {
       
   236 		@Override
       
   237 		public void actionPerformed(ActionEvent e) {
       
   238 		    if (isTextFieldChanged()) {
       
   239 			showBreadCrumbs(true);
       
   240 		    } else {
       
   241 			textField.transferFocusBackward();
       
   242 		    }
       
   243 		}
       
   244 	    };
       
   245 
       
   246 	GUIUtil.installKeyBinding(textField, JComponent.WHEN_FOCUSED,
       
   247 	    "showbreadcrumbsorfocusback", shiftTabAction, shiftTab);
       
   248 
       
   249 	// Make Esc switch to breadcrumbs view unconditionally
       
   250 	Action escAction =
       
   251 	    new AbstractAction() {
       
   252 		@Override
       
   253 		public void actionPerformed(ActionEvent e) {
       
   254 		    showBreadCrumbs(true);
       
   255 		}
       
   256 	    };
       
   257 
       
   258 	GUIUtil.installKeyBinding(textField, JComponent.WHEN_FOCUSED,
       
   259 	    "showbreadcrumbs", escAction, KeyEvent.VK_ESCAPE);
       
   260 
       
   261 	textField.addFocusListener(
       
   262 	    new FocusListener() {
       
   263 		@Override
       
   264 		public void focusGained(FocusEvent e) {
       
   265 		}
       
   266 
       
   267 		@Override
       
   268 		public void focusLost(FocusEvent e) {
       
   269 		    // Switch to breadcrumbs view unless the text has been
       
   270 		    // changed by the user
       
   271 		    if (!e.isTemporary() && isTextFieldChanged()) {
       
   272 			showBreadCrumbs(false);
       
   273 		    }
       
   274 		}
       
   275 	    });
       
   276 
       
   277 	textField.addActionListener(
       
   278 	    new ActionListener() {
       
   279 		@Override
       
   280 		public void actionPerformed(ActionEvent e) {
       
   281 		    String address = getTextField().getText();
       
   282 		    final SimpleNavigable[] pArray = Navigator.toArray(address);
       
   283 		    final Navigator navigator = getNavigator();
       
   284 
       
   285 		    // Launch navigation on non-event thread
       
   286 		    navigator.asyncExec(
       
   287 			new Runnable() {
       
   288 			    @Override
       
   289 			    public void run() {
       
   290 				try {
       
   291 				    navigator.goTo(false, null, pArray);
       
   292 				} catch (Exception e) {
       
   293 				    // Jump back onto event thread for UI work
       
   294 				    EventQueue.invokeLater(
       
   295 					new Runnable() {
       
   296 					    @Override
       
   297 					    public void run() {
       
   298 						showTextField(true);
       
   299 					    }
       
   300 					});
       
   301 				}
       
   302 			    }
       
   303 			});
       
   304 		}
       
   305 	    });
       
   306     }
       
   307 
       
   308     private void createBreadCrumbs() {
       
   309 	// Make bread crumb components use text field font
       
   310 	final Font font = UIManager.getFont("TextField.font");
       
   311 
       
   312 	breadCrumbs = new ControlBreadCrumbs() {
       
   313 	    @Override
       
   314 	    protected Component createBreadCrumb(int index) {
       
   315 		Component c = super.createBreadCrumb(index);
       
   316 		c.setFont(font);
       
   317 		return c;
       
   318 	    }
       
   319 
       
   320 	    @Override
       
   321 	    protected Component createSeparator(int index) {
       
   322 		Component c = super.createSeparator(index);
       
   323 		c.setFont(font);
       
   324 		return c;
       
   325 	    }
       
   326 	};
       
   327 
       
   328 	breadCrumbs.addMouseListener(showAddressListener);
       
   329     }
       
   330 }