OSDN Git Service

fixed: 対象フォルダが入力されたら次のパネルを有効にする
[importpicture/importpicture.git] / src / osm / jp / gpx / matchtime / gui / ParameterPanelImageFile.java
1 package osm.jp.gpx.matchtime.gui;
2
3 import java.awt.event.ActionEvent;
4 import java.io.File;
5 import java.io.FileNotFoundException;
6 import javax.swing.JButton;
7 import javax.swing.JFileChooser;
8
9 @SuppressWarnings("serial")
10 public class ParameterPanelImageFile extends ParameterPanel {
11     JFileChooser fc;
12     public JButton openButton;
13     public ParameterPanelFolder paramDir;
14
15     @SuppressWarnings("OverridableMethodCallInConstructor")
16     public ParameterPanelImageFile(
17             String label, String text, 
18             ParameterPanelFolder paramDir
19     ) {
20         super(label, text);
21
22         // "選択..."
23         SelectButtonAction buttonAction = new SelectButtonAction();
24         openButton = new JButton(i18n.getString("button.select"));
25         openButton.addActionListener(buttonAction);
26         this.add(openButton);
27         
28         //Create a file chooser
29         this.paramDir = paramDir;
30     }
31
32     class SelectButtonAction implements java.awt.event.ActionListener
33     {
34         @SuppressWarnings("override")
35         public void actionPerformed(ActionEvent e) {
36             //Set up the file chooser.
37             File sdir = new File(paramDir.getText());
38             System.out.println(sdir.toPath());
39             if (sdir.isDirectory()) {
40                 fc = new JFileChooser(sdir);
41             }
42             else {
43                 fc = new JFileChooser();
44             }
45
46             //Add a custom file filter and disable the default
47             //(Accept All) file filter.
48             fc.addChoosableFileFilter(new ImageFilter());
49             fc.setAcceptAllFileFilterUsed(false);
50
51             //Add custom icons for file types.
52             fc.setFileView(new ImageFileView());
53
54             //Add the preview pane.
55             fc.setAccessory(new ImagePreview(fc));
56
57             //Show it.
58             int returnVal = fc.showDialog(ParameterPanelImageFile.this, "選択");
59
60             //Process the results.
61             if (returnVal == JFileChooser.APPROVE_OPTION) {
62                 File file = fc.getSelectedFile();
63                 argField.setText(file.getName());
64             }
65
66             //Reset the file chooser for the next time it's shown.
67             fc.setSelectedFile(null);
68         }
69     }
70
71     /**
72      * 
73      * @return 
74      */
75     @Override
76     public boolean isEnable() {
77         if (this.paramDir.isEnable()) {
78             String text = this.argField.getText();
79             if (text != null) {
80                 try {
81                     File dir = this.paramDir.getDirectory(text);
82                     File file = new File(dir, text);
83                     if (file.exists() && file.isFile()) {
84                         String name = file.getName().toUpperCase();
85                         if (name.endsWith(".JPG") || name.endsWith(".JPEG")) {
86                             return true;
87                         }
88                     }
89                 }
90                 catch (FileNotFoundException e) {
91                     return false;
92                 }
93             }
94         }
95         return false;
96     }
97 }