Example usage for javax.swing JFileChooser getPreferredSize

List of usage examples for javax.swing JFileChooser getPreferredSize

Introduction

In this page you can find the example usage for javax.swing JFileChooser getPreferredSize.

Prototype

@Transient
public Dimension getPreferredSize() 

Source Link

Document

If the preferredSize has been set to a non-null value just returns it.

Usage

From source file:processing.app.Base.java

public void handleAddLibrary() {
    JFileChooser fileChooser = new JFileChooser(System.getProperty("user.home"));
    fileChooser.setDialogTitle(tr("Select a zip file or a folder containing the library you'd like to add"));
    fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    fileChooser.setFileFilter(new FileNameExtensionFilter(tr("ZIP files or folders"), "zip"));

    Dimension preferredSize = fileChooser.getPreferredSize();
    fileChooser.setPreferredSize(new Dimension(preferredSize.width + 200, preferredSize.height + 200));

    int returnVal = fileChooser.showOpenDialog(activeEditor);

    if (returnVal != JFileChooser.APPROVE_OPTION) {
        return;// w  ww . j ava  2s.  c  o m
    }

    File sourceFile = fileChooser.getSelectedFile();
    File tmpFolder = null;

    try {
        // unpack ZIP
        if (!sourceFile.isDirectory()) {
            try {
                tmpFolder = FileUtils.createTempFolder();
                ZipDeflater zipDeflater = new ZipDeflater(sourceFile, tmpFolder);
                zipDeflater.deflate();
                File[] foldersInTmpFolder = tmpFolder.listFiles(new OnlyDirs());
                if (foldersInTmpFolder.length != 1) {
                    throw new IOException(tr("Zip doesn't contain a library"));
                }
                sourceFile = foldersInTmpFolder[0];
            } catch (IOException e) {
                activeEditor.statusError(e);
                return;
            }
        }

        File libFolder = sourceFile;
        if (FileUtils.isSubDirectory(new File(PreferencesData.get("sketchbook.path")), libFolder)) {
            activeEditor.statusError(tr("A subfolder of your sketchbook is not a valid library"));
            return;
        }

        if (FileUtils.isSubDirectory(libFolder, new File(PreferencesData.get("sketchbook.path")))) {
            activeEditor.statusError(tr("You can't import a folder that contains your sketchbook"));
            return;
        }

        String libName = libFolder.getName();
        if (!BaseNoGui.isSanitaryName(libName)) {
            String mess = I18n.format(tr("The library \"{0}\" cannot be used.\n"
                    + "Library names must contain only basic letters and numbers.\n"
                    + "(ASCII only and no spaces, and it cannot start with a number)"), libName);
            activeEditor.statusError(mess);
            return;
        }

        String[] headers;
        File libProp = new File(libFolder, "library.properties");
        File srcFolder = new File(libFolder, "src");
        if (libProp.exists() && srcFolder.isDirectory()) {
            headers = BaseNoGui.headerListFromIncludePath(srcFolder);
        } else {
            headers = BaseNoGui.headerListFromIncludePath(libFolder);
        }
        if (headers.length == 0) {
            activeEditor.statusError(tr("Specified folder/zip file does not contain a valid library"));
            return;
        }

        // copy folder
        File destinationFolder = new File(BaseNoGui.getSketchbookLibrariesFolder().folder,
                sourceFile.getName());
        if (!destinationFolder.mkdir()) {
            activeEditor
                    .statusError(I18n.format(tr("A library named {0} already exists"), sourceFile.getName()));
            return;
        }
        try {
            FileUtils.copy(sourceFile, destinationFolder);
        } catch (IOException e) {
            activeEditor.statusError(e);
            return;
        }
        activeEditor.statusNotice(tr("Library added to your libraries. Check \"Include library\" menu"));
    } catch (IOException e) {
        // FIXME error when importing. ignoring :(
    } finally {
        // delete zip created temp folder, if exists
        newLibraryImported = true;
        FileUtils.recursiveDelete(tmpFolder);
    }
}