1 /*
   2  * Copyright 2010 Sun Microsystems, Inc.  All Rights Reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Sun designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Sun in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22  * CA 95054 USA or visit www.sun.com if you need additional information or
  23  * have any questions.
  24  */
  25 package java.awt;
  26 
  27 import java.awt.peer.*;
  28 import java.io.IOException;
  29 import java.io.ObjectInputStream;
  30 import sun.awt.AWTAccessor;
  31 
  32 /**
  33  * The <code>DirectoryDialog</code> class displays a dialog window from which
  34  * the user can select a directory.
  35  * <p>
  36  * It is a modal dialog, that blocks the rest of the application until the user
  37  * has chosen a file.
  38  * </p>
  39  * 
  40  * @author Costantino Cerbo (c.cerbo@gmail.com)
  41  * @since 1.7
  42  */
  43 public class DirectoryDialog extends Dialog {
  44 
  45     static {
  46         AWTAccessor
  47                 .setDirectoryDialogAccessor(new AWTAccessor.DirectoryDialogAccessor() {
  48 
  49                     public void setDirectory(DirectoryDialog directoryDialog,
  50                             String directory) {
  51                         directoryDialog.dir = ("".equals(directory)) ? null
  52                                 : directory;
  53                     }
  54                 });
  55     }
  56 
  57     static {
  58         /* ensure that the necessary native libraries are loaded */
  59         Toolkit.loadLibraries();
  60         GraphicsEnvironment.isHeadless();
  61     }
  62     /*
  63      * The string specifying the directory to display in the file dialog. This
  64      * variable may be <code>null</code>.
  65      * 
  66      * @serial
  67      * 
  68      * @see getDirectory()
  69      * 
  70      * @see setDirectory()
  71      */
  72     private String dir;
  73 
  74     /**
  75      * Creates a dialog for selecting a directory. The title of the file dialog
  76      * is initially empty.
  77      * 
  78      * @param parent
  79      *            the owner of the dialog
  80      * @since 1.7
  81      */
  82     public DirectoryDialog(Frame parent) {
  83         super(parent, true);
  84     }
  85 
  86     /**
  87      * Creates a dialog for selecting a directory.
  88      * 
  89      * @param parent
  90      *            the owner of the dialog
  91      * @param title
  92      *            the title of the dialog
  93      * @since 1.7
  94      */
  95     public DirectoryDialog(Frame parent, String title) {
  96         super(parent, title, true);
  97     }
  98 
  99     /**
 100      * Creates a dialog for selecting a directory. The title of the file dialog
 101      * is initially empty.
 102      * 
 103      * @param parent
 104      *            the owner of the dialog
 105      * @since 1.7
 106      */
 107     public DirectoryDialog(Dialog parent) {
 108         super(parent, "", true);
 109     }
 110 
 111     /**
 112      * Creates a dialog for selecting a directory.
 113      * 
 114      * @param parent
 115      *            the owner of the dialog
 116      * @param title
 117      *            the title of the dialog
 118      * @since 1.7
 119      */
 120     public DirectoryDialog(Dialog parent, String title) {
 121         super(parent, title, true);
 122     }
 123 
 124     /**
 125      * Gets the directory of this file dialog.
 126      * 
 127      * @return the (potentially <code>null</code> or invalid) directory of this
 128      *         <code>FileDialog</code>
 129      * @see java.awt.FileDialog#setDirectory
 130      */
 131     public String getDirectory() {
 132         return dir;
 133     }
 134 
 135     /**
 136      * Sets the directory of this file dialog window to be the specified
 137      * directory. Specifying a <code>null</code> or an invalid directory implies
 138      * an implementation-defined default. This default will not be realized,
 139      * however, until the user has selected a file. Until this point,
 140      * <code>getDirectory()</code> will return the value passed into this
 141      * method.
 142      * <p>
 143      * Specifying "" as the directory is exactly equivalent to specifying
 144      * <code>null</code> as the directory.
 145      * 
 146      * @param dir
 147      *            the specified directory
 148      * @see java.awt.DirectoryDialog#getDirectory
 149      */
 150     public void setDirectory(String dir) {
 151         this.dir = (dir != null && dir.equals("")) ? null : dir;
 152         if (peer != null) {
 153             ((DirectoryDialogPeer) this.peer).setDirectory(this.dir);
 154         }
 155     }
 156 
 157     /**
 158      * Creates the file dialog's peer. The peer allows us to change the look of
 159      * the file dialog without changing its functionality.
 160      */
 161     @Override
 162     public void addNotify() {
 163         synchronized (getTreeLock()) {
 164             if (parent != null && parent.getPeer() == null) {
 165                 parent.addNotify();
 166             }
 167             if (peer == null) {
 168                 peer = getToolkit().createDirectoryDialog(this);
 169             }
 170             super.addNotify();
 171         }
 172     }
 173 
 174     /**
 175      * Returns a string representing the state of this <code>FileDialog</code>
 176      * window. This method is intended to be used only for debugging purposes,
 177      * and the content and format of the returned string may vary between
 178      * implementations. The returned string may be empty but may not be
 179      * <code>null</code>.
 180      * 
 181      * @return the parameter string of this file dialog window
 182      */
 183     @Override
 184     protected String paramString() {
 185         String str = super.paramString();
 186         str += ",dir= " + dir;
 187         return str;
 188     }
 189 
 190     /**
 191      * Reads the <code>ObjectInputStream</code> and performs a backwards
 192      * compatibility check by converting either a <code>dir</code> or a
 193      * <code>file</code> equal to an empty string to <code>null</code>.
 194      * 
 195      * @param s
 196      *            the <code>ObjectInputStream</code> to read
 197      */
 198     private void readObject(ObjectInputStream s) throws ClassNotFoundException,
 199             IOException {
 200         s.defaultReadObject();
 201     }
 202 }