OSDN Git Service

maven
[restamp/Restamp-gui.git] / src / main / java / osm / surveyor / matchtime / gui / ParameterPanelFolder.java
1 package osm.surveyor.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     public ParameterPanelFolder(String label, String text, int chooser) {
28         super(label, text);
29
30         // Create a file chooser
31         this.chooser = chooser;
32
33         // "選択..."
34         selectButton = new JButton(
35             i18n.getString("button.select"),
36             ReStamp.createImageIcon("/images/Open16.gif")
37         );
38         selectButton.addActionListener(this);
39         this.add(selectButton);
40     }
41
42     public void setEnable(boolean f) {
43         super.setEnabled(f);
44         selectButton.setEnabled(f);
45     }
46
47     public File getDirectory() throws FileNotFoundException {
48         String path = this.argField.getText();
49         if (path == null) {
50             throw new FileNotFoundException("Folder is Not specifiyed yet.");
51         }
52         File sdir = new File(path);
53         if (!sdir.exists()) {
54             throw new FileNotFoundException(String.format("Folder '%s' is Not exists.", path));
55         }
56         if (!sdir.isDirectory()) {
57             throw new FileNotFoundException(String.format("Folder '%s' is Not directory.", path));
58         }
59         return sdir;
60     }
61         
62     @Override
63     public void actionPerformed(ActionEvent e) {
64         if (e.getSource() == selectButton){
65             File sdir;
66             try {
67                 sdir = getDirectory();
68             } catch (FileNotFoundException ex) {
69                 sdir = new File(".");
70                 this.argField.setText(sdir.getAbsolutePath());
71             }
72             if (sdir.exists()) {
73                 this.fc = new JFileChooser(sdir);
74             }
75             else {
76                 this.fc = new JFileChooser();
77             }
78             this.fc.setFileSelectionMode(this.chooser);
79
80             int returnVal = this.fc.showOpenDialog(ParameterPanelFolder.this);
81
82             if (returnVal == JFileChooser.APPROVE_OPTION) {
83                 File file = this.fc.getSelectedFile();
84                 this.argField.setText(file.getAbsolutePath());
85             }
86         }
87     }
88
89     /**
90      * 有効な値が設定されているかどうか
91      * @return 
92      */
93     @Override
94     public boolean isEnable() {
95         String text = this.argField.getText();
96         if (text == null) {
97             return false;
98         }
99         try {
100             File dir = new File(text);
101             return (dir.exists() && dir.isDirectory());
102         }
103         catch (Exception e) {
104             return false;
105         }
106     }
107 }