--- /dev/null 2010-05-25 23:44:47.841006844 +0400 +++ new/src/share/classes/java/awt/DirectoryDialog.java 2010-05-26 11:57:58.000000000 +0400 @@ -0,0 +1,202 @@ +/* + * Copyright 2010 Sun Microsystems, Inc. All Rights Reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, + * CA 95054 USA or visit www.sun.com if you need additional information or + * have any questions. + */ +package java.awt; + +import java.awt.peer.*; +import java.io.IOException; +import java.io.ObjectInputStream; +import sun.awt.AWTAccessor; + +/** + * The DirectoryDialog class displays a dialog window from which + * the user can select a directory. + *

+ * It is a modal dialog, that blocks the rest of the application until the user + * has chosen a file. + *

+ * + * @author Costantino Cerbo (c.cerbo@gmail.com) + * @since 1.7 + */ +public class DirectoryDialog extends Dialog { + + static { + AWTAccessor + .setDirectoryDialogAccessor(new AWTAccessor.DirectoryDialogAccessor() { + + public void setDirectory(DirectoryDialog directoryDialog, + String directory) { + directoryDialog.dir = ("".equals(directory)) ? null + : directory; + } + }); + } + + static { + /* ensure that the necessary native libraries are loaded */ + Toolkit.loadLibraries(); + GraphicsEnvironment.isHeadless(); + } + /* + * The string specifying the directory to display in the file dialog. This + * variable may be null. + * + * @serial + * + * @see getDirectory() + * + * @see setDirectory() + */ + private String dir; + + /** + * Creates a dialog for selecting a directory. The title of the file dialog + * is initially empty. + * + * @param parent + * the owner of the dialog + * @since 1.7 + */ + public DirectoryDialog(Frame parent) { + super(parent, true); + } + + /** + * Creates a dialog for selecting a directory. + * + * @param parent + * the owner of the dialog + * @param title + * the title of the dialog + * @since 1.7 + */ + public DirectoryDialog(Frame parent, String title) { + super(parent, title, true); + } + + /** + * Creates a dialog for selecting a directory. The title of the file dialog + * is initially empty. + * + * @param parent + * the owner of the dialog + * @since 1.7 + */ + public DirectoryDialog(Dialog parent) { + super(parent, "", true); + } + + /** + * Creates a dialog for selecting a directory. + * + * @param parent + * the owner of the dialog + * @param title + * the title of the dialog + * @since 1.7 + */ + public DirectoryDialog(Dialog parent, String title) { + super(parent, title, true); + } + + /** + * Gets the directory of this file dialog. + * + * @return the (potentially null or invalid) directory of this + * FileDialog + * @see java.awt.FileDialog#setDirectory + */ + public String getDirectory() { + return dir; + } + + /** + * Sets the directory of this file dialog window to be the specified + * directory. Specifying a null or an invalid directory implies + * an implementation-defined default. This default will not be realized, + * however, until the user has selected a file. Until this point, + * getDirectory() will return the value passed into this + * method. + *

+ * Specifying "" as the directory is exactly equivalent to specifying + * null as the directory. + * + * @param dir + * the specified directory + * @see java.awt.DirectoryDialog#getDirectory + */ + public void setDirectory(String dir) { + this.dir = (dir != null && dir.equals("")) ? null : dir; + if (peer != null) { + ((DirectoryDialogPeer) this.peer).setDirectory(this.dir); + } + } + + /** + * Creates the file dialog's peer. The peer allows us to change the look of + * the file dialog without changing its functionality. + */ + @Override + public void addNotify() { + synchronized (getTreeLock()) { + if (parent != null && parent.getPeer() == null) { + parent.addNotify(); + } + if (peer == null) { + peer = getToolkit().createDirectoryDialog(this); + } + super.addNotify(); + } + } + + /** + * Returns a string representing the state of this FileDialog + * window. This method is intended to be used only for debugging purposes, + * and the content and format of the returned string may vary between + * implementations. The returned string may be empty but may not be + * null. + * + * @return the parameter string of this file dialog window + */ + @Override + protected String paramString() { + String str = super.paramString(); + str += ",dir= " + dir; + return str; + } + + /** + * Reads the ObjectInputStream and performs a backwards + * compatibility check by converting either a dir or a + * file equal to an empty string to null. + * + * @param s + * the ObjectInputStream to read + */ + private void readObject(ObjectInputStream s) throws ClassNotFoundException, + IOException { + s.defaultReadObject(); + } +}