1 11 package org.eclipse.ui.internal.intro.universal; 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.swt.dnd.ByteArrayTransfer; 20 import org.eclipse.swt.dnd.DND; 21 import org.eclipse.swt.dnd.TransferData; 22 23 24 public class ExtensionDataTransfer extends ByteArrayTransfer { 25 26 private static final String MYTYPENAME = "ExtensionData"; private static final int MYTYPEID = registerType(MYTYPENAME); 28 private static ExtensionDataTransfer _instance = new ExtensionDataTransfer(); 29 30 public static ExtensionDataTransfer getInstance() { 31 return _instance; 32 } 33 34 protected String [] getTypeNames() { 35 return new String [] { MYTYPENAME }; 36 } 37 38 protected int[] getTypeIds() { 39 return new int[] { MYTYPEID }; 40 } 41 42 public void javaToNative(Object object, TransferData transferData) { 43 if (!checkMyType(object) || !isSupportedType(transferData)) { 44 DND.error(DND.ERROR_INVALID_DATA); 45 } 46 BaseData[] myTypes = (BaseData[]) object; 47 try { 48 ByteArrayOutputStream out = new ByteArrayOutputStream (); 50 DataOutputStream writeOut = new DataOutputStream (out); 51 for (int i = 0, length = myTypes.length; i < length; i++) { 52 BaseData bd = myTypes[i]; 53 boolean separator = bd instanceof SeparatorData; 54 writeOut.writeBoolean(separator); 55 byte[] buffer = bd.getId().getBytes(); 56 writeOut.writeInt(bd.getId().length()); 57 writeOut.write(buffer); 58 if (bd instanceof ExtensionData) { 59 ExtensionData ed = (ExtensionData)bd; 60 writeOut.writeInt(ed.getName().length()); 61 buffer = ed.getName().getBytes(); 62 writeOut.write(buffer); 63 writeOut.writeInt(ed.getImportance()); 64 } 65 } 66 byte[] buffer = out.toByteArray(); 67 writeOut.close(); 68 super.javaToNative(buffer, transferData); 69 } catch (IOException e) { 70 } 71 } 72 73 public Object nativeToJava(TransferData transferData) { 74 if (isSupportedType(transferData)) { 75 byte[] buffer = (byte[]) super.nativeToJava(transferData); 76 if (buffer == null) 77 return null; 78 79 BaseData[] myData = new BaseData[0]; 80 try { 81 ByteArrayInputStream in = new ByteArrayInputStream (buffer); 82 DataInputStream readIn = new DataInputStream (in); 83 while (readIn.available() > 4) { 84 boolean separator; 85 int importance=0; 86 String id; 87 String name=null; 88 separator = readIn.readBoolean(); 89 int size = readIn.readInt(); 90 byte[] buff = new byte[size]; 91 readIn.read(buff); 92 id = new String (buff); 93 if (!separator) { 94 size = readIn.readInt(); 95 buff = new byte[size]; 96 readIn.read(buff); 97 name = new String (buff); 98 importance = readIn.readInt(); 99 } 100 101 BaseData[] newMyData = new BaseData[myData.length + 1]; 102 System.arraycopy(myData, 0, newMyData, 0, myData.length); 103 if (separator) 104 newMyData[myData.length] = new SeparatorData(id); 105 else 106 newMyData[myData.length] = new ExtensionData(id, name, importance); 107 myData = newMyData; 108 } 109 readIn.close(); 110 } catch (IOException ex) { 111 return null; 112 } 113 return myData; 114 } 115 116 return null; 117 } 118 119 120 boolean checkMyType(Object object) { 121 if (object == null || !(object instanceof BaseData[]) || ((BaseData[]) object).length == 0) { 122 return false; 123 } 124 BaseData[] myTypes = (BaseData[]) object; 125 for (int i = 0; i < myTypes.length; i++) { 126 if (myTypes[i] == null || myTypes[i].getId() == null || myTypes[i] instanceof ExtensionData && ((ExtensionData)myTypes[i]).getName() == null) 127 return false; 128 } 129 return true; 130 } 131 132 protected boolean validate(Object object) { 133 return checkMyType(object); 134 } 135 } 136 | Popular Tags |