KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > izforge > izpack > panels > PathSelectionPanel


1 /*
2  * IzPack - Copyright 2001-2007 Julien Ponge, All Rights Reserved.
3  *
4  * http://www.izforge.com/izpack/
5  * http://developer.berlios.de/projects/izpack/
6  *
7  * Copyright 2004 Klaus Bartz
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */

21
22 package com.izforge.izpack.panels;
23
24 import java.awt.Dimension JavaDoc;
25 import java.awt.GridBagConstraints JavaDoc;
26 import java.awt.GridBagLayout JavaDoc;
27 import java.awt.Insets JavaDoc;
28 import java.awt.event.ActionEvent JavaDoc;
29 import java.awt.event.ActionListener JavaDoc;
30 import java.io.File JavaDoc;
31
32 import javax.swing.JButton JavaDoc;
33 import javax.swing.JFileChooser JavaDoc;
34 import javax.swing.JPanel JavaDoc;
35 import javax.swing.JTextField JavaDoc;
36
37 import com.izforge.izpack.gui.ButtonFactory;
38 import com.izforge.izpack.gui.IzPanelConstraints;
39 import com.izforge.izpack.gui.IzPanelLayout;
40 import com.izforge.izpack.gui.LayoutConstants;
41 import com.izforge.izpack.installer.InstallData;
42 import com.izforge.izpack.installer.IzPanel;
43 import com.izforge.izpack.installer.LayoutHelper;
44
45 /**
46  * This is a sub panel which contains a text field and a browse button for path selection. This is
47  * NOT an IzPanel, else it is made to use in an IzPanel for any path selection. If the IzPanel
48  * parent implements ActionListener, the ActionPerformed method will be called, if
49  * PathSelectionPanel.ActionPerformed was called with a source other than the browse button. This
50  * can be used to perform parentFrame.navigateNext in the IzPanel parent. An example implementation
51  * is done in com.izforge.izpack.panels.PathInputPanel.
52  *
53  * @author Klaus Bartz
54  *
55  */

56 public class PathSelectionPanel extends JPanel JavaDoc implements ActionListener JavaDoc, LayoutConstants
57 {
58
59     /**
60      *
61      */

62     private static final long serialVersionUID = 3618700794577105718L;
63
64     /** The text field for the path. */
65     private JTextField JavaDoc textField;
66
67     /** The 'browse' button. */
68     private JButton JavaDoc browseButton;
69
70     /** IzPanel parent (not the InstallerFrame). */
71     private IzPanel parent;
72
73     /**
74      * The installer internal data.
75      */

76     private InstallData idata;
77
78     /**
79      * The constructor. Be aware, parent is the parent IzPanel, not the installer frame.
80      *
81      * @param parent The parent IzPanel.
82      * @param idata The installer internal data.
83      */

84     public PathSelectionPanel(IzPanel parent, InstallData idata)
85     {
86         super();
87         this.parent = parent;
88         this.idata = idata;
89         createLayout();
90     }
91
92     /**
93      * Creates the layout for this sub panel.
94      */

95     protected void createLayout()
96     {
97         // We woulduse the IzPanelLayout also in this "sub"panel.
98
// In an IzPanel there are support of this layout manager at
99
// more than one places. In this panel not, therefore we have
100
// to make all things needed.
101
// First create a layout helper.
102
LayoutHelper layoutHelper = new LayoutHelper(this);
103         // Start the layout.
104
layoutHelper.startLayout(new IzPanelLayout());
105         // One of the rare points we need explicit a constraints.
106
IzPanelConstraints ipc = IzPanelLayout.getDefaultConstraint(TEXT_CONSTRAINT);
107         // The text field should be stretched.
108
ipc.setXStretch(1.0);
109         textField = new JTextField JavaDoc(idata.getInstallPath(), 10);
110         textField.addActionListener(this);
111         parent.setInitialFocus(textField);
112         add(textField,ipc);
113         // We would have place between text field and button.
114
add(IzPanelLayout.createHorizontalFiller(3));
115         // No explicit constraints for the button (else implicit) because
116
// defaults are OK.
117
browseButton = ButtonFactory.createButton(parent.getInstallerFrame().langpack
118                 .getString("TargetPanel.browse"), parent.getInstallerFrame().icons
119                 .getImageIcon("open"), idata.buttonsHColor);
120         browseButton.addActionListener(this);
121         add(browseButton);
122     }
123
124     // There are problems with the size if no other component needs the
125
// full size. Sometimes directly, somtimes only after a back step.
126

127     public Dimension JavaDoc getMinimumSize()
128     {
129         Dimension JavaDoc ss = super.getPreferredSize();
130         Dimension JavaDoc retval = parent.getSize();
131         retval.height = ss.height;
132         return (retval);
133     }
134
135     /**
136      * Actions-handling method.
137      *
138      * @param e The event.
139      */

140     public void actionPerformed(ActionEvent JavaDoc e)
141     {
142         Object JavaDoc source = e.getSource();
143
144         if (source == browseButton)
145         {
146             // The user wants to browse its filesystem
147

148             // Prepares the file chooser
149
JFileChooser JavaDoc fc = new JFileChooser JavaDoc();
150             fc.setCurrentDirectory(new File JavaDoc(textField.getText()));
151             fc.setMultiSelectionEnabled(false);
152             fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
153             fc.addChoosableFileFilter(fc.getAcceptAllFileFilter());
154
155             // Shows it
156
if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION)
157             {
158                 String JavaDoc path = fc.getSelectedFile().getAbsolutePath();
159                 textField.setText(path);
160             }
161
162         }
163         else
164         {
165             if (parent instanceof ActionListener JavaDoc) ((ActionListener JavaDoc) parent).actionPerformed(e);
166         }
167     }
168
169     /**
170      * Returns the chosen path.
171      *
172      * @return the chosen path
173      */

174     public String JavaDoc getPath()
175     {
176         return (textField.getText());
177     }
178
179     /**
180      * Sets the contents of the text field to the given path.
181      *
182      * @param path the path to be set
183      */

184     public void setPath(String JavaDoc path)
185     {
186         textField.setText(path);
187     }
188
189     /**
190      * Returns the text input field for the path. This methode can be used to differ in a
191      * ActionPerformed method of the parent between the browse button and the text field.
192      *
193      * @return the text input field for the path
194      */

195     public JTextField JavaDoc getPathInputField()
196     {
197         return textField;
198     }
199
200     /**
201      * Returns the browse button object for modification or for use with a different ActionListener.
202      *
203      * @return the browse button to open the JFileChooser
204      */

205     public JButton JavaDoc getBrowseButton()
206     {
207         return browseButton;
208     }
209
210 }
211
Popular Tags