svn-gvsig-desktop / trunk / build / distribution / IzPack / src / lib / com / izforge / izpack / panels / PathSelectionPanel.java @ 21757
History | View | Annotate | Download (6.08 KB)
1 |
/*
|
---|---|
2 |
* $Id: PathSelectionPanel.java 5819 2006-06-14 07:29:09Z cesar $
|
3 |
* IzPack
|
4 |
* Copyright (C) 2004 Klaus Bartz
|
5 |
*
|
6 |
* File : PathSelectionPanel.java
|
7 |
* Description : A sub panel to handle selection of a paths.
|
8 |
* Author's email : bartzkau@users.berlios.de
|
9 |
*
|
10 |
* This program is free software; you can redistribute it and/or
|
11 |
* modify it under the terms of the GNU General Public License
|
12 |
* as published by the Free Software Foundation; either version 2
|
13 |
* of the License, or any later version.
|
14 |
*
|
15 |
* This program is distributed in the hope that it will be useful,
|
16 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18 |
* GNU General Public License for more details.
|
19 |
*
|
20 |
* You should have received a copy of the GNU General Public License
|
21 |
* along with this program; if not, write to the Free Software
|
22 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
23 |
*/
|
24 |
package com.izforge.izpack.panels; |
25 |
|
26 |
import java.awt.Dimension; |
27 |
import java.awt.GridBagConstraints; |
28 |
import java.awt.GridBagLayout; |
29 |
import java.awt.Insets; |
30 |
import java.awt.event.ActionEvent; |
31 |
import java.awt.event.ActionListener; |
32 |
import java.io.File; |
33 |
|
34 |
import javax.swing.JButton; |
35 |
import javax.swing.JFileChooser; |
36 |
import javax.swing.JPanel; |
37 |
import javax.swing.JTextField; |
38 |
|
39 |
import com.izforge.izpack.gui.ButtonFactory; |
40 |
import com.izforge.izpack.installer.InstallData; |
41 |
import com.izforge.izpack.installer.IzPanel; |
42 |
|
43 |
/**
|
44 |
* This is a sub panel which contains a text field
|
45 |
* and a browse button for path selection.
|
46 |
* This is NOT an IzPanel, else it is made to use in an
|
47 |
* IzPanel for any path selection. If the IzPanel parent
|
48 |
* implements ActionListener, the ActionPerformed method
|
49 |
* will be called, if PathSelectionPanel.ActionPerformed
|
50 |
* was called with a source other than the browse button.
|
51 |
* This can be used to perform parentFrame.navigateNext
|
52 |
* in the IzPanel parent.
|
53 |
* An example implementation is done in
|
54 |
* com.izforge.izpack.panels.PathInputPanel.
|
55 |
*
|
56 |
* @author Klaus Bartz
|
57 |
*
|
58 |
*/
|
59 |
public class PathSelectionPanel extends JPanel implements ActionListener |
60 |
{ |
61 |
|
62 |
/** The text field for the path. */
|
63 |
private JTextField textField; |
64 |
|
65 |
/** The 'browse' button. */
|
66 |
private JButton browseButton; |
67 |
|
68 |
/** IzPanel parent (not the InstallerFrame). */
|
69 |
private IzPanel parent;
|
70 |
|
71 |
/**
|
72 |
* The installer internal data.
|
73 |
*/
|
74 |
private InstallData idata;
|
75 |
|
76 |
/**
|
77 |
* The constructor.
|
78 |
* Be aware, parent is the parent IzPanel, not
|
79 |
* 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 |
/**
|
94 |
* Creates the layout for this sub panel.
|
95 |
*/
|
96 |
protected void createLayout() |
97 |
{ |
98 |
GridBagLayout layout = new GridBagLayout(); |
99 |
|
100 |
setLayout(layout); |
101 |
textField = new JTextField(idata.getInstallPath(), 40); |
102 |
textField.addActionListener(this);
|
103 |
parent.setInitialFocus(textField); |
104 |
GridBagConstraints gbConstraints = new GridBagConstraints(); |
105 |
parent.getInstallerFrame().buildConstraints(gbConstraints, 0, 1, |
106 |
GridBagConstraints.RELATIVE, 1, 1.0, 0.0); |
107 |
gbConstraints.fill = GridBagConstraints.HORIZONTAL;
|
108 |
gbConstraints.anchor = GridBagConstraints.WEST;
|
109 |
gbConstraints.insets = new Insets(0,0,0,10); |
110 |
layout.addLayoutComponent(textField, gbConstraints); |
111 |
add(textField); |
112 |
|
113 |
browseButton = |
114 |
ButtonFactory.createButton( |
115 |
parent.getInstallerFrame().langpack.getString("TargetPanel.browse"),
|
116 |
parent.getInstallerFrame().icons.getImageIcon("open"),
|
117 |
idata.buttonsHColor); |
118 |
browseButton.addActionListener(this);
|
119 |
parent.getInstallerFrame().buildConstraints(gbConstraints, 1, 1, |
120 |
GridBagConstraints.REMAINDER, 1, 0.0, 0.0); |
121 |
gbConstraints.fill = GridBagConstraints.HORIZONTAL;
|
122 |
gbConstraints.anchor = GridBagConstraints.EAST;
|
123 |
gbConstraints.insets = new Insets(0,0,0,5); |
124 |
layout.addLayoutComponent(browseButton, gbConstraints); |
125 |
add(browseButton); |
126 |
} |
127 |
|
128 |
// There are problems with the size if no other component needs the
|
129 |
// full size. Sometimes directly, somtimes only after a back step.
|
130 |
|
131 |
public Dimension getMinimumSize() |
132 |
{ |
133 |
Dimension ss = super.getPreferredSize(); |
134 |
Dimension retval = parent.getSize();
|
135 |
retval.height = ss.height; |
136 |
return( retval);
|
137 |
} |
138 |
/**
|
139 |
* Actions-handling method.
|
140 |
*
|
141 |
* @param e The event.
|
142 |
*/
|
143 |
public void actionPerformed(ActionEvent e) |
144 |
{ |
145 |
Object source = e.getSource();
|
146 |
|
147 |
if (source == browseButton)
|
148 |
{ |
149 |
// The user wants to browse its filesystem
|
150 |
|
151 |
// Prepares the file chooser
|
152 |
JFileChooser fc = new JFileChooser(); |
153 |
fc.setCurrentDirectory(new File(textField.getText())); |
154 |
fc.setMultiSelectionEnabled(false);
|
155 |
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
|
156 |
fc.addChoosableFileFilter(fc.getAcceptAllFileFilter()); |
157 |
|
158 |
// Shows it
|
159 |
if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) |
160 |
{ |
161 |
String path = fc.getSelectedFile().getAbsolutePath();
|
162 |
textField.setText(path); |
163 |
} |
164 |
|
165 |
} |
166 |
else
|
167 |
{ |
168 |
if( parent instanceof ActionListener) |
169 |
((ActionListener)parent).actionPerformed(e);
|
170 |
} |
171 |
} |
172 |
|
173 |
/**
|
174 |
* Returns the chosen path.
|
175 |
* @return the chosen path
|
176 |
*/
|
177 |
public String getPath() |
178 |
{ |
179 |
return(textField.getText());
|
180 |
} |
181 |
|
182 |
/**
|
183 |
* Sets the contents of the text field
|
184 |
* to the given path.
|
185 |
* @param path the path to be set
|
186 |
*/
|
187 |
public void setPath(String path) |
188 |
{ |
189 |
textField.setText(path); |
190 |
} |
191 |
/**
|
192 |
* Returns the text input field for the path. This
|
193 |
* methode can be used to differ in a ActionPerformed
|
194 |
* method of the parent between the browse button and
|
195 |
* the text field.
|
196 |
* @return the text input field for the path
|
197 |
*/
|
198 |
public JTextField getPathInputField() |
199 |
{ |
200 |
return textField;
|
201 |
} |
202 |
|
203 |
} |