1 | /*
|
---|
2 | * Copyright 2005 Paul Hinds
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
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, JComponent component) {
|
---|
32 | super(text, icon);
|
---|
33 | this.component = component;
|
---|
34 | }
|
---|
35 | public SelectFileAction(String text, ImageIcon icon, JComponent component,String buttonText) {
|
---|
36 | super(text, icon);
|
---|
37 | this.component = component;
|
---|
38 | }
|
---|
39 | public void actionPerformed(ActionEvent e) {
|
---|
40 | JFileChooser chooser = new JFileChooser();
|
---|
41 | if(buttonText!=null)chooser.setApproveButtonText(buttonText);
|
---|
42 | if(selectedFile!=null && selectedFile.isFile()){
|
---|
43 | if(selectedFile.exists())chooser.setSelectedFile(selectedFile);
|
---|
44 | else chooser.setCurrentDirectory(selectedFile.getParentFile());
|
---|
45 | }
|
---|
46 | else if(selectedFile!=null && selectedFile.isDirectory()){
|
---|
47 | chooser.setCurrentDirectory(selectedFile);
|
---|
48 | }
|
---|
49 | else if(selectedFile!=null && !selectedFile.exists()){
|
---|
50 | chooser.setCurrentDirectory(selectedFile.getParentFile());
|
---|
51 | }
|
---|
52 |
|
---|
53 | int returnVal = chooser.showDialog(component,e.getActionCommand());
|
---|
54 | if(returnVal == JFileChooser.APPROVE_OPTION) {
|
---|
55 | selectedFile = chooser.getSelectedFile();
|
---|
56 | } else {
|
---|
57 | selectedFile = null;
|
---|
58 | }
|
---|
59 | }
|
---|
60 | }
|
---|