OSDN Git Service

18475de75432a46090fde3d475bd7871fc8f7bd8
[restamp/Restamp-gui.git] / src / main / java / osm / surveyor / matchtime / gui / ParameterPanelImageFile.java
1 package osm.surveyor.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     public ParameterPanelImageFile(
16             String label, String text, 
17             ParameterPanelFolder paramDir
18     ) {
19         super(label, text);
20
21         // "選択..."
22         SelectButtonAction buttonAction = new SelectButtonAction();
23         openButton = new JButton(i18n.getString("button.select"));
24         openButton.addActionListener(buttonAction);
25         this.add(openButton);
26         
27         //Create a file chooser
28         this.paramDir = paramDir;
29     }
30     
31     class SelectButtonAction implements java.awt.event.ActionListener
32     {
33         public void actionPerformed(ActionEvent e) {
34             selectImage_Action(e);
35         }
36     }
37
38     public void selectImage_Action(ActionEvent ev) {
39         File sdir = new File(paramDir.getText());
40         System.out.println(sdir.toPath());
41         if (sdir.isDirectory()) {
42             fc = new JFileChooser(sdir);
43         }
44         else {
45             fc = new JFileChooser();
46         }
47
48         fc.addChoosableFileFilter(new ImageFilter());
49         fc.setAcceptAllFileFilterUsed(false);
50         fc.setFileView(new ImageFileView());
51         fc.setAccessory(new ImagePreview(fc));
52
53         //Show it. "選択"
54         int returnVal = fc.showDialog(ParameterPanelImageFile.this, i18n.getString("dialog.select"));
55         if (returnVal == JFileChooser.APPROVE_OPTION) {
56             File file = fc.getSelectedFile();
57             this.argField.setText(file.getName());
58         }
59         fc.setSelectedFile(null);
60     }
61     
62     public File getImageFile() {
63         if (this.paramDir.isEnable()) {
64             String text = this.argField.getText();
65             if (text != null) {
66                 try {
67                     File dir = this.paramDir.getDirectory();
68                     File file = new File(dir, text);
69                     if (file.exists() && file.isFile()) {
70                         return file;
71                     }
72                 }
73                 catch (FileNotFoundException e) {
74                     return null;
75                 }
76             }
77         }
78         return null;
79     }
80     
81     /**
82      * 
83      * @return 
84      */
85     @Override
86     public boolean isEnable() {
87         if (this.paramDir.isEnable()) {
88             String text = this.argField.getText();
89             if (text != null) {
90                 try {
91                     File dir = this.paramDir.getDirectory();
92                     File file = new File(dir, text);
93                     if (file.exists() && file.isFile()) {
94                         String name = file.getName().toUpperCase();
95                         if (name.endsWith(".JPG") || name.endsWith(".JPEG")) {
96                             return true;
97                         }
98                     }
99                 }
100                 catch (FileNotFoundException e) {
101                     return false;
102                 }
103             }
104         }
105         return false;
106     }
107 }