This is an automated email from the ASF dual-hosted git repository.

seanfinan pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/ctakes.git


The following commit(s) were added to refs/heads/main by this push:
     new 5df171e  Fix for rare environments that caused an oobE in the list 
renderer of JFileChooser.
5df171e is described below

commit 5df171e12c8277347243699a95524d4a5cee0e12
Author: Sean Finan <[email protected]>
AuthorDate: Wed Aug 21 11:19:39 2024 -0400

    Fix for rare environments that caused an oobE in the list renderer of 
JFileChooser.
---
 .../ctakes/gui/component/FileChooserPanel.java     | 19 +++++----
 .../ctakes/gui/component/FileTableCellEditor.java  | 17 ++++----
 .../ctakes/gui/pipeline/PiperRunnerPanel.java      | 11 ++----
 .../apache/ctakes/gui/util/FileChooserUtil.java    | 45 ++++++++++++++++++++++
 4 files changed, 68 insertions(+), 24 deletions(-)

diff --git 
a/ctakes-gui/src/main/java/org/apache/ctakes/gui/component/FileChooserPanel.java
 
b/ctakes-gui/src/main/java/org/apache/ctakes/gui/component/FileChooserPanel.java
index cff5d6e..31274cd 100644
--- 
a/ctakes-gui/src/main/java/org/apache/ctakes/gui/component/FileChooserPanel.java
+++ 
b/ctakes-gui/src/main/java/org/apache/ctakes/gui/component/FileChooserPanel.java
@@ -1,5 +1,6 @@
 package org.apache.ctakes.gui.component;
 
+import org.apache.ctakes.gui.util.FileChooserUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -65,13 +66,14 @@ final public class FileChooserPanel extends JPanel {
          super( "Select " + (selectDir ? "Directory" : "File") );
          __textComponent = textComponent;
          __chooser = new JFileChooser();
-         String cwdPath = Paths.get( "" ).toAbsolutePath().toFile().getPath();
-         if ( cwdPath.isEmpty() ) {
-            cwdPath = System.getProperty( "user.dir" );
-         }
-         if ( cwdPath != null && !cwdPath.isEmpty() ) {
-            __chooser.setCurrentDirectory( new File( cwdPath ) );
-         }
+//         String cwdPath = Paths.get( "" 
).toAbsolutePath().toFile().getPath();
+//         if ( cwdPath.isEmpty() ) {
+//            cwdPath = System.getProperty( "user.dir" );
+//         }
+//         if ( cwdPath != null && !cwdPath.isEmpty() ) {
+//            __chooser.setCurrentDirectory( new File( cwdPath ) );
+//         }
+         FileChooserUtil.selectWorkingDir( __chooser );
          __chooser.setFileSelectionMode( (selectDir ? 
JFileChooser.DIRECTORIES_ONLY : JFileChooser.FILES_ONLY) );
          __fileChangeListener = dirChangeListener;
       }
