comparison dwt/dnd/FileTransfer.d @ 45:d8635bb48c7c

Merge with SWT 3.5
author Jacob Carlborg <doob@me.com>
date Mon, 01 Dec 2008 17:07:00 +0100
parents 1a8b3cb347e0
children e655f9f35342
comparison
equal deleted inserted replaced
44:ca5e494f2bbf 45:d8635bb48c7c
1 /******************************************************************************* 1 /*******************************************************************************
2 * Copyright (c) 2000, 2006 IBM Corporation and others. 2 * Copyright (c) 2000, 2008 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials 3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0 4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at 5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html 6 * http://www.eclipse.org/legal/epl-v10.html
7 * 7 *
8 * Contributors: 8 * Contributors:
9 * IBM Corporation - initial API and implementation 9 * IBM Corporation - initial API and implementation
10 * Outhink - support for typeFileURL 10 * Outhink - support for typeFileURL
11 *******************************************************************************/ 11 *******************************************************************************/
12 module dwt.dnd; 12 module dwt.dnd.FileTransfer;
13 13
14 import java.io.*; 14 import dwt.dwthelper.utils;
15 import dwt.internal.carbon.*; 15
16 import dwt.internal.cocoa.NSArray;
17 import dwt.internal.cocoa.NSMutableArray;
18 import dwt.internal.cocoa.NSString;
19 import dwt.internal.cocoa.OS;
16 20
17 /** 21 /**
18 * The class <code>FileTransfer</code> provides a platform specific mechanism 22 * The class <code>FileTransfer</code> provides a platform specific mechanism
19 * for converting a list of files represented as a java <code>String[]</code> to a 23 * for converting a list of files represented as a java <code>String[]</code> to a
20 * platform specific representation of the data and vice versa. 24 * platform specific representation of the data and vice versa.
21 * Each <code>String</code> in the array contains the absolute path for a single 25 * Each <code>String</code> in the array contains the absolute path for a single
22 * file or directory. 26 * file or directory.
23 * See <code>Transfer</code> for additional information.
24 * 27 *
25 * <p>An example of a java <code>String[]</code> containing a list of files is shown 28 * <p>An example of a java <code>String[]</code> containing a list of files is shown
26 * below:</p> 29 * below:</p>
27 * 30 *
28 * <code><pre> 31 * <code><pre>
30 * File file2 = new File("C:\temp\file2"); 33 * File file2 = new File("C:\temp\file2");
31 * String[] fileData = new String[2]; 34 * String[] fileData = new String[2];
32 * fileData[0] = file1.getAbsolutePath(); 35 * fileData[0] = file1.getAbsolutePath();
33 * fileData[1] = file2.getAbsolutePath(); 36 * fileData[1] = file2.getAbsolutePath();
34 * </code></pre> 37 * </code></pre>
38 *
39 * @see Transfer
35 */ 40 */
36 public class FileTransfer : ByteArrayTransfer { 41 public class FileTransfer extends ByteArrayTransfer {
37 42
38 static FileTransfer _instance = new FileTransfer(); 43 static FileTransfer _instance = new FileTransfer();
39 static final String HFS = "hfs "; //$NON-NLS-1$ 44 static final String ID_NAME = OS.NSFilenamesPboardType.getString();
40 static final String FURL = "furl"; //$NON-NLS-1$ 45 static final int ID = registerType(ID_NAME);
41 static final int HFSID = registerType(HFS);
42 static final int FURLID = registerType(FURL);
43 46
44 this() {} 47 FileTransfer() {}
45 48
46 /** 49 /**
47 * Returns the singleton instance of the FileTransfer class. 50 * Returns the singleton instance of the FileTransfer class.
48 * 51 *
49 * @return the singleton instance of the FileTransfer class 52 * @return the singleton instance of the FileTransfer class
54 57
55 /** 58 /**
56 * This implementation of <code>javaToNative</code> converts a list of file names 59 * This implementation of <code>javaToNative</code> converts a list of file names
57 * represented by a java <code>String[]</code> to a platform specific representation. 60 * represented by a java <code>String[]</code> to a platform specific representation.
58 * Each <code>String</code> in the array contains the absolute path for a single 61 * Each <code>String</code> in the array contains the absolute path for a single
59 * file or directory. For additional information see 62 * file or directory.
60 * <code>Transfer#javaToNative</code>.
61 * 63 *
62 * @param object a java <code>String[]</code> containing the file names to be 64 * @param object a java <code>String[]</code> containing the file names to be converted
63 * converted 65 * @param transferData an empty <code>TransferData</code> object that will
64 * @param transferData an empty <code>TransferData</code> object; this 66 * be filled in on return with the platform specific format of the data
65 * object will be filled in on return with the platform specific format of the data 67 *
68 * @see Transfer#nativeToJava
66 */ 69 */
67 public void javaToNative(Object object, TransferData transferData) { 70 public void javaToNative(Object object, TransferData transferData) {
68 if (!checkFile(object) || !isSupportedType(transferData)) { 71 if (!checkFile(object) || !isSupportedType(transferData)) {
69 DND.error(DND.ERROR_INVALID_DATA); 72 DND.error(DND.ERROR_INVALID_DATA);
70 } 73 }
71 String[] files = (String[])object; 74 String[] files = (String[])object;
72 transferData.result = -1; 75 int length = files.length;
73 byte[][] data = new byte[files.length][]; 76 NSMutableArray array = NSMutableArray.arrayWithCapacity(length);
74 for (int i = 0; i < data.length; i++) { 77 for (int i = 0; i < length; i++) {
75 File file = new File(files[i]);
76 bool isDirectory = file.isDirectory();
77 String fileName = files[i]; 78 String fileName = files[i];
78 char [] chars = new char [fileName.length ()]; 79 NSString string = NSString.stringWith(fileName);
79 fileName.getChars (0, chars.length, chars, 0); 80 array.addObject(string);
80 int cfString = OS.CFStringCreateWithCharacters (OS.kCFAllocatorDefault, chars, chars.length);
81 if (cfString is 0) return;
82 try {
83 int url = OS.CFURLCreateWithFileSystemPath(OS.kCFAllocatorDefault, cfString, OS.kCFURLPOSIXPathStyle, isDirectory);
84 if (url is 0) return;
85 try {
86 if (transferData.type is HFSID) {
87 byte[] fsRef = new byte[80];
88 if (!OS.CFURLGetFSRef(url, fsRef)) return;
89 byte[] fsSpec = new byte[70];
90 if (OS.FSGetCatalogInfo(fsRef, 0, null, null, fsSpec, null) !is OS.noErr) return;
91 byte[] hfsflavor = new byte[10 + fsSpec.length];
92 byte[] finfo = new byte[16];
93 OS.FSpGetFInfo(fsSpec, finfo);
94 System.arraycopy(finfo, 0, hfsflavor, 0, 10);
95 System.arraycopy(fsSpec, 0, hfsflavor, 10, fsSpec.length);
96 data[i] = hfsflavor;
97 }
98 if (transferData.type is FURLID) {
99 int encoding = OS.CFStringGetSystemEncoding();
100 int theData = OS.CFURLCreateData(OS.kCFAllocatorDefault, url, encoding, true);
101 if (theData is 0) return;
102 try {
103 int length = OS.CFDataGetLength(theData);
104 byte[] buffer = new byte[length];
105 CFRange range = new CFRange();
106 range.length = length;
107 OS.CFDataGetBytes(theData, range, buffer);
108 data[i] = buffer;
109 } finally {
110 OS.CFRelease(theData);
111 }
112 }
113 } finally {
114 OS.CFRelease(url);
115 }
116 } finally {
117 OS.CFRelease(cfString);
118 }
119 } 81 }
120 transferData.data = data; 82 transferData.data = array;
121 transferData.result = 0;
122 } 83 }
123 /** 84 /**
124 * This implementation of <code>nativeToJava</code> converts a platform specific 85 * This implementation of <code>nativeToJava</code> converts a platform specific
125 * representation of a list of file names to a java <code>String[]</code>. 86 * representation of a list of file names to a java <code>String[]</code>.
126 * Each String in the array contains the absolute path for a single file or directory. 87 * Each String in the array contains the absolute path for a single file or directory.
127 * For additional information see <code>Transfer#nativeToJava</code>.
128 * 88 *
129 * @param transferData the platform specific representation of the data to be 89 * @param transferData the platform specific representation of the data to be converted
130 * been converted 90 * @return a java <code>String[]</code> containing a list of file names if the conversion
131 * @return a java <code>String[]</code> containing a list of file names if the 91 * was successful; otherwise null
132 * conversion was successful; otherwise null 92 *
93 * @see Transfer#javaToNative
133 */ 94 */
134 public Object nativeToJava(TransferData transferData) { 95 public Object nativeToJava(TransferData transferData) {
135 if (!isSupportedType(transferData) || transferData.data is null) return null; 96 if (!isSupportedType(transferData) || transferData.data is null) return null;
136 if (transferData.data.length is 0) return null; 97 NSArray array = (NSArray) transferData.data;
137 int count = transferData.data.length; 98 if (array.count() is 0) return null;
99 int count = (int)/*64*/array.count();
138 String[] fileNames = new String[count]; 100 String[] fileNames = new String[count];
139 for (int i=0; i<count; i++) { 101 for (int i=0; i<count; i++) {
140 byte[] data = transferData.data[i]; 102 NSString string = new NSString(array.objectAtIndex(i));
141 int url = 0; 103 fileNames[i] = string.getString();
142 if (transferData.type is HFSID) {
143 byte[] fsspec = new byte[data.length - 10];
144 System.arraycopy(data, 10, fsspec, 0, fsspec.length);
145 byte[] fsRef = new byte[80];
146 if (OS.FSpMakeFSRef(fsspec, fsRef) !is OS.noErr) return null;
147 url = OS.CFURLCreateFromFSRef(OS.kCFAllocatorDefault, fsRef);
148 if (url is 0) return null;
149 }
150 if (transferData.type is FURLID) {
151 int encoding = OS.kCFStringEncodingUTF8;
152 url = OS.CFURLCreateWithBytes(OS.kCFAllocatorDefault, data, data.length, encoding, 0);
153 if (url is 0) return null;
154 }
155 try {
156 int path = OS.CFURLCopyFileSystemPath(url, OS.kCFURLPOSIXPathStyle);
157 if (path is 0) return null;
158 try {
159 int length = OS.CFStringGetLength(path);
160 if (length is 0) return null;
161 char[] buffer= new char[length];
162 CFRange range = new CFRange();
163 range.length = length;
164 OS.CFStringGetCharacters(path, range, buffer);
165 fileNames[i] = new String(buffer);
166 } finally {
167 OS.CFRelease(path);
168 }
169 } finally {
170 OS.CFRelease(url);
171 }
172 } 104 }
173 return fileNames; 105 return fileNames;
174 } 106 }
175 107
176 protected int[] getTypeIds(){ 108 protected int[] getTypeIds(){
177 return new int[] {FURLID, HFSID}; 109 return new int[] {ID};
178 } 110 }
179 111
180 protected String[] getTypeNames(){ 112 protected String[] getTypeNames(){
181 return new String[] {FURL, HFS}; 113 return new String[] {ID_NAME};
182 } 114 }
183 115
184 bool checkFile(Object object) { 116 bool checkFile(Object object) {
185 if (object is null || !(object instanceof String[]) || ((String[])object).length is 0) return false; 117 if (object is null || !(object instanceof String[]) || ((String[])object).length is 0) return false;
186 String[] Strings = (String[])object; 118 String[] strings = (String[])object;
187 for (int i = 0; i < Strings.length; i++) { 119 for (int i = 0; i < strings.length; i++) {
188 if (Strings[i] is null || Strings[i].length() is 0) return false; 120 if (strings[i] is null || strings[i].length() is 0) return false;
189 } 121 }
190 return true; 122 return true;
191 } 123 }
192 124
193 protected bool validate(Object object) { 125 protected bool validate(Object object) {