1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
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:
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:
116:
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:
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:
141:
142:
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:
161:
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:
171: protected void updateComponent (PaintEvent event)
172: {
173:
174: }
175:
176:
177:
178: void gtkHideFileDialog ()
179: {
180:
181:
182: ((Dialog) awtComponent).hide();
183: }
184:
185:
186: void gtkDisposeFileDialog ()
187: {
188: ((Dialog) awtComponent).dispose();
189: }
190:
191:
192:
193:
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:
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: }