Newer
Older
restamp-gui / src / main / java / osm / surveyor / matchtime / gui / ParameterPanelTime.java
@haya4 haya4 on 7 May 2020 5 KB from ReStamp v3.1
  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. public ButtonGroup baseTimeGroup = new ButtonGroup();
  34. public JRadioButton exifBase = null; // EXIF日時を基準にする/ !(ファイル更新日時を基準にする)
  35. public JRadioButton fupdateBase = null; // File更新日時を基準にする/ !(EXIF日時を基準にする)
  36. public JButton updateButton;
  37. public JButton resetButton;
  38. Window owner;
  39.  
  40. public ParameterPanelTime(
  41. String label,
  42. String text,
  43. ParameterPanelImageFile imageFile
  44. ) {
  45. super(label, text);
  46. this.imageFile = imageFile;
  47. // "ボタン[変更...]"
  48. UpdateButtonAction buttonAction = new UpdateButtonAction(this);
  49. updateButton = new JButton(i18n.getString("button.update"));
  50. updateButton.addActionListener(buttonAction);
  51. this.add(updateButton);
  52. // "ボタン[再設定...]"
  53. ResetButtonAction resetAction = new ResetButtonAction(this);
  54. resetButton = new JButton(i18n.getString("button.reset"));
  55. resetButton.addActionListener(resetAction);
  56. resetButton.setVisible(false);
  57. this.add(resetButton);
  58. }
  59. public ParameterPanelTime setOwner(Window owner) {
  60. this.owner = owner;
  61. return this;
  62. }
  63. public ParameterPanelImageFile getImageFile() {
  64. return this.imageFile;
  65. }
  66.  
  67. /**
  68. * [変更...]ボタンのアクション
  69. */
  70. class UpdateButtonAction implements java.awt.event.ActionListener
  71. {
  72. ParameterPanelTime param;
  73. public UpdateButtonAction(ParameterPanelTime param) {
  74. this.param = param;
  75. }
  76. public void actionPerformed(ActionEvent e) {
  77. fileSelect_Action(param);
  78. (new DialogCorectTime(param, owner)).setVisible(true);
  79. }
  80. }
  81. /**
  82. * [再設定...]ボタンのアクション
  83. */
  84. class ResetButtonAction implements java.awt.event.ActionListener
  85. {
  86. ParameterPanelTime paramPanelTime;
  87. public ResetButtonAction(ParameterPanelTime param) {
  88. this.paramPanelTime = param;
  89. }
  90. public void actionPerformed(ActionEvent e) {
  91. fileSelect_Action(paramPanelTime);
  92. }
  93. }
  94. /**
  95. * 画像ファイルが選択されたときのアクション
  96. * 1.ラジオボタンの選択を参照してTEXTフィールドにファイルの「日時」を設定する
  97. * @param param
  98. */
  99. void fileSelect_Action(ParameterPanelTime param) {
  100. if (imageFile.isEnable()) {
  101. File timeFile = imageFile.getImageFile();
  102.  
  103. // Radio Selecter
  104. sdf.applyPattern(Restamp.TIME_PATTERN);
  105. if ((exifBase != null) && exifBase.isSelected()) {
  106. try {
  107. ImageMetadata meta = Imaging.getMetadata(timeFile);
  108. JpegImageMetadata jpegMetadata = (JpegImageMetadata)meta;
  109. if (jpegMetadata != null) {
  110. TiffImageMetadata exif = jpegMetadata.getExif();
  111. if (exif != null) {
  112. String dateTimeOriginal = exif.getFieldValue(ExifTagConstants.EXIF_TAG_DATE_TIME_ORIGINAL)[0];
  113. long lastModifyTime = sdf.parse(dateTimeOriginal).getTime();
  114. param.argField.setText(dfjp.format(new Date(lastModifyTime)));
  115. }
  116. else {
  117. param.argField.setText("exif == null");
  118. }
  119. }
  120. }
  121. catch (IOException | ParseException | ImageReadException ex) {}
  122. }
  123. else {
  124. long lastModified = timeFile.lastModified();
  125. param.argField.setText(sdf.format(new Date(lastModified)));
  126. }
  127. }
  128. else {
  129. param.argField.setText("");
  130. }
  131. }
  132. @Override
  133. public boolean isEnable() {
  134. if (this.imageFile.isEnable()) {
  135. String text = this.argField.getText();
  136. if (text != null) {
  137. try {
  138. sdf.applyPattern(Restamp.TIME_PATTERN);
  139. sdf.parse(text);
  140. return true;
  141. }
  142. catch (ParseException e) {
  143. return false;
  144. }
  145. }
  146. }
  147. return false;
  148. }
  149. }