001/*
002 * IzPack - Copyright 2001-2005 Julien Ponge, All Rights Reserved.
003 * 
004 * http://www.izforge.com/izpack/
005 * http://developer.berlios.de/projects/izpack/
006 * 
007 * Copyright 2004 Klaus Bartz
008 * 
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 * 
013 *     http://www.apache.org/licenses/LICENSE-2.0
014 *     
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 */
021
022package com.izforge.izpack.panels;
023
024import java.awt.Dimension;
025import java.awt.GridBagConstraints;
026import java.awt.GridBagLayout;
027import java.awt.Insets;
028import java.awt.event.ActionEvent;
029import java.awt.event.ActionListener;
030import java.io.File;
031
032import javax.swing.JButton;
033import javax.swing.JFileChooser;
034import javax.swing.JPanel;
035import javax.swing.JTextField;
036
037import com.izforge.izpack.gui.ButtonFactory;
038import com.izforge.izpack.installer.InstallData;
039import com.izforge.izpack.installer.IzPanel;
040
041/**
042 * This is a sub panel which contains a text field and a browse button for path selection. This is
043 * NOT an IzPanel, else it is made to use in an IzPanel for any path selection. If the IzPanel
044 * parent implements ActionListener, the ActionPerformed method will be called, if
045 * PathSelectionPanel.ActionPerformed was called with a source other than the browse button. This
046 * can be used to perform parentFrame.navigateNext in the IzPanel parent. An example implementation
047 * is done in com.izforge.izpack.panels.PathInputPanel.
048 * 
049 * @author Klaus Bartz
050 * 
051 */
052public class PathSelectionPanel extends JPanel implements ActionListener
053{
054
055    /**
056     * 
057     */
058    private static final long serialVersionUID = 3618700794577105718L;
059
060    /** The text field for the path. */
061    private JTextField textField;
062
063    /** The 'browse' button. */
064    private JButton browseButton;
065
066    /** IzPanel parent (not the InstallerFrame). */
067    private IzPanel parent;
068
069    /**
070     * The installer internal data.
071     */
072    private InstallData idata;
073
074    /**
075     * The constructor. Be aware, parent is the parent IzPanel, not the installer frame.
076     * 
077     * @param parent The parent IzPanel.
078     * @param idata The installer internal data.
079     */
080    public PathSelectionPanel(IzPanel parent, InstallData idata)
081    {
082        super();
083        this.parent = parent;
084        this.idata = idata;
085        createLayout();
086    }
087
088    /**
089     * Creates the layout for this sub panel.
090     */
091    protected void createLayout()
092    {
093        GridBagLayout layout = new GridBagLayout();
094
095        setLayout(layout);
096        textField = new JTextField(idata.getInstallPath(), 40);
097        textField.addActionListener(this);
098        parent.setInitialFocus(textField);
099        GridBagConstraints gbConstraints = new GridBagConstraints();
100        parent.getInstallerFrame().buildConstraints(gbConstraints, 0, 1,
101                GridBagConstraints.RELATIVE, 1, 1.0, 0.0);
102        gbConstraints.fill = GridBagConstraints.HORIZONTAL;
103        gbConstraints.anchor = GridBagConstraints.WEST;
104        gbConstraints.insets = new Insets(0, 0, 0, 10);
105        layout.addLayoutComponent(textField, gbConstraints);
106        add(textField);
107
108        browseButton = ButtonFactory.createButton(parent.getInstallerFrame().langpack
109                .getString("TargetPanel.browse"), parent.getInstallerFrame().icons
110                .getImageIcon("open"), idata.buttonsHColor);
111        browseButton.addActionListener(this);
112        parent.getInstallerFrame().buildConstraints(gbConstraints, 1, 1,
113                GridBagConstraints.REMAINDER, 1, 0.0, 0.0);
114        gbConstraints.fill = GridBagConstraints.HORIZONTAL;
115        gbConstraints.anchor = GridBagConstraints.EAST;
116        gbConstraints.insets = new Insets(0, 0, 0, 5);
117        layout.addLayoutComponent(browseButton, gbConstraints);
118        add(browseButton);
119    }
120
121    // There are problems with the size if no other component needs the
122    // full size. Sometimes directly, somtimes only after a back step.
123
124    public Dimension getMinimumSize()
125    {
126        Dimension ss = super.getPreferredSize();
127        Dimension retval = parent.getSize();
128        retval.height = ss.height;
129        return (retval);
130    }
131
132    /**
133     * Actions-handling method.
134     * 
135     * @param e The event.
136     */
137    public void actionPerformed(ActionEvent e)
138    {
139        Object source = e.getSource();
140
141        if (source == browseButton)
142        {
143            // The user wants to browse its filesystem
144
145            // Prepares the file chooser
146            JFileChooser fc = new JFileChooser();
147            fc.setCurrentDirectory(new File(textField.getText()));
148            fc.setMultiSelectionEnabled(false);
149            fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
150            fc.addChoosableFileFilter(fc.getAcceptAllFileFilter());
151
152            // Shows it
153            if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION)
154            {
155                String path = fc.getSelectedFile().getAbsolutePath();
156                textField.setText(path);
157            }
158
159        }
160        else
161        {
162            if (parent instanceof ActionListener) ((ActionListener) parent).actionPerformed(e);
163        }
164    }
165
166    /**
167     * Returns the chosen path.
168     * 
169     * @return the chosen path
170     */
171    public String getPath()
172    {
173        return (textField.getText());
174    }
175
176    /**
177     * Sets the contents of the text field to the given path.
178     * 
179     * @param path the path to be set
180     */
181    public void setPath(String path)
182    {
183        textField.setText(path);
184    }
185
186    /**
187     * Returns the text input field for the path. This methode can be used to differ in a
188     * ActionPerformed method of the parent between the browse button and the text field.
189     * 
190     * @return the text input field for the path
191     */
192    public JTextField getPathInputField()
193    {
194        return textField;
195    }
196
197}