Merge from the pain train
[official-gcc.git] / libjava / gnu / java / awt / peer / gtk / GtkFileDialogPeer.java
blob4281b49cc65717a942935bc86df223c0e27e75d6
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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 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.Window;
45 import java.awt.peer.FileDialogPeer;
46 import java.io.File;
47 import java.io.FilenameFilter;
49 public class GtkFileDialogPeer extends GtkDialogPeer implements FileDialogPeer
51 static final String FS = System.getProperty("file.separator");
53 private String currentFile = null;
54 private String currentDirectory = null;
55 private FilenameFilter filter;
57 native void create (GtkContainerPeer parent);
58 native void connectSignals ();
59 native void nativeSetFile (String file);
60 public native String nativeGetDirectory();
61 public native void nativeSetDirectory(String directory);
62 native void nativeSetFilenameFilter (FilenameFilter filter);
64 public void create()
66 create((GtkContainerPeer) awtComponent.getParent().getPeer());
68 FileDialog fd = (FileDialog) awtComponent;
70 setDirectory(fd.getDirectory());
71 setFile(fd.getFile());
73 FilenameFilter filter = fd.getFilenameFilter();
74 if (filter != null)
75 setFilenameFilter(filter);
78 public GtkFileDialogPeer (FileDialog fd)
80 super (fd);
83 void setComponentBounds ()
85 if (awtComponent.getHeight () == 0
86 && awtComponent.getWidth () == 0)
88 int[] dims = new int[2];
89 gtkWidgetGetPreferredDimensions (dims);
90 ((GtkFileDialogPeer) this).setBoundsCallback ((Window) awtComponent,
91 awtComponent.getX (),
92 awtComponent.getY (),
93 dims[0], dims[1]);
95 super.setComponentBounds ();
98 public void setFile (String fileName)
100 /* If nothing changed do nothing. This usually happens because
101 the only way we have to set the file name in FileDialog is by
102 calling its SetFile which will call us back. */
103 if ((fileName == null && currentFile == null)
104 || (fileName != null && fileName.equals (currentFile)))
105 return;
107 if (fileName == null || fileName.equals (""))
109 currentFile = "";
110 nativeSetFile ("");
111 return;
114 // GtkFileChooser requires absolute filenames. If the given filename
115 // is not absolute, let's construct it based on current directory.
116 currentFile = fileName;
117 if (fileName.indexOf(FS) == 0)
119 nativeSetFile (fileName);
121 else
123 nativeSetFile (nativeGetDirectory() + FS + fileName);
127 public void setDirectory (String directory)
129 /* If nothing changed so nothing. This usually happens because
130 the only way we have to set the directory in FileDialog is by
131 calling its setDirectory which will call us back. */
132 if ((directory == null && currentDirectory == null)
133 || (directory != null && directory.equals (currentDirectory)))
134 return;
136 if (directory == null || directory.equals (""))
138 currentDirectory = FS;
139 nativeSetFile (FS);
140 return;
143 currentDirectory = directory;
144 nativeSetDirectory (directory);
147 public void setFilenameFilter (FilenameFilter filter)
149 this.filter = filter;
150 nativeSetFilenameFilter(filter);
153 /* This method interacts with the native callback function of the
154 same name. The native function will extract the filename from the
155 GtkFileFilterInfo object and send it to this method, which will
156 in turn call the filter's accept() method and give back the return
157 value. */
158 boolean filenameFilterCallback (String fullname) {
159 String filename = fullname.substring(fullname.lastIndexOf(FS) + 1);
160 String dirname = fullname.substring(0, fullname.lastIndexOf(FS));
161 File dir = new File(dirname);
162 return filter.accept(dir, filename);
165 public Graphics getGraphics ()
167 // GtkFileDialog will repaint by itself
168 return null;
171 void gtkHideFileDialog ()
173 ((Dialog) awtComponent).hide();
176 void gtkDisposeFileDialog ()
178 ((Dialog) awtComponent).dispose();
181 /* Callback to set the file and directory values when the user is finished
182 * with the dialog.
184 void gtkSetFilename (String fileName)
186 FileDialog fd = (FileDialog) awtWidget;
187 if (fileName == null)
189 currentFile = null;
190 fd.setFile(null);
191 return;
194 int sepIndex = fileName.lastIndexOf (FS);
195 if (sepIndex < 0)
197 /* This should never happen on Unix (all paths start with '/') */
198 currentFile = fileName;
200 else
202 if (fileName.length() > (sepIndex + 1))
204 String fn = fileName.substring (sepIndex + 1);
205 currentFile = fn;
207 else
209 currentFile = null;
212 String dn = fileName.substring (0, sepIndex + 1);
213 currentDirectory = dn;
214 fd.setDirectory(dn);
217 fd.setFile (currentFile);