Newer
Older
adjustgpx-gui / src / main / java / osm / jp / gpx / matchtime / gui / ParameterPanelFolder.java
@haya4 haya4 on 8 Mar 2020 3 KB JDK1.8
  1. package osm.jp.gpx.matchtime.gui;
  2.  
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.io.File;
  6. import java.io.FileNotFoundException;
  7. import javax.swing.JButton;
  8. import javax.swing.JFileChooser;
  9.  
  10. @SuppressWarnings("serial")
  11. public class ParameterPanelFolder extends ParameterPanel implements ActionListener
  12. {
  13. JFileChooser fc;
  14. JButton selectButton;
  15. int chooser;
  16.  
  17. /**
  18. * コンストラクタ
  19. * ディレクトリのみ選択可能なダイアログ
  20. * @param label
  21. * @param text
  22. */
  23. public ParameterPanelFolder(String label, String text) {
  24. this(label, text, JFileChooser.DIRECTORIES_ONLY);
  25. }
  26.  
  27. public ParameterPanelFolder(String label, String text, int chooser) {
  28. super(label, text);
  29.  
  30. // Create a file chooser
  31. this.chooser = chooser;
  32.  
  33. // "選択..."
  34. selectButton = new JButton(
  35. i18n.getString("button.select"),
  36. AdjustTerra.createImageIcon("/images/Open16.gif")
  37. );
  38. selectButton.addActionListener(this);
  39. this.add(selectButton);
  40. }
  41.  
  42. public void setEnable(boolean f) {
  43. super.setEnabled(f);
  44. selectButton.setEnabled(f);
  45. }
  46.  
  47. public File getDirectory() throws FileNotFoundException {
  48. String path = this.argField.getText();
  49. if (path == null) {
  50. throw new FileNotFoundException("Folder is Not specifiyed yet.");
  51. }
  52. File sdir = new File(path);
  53. if (!sdir.exists()) {
  54. throw new FileNotFoundException(String.format("Folder '%s' is Not exists.", path));
  55. }
  56. if (!sdir.isDirectory()) {
  57. throw new FileNotFoundException(String.format("Folder '%s' is Not directory.", path));
  58. }
  59. return sdir;
  60. }
  61. @Override
  62. public void actionPerformed(ActionEvent e) {
  63. if (e.getSource() == selectButton){
  64. File sdir;
  65. try {
  66. sdir = getDirectory();
  67. } catch (FileNotFoundException ex) {
  68. sdir = new File(".");
  69. this.argField.setText(sdir.getAbsolutePath());
  70. }
  71. if (sdir.exists()) {
  72. this.fc = new JFileChooser(sdir);
  73. }
  74. else {
  75. this.fc = new JFileChooser();
  76. }
  77. this.fc.setFileSelectionMode(this.chooser);
  78.  
  79. int returnVal = this.fc.showOpenDialog(ParameterPanelFolder.this);
  80.  
  81. if (returnVal == JFileChooser.APPROVE_OPTION) {
  82. File file = this.fc.getSelectedFile();
  83. this.argField.setText(file.getAbsolutePath());
  84. }
  85. }
  86. }
  87.  
  88. /**
  89. * 有効な値が設定されているかどうか
  90. * @return
  91. */
  92. @Override
  93. public boolean isEnable() {
  94. String text = this.argField.getText();
  95. if (text == null) {
  96. return false;
  97. }
  98. try {
  99. File dir = new File(text);
  100. return (dir.exists() && dir.isDirectory());
  101. }
  102. catch (Exception e) {
  103. return false;
  104. }
  105. }
  106. }