KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > intro > universal > ExtensionDataTransfer


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.intro.universal;
12
13 import java.io.ByteArrayInputStream JavaDoc;
14 import java.io.ByteArrayOutputStream JavaDoc;
15 import java.io.DataInputStream JavaDoc;
16 import java.io.DataOutputStream JavaDoc;
17 import java.io.IOException JavaDoc;
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 JavaDoc MYTYPENAME = "ExtensionData"; //$NON-NLS-1$
27
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 JavaDoc[] getTypeNames() {
35         return new String JavaDoc[] { MYTYPENAME };
36     }
37
38     protected int[] getTypeIds() {
39         return new int[] { MYTYPEID };
40     }
41
42     public void javaToNative(Object JavaDoc 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             // write data to a byte array and then ask super to convert to pMedium
49
ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
50             DataOutputStream JavaDoc writeOut = new DataOutputStream JavaDoc(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 JavaDoc e) {
70         }
71     }
72
73     public Object JavaDoc 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 JavaDoc in = new ByteArrayInputStream JavaDoc(buffer);
82                 DataInputStream JavaDoc readIn = new DataInputStream JavaDoc(in);
83                 while (readIn.available() > 4) {
84                     boolean separator;
85                     int importance=0;
86                     String JavaDoc id;
87                     String JavaDoc 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 JavaDoc(buff);
93                     if (!separator) {
94                         size = readIn.readInt();
95                         buff = new byte[size];
96                         readIn.read(buff);
97                         name = new String JavaDoc(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 JavaDoc ex) {
111                 return null;
112             }
113             return myData;
114         }
115
116         return null;
117     }
118
119
120     boolean checkMyType(Object JavaDoc 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 JavaDoc object) {
133         return checkMyType(object);
134     }
135 }
136
Popular Tags