Package ptolemy.gui

Class PtFileChooser

  • All Implemented Interfaces:
    java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable

    public class PtFileChooser
    extends java.awt.Container
    A JFileChooser or FileDialog.

    If PtGUIUtilities.useFileDialog() returns true, then a java.awt.FileDialog is used. Otherwise a javax.swing.JFileChooser is used. In general Mac OS X is the only platform that where useFileDialog() will return true.

    See PtGUIUtilities.useFileDialog() for how to set a runtime Java property to control whether FileDialog or JFileChooser is used.

    Note that this class should be wrapped in a try/finally block, otherwise, under Windows, white boxes will appear in the common pane. See JFileChooserBugFix. Below is an example:

     // Swap backgrounds and avoid white boxes in "common places" dialog
     JFileChooserBugFix jFileChooserBugFix = new JFileChooserBugFix();
     Color background = null;
     PtFileChooser ptFileChooser;
     try {
         ptFileChooser = new PtFileChooser(_basicGraphFrame, title,
                  JFileChooser.OPEN_DIALOG);
         ptFileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
         ptFileChooser.setCurrentDirectory(modelDirectory);
         ptFileChooser.addChoosableFileFilter(new DirectoryFilter());
         int returnVal = ptFileChooser.showDialog(_basicGraphFrame, "Export HTML");
    
         if (returnVal == JFileChooser.APPROVE_OPTION) {
             directory = ptFileChooser.getSelectedFile();
         }
     } finally {
         try {
             if (ptFileChooser != null) {
                 ptFileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
              }
         } finally {
              jFileChooserBugFix.restoreBackground(background);
         }
     }
     

    Only a subset of the methods in JFileChooser are declared. The method names follow the JFileChoose API because that API is more common.

    Since:
    Ptolemy II 10.0
    Version:
    $Id$
    Author:
    Christopher Brooks
    See Also:
    Serialized Form
    Pt.AcceptedRating:
    Red (cxh)
    Pt.ProposedRating:
    Red (cxh)
    • Nested Class Summary

      • Nested classes/interfaces inherited from class java.awt.Container

        java.awt.Container.AccessibleAWTContainer
      • Nested classes/interfaces inherited from class java.awt.Component

        java.awt.Component.AccessibleAWTComponent, java.awt.Component.BaselineResizeBehavior, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
    • Field Summary

      • Fields inherited from class java.awt.Component

        accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
      • Fields inherited from interface java.awt.image.ImageObserver

        ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
    • Constructor Summary

      Constructors 
      Constructor Description
      PtFileChooser​(java.awt.Frame parent, java.lang.String title, int mode)
      Construct a PtFileChooser.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addChoosableFileFilter​(PtFilenameFilter filter)
      Set the file name filter for the dialog.
      java.io.File getCurrentDirectory()
      Return the current directory.
      java.io.File getSelectedFile()
      Return the selected file as an absolute File (a File that is not relative).
      void setCurrentDirectory​(java.io.File directory)
      Set the current directory.
      void setFileSelectionMode​(int mode)
      Set the file selection mode.
      void setSelectedFile​(java.io.File file)
      Set the selected file.
      int showDialog​(java.awt.Container parent, java.lang.String approveButtonText)
      Show the dialog.
      • Methods inherited from class java.awt.Container

        add, add, add, add, add, addContainerListener, addImpl, addNotify, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getInsets, getLayout, getListeners, getMaximumSize, getMinimumSize, getMousePosition, getPreferredSize, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, isValidateRoot, layout, list, list, locate, minimumSize, paint, paintComponents, paramString, preferredSize, print, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, removeNotify, setComponentZOrder, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setFont, setLayout, transferFocusDownCycle, update, validate, validateTree
      • Methods inherited from class java.awt.Component

        action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, disable, disableEvents, dispatchEvent, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAccessibleContext, getBackground, getBaseline, getBaselineResizeBehavior, getBounds, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, printAll, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, revalidate, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeysEnabled, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setMixingCutoutShape, setName, setPreferredSize, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • PtFileChooser

        public PtFileChooser​(java.awt.Frame parent,
                             java.lang.String title,
                             int mode)
        Construct a PtFileChooser.

        If PtGUIUtilities.useFileDialog() returns true, then a java.awt.FileDialog is used. Otherwise a javax.swing.JFileChooser is used. The effect of the mode argument varies depending on which type of dialog is used.

        Parameters:
        parent - The parent component. Used with FileDialog, ignored with JFileChooser.
        title - The title of the dialog
        mode - JFileChooser.OPEN_DIALOG, JFileChooser.SAVE_DIALOG, or JFileChooser.CUSTOM_DIALOG. CUSTOM_DIALOG is ignored by FileDialog. OPEN_DIALOG is a good default.
    • Method Detail

      • addChoosableFileFilter

        public void addChoosableFileFilter​(PtFilenameFilter filter)
        Set the file name filter for the dialog.
        Parameters:
        filter - The FilenameFilter to be used.
      • getCurrentDirectory

        public java.io.File getCurrentDirectory()
        Return the current directory.
        Returns:
        The current directory.
        See Also:
        setCurrentDirectory(File)
      • getSelectedFile

        public java.io.File getSelectedFile()
        Return the selected file as an absolute File (a File that is not relative).
        Returns:
        the selected file.
        See Also:
        setSelectedFile(File)
      • setCurrentDirectory

        public void setCurrentDirectory​(java.io.File directory)
        Set the current directory. If the directory parameter is null, then the value of the "user.dir" property is used.
        Parameters:
        directory - The current directory.
        See Also:
        getCurrentDirectory()
      • setFileSelectionMode

        public void setFileSelectionMode​(int mode)
        Set the file selection mode.

        If FileDialog is being used, then DIRECTORIES_ONLY sets the apple.awt.fileDialogForDirectories parameter. See https://developer.apple.com/library/mac/#documentation/Java/Reference/Java_PropertiesRef/Articles/JavaSystemProperties.html.

        If this method is called with JFileChooser.DIRECTORIES_ONLY, then it should be called again with JFileChooser.FILES_AND_DIRECTORIES. Typically, the first call is in a try block and the second is in a finally block.

        Note that if FileDialog is used, and mode is DIRECTORIES_ONLY, then this class must have been instantiated with a mode of FileBrowser.LOAD or JFileChooser.OPEN_DIALOG in the constructor.

        Parameters:
        mode - One of JFileChooser.FILES_ONLY, JFileChooser.DIRECTORIES_ONLY or JFileChooser.FILES_AND_DIRECTORIES.
      • setSelectedFile

        public void setSelectedFile​(java.io.File file)
        Set the selected file.
        Parameters:
        file - The file to be selected
        See Also:
        getSelectedFile()
      • showDialog

        public int showDialog​(java.awt.Container parent,
                              java.lang.String approveButtonText)
        Show the dialog.
        Parameters:
        parent - Ignored with FileDialog, used with JFileChooser.
        approveButtonText - The text for the approve button if JFileChooser is used. If FileDialog is used, then this argument is ignored.
        Returns:
        One of JFileChooser.CANCEL_OPTION, JFileChooser.APPROVE_OPTION JFileChooser.ERROR_OPTION is returned. If FileDialog is used, then either CANCEL_OPTION or APPROVE_OPTION is returned.