KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > browser > FilePicker


1 /*******************************************************************************
2  * Copyright (c) 2003, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.swt.browser;
12
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.internal.*;
15 import org.eclipse.swt.internal.mozilla.*;
16 import org.eclipse.swt.widgets.*;
17
18 class FilePicker {
19     XPCOMObject supports;
20     XPCOMObject filePicker;
21
22     int refCount = 0;
23     int mode;
24     int /*long*/ parentHandle;
25     String JavaDoc[] files, masks;
26     String JavaDoc defaultFilename, directory, title;
27
28     static final String JavaDoc SEPARATOR = System.getProperty ("file.separator"); //$NON-NLS-1$
29

30 public FilePicker () {
31     createCOMInterfaces ();
32 }
33
34 int AddRef () {
35     refCount++;
36     return refCount;
37 }
38
39 void createCOMInterfaces () {
40     /* Create each of the interfaces that this object implements */
41     supports = new XPCOMObject (new int[] {2, 0, 0}) {
42         public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);}
43         public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();}
44         public int /*long*/ method2 (int /*long*/[] args) {return Release ();}
45     };
46
47     filePicker = new XPCOMObject (new int[] {2, 0, 0, 3, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}) {
48         public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);}
49         public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();}
50         public int /*long*/ method2 (int /*long*/[] args) {return Release ();}
51         public int /*long*/ method3 (int /*long*/[] args) {return Init (args[0], args[1], args[2]);}
52         public int /*long*/ method4 (int /*long*/[] args) {return AppendFilters (args[0]);}
53         public int /*long*/ method5 (int /*long*/[] args) {return AppendFilter (args[0], args[1]);}
54         public int /*long*/ method6 (int /*long*/[] args) {return GetDefaultString (args[0]);}
55         public int /*long*/ method7 (int /*long*/[] args) {return SetDefaultString (args[0]);}
56         public int /*long*/ method8 (int /*long*/[] args) {return GetDefaultExtension (args[0]);}
57         public int /*long*/ method9 (int /*long*/[] args) {return SetDefaultExtension (args[0]);}
58         public int /*long*/ method10 (int /*long*/[] args) {return GetFilterIndex (args[0]);}
59         public int /*long*/ method11 (int /*long*/[] args) {return SetFilterIndex (args[0]);}
60         public int /*long*/ method12 (int /*long*/[] args) {return GetDisplayDirectory (args[0]);}
61         public int /*long*/ method13 (int /*long*/[] args) {return SetDisplayDirectory (args[0]);}
62         public int /*long*/ method14 (int /*long*/[] args) {return GetFile (args[0]);}
63         public int /*long*/ method15 (int /*long*/[] args) {return GetFileURL (args[0]);}
64         public int /*long*/ method16 (int /*long*/[] args) {return GetFiles (args[0]);}
65         public int /*long*/ method17 (int /*long*/[] args) {return Show (args[0]);}
66     };
67 }
68
69 void disposeCOMInterfaces () {
70     if (supports != null) {
71         supports.dispose ();
72         supports = null;
73     }
74     if (filePicker != null) {
75         filePicker.dispose ();
76         filePicker = null;
77     }
78 }
79
80 int /*long*/ getAddress () {
81     return filePicker.getAddress ();
82 }
83
84 int /*long*/ QueryInterface (int /*long*/ riid, int /*long*/ ppvObject) {
85     if (riid == 0 || ppvObject == 0) return XPCOM.NS_ERROR_NO_INTERFACE;
86     nsID guid = new nsID ();
87     XPCOM.memmove (guid, riid, nsID.sizeof);
88     
89     if (guid.Equals (nsISupports.NS_ISUPPORTS_IID)) {
90         XPCOM.memmove (ppvObject, new int /*long*/[] {supports.getAddress ()}, C.PTR_SIZEOF);
91         AddRef ();
92         return XPCOM.NS_OK;
93     }
94     if (guid.Equals (nsIFilePicker.NS_IFILEPICKER_IID)) {
95         XPCOM.memmove(ppvObject, new int /*long*/[] {filePicker.getAddress ()}, C.PTR_SIZEOF);
96         AddRef ();
97         return XPCOM.NS_OK;
98     }
99     if (guid.Equals (nsIFilePicker_1_8.NS_IFILEPICKER_IID)) {
100         XPCOM.memmove(ppvObject, new int /*long*/[] {filePicker.getAddress ()}, C.PTR_SIZEOF);
101         AddRef ();
102         return XPCOM.NS_OK;
103     }
104
105     XPCOM.memmove (ppvObject, new int /*long*/[] {0}, C.PTR_SIZEOF);
106     return XPCOM.NS_ERROR_NO_INTERFACE;
107 }
108
109 int Release () {
110     refCount--;
111     if (refCount == 0) disposeCOMInterfaces ();
112     return refCount;
113 }
114
115 /*
116  * As of Mozilla 1.8 some of nsIFilePicker's string arguments changed type. This method
117  * answers a java string based on the type of string that is appropriate for the Mozilla
118  * version being used.
119  */

