001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.editor;
011:
012: import java.io.ByteArrayInputStream;
013: import java.io.ByteArrayOutputStream;
014: import java.io.IOException;
015: import java.io.ObjectInputStream;
016: import java.io.ObjectOutputStream;
017:
018: import org.eclipse.swt.dnd.ByteArrayTransfer;
019: import org.eclipse.swt.dnd.TransferData;
020:
021: public class ModelDataTransfer extends ByteArrayTransfer {
022: /**
023: * Singleton instance.
024: */
025: private static final ModelDataTransfer instance = new ModelDataTransfer();
026: // Create a unique ID to make sure that different Eclipse
027: // applications use different "types" of <code>ModelDataTransfer</code>
028:
029: public static final String TYPE_PREFIX = "pde-model-transfer-format"; //$NON-NLS-1$
030: private static final String TYPE_NAME = TYPE_PREFIX + ":" //$NON-NLS-1$
031: + System.currentTimeMillis() + ":" //$NON-NLS-1$
032: + instance.hashCode();
033:
034: private static final int TYPEID = registerType(TYPE_NAME);
035:
036: public static ModelDataTransfer getInstance() {
037: return instance;
038: }
039:
040: /**
041: * Constructor for ModelDataTransfer.
042: */
043: public ModelDataTransfer() {
044: super ();
045: }
046:
047: /* (non-Javadoc)
048: * Method declared on Transfer.
049: */
050: protected int[] getTypeIds() {
051: return new int[] { TYPEID };
052: }
053:
054: /* (non-Javadoc)
055: * Returns the type names.
056: *
057: * @return the list of type names
058: */
059: protected String[] getTypeNames() {
060: return new String[] { TYPE_NAME };
061: }
062:
063: /* (non-Javadoc)
064: * Method declared on Transfer.
065: */
066: protected void javaToNative(Object data, TransferData transferData) {
067: if (!(data instanceof Object[])) {
068: return;
069: }
070: Object[] objects = (Object[]) data;
071: int count = objects.length;
072:
073: try {
074: ByteArrayOutputStream out = new ByteArrayOutputStream();
075: ObjectOutputStream objectOut = new ObjectOutputStream(out);
076:
077: //write the number of resources
078: objectOut.writeInt(count);
079:
080: //write each object
081: for (int i = 0; i < objects.length; i++) {
082: objectOut.writeObject(objects[i]);
083: }
084:
085: //cleanup
086: objectOut.close();
087: out.close();
088: byte[] bytes = out.toByteArray();
089: super .javaToNative(bytes, transferData);
090: } catch (IOException e) {
091: //it's best to send nothing if there were problems
092: System.out.println(e);
093: }
094:
095: }
096:
097: /* (non-Javadoc)
098: * Method declared on Transfer.
099: */
100: protected Object nativeToJava(TransferData transferData) {
101: byte[] bytes = (byte[]) super .nativeToJava(transferData);
102: if (bytes == null)
103: return null;
104: try {
105: ObjectInputStream in = new ObjectInputStream(
106: new ByteArrayInputStream(bytes));
107:
108: int count = in.readInt();
109: Object[] objects = new Object[count];
110: for (int i = 0; i < count; i++) {
111: objects[i] = in.readObject();
112: }
113: in.close();
114: return objects;
115: } catch (ClassNotFoundException e) {
116: return null;
117: } catch (IOException e) {
118: return null;
119: }
120: }
121: }
|