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: package com.izforge.izpack.panels;
022:
023: import java.awt.Dimension;
024: import java.awt.event.ActionEvent;
025: import java.awt.event.ActionListener;
026: import java.io.File;
027:
028: import javax.swing.JButton;
029: import javax.swing.JFileChooser;
030: import javax.swing.JPanel;
031: import javax.swing.JTextField;
032:
033: import com.izforge.izpack.gui.ButtonFactory;
034: import com.izforge.izpack.gui.IzPanelConstraints;
035: import com.izforge.izpack.gui.IzPanelLayout;
036: import com.izforge.izpack.gui.LayoutConstants;
037: import com.izforge.izpack.installer.InstallData;
038: import com.izforge.izpack.installer.IzPanel;
039: import com.izforge.izpack.installer.LayoutHelper;
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: * @author Jeff Gordon
051: *
052: */
053: public class UserPathSelectionPanel extends JPanel implements
054: ActionListener, LayoutConstants {
055:
056: /**
057: *
058: */
059: private static final long serialVersionUID = 3618700794577105718L;
060: /** The text field for the path. */
061: private JTextField textField;
062: /** The 'browse' button. */
063: private JButton browseButton;
064: /** IzPanel parent (not the InstallerFrame). */
065: private IzPanel parent;
066: /**
067: * The installer internal data.
068: */
069: private InstallData idata;
070: private String targetPanel;
071: private String variableName;
072: private String defaultPanelName = "TargetPanel";
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 UserPathSelectionPanel(IzPanel parent, InstallData idata,
081: String targetPanel, String variableName) {
082: super ();
083: this .parent = parent;
084: this .idata = idata;
085: this .variableName = variableName;
086: this .targetPanel = targetPanel;
087: createLayout();
088: }
089:
090: /**
091: * Creates the layout for this sub panel.
092: */
093: protected void createLayout() {
094: // We woulduse the IzPanelLayout also in this "sub"panel.
095: // In an IzPanel there are support of this layout manager at
096: // more than one places. In this panel not, therefore we have
097: // to make all things needed.
098: // First create a layout helper.
099: LayoutHelper layoutHelper = new LayoutHelper(this );
100: // Start the layout.
101: layoutHelper.startLayout(new IzPanelLayout());
102: // One of the rare points we need explicit a constraints.
103: IzPanelConstraints ipc = IzPanelLayout
104: .getDefaultConstraint(TEXT_CONSTRAINT);
105: // The text field should be stretched.
106: ipc.setXStretch(1.0);
107: textField = new JTextField(idata.getVariable(variableName), 35);
108: textField.addActionListener(this );
109: parent.setInitialFocus(textField);
110: add(textField, ipc);
111: // We would have place between text field and button.
112: add(IzPanelLayout.createHorizontalFiller(3));
113: // No explicit constraints for the button (else implicit) because
114: // defaults are OK.
115: String buttonText = parent.getInstallerFrame().langpack
116: .getString(targetPanel + ".browse");
117: if (buttonText == null) {
118: buttonText = parent.getInstallerFrame().langpack
119: .getString(defaultPanelName + ".browse");
120: }
121: browseButton = ButtonFactory.createButton(buttonText, parent
122: .getInstallerFrame().icons.getImageIcon("open"),
123: idata.buttonsHColor);
124: browseButton.addActionListener(this );
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: public Dimension getMinimumSize() {
131: Dimension ss = super .getPreferredSize();
132: Dimension retval = parent.getSize();
133: retval.height = ss.height;
134: return (retval);
135: }
136:
137: /**
138: * Actions-handling method.
139: *
140: * @param e The event.
141: */
142: public void actionPerformed(ActionEvent e) {
143: Object source = e.getSource();
144:
145: if (source == browseButton) {
146: // The user wants to browse its filesystem
147:
148: // Prepares the file chooser
149: JFileChooser fc = new JFileChooser();
150: fc.setCurrentDirectory(new File(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: String path = fc.getSelectedFile().getAbsolutePath();
158: textField.setText(path);
159: }
160:
161: } else {
162: if (parent instanceof ActionListener) {
163: ((ActionListener) parent).actionPerformed(e);
164: }
165: }
166: }
167:
168: /**
169: * Returns the chosen path.
170: *
171: * @return the chosen path
172: */
173: public String getPath() {
174: return (textField.getText());
175: }
176:
177: /**
178: * Sets the contents of the text field to the given path.
179: *
180: * @param path the path to be set
181: */
182: public void setPath(String path) {
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: return textField;
194: }
195:
196: /**
197: * Returns the browse button object for modification or for use with a different ActionListener.
198: *
199: * @return the browse button to open the JFileChooser
200: */
201: public JButton getBrowseButton() {
202: return browseButton;
203: }
204: }
|