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