OSDN Git Service

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