120 String JavaDoc parseAString (int /*long*/ string) {
121     return null;
122 }
123
124 /* nsIFilePicker */
125
126 int /*long*/ Init (int /*long*/ parent, int /*long*/ title, int /*long*/ mode) {
127     parentHandle = parent;
128     this.mode = (int)/*64*/mode;
129     this.title = parseAString (title);
130     return XPCOM.NS_OK;
131 }
132
133 int /*long*/ Show (int /*long*/ _retval) {
134     if (mode == nsIFilePicker.modeGetFolder) {
135         /* picking a directory */
136         int result = showDirectoryPicker ();
137         XPCOM.memmove (_retval, new short[] {(short)result}, 2); /* PRInt16 */
138         return XPCOM.NS_OK;
139     }
140
141     /* picking a file */
142     int style = mode == nsIFilePicker.modeSave ? SWT.SAVE : SWT.OPEN;
143     if (mode == nsIFilePicker.modeOpenMultiple) style |= SWT.MULTI;
144     Display display = Display.getCurrent ();
145     Shell parent = null; // TODO compute parent
146
if (parent == null) {
147         parent = new Shell (display);
148     }
149     FileDialog dialog = new FileDialog (parent, style);
150     if (title != null) dialog.setText (title);
151     if (directory != null) dialog.setFilterPath (directory);
152     if (masks != null) dialog.setFilterExtensions (masks);
153     if (defaultFilename != null) dialog.setFileName (defaultFilename);
154     String JavaDoc filename = dialog.open ();
155     files = dialog.getFileNames ();
156     directory = dialog.getFilterPath ();
157     title = defaultFilename = null;
158     masks = null;
159     int result = filename == null ? nsIFilePicker.returnCancel : nsIFilePicker.returnOK;
160     XPCOM.memmove (_retval, new short[] {(short)result}, 2); /* PRInt16 */
161     return XPCOM.NS_OK;
162 }
163
164 int showDirectoryPicker () {
165     Display display = Display.getCurrent ();
166     Shell parent = null; // TODO compute parent
167
if (parent == null) {
168         parent = new Shell (display);
169     }
170     DirectoryDialog dialog = new DirectoryDialog (parent, SWT.NONE);
171     if (title != null) dialog.setText (title);
172     if (directory != null) dialog.setFilterPath (directory);
173     directory = dialog.open ();
174     title = defaultFilename = null;
175     files = masks = null;
176     return directory == null ? nsIFilePicker.returnCancel : nsIFilePicker.returnOK;
177 }
178
179 int /*long*/ GetFiles (int /*long*/ aFiles) {
180     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
181 }
182
183 int /*long*/ GetFileURL (int /*long*/ aFileURL) {
184     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
185 }
186
187 int /*long*/ GetFile (int /*long*/ aFile) {
188     String JavaDoc filename = ""; //$NON-NLS-1$
189
if (directory != null) filename += directory + SEPARATOR;
190     if (files != null && files.length > 0) filename += files[0];
191     nsEmbedString path = new nsEmbedString (filename);
192     int /*long*/[] file = new int /*long*/[1];
193     int rc = XPCOM.NS_NewLocalFile (path.getAddress (), true, file);
194     path.dispose ();
195     if (rc != XPCOM.NS_OK) Mozilla.error (rc);
196     if (file[0] == 0) Mozilla.error (XPCOM.NS_ERROR_NULL_POINTER);
197     XPCOM.memmove (aFile, file, C.PTR_SIZEOF);
198     return XPCOM.NS_OK;
199 }
200
201 int /*long*/ SetDisplayDirectory (int /*long*/ aDisplayDirectory) {
202     if (aDisplayDirectory == 0) return XPCOM.NS_OK;
203     nsILocalFile file = new nsILocalFile (aDisplayDirectory);
204     int /*long*/ pathname = XPCOM.nsEmbedCString_new ();
205     file.GetNativePath (pathname);
206     int length = XPCOM.nsEmbedCString_Length (pathname);
207     int /*long*/ buffer = XPCOM.nsEmbedCString_get (pathname);
208     byte[] bytes = new byte[length];
209     XPCOM.memmove (bytes, buffer, length);
210     XPCOM.nsEmbedCString_delete (pathname);
211     char[] chars = MozillaDelegate.mbcsToWcs (null, bytes);
212     directory = new String JavaDoc (chars);
213     return XPCOM.NS_OK;
214 }
215
216 int /*long*/ GetDisplayDirectory (int /*long*/ aDisplayDirectory) {
217     String JavaDoc directoryName = directory != null ? directory : ""; //$NON-NLS-1$
218
nsEmbedString path = new nsEmbedString (directoryName);
219     int /*long*/[] file = new int /*long*/[1];
220     int rc = XPCOM.NS_NewLocalFile (path.getAddress (), true, file);
221     path.dispose ();
222     if (rc != XPCOM.NS_OK) Mozilla.error (rc);
223     if (file[0] == 0) Mozilla.error (XPCOM.NS_ERROR_NULL_POINTER);
224     XPCOM.memmove (aDisplayDirectory, file, C.PTR_SIZEOF);
225     return XPCOM.NS_OK;
226 }
227
228 int /*long*/ SetFilterIndex (int /*long*/ aFilterIndex) {
229     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
230 }
231
232 int /*long*/ GetFilterIndex (int /*long*/ aFilterIndex) {
233     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
234 }
235
236 int /*long*/ SetDefaultExtension (int /*long*/ aDefaultExtension) {
237     /* note that the type of argument 1 changed as of Mozilla 1.8 */
238     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
239 }
240
241 int /*long*/ GetDefaultExtension (int /*long*/ aDefaultExtension) {
242     /* note that the type of argument 1 changed as of Mozilla 1.8 */
243     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
244 }
245
246 int /*long*/ SetDefaultString (int /*long*/ aDefaultString) {
247     defaultFilename = parseAString (aDefaultString);
248     return XPCOM.NS_OK;
249 }
250
251 int /*long*/ GetDefaultString (int /*long*/ aDefaultString) {
252     /* note that the type of argument 1 changed as of Mozilla 1.8 */
253     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
254 }
255
256 int /*long*/ AppendFilter (int /*long*/ title, int /*long*/ filter) {
257     /* note that the type of arguments 1 and 2 changed as of Mozilla 1.8 */
258     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
259 }
260
261 int /*long*/ AppendFilters (int /*long*/ filterMask) {
262     String JavaDoc[] addFilters = null;
263     switch ((int)/*64*/filterMask) {
264         case nsIFilePicker.filterAll:
265         case nsIFilePicker.filterApps:
266             masks = null; /* this is equivalent to no filter */
267             break;
268         case nsIFilePicker.filterHTML:
269             addFilters = new String JavaDoc[] {"*.htm;*.html"}; //$NON-NLS-1$
270
break;
271         case nsIFilePicker.filterImages:
272             addFilters = new String JavaDoc[] {"*.gif;*.jpeg;*.jpg;*.png"}; //$NON-NLS-1$
273
break;
274         case nsIFilePicker.filterText:
275             addFilters = new String JavaDoc[] {"*.txt"}; //$NON-NLS-1$
276
break;
277         case nsIFilePicker.filterXML:
278             addFilters = new String JavaDoc[] {"*.xml"}; //$NON-NLS-1$
279
break;
280         case nsIFilePicker.filterXUL:
281             addFilters = new String JavaDoc[] {"*.xul"}; //$NON-NLS-1$
282
break;
283     }
284     if (masks == null) {
285         masks = addFilters;
286     } else {
287         if (addFilters != null) {
288             String JavaDoc[] newFilters = new String JavaDoc[masks.length + addFilters.length];
289             System.arraycopy (masks, 0, newFilters, 0, masks.length);
290             System.arraycopy (addFilters, 0, newFilters, masks.length, addFilters.length);
291             masks = newFilters;
292         }
293     }
294     return XPCOM.NS_OK;
295 }
296 }
297
Popular Tags