45 |
import java.awt.event.WindowEvent; |
import java.awt.event.WindowEvent; |
46 |
import java.awt.peer.FileDialogPeer; |
import java.awt.peer.FileDialogPeer; |
47 |
import java.io.FilenameFilter; |
import java.io.FilenameFilter; |
48 |
|
import java.io.File; |
49 |
|
|
50 |
public class GtkFileDialogPeer extends GtkDialogPeer implements FileDialogPeer |
public class GtkFileDialogPeer extends GtkDialogPeer implements FileDialogPeer |
51 |
{ |
{ |
53 |
|
|
54 |
private String currentFile = null; |
private String currentFile = null; |
55 |
private String currentDirectory = null; |
private String currentDirectory = null; |
56 |
|
private FilenameFilter filter; |
57 |
|
|
58 |
native void create (); |
native void create (GtkContainerPeer parent); |
59 |
|
native void connectJObject (); |
60 |
|
native void connectSignals (); |
61 |
|
native void nativeSetFile (String file); |
62 |
|
native public String nativeGetDirectory(); |
63 |
|
native public void nativeSetDirectory(String directory); |
64 |
|
native void nativeSetFilenameFilter (FilenameFilter filter); |
65 |
|
|
66 |
|
public void create() { |
67 |
|
create((GtkContainerPeer) awtComponent.getParent().getPeer()); |
68 |
|
|
69 |
|
FileDialog fd = (FileDialog) awtComponent; |
70 |
|
|
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) |
public GtkFileDialogPeer (FileDialog fd) |
80 |
{ |
{ |
81 |
super (fd); |
super (fd); |
82 |
} |
} |
83 |
|
|
|
native void connectJObject (); |
|
|
native void connectSignals (); |
|
|
native void nativeSetFile (String file); |
|
|
|
|
84 |
public void setFile (String fileName) |
public void setFile (String fileName) |
85 |
{ |
{ |
86 |
/* If nothing changed do nothing. This usually happens because |
/* If nothing changed do nothing. This usually happens because |
97 |
return; |
return; |
98 |
} |
} |
99 |
|
|
100 |
// Remove any directory path from the filename |
// GtkFileChooser requires absolute filenames. If the given filename |
101 |
int sepIndex = fileName.lastIndexOf (FS); |
// is not absolute, let's construct it based on current directory. |
|
if (sepIndex < 0) |
|
|
{ |
|
102 |
currentFile = fileName; |
currentFile = fileName; |
103 |
|
if (fileName.indexOf(FS) == 0) |
104 |
|
{ |
105 |
nativeSetFile (fileName); |
nativeSetFile (fileName); |
106 |
} |
} |
107 |
else |
else |
108 |
{ |
{ |
109 |
if (fileName.length() > (sepIndex + 1)) |
nativeSetFile (nativeGetDirectory() + FS + fileName); |
|
{ |
|
|
String fn = fileName.substring (sepIndex + 1); |
|
|
currentFile = fn; |
|
|
nativeSetFile (fn); |
|
|
} |
|
|
else |
|
|
{ |
|
|
currentFile = ""; |
|
|
nativeSetFile (""); |
|
|
} |
|
110 |
} |
} |
111 |
} |
} |
112 |
|
|
127 |
} |
} |
128 |
|
|
129 |
currentDirectory = directory; |
currentDirectory = directory; |
130 |
|
nativeSetDirectory (directory); |
|
// Gtk expects the directory to end with a file separator |
|
|
if (directory.substring (directory.length () - 1).equals (FS)) |
|
|
nativeSetFile (directory); |
|
|
else |
|
|
nativeSetFile (directory + FS); |
|
131 |
} |
} |
132 |
|
|
133 |
public void setFilenameFilter (FilenameFilter filter) |
public void setFilenameFilter (FilenameFilter filter) |
134 |
{ |
{ |
135 |
/* GTK has no filter callbacks yet. It works by setting a pattern |
this.filter = filter; |
136 |
* (see gtk_file_selection_complete), which we can't convert |
nativeSetFilenameFilter(filter); |
137 |
* to the callback paradigm. With GTK-2.4 there will be a |
} |
138 |
* gtk_file_filter_add_custom function that we can use. */ |
|
139 |
|
/* This method interacts with the native callback function of the |
140 |
|
same name. The native function will extract the filename from the |
141 |
|
GtkFileFilterInfo object and send it to this method, which will |
142 |
|
in turn call the filter's accept() method and give back the return |
143 |
|
value. */ |
144 |
|
boolean filenameFilterCallback (String fullname) { |
145 |
|
String filename = fullname.substring(fullname.lastIndexOf(FS) + 1); |
146 |
|
String dirname = fullname.substring(0, fullname.lastIndexOf(FS)); |
147 |
|
File dir = new File(dirname); |
148 |
|
return filter.accept(dir, filename); |
149 |
} |
} |
150 |
|
|
151 |
public Graphics getGraphics () |
public Graphics getGraphics () |