OSDN Git Service

894e81ea9496a4cdb076270132012dfd8700de11
[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 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("Image folder is Not specifiyed yet.");
52         }
53         File sdir = new File(path);
54         if (!sdir.exists()) {
55             throw new FileNotFoundException(String.format("Image folder '%s' is Not exists.", path));
56         }
57         if (!sdir.isDirectory()) {
58             throw new FileNotFoundException(String.format("Image 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             System.out.println("ParameterPanelFolder.actionPerformed(openButton)");
67             File sdir;
68             try {
69                 sdir = getDirectory();
70             } catch (FileNotFoundException ex) {
71                 sdir = new File(".");
72                 this.argField.setText(sdir.getAbsolutePath());
73             }
74             if (sdir.exists()) {
75                 this.fc = new JFileChooser(sdir);
76             }
77             else {
78                 this.fc = new JFileChooser();
79             }
80             this.fc.setFileSelectionMode(this.chooser);
81
82             int returnVal = this.fc.showOpenDialog(ParameterPanelFolder.this);
83
84             if (returnVal == JFileChooser.APPROVE_OPTION) {
85                 File file = this.fc.getSelectedFile();
86                 this.argField.setText(file.getAbsolutePath());
87             }
88         }
89     }
90
91     /**
92      * 有効な値が設定されているかどうか
93      * @return 
94      */
95     @Override
96     public boolean isEnable() {
97         String text = this.argField.getText();
98         if (text == null) {
99             return false;
100         }
101         try {
102             File dir = new File(text);
103             return (dir.exists() && dir.isDirectory());
104         }
105         catch (Exception e) {
106             return false;
107         }
108     }
109 }