Example usage for java.awt FileDialog setDirectory

List of usage examples for java.awt FileDialog setDirectory

Introduction

In this page you can find the example usage for java.awt FileDialog setDirectory.

Prototype

public void setDirectory(String dir) 

Source Link

Document

Sets the directory of this file dialog window to be the specified directory.

Usage

From source file:processing.app.tools.Archiver.java

public void run() {
    SketchController sketch = editor.getSketchController();

    // first save the sketch so that things don't archive strangely
    boolean success = false;
    try {//w ww  .j a  v a2 s .co m
        success = sketch.save();
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (!success) {
        Base.showWarning(tr("Couldn't archive sketch"),
                tr("Archiving the sketch has been canceled because\nthe sketch couldn't save properly."), null);
        return;
    }

    File location = sketch.getSketch().getFolder();
    String name = location.getName();
    File parent = new File(location.getParent());

    //System.out.println("loc " + location);
    //System.out.println("par " + parent);

    File newbie = null;
    String namely = null;
    int index = 0;
    do {
        // only use the date if the sketch name isn't the default name
        useDate = !name.startsWith("sketch_");

        if (useDate) {
            String purty = dateFormat.format(new Date());
            String stamp = purty + ((char) ('a' + index));
            namely = name + "-" + stamp;
            newbie = new File(parent, namely + ".zip");

        } else {
            String diggie = numberFormat.format(index + 1);
            namely = name + "-" + diggie;
            newbie = new File(parent, namely + ".zip");
        }
        index++;
    } while (newbie.exists());

    // open up a prompt for where to save this fella
    FileDialog fd = new FileDialog(editor, tr("Archive sketch as:"), FileDialog.SAVE);
    fd.setDirectory(parent.getAbsolutePath());
    fd.setFile(newbie.getName());
    fd.setVisible(true);

    String directory = fd.getDirectory();
    String filename = fd.getFile();

    // only write the file if not canceled
    if (filename != null) {
        newbie = new File(directory, filename);

        ZipOutputStream zos = null;
        try {
            //System.out.println(newbie);
            zos = new ZipOutputStream(new FileOutputStream(newbie));

            // recursively fill the zip file
            buildZip(location, name, zos);

            // close up the jar file
            zos.flush();
            editor.statusNotice("Created archive " + newbie.getName() + ".");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(zos);
        }
    } else {
        editor.statusNotice(tr("Archive sketch canceled."));
    }
}