components/visual-panels/core/src/java/vpanels/panel/com/oracle/solaris/vp/panel/swing/view/SelectorPanel.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) 2010, 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 javax.swing.*;
       
    31 import com.oracle.solaris.vp.util.swing.*;
       
    32 import com.oracle.solaris.vp.util.swing.layout.*;
       
    33 
       
    34 @SuppressWarnings({"serial"})
       
    35 public class SelectorPanel<C extends Component> extends SettingsPanel {
       
    36     //
       
    37     // Static data
       
    38     //
       
    39 
       
    40     public static final int SELECTION_VIEWPORT_WIDTH = 175;
       
    41 
       
    42     //
       
    43     // Instance data
       
    44     //
       
    45 
       
    46     private C comp;
       
    47     private JScrollPane scroll;
       
    48     private JPopupMenu popup;
       
    49     private JLabel titleLabel;
       
    50     private JPanel buttonPanel;
       
    51     private JPanel contentCardPane;
       
    52     private boolean showButtonIcons = true;
       
    53 
       
    54     //
       
    55     // Constructors
       
    56     //
       
    57 
       
    58     public SelectorPanel() {
       
    59 	popup = new JPopupMenu();
       
    60 
       
    61 	scroll = new ExtScrollPane();
       
    62 	scroll.getViewport().setOpaque(true);
       
    63 
       
    64 	titleLabel = new AutoHideLabel();
       
    65 	setSelectionTitle(null);
       
    66 
       
    67 	RowLayout rLayout = new RowLayout(HorizontalAnchor.FILL);
       
    68 	RowLayoutConstraint r = new RowLayoutConstraint(
       
    69 	    VerticalAnchor.CENTER, GUIUtil.getButtonGap());
       
    70 	rLayout.setDefaultConstraint(r);
       
    71 
       
    72 	buttonPanel = new AutoHidePanel();
       
    73 	buttonPanel.setOpaque(false);
       
    74 	buttonPanel.setLayout(rLayout);
       
    75 
       
    76 	JPanel selectionPanel = new JPanel();
       
    77 	selectionPanel.setOpaque(false);
       
    78 
       
    79 	int gap = GUIUtil.getGap();
       
    80 	int lGap = GUIUtil.getVerticalLabelGap();
       
    81 
       
    82 	ColumnLayout cLayout = new ColumnLayout(VerticalAnchor.FILL);
       
    83 	ColumnLayoutConstraint c = new ColumnLayoutConstraint(
       
    84 	    HorizontalAnchor.FILL, lGap);
       
    85 
       
    86 	selectionPanel.setLayout(cLayout);
       
    87 	selectionPanel.add(titleLabel, c);
       
    88 	selectionPanel.add(scroll, c.clone().setWeight(1));
       
    89 	selectionPanel.add(buttonPanel, c.setGap(gap).setHorizontalAnchor(
       
    90 	    HorizontalAnchor.RIGHT));
       
    91 
       
    92 	contentCardPane = new JPanel(new CardLayout());
       
    93 	contentCardPane.setOpaque(false);
       
    94 
       
    95 	JPanel panel = getContentPane();
       
    96 	panel.setLayout(new BorderLayout(gap, gap));
       
    97 
       
    98 	panel.add(selectionPanel, BorderLayout.WEST);
       
    99 	panel.add(contentCardPane, BorderLayout.CENTER);
       
   100     }
       
   101 
       
   102     //
       
   103     // SelectorPanel methods
       
   104     //
       
   105 
       
   106     public void addButtonActions(Action... actions) {
       
   107 	for (Action action : actions) {
       
   108 	    JButton button = new JButton(action);
       
   109 
       
   110 	    if (!showButtonIcons) {
       
   111 		button.setDisabledIcon(null);
       
   112 		button.setDisabledSelectedIcon(null);
       
   113 		button.setIcon(null);
       
   114 		button.setPressedIcon(null);
       
   115 		button.setRolloverIcon(null);
       
   116 		button.setRolloverSelectedIcon(null);
       
   117 		button.setSelectedIcon(null);
       
   118 	    }
       
   119 
       
   120 	    buttonPanel.add(button);
       
   121 	}
       
   122     }
       
   123 
       
   124     public void addPopupMenuActions(Action... actions) {
       
   125 	for (Action action : actions) {
       
   126 	    popup.add(action);
       
   127 	}
       
   128     }
       
   129 
       
   130     public JPanel getContentCardPane() {
       
   131 	return contentCardPane;
       
   132     }
       
   133 
       
   134     public JPopupMenu getPopupMenu() {
       
   135 	return popup;
       
   136     }
       
   137 
       
   138     public C getSelectionComponent() {
       
   139 	return comp;
       
   140     }
       
   141 
       
   142     /**
       
   143      * Gets whether buttons created via {@link #addButtonActions} should show
       
   144      * icons.
       
   145      *
       
   146      * @see	    #setShowButtonIcons
       
   147      */
       
   148     public boolean getShowButtonIcons() {
       
   149 	return showButtonIcons;
       
   150     }
       
   151 
       
   152     protected void setSelectionComponent(C comp) {
       
   153 	this.comp = comp;
       
   154 	scroll.setViewportView(comp);
       
   155 	scroll.getViewport().setBackground(comp.getBackground());
       
   156 
       
   157 	comp.addMouseListener(
       
   158 	    new MouseAdapter() {
       
   159 		@Override
       
   160 		public void mouseReleased(final MouseEvent e) {
       
   161 		    showPopup(e);
       
   162 		}
       
   163 
       
   164 		@Override
       
   165 		public void mousePressed(final MouseEvent e) {
       
   166 		    showPopup(e);
       
   167 		}
       
   168 	    });
       
   169 
       
   170 	titleLabel.setLabelFor(comp);
       
   171     }
       
   172 
       
   173     public void setSelectionIcon(Icon icon) {
       
   174 	titleLabel.setIcon(icon);
       
   175     }
       
   176 
       
   177     public void setSelectionTitle(String title) {
       
   178 	titleLabel.setText(title);
       
   179     }
       
   180 
       
   181     /**
       
   182      * Sets whether buttons created via {@link #addButtonActions} should show
       
   183      * icons.  The default value is {@code true}.
       
   184      *
       
   185      * @see	    #getShowButtonIcons
       
   186      */
       
   187     public void setShowButtonIcons(boolean showButtonIcons) {
       
   188 	this.showButtonIcons = showButtonIcons;
       
   189     }
       
   190 
       
   191     public void showPopup(int x, int y) {
       
   192 	if (popup.getSubElements().length != 0) {
       
   193 	    popup.show(comp, x, y);
       
   194 	}
       
   195     }
       
   196 
       
   197     //
       
   198     // Private methods
       
   199     //
       
   200 
       
   201     private void showPopup(MouseEvent e) {
       
   202 	if (e.isPopupTrigger()) {
       
   203 	    showPopup(e.getX(), e.getY());
       
   204 	}
       
   205     }
       
   206 }