OSDN Git Service

dec5b46be8f78dfcdc2fa53f9da9e509443a5a7b
[restamp/Restamp-gui.git] / src / main / java / osm / surveyor / matchtime / gui / ParameterPanelTime.java
1 package osm.surveyor.matchtime.gui;
2
3 import java.awt.Window;
4 import java.awt.event.ActionEvent;
5 import java.io.File;
6 import java.io.IOException;
7 import java.text.DateFormat;
8 import java.text.ParseException;
9 import java.text.SimpleDateFormat;
10 import java.util.Date;
11 import javax.swing.ButtonGroup;
12 import javax.swing.JButton;
13 import javax.swing.JRadioButton;
14 import org.apache.commons.imaging.ImageReadException;
15 import org.apache.commons.imaging.Imaging;
16 import org.apache.commons.imaging.common.ImageMetadata;
17 import org.apache.commons.imaging.formats.jpeg.JpegImageMetadata;
18 import org.apache.commons.imaging.formats.tiff.TiffImageMetadata;
19 import org.apache.commons.imaging.formats.tiff.constants.ExifTagConstants;
20 import osm.surveyor.matchtime.Restamp;
21 import static osm.surveyor.matchtime.gui.ReStamp.dfjp;
22 import osm.surveyor.matchtime.gui.restamp.DialogCorectTime;
23
24 /**
25  * パラメータを設定する為のパネル。
26  * この1インスタンスで、1パラメータをあらわす。
27  */
28 public class ParameterPanelTime extends ParameterPanel {
29         private static final long serialVersionUID = 9118495619374256843L;
30         SimpleDateFormat sdf = (SimpleDateFormat)DateFormat.getDateTimeInstance();
31     ParameterPanelImageFile imageFile;  // 基準時刻画像
32     
33     // 基準時刻の指定グループ (排他選択)
34     public ButtonGroup baseTimeGroup = new ButtonGroup();
35     public JRadioButton exifBase = null;       // EXIF日時を基準にする/ !(ファイル更新日時を基準にする)
36     public JRadioButton fupdateBase = null;    // File更新日時を基準にする/ !(EXIF日時を基準にする)
37     
38     public JButton updateButton;
39     public JButton resetButton;
40     Window owner;
41
42     public ParameterPanelTime(
43             String label, 
44             String text, 
45             ParameterPanelImageFile imageFile
46     ) {
47         super(label, text);
48         this.imageFile = imageFile;
49         
50         // "ボタン[変更...]"
51         UpdateButtonAction buttonAction = new UpdateButtonAction(this);
52         updateButton = new JButton(i18n.getString("button.update"));
53         updateButton.addActionListener(buttonAction);
54         this.add(updateButton);
55         
56         // "ボタン[再設定...]"
57         ResetButtonAction resetAction = new ResetButtonAction(this);
58         resetButton = new JButton(i18n.getString("button.reset"));
59         resetButton.addActionListener(resetAction);
60         resetButton.setVisible(false);
61         this.add(resetButton);
62     }
63     
64     public ParameterPanelTime setOwner(Window owner) {
65         this.owner = owner;
66         return this;
67     }
68     
69     public ParameterPanelImageFile getImageFile() {
70         return this.imageFile;
71     }
72
73     /**
74      * [変更...]ボタンのアクション
75      */
76     class UpdateButtonAction implements java.awt.event.ActionListener
77     {
78         ParameterPanelTime param;
79         
80         public UpdateButtonAction(ParameterPanelTime param) {
81             this.param = param;
82         }
83         
84         public void actionPerformed(ActionEvent e) {
85             fileSelect_Action(param);
86             (new DialogCorectTime(param, owner)).setVisible(true);
87         }
88     }
89     
90     /**
91      * [再設定...]ボタンのアクション
92      */
93     class ResetButtonAction implements java.awt.event.ActionListener
94     {
95         ParameterPanelTime paramPanelTime;
96         
97         public ResetButtonAction(ParameterPanelTime param) {
98             this.paramPanelTime = param;
99         }
100         
101         public void actionPerformed(ActionEvent e) {
102             fileSelect_Action(paramPanelTime);
103         }
104     }
105     
106     /**
107      * 画像ファイルが選択されたときのアクション
108      * 1.ラジオボタンの選択を参照してTEXTフィールドにファイルの「日時」を設定する
109      * @param param
110      */
111     void fileSelect_Action(ParameterPanelTime param) {
112         if (imageFile.isEnable()) {
113             File timeFile = imageFile.getImageFile();
114
115             // Radio Selecter
116             sdf.applyPattern(Restamp.TIME_PATTERN);
117             if ((exifBase != null) && exifBase.isSelected()) {
118                 try {
119                     ImageMetadata meta = Imaging.getMetadata(timeFile);
120                     JpegImageMetadata jpegMetadata = (JpegImageMetadata)meta;
121                     if (jpegMetadata != null) {
122                         TiffImageMetadata exif = jpegMetadata.getExif();
123                         if (exif != null) {
124                             String dateTimeOriginal = exif.getFieldValue(ExifTagConstants.EXIF_TAG_DATE_TIME_ORIGINAL)[0];
125                             long lastModifyTime = sdf.parse(dateTimeOriginal).getTime();
126                             param.argField.setText(dfjp.format(new Date(lastModifyTime)));
127                         }
128                         else {
129                             param.argField.setText("exif == null");
130                         }
131                     }
132                 }
133                 catch (IOException | ParseException | ImageReadException ex) {}
134             }
135             else {
136                 long lastModified = timeFile.lastModified();
137                 param.argField.setText(sdf.format(new Date(lastModified)));
138             }
139         }
140         else {
141             param.argField.setText("");
142         }
143     }
144     
145     @Override
146     public boolean isEnable() {
147         if (this.imageFile.isEnable()) {
148             String text = this.argField.getText();
149             if (text != null) {
150                 try {
151                     sdf.applyPattern(Restamp.TIME_PATTERN);
152                     sdf.parse(text);
153                     return true;
154                 }
155                 catch (ParseException e) {
156                     return false;
157                 }
158             }
159         }
160         return false;
161     }
162 }