/*========================================================================= Program: FusionViewer Module: $RCSfile: ImageFileChooser.java,v $ Language: Java Date: $Date: 2007/02/02 19:27:57 $ Version: $Revision: 1.3 $ Copyright (c) Insightful Corporation. All rights reserved. See Copyright.txt for details. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notice for more information. =========================================================================*/ package org.fusionviewer.ui; import java.awt.BorderLayout; import java.awt.Component; import java.awt.Frame; import java.io.File; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFileChooser; import javax.swing.JPanel; import javax.swing.filechooser.FileFilter; import com.sun.demo.jfc.ExampleFileFilter; /**\class ImageFileChooser *\brief Dialog box to select an image file or directory of image files. */ public class ImageFileChooser extends JDialog { private static FileFilter m_allImageFileFilter; private boolean m_okPressed = false; static { m_allImageFileFilter = new ExampleFileFilter( new String[] {"mhd", "h33", "dcm", "hdr", "jpg", "jpeg", "tif", "tiff", "png"}, "Images"); } /** * Create a dialog box with the indicated owner * * @param owner parent frame of this dialog box * @param path image absolute path */ public ImageFileChooser(Frame owner, String path) { super(owner, true); JPanel panel = new JPanel(new BorderLayout(10, 10)); panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); panel.add(createFileChooser(path), BorderLayout.CENTER); panel.add(createButtons(), BorderLayout.PAGE_END); getContentPane().add(panel); setTitle("Open Images"); pack(); PlatformUtility.centerWindow(this); } /* * Create buttons along the bottom */ private Component createButtons() { JPanel panel = new JPanel(); panel.add(new JButton("Open All")); panel.add(new JButton("Cancel")); panel.add(new JButton("Open")); return panel; } /** * Shows the dialog and blocks until the user dismisses it. * * @return true if the user clicked OK. */ public boolean run() { setVisible(true); return m_okPressed; } /* * Creates a JFileChooser that allows the user to browse for files. The * initial path is set to the argument path. */ static JFileChooser createFileChooser(String path) { JFileChooser chooser = null; if (path == null) { chooser = new JFileChooser(); } else { path = path.trim(); // Clear leading and trailing whitespace String defaultPath = ""; if (path.length() > 0) { File defaultFile = new File(path); if (!defaultFile.exists() || !defaultFile.isDirectory()) defaultFile = defaultFile.getParentFile(); if ((defaultFile != null) && defaultFile.exists()) defaultPath = defaultFile.getPath(); } if (defaultPath.length() > 0) chooser = new JFileChooser(defaultPath); else chooser = new JFileChooser(); } chooser.addChoosableFileFilter(m_allImageFileFilter); chooser.setControlButtonsAreShown(false); return chooser; } }