1 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 parentHandle; 25 String [] files, masks; 26 String defaultFilename, directory, title; 27 28 static final String SEPARATOR = System.getProperty ("file.separator"); 30 public FilePicker () { 31 createCOMInterfaces (); 32 } 33 34 int AddRef () { 35 refCount++; 36 return refCount; 37 } 38 39 void createCOMInterfaces () { 40 41 supports = new XPCOMObject (new int[] {2, 0, 0}) { 42 public int method0 (int [] args) {return QueryInterface (args[0], args[1]);} 43 public int method1 (int [] args) {return AddRef ();} 44 public int method2 (int [] 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 method0 (int [] args) {return QueryInterface (args[0], args[1]);} 49 public int method1 (int [] args) {return AddRef ();} 50 public int method2 (int [] args) {return Release ();} 51 public int method3 (int [] args) {return Init (args[0], args[1], args[2]);} 52 public int method4 (int [] args) {return AppendFilters (args[0]);} 53 public int method5 (int [] args) {return AppendFilter (args[0], args[1]);} 54 public int method6 (int [] args) {return GetDefaultString (args[0]);} 55 public int method7 (int [] args) {return SetDefaultString (args[0]);} 56 public int method8 (int [] args) {return GetDefaultExtension (args[0]);} 57 public int method9 (int [] args) {return SetDefaultExtension (args[0]);} 58 public int method10 (int [] args) {return GetFilterIndex (args[0]);} 59 public int method11 (int [] args) {return SetFilterIndex (args[0]);} 60 public int method12 (int [] args) {return GetDisplayDirectory (args[0]);} 61 public int method13 (int [] args) {return SetDisplayDirectory (args[0]);} 62 public int method14 (int [] args) {return GetFile (args[0]);} 63 public int method15 (int [] args) {return GetFileURL (args[0]);} 64 public int method16 (int [] args) {return GetFiles (args[0]);} 65 public int method17 (int [] 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 getAddress () { 81 return filePicker.getAddress (); 82 } 83 84 int QueryInterface (int riid, int 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 [] {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 [] {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 [] {filePicker.getAddress ()}, C.PTR_SIZEOF); 101 AddRef (); 102 return XPCOM.NS_OK; 103 } 104 105 XPCOM.memmove (ppvObject, new int [] {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 120 String parseAString (int string) { 121 return null; 122 } 123 124 125 126 int Init (int parent, int title, int mode) { 127 parentHandle = parent; 128 this.mode = (int)mode; 129 this.title = parseAString (title); 130 return XPCOM.NS_OK; 131 } 132 133 int Show (int _retval) { 134 if (mode == nsIFilePicker.modeGetFolder) { 135 136 int result = showDirectoryPicker (); 137 XPCOM.memmove (_retval, new short[] {(short)result}, 2); 138 return XPCOM.NS_OK; 139 } 140 141 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; 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 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); 161 return XPCOM.NS_OK; 162 } 163 164 int showDirectoryPicker () { 165 Display display = Display.getCurrent (); 166 Shell parent = null; 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 GetFiles (int aFiles) { 180 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; 181 } 182 183 int GetFileURL (int aFileURL) { 184 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; 185 } 186 187 int GetFile (int aFile) { 188 String filename = ""; if (directory != null) filename += directory + SEPARATOR; 190 if (files != null && files.length > 0) filename += files[0]; 191 nsEmbedString path = new nsEmbedString (filename); 192 int [] file = new int [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 SetDisplayDirectory (int aDisplayDirectory) { 202 if (aDisplayDirectory == 0) return XPCOM.NS_OK; 203 nsILocalFile file = new nsILocalFile (aDisplayDirectory); 204 int pathname = XPCOM.nsEmbedCString_new (); 205 file.GetNativePath (pathname); 206 int length = XPCOM.nsEmbedCString_Length (pathname); 207 int 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 (chars); 213 return XPCOM.NS_OK; 214 } 215 216 int GetDisplayDirectory (int aDisplayDirectory) { 217 String directoryName = directory != null ? directory : ""; nsEmbedString path = new nsEmbedString (directoryName); 219 int [] file = new int [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 SetFilterIndex (int aFilterIndex) { 229 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; 230 } 231 232 int GetFilterIndex (int aFilterIndex) { 233 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; 234 } 235 236 int SetDefaultExtension (int aDefaultExtension) { 237 238 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; 239 } 240 241 int GetDefaultExtension (int aDefaultExtension) { 242 243 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; 244 } 245 246 int SetDefaultString (int aDefaultString) { 247 defaultFilename = parseAString (aDefaultString); 248 return XPCOM.NS_OK; 249 } 250 251 int GetDefaultString (int aDefaultString) { 252 253 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; 254 } 255 256 int AppendFilter (int title, int filter) { 257 258 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; 259 } 260 261 int AppendFilters (int filterMask) { 262 String [] addFilters = null; 263 switch ((int)filterMask) { 264 case nsIFilePicker.filterAll: 265 case nsIFilePicker.filterApps: 266 masks = null; 267 break; 268 case nsIFilePicker.filterHTML: 269 addFilters = new String [] {"*.htm;*.html"}; break; 271 case nsIFilePicker.filterImages: 272 addFilters = new String [] {"*.gif;*.jpeg;*.jpg;*.png"}; break; 274 case nsIFilePicker.filterText: 275 addFilters = new String [] {"*.txt"}; break; 277 case nsIFilePicker.filterXML: 278 addFilters = new String [] {"*.xml"}; break; 280 case nsIFilePicker.filterXUL: 281 addFilters = new String [] {"*.xul"}; break; 283 } 284 if (masks == null) { 285 masks = addFilters; 286 } else { 287 if (addFilters != null) { 288 String [] newFilters = new String [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 |