001: /*
002: * IzPack - Copyright 2001-2008 Julien Ponge, All Rights Reserved.
003: *
004: * http://izpack.org/
005: * http://izpack.codehaus.org/
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:
022: package com.izforge.izpack.panels;
023:
024: import java.awt.Dimension;
025: import java.awt.event.ActionEvent;
026: import java.awt.event.ActionListener;
027: import java.io.File;
028:
029: import javax.swing.JButton;
030: import javax.swing.JFileChooser;
031: import javax.swing.JPanel;
032: import javax.swing.JTextField;
033:
034: import com.izforge.izpack.gui.ButtonFactory;
035: import com.izforge.izpack.gui.IzPanelConstraints;
036: import com.izforge.izpack.gui.IzPanelLayout;
037: import com.izforge.izpack.gui.LayoutConstants;
038: import com.izforge.izpack.installer.InstallData;
039: import com.izforge.izpack.installer.IzPanel;
040: import com.izforge.izpack.installer.LayoutHelper;
041:
042: /**
043: * This is a sub panel which contains a text field and a browse button for path selection. This is
044: * NOT an IzPanel, else it is made to use in an IzPanel for any path selection. If the IzPanel
045: * parent implements ActionListener, the ActionPerformed method will be called, if
046: * PathSelectionPanel.ActionPerformed was called with a source other than the browse button. This
047: * can be used to perform parentFrame.navigateNext in the IzPanel parent. An example implementation
048: * is done in com.izforge.izpack.panels.PathInputPanel.
049: *
050: * @author Klaus Bartz
051: *
052: */
053: public class PathSelectionPanel extends JPanel implements
054: ActionListener, LayoutConstants {
055:
056: /**
057: *
058: */
059: private static final long serialVersionUID = 3618700794577105718L;
060:
061: /** The text field for the path. */
062: private JTextField textField;
063:
064: /** The 'browse' button. */
065: private JButton browseButton;
066:
067: /** IzPanel parent (not the InstallerFrame). */
068: private IzPanel parent;
069:
070: /**
071: * The installer internal data.
072: */
073: private InstallData idata;
074:
075: /**
076: * The constructor. Be aware, parent is the parent IzPanel, not the installer frame.
077: *
078: * @param parent The parent IzPanel.
079: * @param idata The installer internal data.
080: */
081: public PathSelectionPanel(IzPanel parent, InstallData idata) {
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: // We would use the IzPanelLayout also in this "sub" panel.
093: // In an IzPanel there is support for this layout manager in
094: // more than one place, but not in this panel so we have
095: // to make all things needed.
096: // First create a layout helper.
097: LayoutHelper layoutHelper = new LayoutHelper(this );
098: // Start the layout.
099: layoutHelper.startLayout(new IzPanelLayout());
100: // One of the rare points we need explicit a constraints.
101: IzPanelConstraints ipc = IzPanelLayout
102: .getDefaultConstraint(TEXT_CONSTRAINT);
103: // The text field should be stretched.
104: ipc.setXStretch(1.0);
105: textField = new JTextField(idata.getInstallPath(), 10);
106: textField.addActionListener(this );
107: parent.setInitialFocus(textField);
108: add(textField, ipc);
109: // We would have place between text field and button.
110: add(IzPanelLayout.createHorizontalFiller(3));
111: // No explicit constraints for the button (else implicit) because
112: // defaults are OK.
113: browseButton = ButtonFactory.createButton(parent
114: .getInstallerFrame().langpack
115: .getString("TargetPanel.browse"), parent
116: .getInstallerFrame().icons.getImageIcon("open"),
117: idata.buttonsHColor);
118: browseButton.addActionListener(this );
119: add(browseButton);
120: }
121:
122: // There are problems with the size if no other component needs the
123: // full size. Sometimes directly, somtimes only after a back step.
124:
125: public Dimension getMinimumSize() {
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: Object source = e.getSource();
139:
140: if (source == browseButton) {
141: // The user wants to browse its filesystem
142:
143: // Prepares the file chooser
144: JFileChooser fc = new JFileChooser();
145: fc.setCurrentDirectory(new File(textField.getText()));
146: fc.setMultiSelectionEnabled(false);
147: fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
148: fc.addChoosableFileFilter(fc.getAcceptAllFileFilter());
149:
150: // Shows it
151: if (fc.showOpenDialog(this ) == JFileChooser.APPROVE_OPTION) {
152: String path = fc.getSelectedFile().getAbsolutePath();
153: textField.setText(path);
154: }
155:
156: } else {
157: if (parent instanceof ActionListener)
158: ((ActionListener) parent).actionPerformed(e);
159: }
160: }
161:
162: /**
163: * Returns the chosen path.
164: *
165: * @return the chosen path
166: */
167: public String getPath() {
168: return (textField.getText());
169: }
170:
171: /**
172: * Sets the contents of the text field to the given path.
173: *
174: * @param path the path to be set
175: */
176: public void setPath(String path) {
177: textField.setText(path);
178: }
179:
180: /**
181: * Returns the text input field for the path. This methode can be used to differ in a
182: * ActionPerformed method of the parent between the browse button and the text field.
183: *
184: * @return the text input field for the path
185: */
186: public JTextField getPathInputField() {
187: return textField;
188: }
189:
190: /**
191: * Returns the browse button object for modification or for use with a different ActionListener.
192: *
193: * @return the browse button to open the JFileChooser
194: */
195: public JButton getBrowseButton() {
196: return browseButton;
197: }
198:
199: }
|