OSDN Git Service

対象フォルダが入力されたら次のパネルを有効にする
[importpicture/importpicture.git] / src / 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 openButton;
15     int chooser;
16
17     @SuppressWarnings({"OverridableMethodCallInConstructor", "LeakingThisInConstructor"})
18     public ParameterPanelFolder(String label, String text, int chooser) {
19         super(label, text);
20
21         // Create a file chooser
22         this.chooser = chooser;
23
24         // "選択..."
25         openButton = new JButton(
26             i18n.getString("button.select"),
27             AdjustTime.createImageIcon("images/Open16.gif")
28         );
29         openButton.addActionListener(this);
30         this.add(openButton);
31     }
32
33     public ParameterPanelFolder(String label, String text) {
34         this(label, text, JFileChooser.DIRECTORIES_ONLY);
35     }
36
37     public void setEnable(boolean f) {
38         super.setEnabled(f);
39         openButton.setEnabled(f);
40     }
41
42     public File getDirectory() throws FileNotFoundException {
43         String path = this.argField.getText();
44         if (path == null) {
45             throw new FileNotFoundException("Image folder is Not specifiyed yet.");
46         }
47         File sdir = new File(path);
48         if (!sdir.exists()) {
49             throw new FileNotFoundException(String.format("Image folder '%s' is Not exists.", path));
50         }
51         if (!sdir.isDirectory()) {
52             throw new FileNotFoundException(String.format("Image folder '%s' is Not directory.", path));
53         }
54         return sdir;
55     }
56         
57     @Override
58     public void actionPerformed(ActionEvent e) {
59         if (e.getSource() == openButton){
60             try {
61                 System.out.println("ParameterPanelFolder.actionPerformed(openButton)");
62                 File sdir = getDirectory();
63                 if (sdir.exists()) {
64                     this.fc = new JFileChooser(sdir);
65                 }
66                 else {
67                     this.fc = new JFileChooser();
68                 }
69                 this.fc.setFileSelectionMode(this.chooser);
70                 
71                 int returnVal = this.fc.showOpenDialog(ParameterPanelFolder.this);
72                 
73                 if (returnVal == JFileChooser.APPROVE_OPTION) {
74                     File file = this.fc.getSelectedFile();
75                     this.argField.setText(file.getAbsolutePath());
76                 }
77             } catch (FileNotFoundException ex) {
78                 this.argField.setText(ex.toString());
79             }
80         }
81     }
82
83     /**
84      * 有効な値が設定されているかどうか
85      * @return 
86      */
87     @Override
88     public boolean isEnable() {
89         String text = this.argField.getText();
90         if (text == null) {
91             return false;
92         }
93         try {
94             File dir = new File(text);
95             return (dir.exists() && dir.isDirectory());
96         }
97         catch (Exception e) {
98             return false;
99         }
100     }
101 }