Skip to content

Commit

Permalink
Fix for rare environments that caused an oobE in the list renderer of…
Browse files Browse the repository at this point in the history
… JFileChooser.
  • Loading branch information
seanfinan committed Aug 21, 2024
1 parent cfb4f7b commit 5df171e
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -65,13 +66,14 @@ private OpenFileChooserAction( final JTextComponent textComponent, final boolean
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;
}
Expand All @@ -82,7 +84,8 @@ public void actionPerformed( final ActionEvent event ) {
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 );
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -35,14 +36,14 @@ public FileTableCellEditor() {
_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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
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;
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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() );
}
});
}


}

0 comments on commit 5df171e

Please sign in to comment.