Skip to content
Snippets Groups Projects
BigDataViewerPlugIn.java 2.93 KiB
Newer Older
  • Learn to ignore specific revisions
  • import ij.plugin.PlugIn;
    
    
    import java.awt.FileDialog;
    import java.awt.Frame;
    
    import java.io.File;
    
    import java.util.concurrent.atomic.AtomicBoolean;
    
    
    import javax.swing.JFileChooser;
    import javax.swing.filechooser.FileFilter;
    
    import bdv.BigDataViewer;
    import bdv.ij.util.ProgressWriterIJ;
    
    public class BigDataViewerPlugIn implements PlugIn
    {
    
    	@Override
    	public void run( final String arg )
    	{
    
    		File file = null;
    
    		if ( Prefs.useJFileChooser )
    
    			final JFileChooser fileChooser = new JFileChooser();
    
    			fileChooser.setSelectedFile( new File( lastDatasetPath ) );
    
    			fileChooser.setFileFilter( new FileFilter()
    
    				@Override
    				public String getDescription()
    				{
    					return "xml files";
    				}
    
    				@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.setDirectory( new File( lastDatasetPath ).getParent() );
    			fd.setFile( new File( lastDatasetPath ).getName() );
    
    			final AtomicBoolean workedWithFilenameFilter = new AtomicBoolean( false );
    
    			fd.setFilenameFilter( new FilenameFilter()
    
    				@Override
    				public boolean accept( final File dir, final String name )
    
    					if ( firstTime )
    					{
    						workedWithFilenameFilter.set( true );
    						firstTime = false;
    					}
    
    
    					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;
    
    			if ( isMac() && !workedWithFilenameFilter.get() )
    
    			{
    				fd.setFilenameFilter( null );
    				fd.setVisible( true );
    			}
    
    			final String filename = fd.getFile();
    			if ( filename != null )
    			{
    				file = new File( fd.getDirectory() + filename );
    
    				BigDataViewer.view( file.getAbsolutePath(), new ProgressWriterIJ() );
    			}
    			catch ( final Exception e )
    			{
    				throw new RuntimeException( e );
    			}
    		}
    	}
    
    
    	private boolean isMac()
    	{
    		final String OS = System.getProperty( "os.name", "generic" ).toLowerCase( Locale.ENGLISH );
    		return ( OS.indexOf( "mac" ) >= 0 ) || ( OS.indexOf( "darwin" ) >= 0 );
    	}