1 11 12 package org.eclipse.ui.part; 13 14 import java.io.ByteArrayInputStream ; 15 import java.io.ByteArrayOutputStream ; 16 import java.io.DataInputStream ; 17 import java.io.DataOutputStream ; 18 import java.io.IOException ; 19 20 import org.eclipse.core.resources.IMarker; 21 import org.eclipse.core.resources.IResource; 22 import org.eclipse.core.resources.IWorkspace; 23 import org.eclipse.core.runtime.IPath; 24 import org.eclipse.core.runtime.Path; 25 import org.eclipse.swt.dnd.ByteArrayTransfer; 26 import org.eclipse.swt.dnd.TransferData; 27 28 48 public class MarkerTransfer extends ByteArrayTransfer { 49 50 53 private static final MarkerTransfer instance = new MarkerTransfer(); 54 55 private static final String TYPE_NAME = "marker-transfer-format" + System.currentTimeMillis() + ":" + instance.hashCode(); 59 private static final int TYPEID = registerType(TYPE_NAME); 60 61 private IWorkspace workspace; 62 63 66 private MarkerTransfer() { 67 } 68 69 77 private IMarker findMarker(String pathString, long id) { 78 IPath path = new Path(pathString); 79 IResource resource = workspace.getRoot().findMember(path); 80 if (resource != null) { 81 return resource.getMarker(id); 82 } 83 return null; 84 } 85 86 91 public static MarkerTransfer getInstance() { 92 return instance; 93 } 94 95 98 protected int[] getTypeIds() { 99 return new int[] { TYPEID }; 100 } 101 102 107 protected String [] getTypeNames() { 108 return new String [] { TYPE_NAME }; 109 } 110 111 117 protected void javaToNative(Object object, TransferData transferData) { 118 126 Object [] markers = (Object []) object; 127 lazyInit(markers); 128 129 ByteArrayOutputStream byteOut = new ByteArrayOutputStream (); 130 DataOutputStream out = new DataOutputStream (byteOut); 131 132 byte[] bytes = null; 133 134 try { 135 136 out.writeInt(markers.length); 137 138 139 for (int i = 0; i < markers.length; i++) { 140 writeMarker((IMarker) markers[i], out); 141 } 142 out.close(); 143 bytes = byteOut.toByteArray(); 144 } catch (IOException e) { 145 } 147 148 if (bytes != null) { 149 super.javaToNative(bytes, transferData); 150 } 151 } 152 153 156 private void lazyInit(Object [] markers) { 157 if (workspace == null) { 158 if (markers != null && markers.length > 0) { 159 this.workspace = ((IMarker) markers[0]).getResource() 160 .getWorkspace(); 161 } 162 } 163 } 164 165 168 protected Object nativeToJava(TransferData transferData) { 169 byte[] bytes = (byte[]) super.nativeToJava(transferData); 170 DataInputStream in = new DataInputStream ( 171 new ByteArrayInputStream (bytes)); 172 173 try { 174 175 int n = in.readInt(); 176 177 178 IMarker[] markers = new IMarker[n]; 179 for (int i = 0; i < n; i++) { 180 IMarker marker = readMarker(in); 181 if (marker == null) { 182 return null; 183 } 184 markers[i] = marker; 185 } 186 return markers; 187 } catch (IOException e) { 188 return null; 189 } 190 } 191 192 199 private IMarker readMarker(DataInputStream dataIn) throws IOException { 200 205 String path = dataIn.readUTF(); 206 long id = dataIn.readLong(); 207 return findMarker(path, id); 208 } 209 210 217 private void writeMarker(IMarker marker, DataOutputStream dataOut) 218 throws IOException { 219 224 225 dataOut.writeUTF(marker.getResource().getFullPath().toString()); 226 dataOut.writeLong(marker.getId()); 227 } 228 } 229 | Popular Tags |