01: /*
02: * Copyright 2005 Paul Hinds
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.tp23.gui.widget;
17:
18: import java.awt.event.ActionEvent;
19: import java.io.File;
20:
21: import javax.swing.AbstractAction;
22: import javax.swing.ImageIcon;
23: import javax.swing.JComponent;
24: import javax.swing.JFileChooser;
25:
26: public class SelectFileAction extends AbstractAction {
27: protected JComponent component = null;
28: protected File selectedFile = null;
29: protected String buttonText = null;
30:
31: public SelectFileAction(String text, ImageIcon icon,
32: JComponent component) {
33: super (text, icon);
34: this .component = component;
35: }
36:
37: public SelectFileAction(String text, ImageIcon icon,
38: JComponent component, String buttonText) {
39: super (text, icon);
40: this .component = component;
41: }
42:
43: public void actionPerformed(ActionEvent e) {
44: JFileChooser chooser = new JFileChooser();
45: if (buttonText != null)
46: chooser.setApproveButtonText(buttonText);
47: if (selectedFile != null && selectedFile.isFile()) {
48: if (selectedFile.exists())
49: chooser.setSelectedFile(selectedFile);
50: else
51: chooser.setCurrentDirectory(selectedFile
52: .getParentFile());
53: } else if (selectedFile != null && selectedFile.isDirectory()) {
54: chooser.setCurrentDirectory(selectedFile);
55: } else if (selectedFile != null && !selectedFile.exists()) {
56: chooser.setCurrentDirectory(selectedFile.getParentFile());
57: }
58:
59: int returnVal = chooser.showDialog(component, e
60: .getActionCommand());
61: if (returnVal == JFileChooser.APPROVE_OPTION) {
62: selectedFile = chooser.getSelectedFile();
63: } else {
64: selectedFile = null;
65: }
66: }
67: }
|