1 11 package org.eclipse.ui.part; 12 13 import java.io.ByteArrayInputStream ; 14 import java.io.ByteArrayOutputStream ; 15 import java.io.DataInputStream ; 16 import java.io.DataOutputStream ; 17 import java.io.IOException ; 18 19 import org.eclipse.core.resources.IResource; 20 import org.eclipse.core.resources.IWorkspace; 21 import org.eclipse.core.resources.ResourcesPlugin; 22 import org.eclipse.core.runtime.Path; 23 import org.eclipse.swt.dnd.ByteArrayTransfer; 24 import org.eclipse.swt.dnd.TransferData; 25 26 54 public class ResourceTransfer extends ByteArrayTransfer { 55 56 59 private static final ResourceTransfer instance = new ResourceTransfer(); 60 61 private static final String TYPE_NAME = "resource-transfer-format:" + System.currentTimeMillis() + ":" + instance.hashCode(); 65 private static final int TYPEID = registerType(TYPE_NAME); 66 67 private IWorkspace workspace = ResourcesPlugin.getWorkspace(); 68 69 72 private ResourceTransfer() { 73 } 74 75 80 public static ResourceTransfer getInstance() { 81 return instance; 82 } 83 84 87 protected int[] getTypeIds() { 88 return new int[] { TYPEID }; 89 } 90 91 96 protected String [] getTypeNames() { 97 return new String [] { TYPE_NAME }; 98 } 99 100 103 protected void javaToNative(Object data, TransferData transferData) { 104 if (!(data instanceof IResource[])) { 105 return; 106 } 107 108 IResource[] resources = (IResource[]) data; 109 116 117 int resourceCount = resources.length; 118 119 try { 120 ByteArrayOutputStream out = new ByteArrayOutputStream (); 121 DataOutputStream dataOut = new DataOutputStream (out); 122 123 dataOut.writeInt(resourceCount); 125 126 for (int i = 0; i < resources.length; i++) { 128 writeResource(dataOut, resources[i]); 129 } 130 131 dataOut.close(); 133 out.close(); 134 byte[] bytes = out.toByteArray(); 135 super.javaToNative(bytes, transferData); 136 } catch (IOException e) { 137 } 139 } 140 141 144 protected Object nativeToJava(TransferData transferData) { 145 152 153 byte[] bytes = (byte[]) super.nativeToJava(transferData); 154 if (bytes == null) { 155 return null; 156 } 157 DataInputStream in = new DataInputStream ( 158 new ByteArrayInputStream (bytes)); 159 try { 160 int count = in.readInt(); 161 IResource[] results = new IResource[count]; 162 for (int i = 0; i < count; i++) { 163 results[i] = readResource(in); 164 } 165 return results; 166 } catch (IOException e) { 167 return null; 168 } 169 } 170 171 178 private IResource readResource(DataInputStream dataIn) throws IOException { 179 int type = dataIn.readInt(); 180 String path = dataIn.readUTF(); 181 switch (type) { 182 case IResource.FOLDER: 183 return workspace.getRoot().getFolder(new Path(path)); 184 case IResource.FILE: 185 return workspace.getRoot().getFile(new Path(path)); 186 case IResource.PROJECT: 187 return workspace.getRoot().getProject(path); 188 } 189 throw new IllegalArgumentException ( 190 "Unknown resource type in ResourceTransfer.readResource"); } 192 193 200 private void writeResource(DataOutputStream dataOut, IResource resource) 201 throws IOException { 202 dataOut.writeInt(resource.getType()); 203 dataOut.writeUTF(resource.getFullPath().toString()); 204 } 205 } 206 | Popular Tags |