OSDN Git Service

fixed: GUIを一新した
[importpicture/importpicture.git] / src / main / java / osm / jp / gpx / matchtime / gui / ParameterPanelFolder.java
1 package osm.jp.gpx.matchtime.gui;
2
3 import java.awt.event.ActionEvent;
4 import java.awt.event.ActionListener;
5 import java.io.File;
6 import java.io.FileNotFoundException;
7 import javax.swing.JButton;
8 import javax.swing.JFileChooser;
9
10 @SuppressWarnings("serial")
11 public class ParameterPanelFolder extends ParameterPanel implements ActionListener
12 {
13     JFileChooser fc;
14     JButton selectButton;
15     int chooser;
16
17     /**
18      * コンストラクタ
19      * ディレクトリのみ選択可能なダイアログ
20      * @param label
21      * @param text 
22      */
23     public ParameterPanelFolder(String label, String text) {
24         this(label, text, JFileChooser.DIRECTORIES_ONLY);
25     }
26
27     @SuppressWarnings({"OverridableMethodCallInConstructor", "LeakingThisInConstructor"})
28     public ParameterPanelFolder(String label, String text, int chooser) {
29         super(label, text);
30
31         // Create a file chooser
32         this.chooser = chooser;
33
34         // "選択..."
35         selectButton = new JButton(
36             i18n.getString("button.select"),
37             AdjustTime.createImageIcon("images/Open16.gif")
38         );
39         selectButton.addActionListener(this);
40         this.add(selectButton);
41     }
42
43     public void setEnable(boolean f) {
44         super.setEnabled(f);
45         selectButton.setEnabled(f);
46     }
47
48     public File getDirectory() throws FileNotFoundException {
49         String path = this.argField.getText();
50         if (path == null) {
51             throw new FileNotFoundException("Folder is Not specifiyed yet.");
52         }
53         File sdir = new File(path);
54         if (!sdir.exists()) {
55             throw new FileNotFoundException(String.format("Folder '%s' is Not exists.", path));
56         }
57         if (!sdir.isDirectory()) {
58             throw new FileNotFoundException(String.format("Folder '%s' is Not directory.", path));
59         }
60         return sdir;
61     }
62         
63     @Override
64     public void actionPerformed(ActionEvent e) {
65         if (e.getSource() == selectButton){
66             File sdir;
67             try {
68                 sdir = getDirectory();
69             } catch (FileNotFoundException ex) {
70                 sdir = new File(".");
71                 this.argField.setText(sdir.getAbsolutePath());
72             }
73             if (sdir.exists()) {
74                 this.fc = new JFileChooser(sdir);
75             }
76             else {
77                 this.fc = new JFileChooser();
78             }
79             this.fc.setFileSelectionMode(this.chooser);
80
81             int returnVal = this.fc.showOpenDialog(ParameterPanelFolder.this);
82
83             if (returnVal == JFileChooser.APPROVE_OPTION) {
84                 File file = this.fc.getSelectedFile();
85                 this.argField.setText(file.getAbsolutePath());
86             }
87         }
88     }
89
90     /**
91      * 有効な値が設定されているかどうか
92      * @return 
93      */
94     @Override
95     public boolean isEnable() {
96         String text = this.argField.getText();
97         if (text == null) {
98             return false;
99         }
100         try {
101             File dir = new File(text);
102             return (dir.exists() && dir.isDirectory());
103         }
104         catch (Exception e) {
105             return false;
106         }
107     }
108 }