1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 /***
19 * <p>Title: WSMO Studio</p>
20 * <p>Description: Semantic Web Service Editor</p>
21 * <p>Copyright: Copyright (c) 2004-2006</p>
22 * <p>Company: OntoText Lab. / SIRMA </p>
23 */
24
25 package org.wsmostudio.ui.dnd;
26
27 import org.eclipse.swt.dnd.ByteArrayTransfer;
28 import java.io.*;
29 import java.util.*;
30
31 import org.eclipse.swt.dnd.TransferData;
32 import org.wsmo.common.*;
33
34 public class WSMOTransfer extends ByteArrayTransfer {
35
36 protected static WSMOTransfer instance = new WSMOTransfer();
37 private static final String TYPE_NAME = "wsmo-transfer-format";
38 private static final int TYPEID = registerType(TYPE_NAME);
39
40 public static WSMOTransfer getInstance() {
41 return instance;
42 }
43
44 protected String[] fromByteArray(byte[] bytes) {
45
46 DataInputStream in = new DataInputStream(new ByteArrayInputStream(bytes));
47 List<String> buffer = new LinkedList<String>();
48 String iriStr;
49 do {
50 iriStr = null;
51 try {
52 iriStr = in.readUTF();
53 buffer.add(iriStr);
54 }
55 catch (IOException e) {
56 break;
57 }
58 }
59 while(iriStr != null);
60 String[] result = new String[buffer.size()];
61 return buffer.toArray(result);
62 }
63
64
65
66 protected int[] getTypeIds() {
67 return new int[] { TYPEID };
68 }
69
70
71
72 protected String[] getTypeNames() {
73 return new String[] { TYPE_NAME };
74 }
75
76
77
78
79 protected void javaToNative(Object object, TransferData transferData) {
80 byte[] bytes = toByteArray((List<Entity>)object);
81 if (bytes != null)
82 super.javaToNative(bytes, transferData);
83 }
84
85
86
87 protected Object nativeToJava(TransferData transferData) {
88 byte[] bytes = (byte[])super.nativeToJava(transferData);
89 return fromByteArray(bytes);
90 }
91
92 protected byte[] toByteArray(List<Entity> entities) {
93 ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
94 DataOutputStream out = new DataOutputStream(byteOut);
95
96 byte[] bytes = null;
97
98 try {
99 for (Entity entity : entities) {
100 out.writeUTF(entity.getIdentifier().toString());
101 }
102 out.close();
103 bytes = byteOut.toByteArray();
104 }
105 catch (IOException e) {}
106 return bytes;
107 }
108 }
109
110
111
112
113
114
115
116
117
118