OSDN Git Service

doing: AdjustTime - 1〜2パネル
[importpicture/importpicture.git] / src / osm / jp / gpx / matchtime / gui / ParameterPanelTime.java
index 19a0e63..8b21ccb 100644 (file)
 package osm.jp.gpx.matchtime.gui;
 
+import java.awt.Window;
 import java.awt.event.ActionEvent;
-import java.awt.event.ActionListener;
+import java.io.File;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import javax.swing.ButtonGroup;
 import javax.swing.JButton;
-import javax.swing.JFileChooser;
+import javax.swing.JRadioButton;
+import org.apache.commons.imaging.Imaging;
+import org.apache.commons.imaging.common.ImageMetadata;
+import org.apache.commons.imaging.formats.jpeg.JpegImageMetadata;
+import org.apache.commons.imaging.formats.tiff.TiffImageMetadata;
+import org.apache.commons.imaging.formats.tiff.constants.ExifTagConstants;
+import osm.jp.gpx.AppParameters;
+import osm.jp.gpx.Restamp;
+import static osm.jp.gpx.matchtime.gui.AdjustTime.dfjp;
+import osm.jp.gpx.matchtime.gui.restamp.DialogCorectTime;
 
-@SuppressWarnings("serial")
-public class ParameterPanelTime extends ParameterPanel implements ActionListener {
-    JFileChooser fc;
-    public JButton doButton;
+/**
+ * パラメータを設定する為のパネル。
+ * この1インスタンスで、1パラメータをあらわす。
+ */
+public class ParameterPanelTime extends ParameterPanel {
+    SimpleDateFormat sdf = (SimpleDateFormat)DateFormat.getDateTimeInstance();
+    ParameterPanelImageFile imageFile;  // 基準時刻画像
+    
+    
+    // 基準時刻の指定グループ (排他選択)
+    public ButtonGroup baseTimeGroup = new ButtonGroup();
+    public JRadioButton exifBase = null;       // EXIF日時を基準にする/ !(ファイル更新日時を基準にする)
+    public JRadioButton fupdateBase = null;    // File更新日時を基準にする/ !(EXIF日時を基準にする)
+    
+    public JButton updateButton;
+    public JButton resetButton;
+    Window owner;
 
-    @SuppressWarnings({"OverridableMethodCallInConstructor", "LeakingThisInConstructor"})
-    public ParameterPanelTime(String label, String text) {
+    @SuppressWarnings("OverridableMethodCallInConstructor")
+    public ParameterPanelTime(
+            String label, 
+            String text, 
+            ParameterPanelImageFile imageFile
+    ) {
         super(label, text);
+        this.imageFile = imageFile;
+        
+        // "ボタン[変更...]"
+        UpdateButtonAction buttonAction = new UpdateButtonAction(this);
+        updateButton = new JButton(i18n.getString("button.update"));
+        updateButton.addActionListener(buttonAction);
+        this.add(updateButton);
+        
+        // "ボタン[再設定...]"
+        ResetButtonAction resetAction = new ResetButtonAction(this);
+        resetButton = new JButton(i18n.getString("button.reset"));
+        resetButton.addActionListener(resetAction);
+        resetButton.setVisible(false);
+        this.add(resetButton);
+    }
+    
+    public ParameterPanelTime setOwner(Window owner) {
+        this.owner = owner;
+        return this;
+    }
+    
+    /**
+     * 「EXIFの日時を基準にする」
+     * @param label         テキスト
+     * @param params        プロパティ
+     */
+    public void addExifBase(String label, AppParameters params) {
+        boolean selected = false;
+        if (params.getProperty(AppParameters.GPX_BASETIME).equals("EXIF_TIME")) {
+            selected = true;
+        }
+        exifBase = new JRadioButton(label, selected);
+        baseTimeGroup.add(exifBase);
+    }
+
+    /**
+     * 「File更新日時を基準にする」
+     * @param label         テキスト
+     * @param params        プロパティ
+     */
+    public void addFileUpdate(String label, AppParameters params) {
+        boolean selected = false;
+        if (params.getProperty(AppParameters.GPX_BASETIME).equals("FILE_UPDATE_TIME")) {
+            selected = true;
+        }
+        fupdateBase = new JRadioButton(label, selected);
+        baseTimeGroup.add(fupdateBase);
+    }
+    
+    public ParameterPanelImageFile getImageFile() {
+        return this.imageFile;
+    }
+
+    
+    /**
+     * [変更...]ボタンのアクション
+     */
+    class UpdateButtonAction implements java.awt.event.ActionListener
+    {
+        ParameterPanelTime param;
+        
+        public UpdateButtonAction(ParameterPanelTime param) {
+            this.param = param;
+        }
+        
+        @SuppressWarnings("override")
+        public void actionPerformed(ActionEvent e) {
+            fileSelect_Action(param);
+            (new DialogCorectTime(param, owner)).setVisible(true);
+        }
+    }
+    
+    /**
+     * [再設定...]ボタンのアクション
+     */
+    class ResetButtonAction implements java.awt.event.ActionListener
+    {
+        ParameterPanelTime paramPanelTime;
+        
+        public ResetButtonAction(ParameterPanelTime param) {
+            this.paramPanelTime = param;
+        }
+        
+        @SuppressWarnings("override")
+        public void actionPerformed(ActionEvent e) {
+            fileSelect_Action(paramPanelTime);
+        }
+    }
+    
+    /**
+     * 画像ファイルが選択されたときのアクション
+     * 1.ラジオボタンの選択を参照してTEXTフィールドにファイルの「日時」を設定する
+     * @param param
+     */
+    void fileSelect_Action(ParameterPanelTime param) {
+        if (imageFile.isEnable()) {
+            File timeFile = imageFile.getImageFile();
 
-        doButton = new JButton("処理実行", AdjustTime.createImageIcon("images/media_playback_start.png"));
-        doButton.addActionListener(this);
-        this.add(doButton);
+            // Radio Selecter
+            sdf.applyPattern(Restamp.TIME_PATTERN);
+            if ((exifBase != null) && exifBase.isSelected()) {
+                try {
+                    ImageMetadata meta = Imaging.getMetadata(timeFile);
+                    JpegImageMetadata jpegMetadata = (JpegImageMetadata)meta;
+                    if (jpegMetadata != null) {
+                        TiffImageMetadata exif = jpegMetadata.getExif();
+                        if (exif != null) {
+                            String dateTimeOriginal = exif.getFieldValue(ExifTagConstants.EXIF_TAG_DATE_TIME_ORIGINAL)[0];
+                            long lastModifyTime = sdf.parse(dateTimeOriginal).getTime();
+                            param.argField.setText(dfjp.format(new Date(lastModifyTime)));
+                        }
+                        else {
+                            param.argField.setText("exif == null");
+                        }
+                    }
+                }
+                catch (Exception ex) {
+                    while(true);
+                }
+            }
+            else {
+                long lastModified = timeFile.lastModified();
+                param.argField.setText(sdf.format(new Date(lastModified)));
+            }
+        }
+        else {
+            param.argField.setText("");
+        }
     }
-       
+    
     @Override
-    public void actionPerformed(ActionEvent e) {
+    public boolean isEnable() {
+        if (this.imageFile.isEnable()) {
+            String text = this.argField.getText();
+            if (text != null) {
+                try {
+                    sdf.applyPattern(Restamp.TIME_PATTERN);
+                    sdf.parse(text);
+                    return true;
+                }
+                catch (Exception e) {
+                    return false;
+                }
+            }
+        }
+        return false;
     }
-}
\ No newline at end of file
+}