Newer
Older
restamp-gui / src / main / java / osm / surveyor / matchtime / gui / ParameterPanelImageFile.java
@haya4 haya4 on 23 Nov 2020 3 KB maven
  1. package osm.surveyor.matchtime.gui;
  2.  
  3. import java.awt.event.ActionEvent;
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6. import javax.swing.JButton;
  7. import javax.swing.JFileChooser;
  8.  
  9. @SuppressWarnings("serial")
  10. public class ParameterPanelImageFile extends ParameterPanel {
  11. JFileChooser fc;
  12. public JButton openButton;
  13. public ParameterPanelFolder paramDir;
  14.  
  15. public ParameterPanelImageFile(
  16. String label, String text,
  17. ParameterPanelFolder paramDir
  18. ) {
  19. super(label, text);
  20.  
  21. // "選択..."
  22. SelectButtonAction buttonAction = new SelectButtonAction();
  23. openButton = new JButton(i18n.getString("button.select"));
  24. openButton.addActionListener(buttonAction);
  25. this.add(openButton);
  26. //Create a file chooser
  27. this.paramDir = paramDir;
  28. }
  29. class SelectButtonAction implements java.awt.event.ActionListener
  30. {
  31. public void actionPerformed(ActionEvent e) {
  32. selectImage_Action(e);
  33. }
  34. }
  35.  
  36. public void selectImage_Action(ActionEvent ev) {
  37. File sdir = new File(paramDir.getText());
  38. System.out.println(sdir.toPath());
  39. if (sdir.isDirectory()) {
  40. fc = new JFileChooser(sdir);
  41. }
  42. else {
  43. fc = new JFileChooser();
  44. }
  45.  
  46. fc.addChoosableFileFilter(new ImageFilter());
  47. fc.setAcceptAllFileFilterUsed(false);
  48. fc.setFileView(new ImageFileView());
  49. fc.setAccessory(new ImagePreview(fc));
  50.  
  51. //Show it. "選択"
  52. int returnVal = fc.showDialog(ParameterPanelImageFile.this, i18n.getString("dialog.select"));
  53. if (returnVal == JFileChooser.APPROVE_OPTION) {
  54. File file = fc.getSelectedFile();
  55. this.argField.setText(file.getName());
  56. }
  57. fc.setSelectedFile(null);
  58. }
  59. public File getImageFile() {
  60. if (this.paramDir.isEnable()) {
  61. String text = this.argField.getText();
  62. if (text != null) {
  63. try {
  64. File dir = this.paramDir.getDirectory();
  65. File file = new File(dir, text);
  66. if (file.exists() && file.isFile()) {
  67. return file;
  68. }
  69. }
  70. catch (FileNotFoundException e) {
  71. return null;
  72. }
  73. }
  74. }
  75. return null;
  76. }
  77. /**
  78. *
  79. * @return
  80. */
  81. @Override
  82. public boolean isEnable() {
  83. if (this.paramDir.isEnable()) {
  84. String text = this.argField.getText();
  85. if (text != null) {
  86. try {
  87. File dir = this.paramDir.getDirectory();
  88. File file = new File(dir, text);
  89. if (file.exists() && file.isFile()) {
  90. String name = file.getName().toUpperCase();
  91. if (name.endsWith(".JPG") || name.endsWith(".JPEG")) {
  92. return true;
  93. }
  94. }
  95. }
  96. catch (FileNotFoundException e) {
  97. return false;
  98. }
  99. }
  100. }
  101. return false;
  102. }
  103. }