| 1 | /* FileDialog.java -- A filename selection dialog box
|
|---|
| 2 | Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
|
|---|
| 3 |
|
|---|
| 4 | This file is part of GNU Classpath.
|
|---|
| 5 |
|
|---|
| 6 | GNU Classpath is free software; you can redistribute it and/or modify
|
|---|
| 7 | it under the terms of the GNU General Public License as published by
|
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option)
|
|---|
| 9 | any later version.
|
|---|
| 10 |
|
|---|
| 11 | GNU Classpath is distributed in the hope that it will be useful, but
|
|---|
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 14 | General Public License for more details.
|
|---|
| 15 |
|
|---|
| 16 | You should have received a copy of the GNU General Public License
|
|---|
| 17 | along with GNU Classpath; see the file COPYING. If not, write to the
|
|---|
| 18 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|---|
| 19 | 02111-1307 USA.
|
|---|
| 20 |
|
|---|
| 21 | Linking this library statically or dynamically with other modules is
|
|---|
| 22 | making a combined work based on this library. Thus, the terms and
|
|---|
| 23 | conditions of the GNU General Public License cover the whole
|
|---|
| 24 | combination.
|
|---|
| 25 |
|
|---|
| 26 | As a special exception, the copyright holders of this library give you
|
|---|
| 27 | permission to link this library with independent modules to produce an
|
|---|
| 28 | executable, regardless of the license terms of these independent
|
|---|
| 29 | modules, and to copy and distribute the resulting executable under
|
|---|
| 30 | terms of your choice, provided that you also meet, for each linked
|
|---|
| 31 | independent module, the terms and conditions of the license of that
|
|---|
| 32 | module. An independent module is a module which is not derived from
|
|---|
| 33 | or based on this library. If you modify this library, you may extend
|
|---|
| 34 | this exception to your version of the library, but you are not
|
|---|
| 35 | obligated to do so. If you do not wish to do so, delete this
|
|---|
| 36 | exception statement from your version. */
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 | package java.awt;
|
|---|
| 40 |
|
|---|
| 41 | import java.awt.peer.FileDialogPeer;
|
|---|
| 42 | import java.awt.peer.DialogPeer;
|
|---|
| 43 | import java.awt.peer.WindowPeer;
|
|---|
| 44 | import java.awt.peer.ContainerPeer;
|
|---|
| 45 | import java.awt.peer.ComponentPeer;
|
|---|
| 46 | import java.io.FilenameFilter;
|
|---|
| 47 |
|
|---|
| 48 | /**
|
|---|
| 49 | * This class implements a file selection dialog box widget.
|
|---|
| 50 | *
|
|---|
| 51 | * @author Aaron M. Renn ([email protected])
|
|---|
| 52 | * @author Tom Tromey <[email protected]>
|
|---|
| 53 | */
|
|---|
| 54 | public class FileDialog extends Dialog implements java.io.Serializable
|
|---|
| 55 | {
|
|---|
| 56 |
|
|---|
| 57 | /*
|
|---|
| 58 | * Static Variables
|
|---|
| 59 | */
|
|---|
| 60 |
|
|---|
| 61 | /**
|
|---|
| 62 | * Indicates that the purpose of the dialog is for opening a file.
|
|---|
| 63 | */
|
|---|
| 64 | public static final int LOAD = 0;
|
|---|
| 65 |
|
|---|
| 66 | /**
|
|---|
| 67 | * Indicates that the purpose of the dialog is for saving a file.
|
|---|
| 68 | */
|
|---|
| 69 | public static final int SAVE = 1;
|
|---|
| 70 |
|
|---|
| 71 | // Serialization constant
|
|---|
| 72 | private static final long serialVersionUID = 5035145889651310422L;
|
|---|
| 73 |
|
|---|
| 74 | /*************************************************************************/
|
|---|
| 75 |
|
|---|
| 76 | /*
|
|---|
| 77 | * Instance Variables
|
|---|
| 78 | */
|
|---|
| 79 |
|
|---|
| 80 | /**
|
|---|
| 81 | * @serial The directory for this file dialog.
|
|---|
| 82 | */
|
|---|
| 83 | private String dir;
|
|---|
| 84 |
|
|---|
| 85 | /**
|
|---|
| 86 | * @serial The filename for this file dialog
|
|---|
| 87 | */
|
|---|
| 88 | private String file;
|
|---|
| 89 |
|
|---|
| 90 | /**
|
|---|
| 91 | * @serial The filter for selecting filenames to display
|
|---|
| 92 | */
|
|---|
| 93 | private FilenameFilter filter;
|
|---|
| 94 |
|
|---|
| 95 | /**
|
|---|
| 96 | * @serial The mode of this dialog, either <code>LOAD</code> or
|
|---|
| 97 | * <code>SAVE</code>.
|
|---|
| 98 | */
|
|---|
| 99 | private int mode;
|
|---|
| 100 |
|
|---|
| 101 | /*************************************************************************/
|
|---|
| 102 |
|
|---|
| 103 | /*
|
|---|
| 104 | * Constructors
|
|---|
| 105 | */
|
|---|
| 106 |
|
|---|
| 107 | /**
|
|---|
| 108 | * Initializes a new instance of <code>FileDialog</code> with the
|
|---|
| 109 | * specified parent. This dialog will have no title and will be for
|
|---|
| 110 | * loading a file.
|
|---|
| 111 | *
|
|---|
| 112 | * @param parent The parent frame for this dialog.
|
|---|
| 113 | */
|
|---|
| 114 | public
|
|---|
| 115 | FileDialog(Frame parent)
|
|---|
| 116 | {
|
|---|
| 117 | this(parent, "", LOAD);
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | /*************************************************************************/
|
|---|
| 121 |
|
|---|
| 122 | /**
|
|---|
| 123 | * Initialized a new instance of <code>FileDialog</code> with the
|
|---|
| 124 | * specified parent and title. This dialog will be for opening a file.
|
|---|
| 125 | *
|
|---|
| 126 | * @param parent The parent frame for this dialog.
|
|---|
| 127 | * @param title The title for this dialog.
|
|---|
| 128 | */
|
|---|
| 129 | public
|
|---|
| 130 | FileDialog(Frame parent, String title)
|
|---|
| 131 | {
|
|---|
| 132 | this(parent, title, LOAD);
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | /*************************************************************************/
|
|---|
| 136 |
|
|---|
| 137 | /**
|
|---|
| 138 | * Initialized a new instance of <code>FileDialog</code> with the
|
|---|
| 139 | * specified parent, title, and mode.
|
|---|
| 140 | *
|
|---|
| 141 | * @param parent The parent frame for this dialog.
|
|---|
| 142 | * @param title The title for this dialog.
|
|---|
| 143 | * @param mode The mode of the dialog, either <code>LOAD</code> or
|
|---|
| 144 | * <code>SAVE</code>.
|
|---|
| 145 | */
|
|---|
| 146 | public
|
|---|
| 147 | FileDialog(Frame parent, String title, int mode)
|
|---|
| 148 | {
|
|---|
| 149 | super(parent, title, true);
|
|---|
| 150 |
|
|---|
| 151 | if ((mode != LOAD) && (mode != SAVE))
|
|---|
| 152 | throw new IllegalArgumentException("Bad mode: " + mode);
|
|---|
| 153 |
|
|---|
| 154 | this.mode = mode;
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | /*************************************************************************/
|
|---|
| 158 |
|
|---|
| 159 | /*
|
|---|
| 160 | * Instance Methods
|
|---|
| 161 | */
|
|---|
| 162 |
|
|---|
| 163 | /**
|
|---|
| 164 | * Returns the mode of this dialog, either <code>LOAD</code> or
|
|---|
| 165 | * <code>SAVE</code>.
|
|---|
| 166 | *
|
|---|
| 167 | * @return The mode of this dialog.
|
|---|
| 168 | */
|
|---|
| 169 | public int
|
|---|
| 170 | getMode()
|
|---|
| 171 | {
|
|---|
| 172 | return(mode);
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | /*************************************************************************/
|
|---|
| 176 |
|
|---|
| 177 | /**
|
|---|
| 178 | * Sets the mode of this dialog to either <code>LOAD</code> or
|
|---|
| 179 | * <code>SAVE</code>. This method is only effective before the native
|
|---|
| 180 | * peer is created.
|
|---|
| 181 | *
|
|---|
| 182 | * @param mode The new mode of this file dialog.
|
|---|
| 183 | */
|
|---|
| 184 | public void
|
|---|
| 185 | setMode(int mode)
|
|---|
| 186 | {
|
|---|
| 187 | if ((mode != LOAD) && (mode != SAVE))
|
|---|
| 188 | throw new IllegalArgumentException("Bad mode: " + mode);
|
|---|
| 189 |
|
|---|
| 190 | this.mode = mode;
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | /*************************************************************************/
|
|---|
| 194 |
|
|---|
| 195 | /**
|
|---|
| 196 | * Returns the directory for this file dialog.
|
|---|
| 197 | *
|
|---|
| 198 | * @return The directory for this file dialog.
|
|---|
| 199 | */
|
|---|
| 200 | public String
|
|---|
| 201 | getDirectory()
|
|---|
| 202 | {
|
|---|
| 203 | return(dir);
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | /*************************************************************************/
|
|---|
| 207 |
|
|---|
| 208 | /**
|
|---|
| 209 | * Sets the directory for this file dialog.
|
|---|
| 210 | *
|
|---|
| 211 | * @param dir The new directory for this file dialog.
|
|---|
| 212 | */
|
|---|
| 213 | public synchronized void
|
|---|
| 214 | setDirectory(String dir)
|
|---|
| 215 | {
|
|---|
| 216 | this.dir = dir;
|
|---|
| 217 | if (peer != null)
|
|---|
| 218 | {
|
|---|
| 219 | FileDialogPeer f = (FileDialogPeer) peer;
|
|---|
| 220 | f.setDirectory (dir);
|
|---|
| 221 | }
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | /*************************************************************************/
|
|---|
| 225 |
|
|---|
| 226 | /**
|
|---|
| 227 | * Returns the file that is selected in this dialog.
|
|---|
| 228 | *
|
|---|
| 229 | * @return The file that is selected in this dialog.
|
|---|
| 230 | */
|
|---|
| 231 | public String
|
|---|
| 232 | getFile()
|
|---|
| 233 | {
|
|---|
| 234 | return(file);
|
|---|
| 235 | }
|
|---|
| 236 |
|
|---|
| 237 | /*************************************************************************/
|
|---|
| 238 |
|
|---|
| 239 | /**
|
|---|
| 240 | * Sets the selected file for this dialog.
|
|---|
| 241 | *
|
|---|
| 242 | * @param file The selected file for this dialog.
|
|---|
| 243 | */
|
|---|
| 244 | public synchronized void
|
|---|
| 245 | setFile(String file)
|
|---|
| 246 | {
|
|---|
| 247 | this.file = file;
|
|---|
| 248 | if (peer != null)
|
|---|
| 249 | {
|
|---|
| 250 | FileDialogPeer f = (FileDialogPeer) peer;
|
|---|
| 251 | f.setFile (file);
|
|---|
| 252 | }
|
|---|
| 253 | }
|
|---|
| 254 |
|
|---|
| 255 | /*************************************************************************/
|
|---|
| 256 |
|
|---|
| 257 | /**
|
|---|
| 258 | * Returns the filename filter being used by this dialog.
|
|---|
| 259 | *
|
|---|
| 260 | * @param The filename filter being used by this dialog.
|
|---|
| 261 | */
|
|---|
| 262 | public FilenameFilter
|
|---|
| 263 | getFilenameFilter()
|
|---|
| 264 | {
|
|---|
| 265 | return(filter);
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 | /*************************************************************************/
|
|---|
| 269 |
|
|---|
| 270 | /**
|
|---|
| 271 | * Sets the filename filter used by this dialog.
|
|---|
| 272 | *
|
|---|
| 273 | * @param filter The new filename filter for this file dialog box.
|
|---|
| 274 | */
|
|---|
| 275 | public synchronized void
|
|---|
| 276 | setFilenameFilter(FilenameFilter filter)
|
|---|
| 277 | {
|
|---|
| 278 | this.filter = filter;
|
|---|
| 279 | if (peer != null)
|
|---|
| 280 | {
|
|---|
| 281 | FileDialogPeer f = (FileDialogPeer) peer;
|
|---|
| 282 | f.setFilenameFilter (filter);
|
|---|
| 283 | }
|
|---|
| 284 | }
|
|---|
| 285 |
|
|---|
| 286 | /*************************************************************************/
|
|---|
| 287 |
|
|---|
| 288 | /**
|
|---|
| 289 | * Creates the native peer for this file dialog box.
|
|---|
| 290 | */
|
|---|
| 291 | public void
|
|---|
| 292 | addNotify()
|
|---|
| 293 | {
|
|---|
| 294 | if (peer == null)
|
|---|
| 295 | peer = getToolkit ().createFileDialog (this);
|
|---|
| 296 | super.addNotify ();
|
|---|
| 297 | }
|
|---|
| 298 |
|
|---|
| 299 | /*************************************************************************/
|
|---|
| 300 |
|
|---|
| 301 | /**
|
|---|
| 302 | * Returns a debugging string for this object.
|
|---|
| 303 | *
|
|---|
| 304 | * @return A debugging string for this object.
|
|---|
| 305 | */
|
|---|
| 306 | protected String
|
|---|
| 307 | paramString()
|
|---|
| 308 | {
|
|---|
| 309 | return ("dir=" + dir + ",file=" + file +
|
|---|
| 310 | ",mode=" + mode + "," + super.paramString());
|
|---|
| 311 | }
|
|---|
| 312 |
|
|---|
| 313 | } // class FileDialog
|
|---|
| 314 |
|
|---|