Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / gnu / java / awt / peer / gtk / GtkFileDialogPeer.java
bloba0ae9e9eef57fe13bdeded087d4b786f294a5456
1 /* GtkFileDialogPeer.java -- Implements FileDialogPeer with GTK
2 Copyright (C) 1998, 1999, 2002, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
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.
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. */
39 package gnu.java.awt.peer.gtk;
41 import java.awt.Dialog;
42 import java.awt.FileDialog;
43 import java.awt.Graphics;
44 import java.awt.peer.FileDialogPeer;
45 import java.io.File;
46 import java.io.FilenameFilter;
48 public class GtkFileDialogPeer extends GtkDialogPeer implements FileDialogPeer
50 static final String FS = System.getProperty("file.separator");
52 private String currentFile = null;
53 private String currentDirectory = null;
54 private FilenameFilter filter;
56 native void create (GtkContainerPeer parent, int mode);
57 native void connectSignals ();
58 native void nativeSetFile (String file);
59 public native String nativeGetDirectory();
60 public native void nativeSetDirectory(String directory);
61 native void nativeSetFilenameFilter (FilenameFilter filter);
63 public void create()
65 create((GtkContainerPeer) awtComponent.getParent().getPeer(),
66 ((FileDialog) awtComponent).getMode());
68 FileDialog fd = (FileDialog) awtComponent;
70 nativeSetDirectory(System.getProperty("user.dir"));
71 setDirectory(fd.getDirectory());
72 setFile(fd.getFile());
74 FilenameFilter filter = fd.getFilenameFilter();
75 if (filter != null)
76 setFilenameFilter(filter);
79 public GtkFileDialogPeer (FileDialog fd)
81 super (fd);
84 void setComponentBounds ()
86 if (awtComponent.getHeight () == 0
87 && awtComponent.getWidth () == 0)
89 int[] dims = new int[2];
90 gtkWidgetGetPreferredDimensions (dims);
92 if (dims[0] != awtComponent.getWidth()
93 || dims[1] != awtComponent.getHeight())
94 awtComponent.setSize(dims[0], dims[1]);
96 super.setComponentBounds ();
99 public void setFile (String fileName)
101 /* If nothing changed do nothing. This usually happens because
102 the only way we have to set the file name in FileDialog is by
103 calling its SetFile which will call us back. */
104 if ((fileName == null && currentFile == null)
105 || (fileName != null && fileName.equals (currentFile)))
106 return;
108 if (fileName == null || fileName.equals (""))
110 currentFile = "";
111 nativeSetFile ("");
112 return;
115 // GtkFileChooser requires absolute filenames. If the given filename
116 // is not absolute, let's construct it based on current directory.
117 currentFile = fileName;
118 if (fileName.indexOf(FS) == 0)
119 nativeSetFile(fileName);
120 else
121 nativeSetFile(nativeGetDirectory() + FS + fileName);
124 public void setDirectory (String directory)
126 /* If nothing changed so nothing. This usually happens because
127 the only way we have to set the directory in FileDialog is by
128 calling its setDirectory which will call us back. */
129 if ((directory == null && currentDirectory == null)
130 || (directory != null && directory.equals(currentDirectory)))
131 return;
133 if (directory == null || directory.equals(""))
135 currentDirectory = FS;
136 nativeSetDirectory(FS);
137 return;
140 // GtkFileChooser requires absolute directory names. If the given directory
141 // name is not absolute, construct it based on current directory if it is not
142 // null. Otherwise, use FS.
143 currentDirectory = directory;
144 if (directory.indexOf(FS) == 0)
145 nativeSetDirectory(directory);
146 else
147 nativeSetDirectory(nativeGetDirectory() + FS + directory);
150 public void setFilenameFilter (FilenameFilter filter)
152 this.filter = filter;
153 nativeSetFilenameFilter(filter);
156 /* This method interacts with the native callback function of the
157 same name. The native function will extract the filename from the
158 GtkFileFilterInfo object and send it to this method, which will
159 in turn call the filter's accept() method and give back the return
160 value. */
161 // called back by native side: filename_filter_cb
162 boolean filenameFilterCallback (String fullname) {
163 String filename = fullname.substring(fullname.lastIndexOf(FS) + 1);
164 String dirname = fullname.substring(0, fullname.lastIndexOf(FS));
165 File dir = new File(dirname);
166 return filter.accept(dir, filename);
169 public Graphics getGraphics ()
171 // GtkFileDialog will repaint by itself
172 return null;
175 // called back by native side: handle_response_cb
176 // only called from the GTK thread
177 void gtkHideFileDialog ()
179 // hide calls back the peer's setVisible method, so locking is a
180 // problem.
181 ((Dialog) awtComponent).hide();
184 // called back by native side: handle_response_cb
185 void gtkDisposeFileDialog ()
187 ((Dialog) awtComponent).dispose();
190 // Callback to set the file and directory values when the user is finished
191 // with the dialog.
192 // called back by native side: handle_response_cb
193 void gtkSetFilename (String fileName)
195 FileDialog fd = (FileDialog) awtWidget;
196 if (fileName == null)
198 currentFile = null;
199 fd.setFile(null);
200 return;
203 int sepIndex = fileName.lastIndexOf (FS);
204 if (sepIndex < 0)
206 /* This should never happen on Unix (all paths start with '/') */
207 currentFile = fileName;
209 else
211 if (fileName.length() > (sepIndex + 1))
213 String fn = fileName.substring (sepIndex + 1);
214 currentFile = fn;
216 else
218 currentFile = null;
221 String dn = fileName.substring (0, sepIndex + 1);
222 currentDirectory = dn;
223 fd.setDirectory(dn);
226 fd.setFile (currentFile);