components/visual-panels/coreadm/src/java/vpanels/app/coreadm/com/oracle/solaris/vp/panels/coreadm/client/swing/path/TokenComboBox.java
changeset 3553 f1d133b09a8c
parent 3552 077ebe3d0d24
child 3554 ef58713bafc4
equal deleted inserted replaced
3552:077ebe3d0d24 3553:f1d133b09a8c
     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.panels.coreadm.client.swing.path;
       
    27 
       
    28 import java.awt.*;
       
    29 import java.awt.event.*;
       
    30 import javax.swing.*;
       
    31 import javax.swing.border.Border;
       
    32 import com.oracle.solaris.vp.util.misc.finder.Finder;
       
    33 import com.oracle.solaris.vp.util.swing.Spacer;
       
    34 
       
    35 @SuppressWarnings({"serial"})
       
    36 public class TokenComboBox extends JComboBox {
       
    37     //
       
    38     // Inner classes
       
    39     //
       
    40 
       
    41     public static class TokenListCellRenderer extends JPanel
       
    42 	implements ListCellRenderer {
       
    43 
       
    44 	//
       
    45 	// Static data
       
    46 	//
       
    47 
       
    48 	protected static final Border BORDER =
       
    49 	    BorderFactory.createEmptyBorder(1, 1, 1, 1);
       
    50 
       
    51 	protected static final String CARD_INSERT = "insert";
       
    52 	protected static final String CARD_IN_LIST = "inlist";
       
    53 
       
    54 	//
       
    55 	// Instance data
       
    56 	//
       
    57 
       
    58 	private TokenLabel token;
       
    59 	private JLabel text;
       
    60 	private Spacer spacer = new Spacer();
       
    61 
       
    62 	//
       
    63 	// Constructors
       
    64 	//
       
    65 
       
    66 	public TokenListCellRenderer() {
       
    67 	    setBorder(BORDER);
       
    68 	    setBackground(Color.white);
       
    69 
       
    70 	    Dimension d = null;
       
    71 	    token = new TokenLabel();
       
    72 	    for (String t : PathDocument.TOKENS) {
       
    73 		token.setText(t);
       
    74 		Dimension p = token.getPreferredSize();
       
    75 		if (d == null || p.width > d.width) {
       
    76 		    d = p;
       
    77 		}
       
    78 	    }
       
    79 	    token.setPreferredSize(d);
       
    80 	    token.setHorizontalAlignment(SwingConstants.CENTER);
       
    81 
       
    82 	    text = new JLabel();
       
    83 	    text.setBackground(UIManager.getColor(
       
    84 		"List.selectionBackground"));
       
    85 
       
    86 	    JPanel inListPanel = new JPanel(
       
    87 		new BorderLayout(text.getIconTextGap(), 0));
       
    88 	    inListPanel.setOpaque(false);
       
    89 
       
    90 	    inListPanel.add(token, BorderLayout.WEST);
       
    91 	    inListPanel.add(text, BorderLayout.CENTER);
       
    92 
       
    93 	    JTextField insertLabel = new JTextField(
       
    94 		Finder.getString("path.combo.insert"));
       
    95 	    insertLabel.setHorizontalAlignment(SwingConstants.CENTER);
       
    96 
       
    97 	    setLayout(new CardLayout());
       
    98 	    add(insertLabel, CARD_INSERT);
       
    99 	    add(inListPanel, CARD_IN_LIST);
       
   100 	    setOpaque(false);
       
   101 	}
       
   102 
       
   103 	//
       
   104 	// ListCellRenderer methods
       
   105 	//
       
   106 
       
   107 	@Override
       
   108 	public Component getListCellRendererComponent(JList list, Object value,
       
   109 	    int index, boolean isSelected, boolean cellHasFocus) {
       
   110 
       
   111 	    token.setText((String)value);
       
   112 	    text.setText(token.getToolTipText());
       
   113 	    text.setOpaque(isSelected);
       
   114 	    text.setForeground(UIManager.getColor(isSelected ?
       
   115 		"List.selectionForeground" :
       
   116 		"List.foreground"));
       
   117 
       
   118 	    if (index == -1) {
       
   119 		((CardLayout)getLayout()).show(this, CARD_INSERT);
       
   120 	    } else {
       
   121 		if (value == null) {
       
   122 		    return spacer;
       
   123 		}
       
   124 
       
   125 		((CardLayout)getLayout()).show(this, CARD_IN_LIST);
       
   126 	    }
       
   127 
       
   128 	    return this;
       
   129 	}
       
   130     }
       
   131 
       
   132     //
       
   133     // Static data
       
   134     //
       
   135 
       
   136     protected static final String[] DATA;
       
   137     static {
       
   138 	DATA = new String[PathDocument.TOKENS.length + 1];
       
   139 	System.arraycopy(PathDocument.TOKENS, 0, DATA, 1,
       
   140 	    PathDocument.TOKENS.length);
       
   141     }
       
   142 
       
   143     //
       
   144     // Constructors
       
   145     //
       
   146 
       
   147     public TokenComboBox() {
       
   148 	super(DATA);
       
   149 	setRenderer(new TokenListCellRenderer());
       
   150 
       
   151 	// Continuously reset selected item to null (-- Insert --)
       
   152 	addActionListener(
       
   153 	    new ActionListener() {
       
   154 		@Override
       
   155 		public void actionPerformed(ActionEvent e) {
       
   156 		    if (getSelectedItem() != null) {
       
   157 			// Change selection *after* notification of the current
       
   158 			// selection is done
       
   159 			EventQueue.invokeLater(
       
   160 			    new Runnable() {
       
   161 				@Override
       
   162 				public void run() {
       
   163 				    setSelectedItem(null);
       
   164 				}
       
   165 			    });
       
   166 		    }
       
   167 		}
       
   168 	    });
       
   169     }
       
   170 
       
   171     //
       
   172     // Static methods
       
   173     //
       
   174 
       
   175     // XXX - Test, remove
       
   176     public static void main(String args[]) {
       
   177 	TokenComboBox combo = new TokenComboBox();
       
   178 
       
   179 	JPanel p = new JPanel(new BorderLayout());
       
   180 	p.add(combo);
       
   181 	p.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
       
   182 
       
   183 	JFrame frame = new JFrame();
       
   184 	Container c = frame.getContentPane();
       
   185 	c.setLayout(new BorderLayout());
       
   186 	c.add(p, BorderLayout.CENTER);
       
   187 	frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
       
   188 	frame.pack();
       
   189 	frame.setVisible(true);
       
   190     }
       
   191 }