Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. /
 
 
 package org.apache.cayenne.swing.control;
 
 import java.io.File;
 
 
 
 import  com.jgoodies.forms.builder.DefaultFormBuilder;
 import  com.jgoodies.forms.layout.FormLayout;

A control that renders as a text field and a button to choose a file. Fires a property change event when a current directory is changed, either explictly or during a file selection by the user.

Author(s):
Andrus Adamchik
 
 public class FileChooser extends JPanel {
 
     public static final String CURRENT_DIRECTORY_PROPERTY = "currentDirectory";
 
     protected boolean existingOnly;
     protected boolean allowFiles;
     protected boolean allowDirectories;
     protected FileFilter fileFilter;
     protected String title;
     protected File currentDirectory;
 
     protected JTextField fileName;
     protected JButton chooseButton;
 
     public FileChooser() {
         this. = false;
         this. = true;
 
         this. = new JTextField();
         this. = new JButton("...");
 
         FormLayout layout = new FormLayout("pref:grow, 3dlu, pref""");
         DefaultFormBuilder builder = new DefaultFormBuilder(thislayout);
         builder.append();
 
         .addActionListener(new ActionListener() {
 
             public void actionPerformed(ActionEvent e) {
                 chooseFileAction();
             }
         });
     }
 
     public FileChooser(String titleboolean allowFilesboolean allowDirectories) {
         this();
 
         this. = allowFiles;
         this. = allowDirectories;
         this. = title;
     }
 
     public File getFile() {
         String value = .getText();
         if (Util.isEmptyString(value)) {
             return null;
         }
 
         File file = new File(value);
         if ( && !file.exists()) {
             return null;
         }
 
         return file;
     }
 
    public void setFile(File file) {
        .setText(file != null ? file.getAbsolutePath() : "");
    }
    protected void chooseFileAction() {
        int mode = getSelectionMode();
        JFileChooser chooser = new JFileChooser();
        chooser.setFileSelectionMode(mode);
        chooser.setDialogType(.);
        chooser.setAcceptAllFileFilterUsed(true);
        if ( != null) {
            chooser.setFileFilter();
        }
        if ( != null) {
            chooser.setCurrentDirectory();
        }
        chooser.setDialogTitle(makeTitle(mode));
        int result = chooser.showOpenDialog(SwingUtilities.getWindowAncestor(this));
        if (result == .) {
            File selected = chooser.getSelectedFile();
            .setText(selected != null ? selected.getAbsolutePath() : "");
        }
        setCurrentDirectory(chooser.getCurrentDirectory());
    }
    protected String makeTitle(int selectionMode) {
        if ( != null) {
            return ;
        }
        switch (selectionMode) {
            case .:
                return "Choose a file or a directory";
            case .:
                return "Choose a directory";
            default:
                return "Choose a file";
        }
    }
    protected int getSelectionMode() {
        if ( && ) {
            return .;
        }
        else if ( && !) {
            return .;
        }
        else if (! && ) {
            return .;
        }
        else {
            // invalid combination... return files...
            return .;
        }
    }
    public boolean isAllowDirectories() {
        return ;
    }
    public void setAllowDirectories(boolean allowDirectories) {
        this. = allowDirectories;
    }
    public boolean isAllowFiles() {
        return ;
    }
    public void setAllowFiles(boolean allowFiles) {
        this. = allowFiles;
    }
    public FileFilter getFileFilter() {
        return ;
    }
    public void setFileFilter(FileFilter filteFiler) {
        this. = filteFiler;
    }
    public String getTitle() {
        return ;
    }
    public void setTitle(String title) {
        this. = title;
    }
    public boolean isExistingOnly() {
        return ;
    }
    public void setExistingOnly(boolean existingOnly) {
        this. = existingOnly;
    }
    public void setColumns(int col) {
        .setColumns(col);
    }
    public int getColumns() {
        return .getColumns();
    }

    
Returns the last directory visited when picking a file.
    public File getCurrentDirectory() {
        return ;
    }
    public void setCurrentDirectory(File currentDirectory) {
        if (!Util.nullSafeEquals(this.currentDirectory)) {
            File oldValue = this.;
            this. = currentDirectory;
            firePropertyChange(oldValuecurrentDirectory);
        }
    }
New to GrepCode? Check out our FAQ X