1 |
769 |
jeremybenn |
/* GtkFileDialogPeer.java -- Implements FileDialogPeer with GTK
|
2 |
|
|
Copyright (C) 1998, 1999, 2002, 2004, 2005 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., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
|
|
02110-1301 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 gnu.java.awt.peer.gtk;
|
40 |
|
|
|
41 |
|
|
import java.awt.Dialog;
|
42 |
|
|
import java.awt.FileDialog;
|
43 |
|
|
import java.awt.event.PaintEvent;
|
44 |
|
|
import java.awt.peer.FileDialogPeer;
|
45 |
|
|
import java.io.File;
|
46 |
|
|
import java.io.FilenameFilter;
|
47 |
|
|
|
48 |
|
|
public class GtkFileDialogPeer extends GtkDialogPeer implements FileDialogPeer
|
49 |
|
|
{
|
50 |
|
|
static final String FS = System.getProperty("file.separator");
|
51 |
|
|
|
52 |
|
|
private String currentFile = null;
|
53 |
|
|
private String currentDirectory = null;
|
54 |
|
|
private FilenameFilter filter;
|
55 |
|
|
|
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);
|
62 |
|
|
|
63 |
|
|
public void create()
|
64 |
|
|
{
|
65 |
|
|
create((GtkContainerPeer) awtComponent.getParent().getPeer(),
|
66 |
|
|
((FileDialog) awtComponent).getMode());
|
67 |
|
|
|
68 |
|
|
FileDialog fd = (FileDialog) awtComponent;
|
69 |
|
|
|
70 |
|
|
nativeSetDirectory(System.getProperty("user.dir"));
|
71 |
|
|
setDirectory(fd.getDirectory());
|
72 |
|
|
setFile(fd.getFile());
|
73 |
|
|
|
74 |
|
|
FilenameFilter filter = fd.getFilenameFilter();
|
75 |
|
|
if (filter != null)
|
76 |
|
|
setFilenameFilter(filter);
|
77 |
|
|
}
|
78 |
|
|
|
79 |
|
|
public GtkFileDialogPeer (FileDialog fd)
|
80 |
|
|
{
|
81 |
|
|
super (fd);
|
82 |
|
|
}
|
83 |
|
|
|
84 |
|
|
void setComponentBounds ()
|
85 |
|
|
{
|
86 |
|
|
if (awtComponent.getHeight () == 0
|
87 |
|
|
&& awtComponent.getWidth () == 0)
|
88 |
|
|
{
|
89 |
|
|
int[] dims = new int[2];
|
90 |
|
|
gtkWidgetGetPreferredDimensions (dims);
|
91 |
|
|
|
92 |
|
|
if (dims[0] != awtComponent.getWidth()
|
93 |
|
|
|| dims[1] != awtComponent.getHeight())
|
94 |
|
|
awtComponent.setSize(dims[0], dims[1]);
|
95 |
|
|
}
|
96 |
|
|
super.setComponentBounds ();
|
97 |
|
|
}
|
98 |
|
|
|
99 |
|
|
public void setFile (String fileName)
|
100 |
|
|
{
|
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;
|
107 |
|
|
|
108 |
|
|
if (fileName == null || fileName.equals (""))
|
109 |
|
|
{
|
110 |
|
|
currentFile = "";
|
111 |
|
|
nativeSetFile ("");
|
112 |
|
|
return;
|
113 |
|
|
}
|
114 |
|
|
|
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);
|
122 |
|
|
}
|
123 |
|
|
|
124 |
|
|
public void setDirectory (String directory)
|
125 |
|
|
{
|
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;
|
132 |
|
|
|
133 |
|
|
if (directory == null || directory.equals(""))
|
134 |
|
|
{
|
135 |
|
|
currentDirectory = FS;
|
136 |
|
|
nativeSetDirectory(FS);
|
137 |
|
|
return;
|
138 |
|
|
}
|
139 |
|
|
|
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);
|
148 |
|
|
}
|
149 |
|
|
|
150 |
|
|
public void setFilenameFilter (FilenameFilter filter)
|
151 |
|
|
{
|
152 |
|
|
this.filter = filter;
|
153 |
|
|
nativeSetFilenameFilter(filter);
|
154 |
|
|
}
|
155 |
|
|
|
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 |
|
|
{
|
164 |
|
|
String filename = fullname.substring(fullname.lastIndexOf(FS) + 1);
|
165 |
|
|
String dirname = fullname.substring(0, fullname.lastIndexOf(FS));
|
166 |
|
|
File dir = new File(dirname);
|
167 |
|
|
return filter.accept(dir, filename);
|
168 |
|
|
}
|
169 |
|
|
|
170 |
|
|
// Sun does not call FileDialog.update.
|
171 |
|
|
protected void updateComponent (PaintEvent event)
|
172 |
|
|
{
|
173 |
|
|
// Override GtkComponetPeer.updateComponent to do nothing.
|
174 |
|
|
}
|
175 |
|
|
|
176 |
|
|
// called back by native side: handle_response_cb
|
177 |
|
|
// only called from the GTK thread
|
178 |
|
|
void gtkHideFileDialog ()
|
179 |
|
|
{
|
180 |
|
|
// hide calls back the peer's setVisible method, so locking is a
|
181 |
|
|
// problem.
|
182 |
|
|
((Dialog) awtComponent).hide();
|
183 |
|
|
}
|
184 |
|
|
|
185 |
|
|
// called back by native side: handle_response_cb
|
186 |
|
|
void gtkDisposeFileDialog ()
|
187 |
|
|
{
|
188 |
|
|
((Dialog) awtComponent).dispose();
|
189 |
|
|
}
|
190 |
|
|
|
191 |
|
|
// Callback to set the file and directory values when the user is finished
|
192 |
|
|
// with the dialog.
|
193 |
|
|
// called back by native side: handle_response_cb
|
194 |
|
|
void gtkSetFilename (String fileName)
|
195 |
|
|
{
|
196 |
|
|
FileDialog fd = (FileDialog) awtWidget;
|
197 |
|
|
if (fileName == null)
|
198 |
|
|
{
|
199 |
|
|
currentFile = null;
|
200 |
|
|
fd.setFile(null);
|
201 |
|
|
return;
|
202 |
|
|
}
|
203 |
|
|
|
204 |
|
|
int sepIndex = fileName.lastIndexOf (FS);
|
205 |
|
|
if (sepIndex < 0)
|
206 |
|
|
{
|
207 |
|
|
/* This should never happen on Unix (all paths start with '/') */
|
208 |
|
|
currentFile = fileName;
|
209 |
|
|
}
|
210 |
|
|
else
|
211 |
|
|
{
|
212 |
|
|
if (fileName.length() > (sepIndex + 1))
|
213 |
|
|
{
|
214 |
|
|
String fn = fileName.substring (sepIndex + 1);
|
215 |
|
|
currentFile = fn;
|
216 |
|
|
}
|
217 |
|
|
else
|
218 |
|
|
{
|
219 |
|
|
currentFile = null;
|
220 |
|
|
}
|
221 |
|
|
|
222 |
|
|
String dn = fileName.substring (0, sepIndex + 1);
|
223 |
|
|
currentDirectory = dn;
|
224 |
|
|
fd.setDirectory(dn);
|
225 |
|
|
}
|
226 |
|
|
|
227 |
|
|
fd.setFile (currentFile);
|
228 |
|
|
}
|
229 |
|
|
}
|