OSDN Git Service

AdjustTime
[importpicture/importpicture.git] / src / main / java / osm / jp / gpx / matchtime / gui / ParameterPanelTime.java
1 package osm.jp.gpx.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.jp.gpx.AppParameters;
21 import osm.jp.gpx.Restamp;
22 import static osm.jp.gpx.matchtime.gui.AdjustTerra.dfjp;
23 import osm.jp.gpx.matchtime.gui.restamp.DialogCorectTime;
24
25 /**
26  * パラメータを設定する為のパネル。
27  * この1インスタンスで、1パラメータをあらわす。
28  */
29 public class ParameterPanelTime extends ParameterPanel {
30         private static final long serialVersionUID = 1683226418990348336L;
31         SimpleDateFormat sdf = (SimpleDateFormat)DateFormat.getDateTimeInstance();
32     ParameterPanelImageFile imageFile;  // 基準時刻画像
33     
34     
35     // 基準時刻の指定グループ (排他選択)
36     public ButtonGroup baseTimeGroup = new ButtonGroup();
37     public JRadioButton exifBase = null;       // EXIF日時を基準にする/ !(ファイル更新日時を基準にする)
38     public JRadioButton fupdateBase = null;    // File更新日時を基準にする/ !(EXIF日時を基準にする)
39     
40     public JButton updateButton;
41     public JButton resetButton;
42     Window owner;
43
44     public ParameterPanelTime(
45             String label, 
46             String text, 
47             ParameterPanelImageFile imageFile
48     ) {
49         super(label, text);
50         this.imageFile = imageFile;
51         
52         // "ボタン[変更...]"
53         UpdateButtonAction buttonAction = new UpdateButtonAction(this);
54         updateButton = new JButton(i18n.getString("button.update"));
55         updateButton.addActionListener(buttonAction);
56         this.add(updateButton);
57         
58         // "ボタン[再設定...]"
59         ResetButtonAction resetAction = new ResetButtonAction(this);
60         resetButton = new JButton(i18n.getString("button.reset"));
61         resetButton.addActionListener(resetAction);
62         resetButton.setVisible(false);
63         this.add(resetButton);
64     }
65     
66     public ParameterPanelTime setOwner(Window owner) {
67         this.owner = owner;
68         return this;
69     }
70     
71     /**
72      * 「EXIFの日時を基準にする」
73      * @param label         テキスト
74      * @param params        プロパティ
75      */
76     public void addExifBase(String label, AppParameters params) {
77         boolean selected = false;
78         if (params.getProperty(AppParameters.GPX_BASETIME).equals("EXIF_TIME")) {
79             selected = true;
80         }
81         exifBase = new JRadioButton(label, selected);
82         baseTimeGroup.add(exifBase);
83     }
84
85     /**
86      * 「File更新日時を基準にする」
87      * @param label         テキスト
88      * @param params        プロパティ
89      */
90     public void addFileUpdate(String label, AppParameters params) {
91         boolean selected = false;
92         if (params.getProperty(AppParameters.GPX_BASETIME).equals("FILE_UPDATE")) {
93             selected = true;
94         }
95         fupdateBase = new JRadioButton(label, selected);
96         baseTimeGroup.add(fupdateBase);
97     }
98     
99     public ParameterPanelImageFile getImageFile() {
100         return this.imageFile;
101     }
102
103     
104     /**
105      * [変更...]ボタンのアクション
106      */
107     class UpdateButtonAction implements java.awt.event.ActionListener
108     {
109         ParameterPanelTime param;
110         
111         public UpdateButtonAction(ParameterPanelTime param) {
112             this.param = param;
113         }
114         
115         public void actionPerformed(ActionEvent e) {
116             fileSelect_Action(param);
117             (new DialogCorectTime(param, owner)).setVisible(true);
118         }
119     }
120     
121     /**
122      * [再設定...]ボタンのアクション
123      */
124     class ResetButtonAction implements java.awt.event.ActionListener
125     {
126         ParameterPanelTime paramPanelTime;
127         
128         public ResetButtonAction(ParameterPanelTime param) {
129             this.paramPanelTime = param;
130         }
131         
132         public void actionPerformed(ActionEvent e) {
133             fileSelect_Action(paramPanelTime);
134         }
135     }
136     
137     /**
138      * 画像ファイルが選択されたときのアクション
139      * 1.ラジオボタンの選択を参照してTEXTフィールドにファイルの「日時」を設定する
140      * @param param
141      */
142     void fileSelect_Action(ParameterPanelTime param) {
143         if (imageFile.isEnable()) {
144             File timeFile = imageFile.getImageFile();
145
146             // Radio Selecter
147             sdf.applyPattern(Restamp.TIME_PATTERN);
148             if ((exifBase != null) && exifBase.isSelected()) {
149                 try {
150                     ImageMetadata meta = Imaging.getMetadata(timeFile);
151                     JpegImageMetadata jpegMetadata = (JpegImageMetadata)meta;
152                     if (jpegMetadata != null) {
153                         TiffImageMetadata exif = jpegMetadata.getExif();
154                         if (exif != null) {
155                             String dateTimeOriginal = exif.getFieldValue(ExifTagConstants.EXIF_TAG_DATE_TIME_ORIGINAL)[0];
156                             long lastModifyTime = sdf.parse(dateTimeOriginal).getTime();
157                             param.argField.setText(dfjp.format(new Date(lastModifyTime)));
158                         }
159                         else {
160                             param.argField.setText("exif == null");
161                         }
162                     }
163                 }
164                 catch (IOException | ParseException | ImageReadException ex) {}
165             }
166             else {
167                 long lastModified = timeFile.lastModified();
168                 param.argField.setText(sdf.format(new Date(lastModified)));
169             }
170         }
171         else {
172             param.argField.setText("");
173         }
174     }
175     
176     @Override
177     public boolean isEnable() {
178         if (this.imageFile.isEnable()) {
179             String text = this.argField.getText();
180             if (text != null) {
181                 try {
182                     sdf.applyPattern(Restamp.TIME_PATTERN);
183                     sdf.parse(text);
184                     return true;
185                 }
186                 catch (ParseException e) {
187                     return false;
188                 }
189             }
190         }
191         return false;
192     }
193 }