@@ -82,7 +84,8 @@ final public class FileChooserPanel extends JPanel {
          if ( startDirPath != null && !startDirPath.isEmpty() ) {
             final File startingDir = new File( startDirPath );
             if ( startingDir.exists() ) {
-               __chooser.setCurrentDirectory( startingDir );
+//               __chooser.setCurrentDirectory( startingDir );
+               FileChooserUtil.selectDir( __chooser, startingDir.getPath() );
             }
          }
          final int option = __chooser.showOpenDialog( null );
diff --git 
a/ctakes-gui/src/main/java/org/apache/ctakes/gui/component/FileTableCellEditor.java
 
b/ctakes-gui/src/main/java/org/apache/ctakes/gui/component/FileTableCellEditor.java
index f599443..0111e75 100644
--- 
a/ctakes-gui/src/main/java/org/apache/ctakes/gui/component/FileTableCellEditor.java
+++ 
b/ctakes-gui/src/main/java/org/apache/ctakes/gui/component/FileTableCellEditor.java
@@ -1,6 +1,7 @@
 package org.apache.ctakes.gui.component;
 
 
+import org.apache.ctakes.gui.util.FileChooserUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -35,14 +36,14 @@ final public class FileTableCellEditor extends 
AbstractCellEditor
       _button.setToolTipText( "Select File" );
       _button.addActionListener( this );
       _chooser = new JFileChooser();
-      String cwdPath = Paths.get( "" ).toAbsolutePath().toFile().getPath();
-      if ( cwdPath.isEmpty() ) {
-         cwdPath = System.getProperty( "user.dir" );
-      }
-      if ( cwdPath != null && !cwdPath.isEmpty() ) {
-         _chooser.setCurrentDirectory( new File( cwdPath ) );
-      }
-
+//      String cwdPath = Paths.get( "" ).toAbsolutePath().toFile().getPath();
+//      if ( cwdPath.isEmpty() ) {
+//         cwdPath = System.getProperty( "user.dir" );
+//      }
+//      if ( cwdPath != null && !cwdPath.isEmpty() ) {
+//         _chooser.setCurrentDirectory( new File( cwdPath ) );
+//      }
+      FileChooserUtil.selectWorkingDir( _chooser );
    }
 
    public JFileChooser getFileChooser() {
diff --git 
a/ctakes-gui/src/main/java/org/apache/ctakes/gui/pipeline/PiperRunnerPanel.java 
b/ctakes-gui/src/main/java/org/apache/ctakes/gui/pipeline/PiperRunnerPanel.java
index e67597d..5314ce8 100644
--- 
a/ctakes-gui/src/main/java/org/apache/ctakes/gui/pipeline/PiperRunnerPanel.java
+++ 
b/ctakes-gui/src/main/java/org/apache/ctakes/gui/pipeline/PiperRunnerPanel.java
@@ -11,6 +11,7 @@ import org.apache.ctakes.gui.component.*;
 import org.apache.ctakes.gui.pipeline.bit.parameter.ParameterCellRenderer;
 import org.apache.ctakes.gui.pipeline.piper.PiperFileView;
 import org.apache.ctakes.gui.pipeline.piper.PiperTextFilter;
+import org.apache.ctakes.gui.util.FileChooserUtil;
 import org.apache.ctakes.gui.util.IconLoader;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -98,14 +99,8 @@ final public class PiperRunnerPanel extends JPanel {
       _piperChooser.setFileView( new PiperFileView() );
       _parmChooser.setFileFilter( new FileNameExtensionFilter( "Pipeline 
Definition (Piper) Parameter File", CLI_EXTENSION ) );
       _parmChooser.setFileView( new PiperFileView() );
-      String cwdPath = Paths.get( "" ).toAbsolutePath().toFile().getPath();
-      if ( cwdPath.isEmpty() ) {
-         cwdPath = System.getProperty( "user.dir" );
-      }
-      if ( cwdPath != null && !cwdPath.isEmpty() ) {
-         _piperChooser.setCurrentDirectory( new File( cwdPath ) );
-         _parmChooser.setCurrentDirectory( new File( cwdPath ) );
-      }
+      FileChooserUtil.selectWorkingDir( _piperChooser );
+      FileChooserUtil.selectWorkingDir( _parmChooser );
    }
 
    private JToolBar createToolBar() {
diff --git 
a/ctakes-gui/src/main/java/org/apache/ctakes/gui/util/FileChooserUtil.java 
b/ctakes-gui/src/main/java/org/apache/ctakes/gui/util/FileChooserUtil.java
new file mode 100644
index 0000000..6e517bf
--- /dev/null
+++ b/ctakes-gui/src/main/java/org/apache/ctakes/gui/util/FileChooserUtil.java
@@ -0,0 +1,45 @@
+package org.apache.ctakes.gui.util;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.swing.*;
+import java.io.File;
+import java.nio.file.Paths;
+
+/**
+ * @author SPF , chip-nlp
+ * @since {8/21/2024}
+ */
+final public class FileChooserUtil {
+   static private final Logger LOGGER = LoggerFactory.getLogger( 
"FileChooserUtil" );
+
+   private FileChooserUtil() {}
+
+   static public void selectWorkingDir( final JFileChooser chooser ) {
+      String cwd = Paths.get( "" ).toAbsolutePath().toFile().getPath();
+      if ( cwd.isEmpty() ) {
+         cwd = System.getProperty( "user.dir" );
+      }
+      selectDir( chooser, cwd );
+   }
+
+   static public void selectDir( final JFileChooser chooser, final String dir 
) {
+      if ( dir == null || dir.isEmpty() ) {
+         LOGGER.debug( "Selected directory is null or empty." );
+         return;
+      }
+      SwingUtilities.invokeLater( () ->
+      {
+         try {
+            chooser.setCurrentDirectory( new File( dir ) );
+         } catch ( IndexOutOfBoundsException oobE ) {
+            LOGGER.error( "FileChooser could not change directory to {}", dir 
);
+            LOGGER.error( oobE.getMessage() );
+            LOGGER.warn( "Keeping current directory {}", 
chooser.getCurrentDirectory() );
+         }
+      });
+   }
+
+
+}

Reply via email to