1 23 package org.objectweb.clif.scenario.util.isac.gui.tree.dnd; 24 25 import java.io.ByteArrayInputStream ; 26 import java.io.ByteArrayOutputStream ; 27 import java.io.DataInputStream ; 28 import java.io.DataOutputStream ; 29 import java.io.IOException ; 30 import java.util.Vector ; 31 32 import org.apache.log4j.Category; 33 import org.eclipse.swt.dnd.ByteArrayTransfer; 34 import org.eclipse.swt.dnd.TransferData; 35 import org.objectweb.clif.scenario.util.isac.util.tree.ScenarioNode; 36 37 44 48 public class ScenarioTreeTransfer extends ByteArrayTransfer { 49 static Category cat = Category.getInstance(ScenarioTreeTransfer.class.getName()); 50 private static ScenarioTreeTransfer instance = new ScenarioTreeTransfer(); 51 private static final String TYPE_NAME = "ScenarioTree-transfer-format"; 52 private static final int TYPEID = registerType(TYPE_NAME); 53 54 57 public static ScenarioTreeTransfer getInstance() { 58 cat.debug("-> getInstance") ; 59 return instance; 60 } 61 62 65 private ScenarioTreeTransfer() { 66 cat.debug("-> constructor") ; 67 } 68 69 protected ScenarioNode fromByteArray(byte[] bytes) { 70 cat.debug("-> fromByteArray") ; 71 if (bytes == null) 72 return null ; 73 DataInputStream in = 74 new DataInputStream (new ByteArrayInputStream (bytes)); 75 76 try { 77 ScenarioNode tree = readScenarioTree(in); 78 if (tree != null) 79 return tree; 80 } catch (IOException e) { 81 } 83 return null; 84 } 85 86 90 protected int[] getTypeIds() { 91 cat.debug("-> getTypeIds") ; 92 return new int[] { TYPEID }; 93 } 94 95 99 protected String [] getTypeNames() { 100 cat.debug("-> getTypeNames") ; 101 return new String [] { TYPE_NAME }; 102 } 103 104 109 protected void javaToNative(Object object, TransferData transferData) { 110 cat.debug("-> javaToNative") ; 111 byte[] bytes = toByteArray((ScenarioNode) object); 112 if (bytes != null) 113 super.javaToNative(bytes, transferData); 114 } 115 116 119 protected Object nativeToJava(TransferData transferData) { 120 cat.debug("-> nativeToJava") ; 121 byte[] bytes = (byte[]) super.nativeToJava(transferData); 122 return fromByteArray(bytes); 123 } 124 125 128 private ScenarioNode readScenarioTree(DataInputStream dataIn) 129 throws IOException { 130 cat.debug("-> readScenarioTree") ; 131 138 String key = dataIn.readUTF(); 139 int n = dataIn.readInt(); 140 ScenarioNode newParent = new ScenarioNode(key); 141 for (int i = 0; i < n; i++) { 142 newParent.addChild(readScenarioTree(dataIn)); 143 } 144 return newParent; 145 } 146 147 protected byte[] toByteArray(ScenarioNode tree) { 148 cat.debug("-> toByteArray") ; 149 154 ByteArrayOutputStream byteOut = new ByteArrayOutputStream (); 155 DataOutputStream out = new DataOutputStream (byteOut); 156 157 byte[] bytes = null; 158 159 try { 160 writeScenarioTree(tree, out); 161 out.close(); 162 bytes = byteOut.toByteArray(); 163 } catch (IOException e) { 164 } 166 return bytes; 167 } 168 169 172 private void writeScenarioTree(ScenarioNode tree, DataOutputStream dataOut) 173 throws IOException { 174 cat.debug("-> writeScenarioTree") ; 175 182 dataOut.writeUTF(tree.getKey()); 183 Vector children = tree.getChildren(); 184 dataOut.writeInt(children.size()); 185 for (int i = 0; i < children.size(); i++) { 186 writeScenarioTree((ScenarioNode) children.elementAt(i), dataOut); 187 } 188 } 189 } 190 191 | Popular Tags |