components/visual-panels/core/src/java/util/com/oracle/solaris/vp/util/swing/BrowsableFilePanel.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.util.swing;
       
    27 
       
    28 import java.awt.EventQueue;
       
    29 import java.awt.event.*;
       
    30 import javax.swing.*;
       
    31 import javax.swing.plaf.ComponentUI;
       
    32 import com.oracle.solaris.vp.util.misc.IOUtil;
       
    33 import com.oracle.solaris.vp.util.misc.finder.Finder;
       
    34 import com.oracle.solaris.vp.util.swing.layout.*;
       
    35 
       
    36 @SuppressWarnings({"serial"})
       
    37 public class BrowsableFilePanel extends JPanel {
       
    38     //
       
    39     // Instance data
       
    40     //
       
    41 
       
    42     private JTextField field;
       
    43     private JButton browseButton;
       
    44     private JFileChooser chooser;
       
    45 
       
    46     //
       
    47     // Constructors
       
    48     //
       
    49 
       
    50     public BrowsableFilePanel(final boolean readOnly) {
       
    51 	chooser = new JFileChooser() {
       
    52 	    @Override
       
    53 	    protected void setUI(ComponentUI newUI) {
       
    54 		UIManager.put("FileChooser.readOnly", readOnly);
       
    55 		super.setUI(newUI);
       
    56 	    }
       
    57 	};
       
    58 
       
    59 	field = new JTextField(GUIUtil.getTextFieldWidth());
       
    60 	browseButton = new JButton(Finder.getString("filepanel.button"));
       
    61 
       
    62 	browseButton.addActionListener(
       
    63 	    new ActionListener() {
       
    64 		@Override
       
    65 		public void actionPerformed(ActionEvent e) {
       
    66 		    EventQueue.invokeLater(
       
    67 			new Runnable() {
       
    68 			    @Override
       
    69 			    public void run() {
       
    70 				JTextField field = getField();
       
    71 				JFileChooser chooser = getFileChooser();
       
    72 				chooser.setSelectedFile(
       
    73 				    chooser.getFileSystemView().
       
    74 				    createFileObject(field.getText()));
       
    75 
       
    76 				int option = chooser.showDialog(field, null);
       
    77 
       
    78 				if (option == JFileChooser.APPROVE_OPTION) {
       
    79 				    field.setText(IOUtil.getFullName(
       
    80 					chooser.getSelectedFile()));
       
    81 				}
       
    82 			    }
       
    83 			});
       
    84 		}
       
    85 	    });
       
    86 
       
    87 	setLayout(new RowLayout());
       
    88 
       
    89 	RowLayoutConstraint r = new RowLayoutConstraint(
       
    90 	    VerticalAnchor.CENTER, GUIUtil.getHalfGap());
       
    91 
       
    92 	add(field, r.setWeight(1));
       
    93 	add(browseButton, r.setWeight(0));
       
    94     }
       
    95 
       
    96     public BrowsableFilePanel() {
       
    97 	this(true);
       
    98     }
       
    99 
       
   100     //
       
   101     // BrowsableFilePanel methods
       
   102     //
       
   103 
       
   104     public JTextField getField() {
       
   105 	return field;
       
   106     }
       
   107 
       
   108     public JFileChooser getFileChooser() {
       
   109 	return chooser;
       
   110     }
       
   111 
       
   112     public JButton getBrowseButton() {
       
   113 	return browseButton;
       
   114     }
       
   115 }