Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9a74c8dcc964ad2ff1ee72cf97ccf9378a189ea5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*******************************************************************************
 * Copyright (c) 2004, 2008 John Krasnay and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     John Krasnay - initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.xml.vex.ui.internal.swt;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.eclipse.swt.dnd.ByteArrayTransfer;
import org.eclipse.swt.dnd.TransferData;
import org.eclipse.wst.xml.vex.core.internal.dom.DocumentFragment;

/**
 * Transfer object that handles Vex DocumentFragments.
 */
public class DocumentFragmentTransfer extends ByteArrayTransfer {

	/**
	 * Returns the singleton instance of the DocumentFragmentTransfer.
	 */
	public static DocumentFragmentTransfer getInstance() {
		if (instance == null) {
			instance = new DocumentFragmentTransfer();
		}
		return instance;
	}

	protected String[] getTypeNames() {
		return typeNames;
	}

	protected int[] getTypeIds() {
		return typeIds;
	}

	public void javaToNative(Object object, TransferData transferData) {
		if (object == null || !(object instanceof DocumentFragment))
			return;

		if (isSupportedType(transferData)) {
			DocumentFragment frag = (DocumentFragment) object;
			try {
				// write data to a byte array and then ask super to convert to
				// pMedium
				ByteArrayOutputStream out = new ByteArrayOutputStream();
				ObjectOutputStream oos = new ObjectOutputStream(out);
				oos.writeObject(frag);
				byte[] buffer = out.toByteArray();
				oos.close();
				super.javaToNative(buffer, transferData);
			} catch (IOException e) {
			}
		}
	}

	public Object nativeToJava(TransferData transferData) {

		if (isSupportedType(transferData)) {
			byte[] buffer = (byte[]) super.nativeToJava(transferData);
			if (buffer == null)
				return null;

			try {
				ByteArrayInputStream in = new ByteArrayInputStream(buffer);
				ObjectInputStream ois = new ObjectInputStream(in);
				Object object = ois.readObject();
				ois.close();
				return object;
			} catch (ClassNotFoundException ex) {
				return null;
			} catch (IOException ex) {
				return null;
			}
		}

		return null;
	}

	// =================================================== PRIVATE

	private static final String[] typeNames = { DocumentFragment.MIME_TYPE };
	private static final int[] typeIds = { ByteArrayTransfer
			.registerType(DocumentFragment.MIME_TYPE) };

	private static DocumentFragmentTransfer instance;

	private DocumentFragmentTransfer() {
	}
}

Back to the top