classpath
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

javax.swing.plaf.metal.MetalFileChooserUI: proposed ActionListener for f


From: Chris Lansdown
Subject: javax.swing.plaf.metal.MetalFileChooserUI: proposed ActionListener for fileTextField
Date: Tue, 27 Dec 2005 14:33:18 -0500
User-agent: Mutt/1.5.9i

Hi,

The following is a class I've developed to be added as an ActionListener to
the fileTextField component in a MetalFileChooserUI's ui for JFileChooser.
It implements the same (or at least very similar, right now) behavior to
Sun's file text field.

Please note: while I've sent my copyright assignment form in, I haven't
heard back from Jonas yet (understandable; I sent it in on friday).

I'm hoping for comments, suggestions, etc.

The code:


  private class FileTextFieldListener implements ActionListener 
  {
    public void actionPerformed(ActionEvent ev) 
    {
      try {
        JFileChooser fc = getFileChooser();
        int type = fc.getDialogType();
        File f = new File(fileTextField.getText());
        if(!f.isAbsolute()) 
        {
          f = new File(fc.getCurrentDirectory(), fileTextField.getText()) ;
        }
        f = f.getCanonicalFile();
        if(type == JFileChooser.OPEN_DIALOG) 
        {
          if(f.isDirectory() && f.exists()) 
          {
            fc.setCurrentDirectory(f);
            fc.rescanCurrentDirectory();
            fileTextField.setText("");
          } 
          else if(f.isFile() && f.exists()) 
          {
            fc.setCurrentDirectory(f.getParentFile().getCanonicalFile());
            fc.setSelectedFile(f);
            fc.approveSelection();
            closeDialog();
          }
          else
          {
            while(!f.exists())
              f = f.getParentFile().getCanonicalFile();
            fc.setCurrentDirectory(f);
            fc.rescanCurrentDirectory();
            fc.setSelectedFile(null);
            fileTextField.setText("");
          }
        }
        else
        {
          if(f.isDirectory())
          {
            fc.setCurrentDirectory(f);
            fc.rescanCurrentDirectory();
            fileTextField.setText("");
          }
          else
          {
            fc.setCurrentDirectory(f.getParentFile().getCanonicalFile());
            fc.setSelectedFile(f);
            fc.approveSelection();
            closeDialog();
          }
        }
      } 
      catch(IOException ex) 
      {
        fileTextField.setText("");
      }
    }
  }
  
  /**
   * Closes the dialog.
   */
  void closeDialog()
  {
    Window owner = SwingUtilities.windowForComponent(getFileChooser());
    if (owner instanceof JDialog)
      ((JDialog) owner).dispose();
  }



Thanks,
Chris Lansdown

-- 
"Let us endeavor so to live that when we come to die even the undertaker 
will be sorry."  -- Mark Twain, "Pudd'nhead Wilson's Calendar"
========== Evil Overlord Quote of the Day (www.eviloverlord.com) =========
217. If I'm wearing the key to the hero's shackles around my neck and his
former girlfriend now volunteers to become my mistress and we are all
alone in my bedchamber on my bed and she offers me a goblet of wine, I
will politely decline the offer. 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]