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 import java.io.StringReader ; 20 import java.io.StringWriter ; 21 22 import org.eclipse.core.runtime.IAdaptable; 23 import org.eclipse.swt.dnd.ByteArrayTransfer; 24 import org.eclipse.swt.dnd.TransferData; 25 import org.eclipse.ui.IEditorInput; 26 import org.eclipse.ui.IElementFactory; 27 import org.eclipse.ui.IPersistableElement; 28 import org.eclipse.ui.PlatformUI; 29 import org.eclipse.ui.WorkbenchException; 30 import org.eclipse.ui.XMLMemento; 31 32 83 public class EditorInputTransfer extends ByteArrayTransfer { 84 85 88 private static final EditorInputTransfer instance = new EditorInputTransfer(); 89 90 private static final String TYPE_NAME = "editor-input-transfer-format:" + System.currentTimeMillis() + ":" + instance.hashCode(); 94 private static final int TYPEID = registerType(TYPE_NAME); 95 96 public static class EditorInputData { 97 98 public String editorId; 99 100 public IEditorInput input; 101 102 private EditorInputData(String editorId, IEditorInput input) { 103 this.editorId = editorId; 104 this.input = input; 105 } 106 } 107 108 111 private EditorInputTransfer() { 112 } 113 114 119 public static EditorInputTransfer getInstance() { 120 return instance; 121 } 122 123 126 protected int[] getTypeIds() { 127 return new int[] { TYPEID }; 128 } 129 130 135 protected String [] getTypeNames() { 136 return new String [] { TYPE_NAME }; 137 } 138 139 142 public void javaToNative(Object data, TransferData transferData) { 143 144 if (!(data instanceof EditorInputData[])) { 145 return; 146 } 147 148 EditorInputData[] editorInputs = (EditorInputData[]) data; 149 157 158 int editorInputCount = editorInputs.length; 159 160 try { 161 ByteArrayOutputStream out = new ByteArrayOutputStream (); 162 DataOutputStream dataOut = new DataOutputStream (out); 163 164 dataOut.writeInt(editorInputCount); 166 167 for (int i = 0; i < editorInputs.length; i++) { 169 writeEditorInput(dataOut, editorInputs[i]); 170 } 171 172 dataOut.close(); 174 out.close(); 175 byte[] bytes = out.toByteArray(); 176 super.javaToNative(bytes, transferData); 177 } catch (IOException e) { 178 } 179 } 180 181 184 public Object nativeToJava(TransferData transferData) { 185 186 byte[] bytes = (byte[]) super.nativeToJava(transferData); 187 if (bytes == null) { 188 return null; 189 } 190 DataInputStream in = new DataInputStream ( 191 new ByteArrayInputStream (bytes)); 192 try { 193 int count = in.readInt(); 194 EditorInputData[] results = new EditorInputData[count]; 195 for (int i = 0; i < count; i++) { 196 results[i] = readEditorInput(in); 197 } 198 return results; 199 } catch (IOException e) { 200 return null; 201 } catch (WorkbenchException e) { 202 return null; 203 } 204 205 } 206 207 212 private EditorInputData readEditorInput(DataInputStream dataIn) 213 throws IOException , WorkbenchException { 214 215 String editorId = dataIn.readUTF(); 216 String factoryId = dataIn.readUTF(); 217 String xmlString = dataIn.readUTF(); 218 219 if (xmlString == null || xmlString.length() == 0) { 220 return null; 221 } 222 223 StringReader reader = new StringReader (xmlString); 224 225 XMLMemento memento = XMLMemento.createReadRoot(reader); 227 228 IElementFactory factory = PlatformUI.getWorkbench().getElementFactory( 229 factoryId); 230 231 if (factory != null) { 232 IAdaptable adaptable = factory.createElement(memento); 233 if (adaptable != null && (adaptable instanceof IEditorInput)) { 234 return new EditorInputData(editorId, (IEditorInput) adaptable); 235 } 236 } 237 238 return null; 239 } 240 241 246 private void writeEditorInput(DataOutputStream dataOut, 247 EditorInputData editorInputData) throws IOException { 248 dataOut.writeUTF(editorInputData.editorId); 250 251 if (editorInputData.input != null) { 253 XMLMemento memento = XMLMemento.createWriteRoot("IEditorInput"); 256 IPersistableElement element = editorInputData.input 257 .getPersistable(); 258 if (element != null) { 259 element.saveState(memento); 261 262 StringWriter writer = new StringWriter (); 264 memento.save(writer); 265 writer.close(); 266 267 dataOut.writeUTF(element.getFactoryId()); 269 dataOut.writeUTF(writer.toString()); 270 } 271 } 272 } 273 274 public static EditorInputData createEditorInputData(String editorId, 275 IEditorInput input) { 276 return new EditorInputData(editorId, input); 277 } 278 279 } 280 | Popular Tags |