Skip to content
Snippets Groups Projects
Commit c889d8a2 authored by Tobias Pietzsch's avatar Tobias Pietzsch
Browse files

use JFileChooser or FileDialog depending on ij.Prefs.useJFileChooser

parent 9cedebd5
No related branches found
No related tags found
No related merge requests found
package bdv.ij;
import ij.Prefs;
import ij.plugin.PlugIn;
import java.awt.FileDialog;
import java.awt.Frame;
import java.io.File;
import java.io.FilenameFilter;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
......@@ -15,37 +19,68 @@ public class BigDataViewerPlugIn implements PlugIn
@Override
public void run( final String arg )
{
final JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileFilter( new FileFilter()
File file = null;
if ( Prefs.useJFileChooser )
{
@Override
public String getDescription()
final JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileFilter( new FileFilter()
{
return "xml files";
}
@Override
public String getDescription()
{
return "xml files";
}
@Override
public boolean accept( final File f )
@Override
public boolean accept( final File f )
{
if ( f.isDirectory() )
return true;
if ( f.isFile() )
{
final String s = f.getName();
final int i = s.lastIndexOf('.');
if (i > 0 && i < s.length() - 1) {
final String ext = s.substring(i+1).toLowerCase();
return ext.equals( "xml" );
}
}
return false;
}
} );
final int returnVal = fileChooser.showOpenDialog( null );
if ( returnVal == JFileChooser.APPROVE_OPTION )
file = fileChooser.getSelectedFile();
}
else // use FileDialog
{
final FileDialog fd = new FileDialog( ( Frame ) null, "Open", FileDialog.LOAD );
fd.setFilenameFilter( new FilenameFilter()
{
if ( f.isDirectory() )
return true;
if ( f.isFile() )
@Override
public boolean accept( final File dir, final String name )
{
final String s = f.getName();
final int i = s.lastIndexOf('.');
if (i > 0 && i < s.length() - 1) {
final String ext = s.substring(i+1).toLowerCase();
return ext.equals( "xml" );
}
final int i = name.lastIndexOf( '.' );
if ( i > 0 && i < name.length() - 1 )
{
final String ext = name.substring( i + 1 ).toLowerCase();
return ext.equals( "xml" );
}
return false;
}
return false;
} );
fd.setVisible( true );
final String filename = fd.getFile();
if ( filename != null )
{
file = new File( fd.getDirectory() + filename );
}
} );
}
final int returnVal = fileChooser.showOpenDialog( null );
if ( returnVal == JFileChooser.APPROVE_OPTION )
if ( file != null )
{
final File file = fileChooser.getSelectedFile();
try
{
BigDataViewer.view( file.getAbsolutePath(), new ProgressWriterIJ() );
......
package bdv.ij.util;
import ij.Prefs;
import java.awt.Button;
import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Panel;
......@@ -9,6 +13,7 @@ import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.regex.Matcher;
......@@ -42,6 +47,8 @@ public class PluginHelper
dialog.add( panel );
}
public static boolean useFileDialog = true;
public static class ChooseXmlFileListener implements ActionListener
{
TextField text;
......@@ -55,45 +62,80 @@ public class PluginHelper
public void actionPerformed( final ActionEvent e )
{
File directory = new File( text.getText() );
final String fn = directory.getName();
while ( directory != null && !directory.exists() )
directory = directory.getParentFile();
final JFileChooser fc = new JFileChooser( directory );
fc.setFileFilter( new FileFilter()
if ( Prefs.useJFileChooser )
{
@Override
public String getDescription()
final JFileChooser fc = new JFileChooser( directory );
fc.setSelectedFile( new File( fn ) );
fc.setFileFilter( new FileFilter()
{
return "xml files";
}
@Override
public String getDescription()
{
return "xml files";
}
@Override
public boolean accept( final File f )
{
if ( f.isDirectory() )
return true;
if ( f.isFile() )
@Override
public boolean accept( final File f )
{
final String s = f.getName();
final int i = s.lastIndexOf('.');
if (i > 0 && i < s.length() - 1) {
final String ext = s.substring(i+1).toLowerCase();
return ext.equals( "xml" );
}
if ( f.isDirectory() )
return true;
if ( f.isFile() )
{
final String s = f.getName();
final int i = s.lastIndexOf( '.' );
if ( i > 0 && i < s.length() - 1 )
{
final String ext = s.substring( i + 1 ).toLowerCase();
return ext.equals( "xml" );
}
}
return false;
}
return false;
}
} );
} );
fc.setFileSelectionMode( JFileChooser.FILES_ONLY );
fc.setFileSelectionMode( JFileChooser.FILES_ONLY );
final int returnVal = fc.showSaveDialog( null );
if ( returnVal == JFileChooser.APPROVE_OPTION )
final int returnVal = fc.showSaveDialog( null );
if ( returnVal == JFileChooser.APPROVE_OPTION )
{
String f = fc.getSelectedFile().getAbsolutePath();
if ( !f.endsWith( ".xml" ) )
f += ".xml";
text.setText( f );
}
}
else // use FileDialog
{
String f = fc.getSelectedFile().getAbsolutePath();
if ( ! f.endsWith( ".xml" ) )
f += ".xml";
text.setText( f );
final FileDialog fd = new FileDialog( ( Frame ) null, "Save", FileDialog.SAVE );
fd.setDirectory( directory.getAbsolutePath() );
fd.setFile( fn );
fd.setFilenameFilter( new FilenameFilter()
{
@Override
public boolean accept( final File dir, final String name )
{
final int i = name.lastIndexOf( '.' );
if ( i > 0 && i < name.length() - 1 )
{
final String ext = name.substring( i + 1 ).toLowerCase();
return ext.equals( "xml" );
}
return false;
}
} );
fd.setVisible( true );
final String filename = fd.getFile();
if ( filename != null )
{
String f = new File( fd.getDirectory() + filename ).getAbsolutePath();
if ( !f.endsWith( ".xml" ) )
f += ".xml";
text.setText( f );
}
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment