alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 *
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 *
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.modules.javadoc.ui;

import org.openide.util.HelpCtx;
import org.openide.windows.WindowManager;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileFilter;
import java.io.File;
import java.awt.*;

/**
 * Utility methods to let user choose some content in UI.
 *
 * @author Petr Kuzel
 */
public final class Choosers {

    /**
     * Let user choose any directory on local host.
     *
     * @param defaultDir initial selection or null
     * @param helpID help content id or null
     * @return choosed file or null
     */
    public static File chooseDirectory(File defaultDir, String helpID) {

        JFileChooser chooser = new FSChooser();
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.setDialogTitle(ResourceUtils.getBundledString("CTL_DestChooser_Title"));   //NOI18N

        if ( defaultDir != null) {
            chooser.setSelectedFile( defaultDir );
        }

        if (helpID != null) {
            HelpCtx.setHelpIDString (chooser, helpID);
        }

        if (chooser.showDialog(WindowManager.getDefault ().getMainWindow (),
                               ResourceUtils.getBundledString("CTL_Destination_Approve_Button"))   //NOI18N
                == JFileChooser.APPROVE_OPTION) {
            return chooser.getSelectedFile();
        }
        return null;
    }

    /**
     * Let user choose any archive on local host.
     *
     * @param defaultDir initial selection or null
     * @param helpID help content id or null
     * @return choosed file or null
     */
    public static File chooseJar(File defaultDir, String helpID) {
        JFileChooser chooser = new FSChooser();
        FileFilter filter = new FileFilter(){
            public boolean accept(File f) {
                return (f.isDirectory() || f.getName().endsWith(".jar") || f.getName().endsWith(".zip")); // NOI18N
            }
            public String getDescription() {
                return ResourceUtils.getBundledString("CTL_JarArchivesMask");
            }
        };

        chooser.setFileFilter(filter);

        if (defaultDir != null && defaultDir.isDirectory()) {
            chooser.setCurrentDirectory(defaultDir);
        }

        if (helpID != null) {
            HelpCtx.setHelpIDString (chooser, helpID);
        }

        chooser.setDialogTitle(ResourceUtils.getBundledString("CTL_MountJar_Dialog_Title"));
        if (chooser.showDialog(WindowManager.getDefault().getMainWindow(),
        ResourceUtils.getBundledString("CTL_Mount_Approve_Button"))
        == JFileChooser.APPROVE_OPTION) {
            return chooser.getSelectedFile();
        } else {
            return null;
        }
    }

    /**
     * It probably does some UI tweaks.
     */
    static final class FSChooser extends JFileChooser {
        /** generated Serialized Version UID */
        static final long serialVersionUID = 4351976125475278278L;

        public FSChooser() {
            setBorder(new EmptyBorder(11, 11, 11, 11));
        }

        public Dimension getPreferredSize() {
            Dimension pref = super.getPreferredSize();
            JTextArea template = new JTextArea();
            template.setRows(15);
            template.setColumns(60);
            Dimension min = template.getPreferredSize();
            return new Dimension(Math.max(min.width, pref.width), Math.max(min.height, pref.height));
        }
    }

}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.