OSDN Git Service

doing: AdjustTime - 1〜2パネル
[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             selectImage_Action(e);
37         }
38     }
39
40     public void selectImage_Action(ActionEvent ev) {
41         File sdir = new File(paramDir.getText());
42         System.out.println(sdir.toPath());
43         if (sdir.isDirectory()) {
44             fc = new JFileChooser(sdir);
45         }
46         else {
47             fc = new JFileChooser();
48         }
49
50         fc.addChoosableFileFilter(new ImageFilter());
51         fc.setAcceptAllFileFilterUsed(false);
52         fc.setFileView(new ImageFileView());
53         fc.setAccessory(new ImagePreview(fc));
54
55         //Show it. "選択"
56         int returnVal = fc.showDialog(ParameterPanelImageFile.this, i18n.getString("dialog.select"));
57         if (returnVal == JFileChooser.APPROVE_OPTION) {
58             File file = fc.getSelectedFile();
59             this.argField.setText(file.getName());
60         }
61         fc.setSelectedFile(null);
62     }
63     
64     public File getImageFile() {
65         if (this.paramDir.isEnable()) {
66             String text = this.argField.getText();
67             if (text != null) {
68                 try {
69                     File dir = this.paramDir.getDirectory();
70                     File file = new File(dir, text);
71                     if (file.exists() && file.isFile()) {
72                         return file;
73                     }
74                 }
75                 catch (FileNotFoundException e) {
76                     return null;
77                 }
78             }
79         }
80         return null;
81     }
82     
83     /**
84      * 
85      * @return 
86      */
87     @Override
88     public boolean isEnable() {
89         if (this.paramDir.isEnable()) {
90             String text = this.argField.getText();
91             if (text != null) {
92                 try {
93                     File dir = this.paramDir.getDirectory();
94                     File file = new File(dir, text);
95                     if (file.exists() && file.isFile()) {
96                         String name = file.getName().toUpperCase();
97                         if (name.endsWith(".JPG") || name.endsWith(".JPEG")) {
98                             return true;
99                         }
100                     }
101                 }
102                 catch (FileNotFoundException e) {
103                     return false;
104                 }
105             }
106         }
107         return false;
108     }
109 }