Skip to main content
summaryrefslogtreecommitdiffstats
blob: 16cf9d3e7525fb0fcbcd5f65011bc5da92a65e24 (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
100
101
102
103
104
105
106
107
108
109
/*******************************************************************************
 * Copyright (c) 2004 - 2006 Mylar committers 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
 *******************************************************************************/

package org.eclipse.mylyn.internal.tasks.ui;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import org.eclipse.mylyn.tasks.core.AbstractTask;
import org.eclipse.mylyn.tasks.ui.TasksUiPlugin;
import org.eclipse.swt.dnd.ByteArrayTransfer;
import org.eclipse.swt.dnd.TransferData;

/**
 * @author Mik Kersten
 */
public class TaskTransfer extends ByteArrayTransfer {

	private static final TaskTransfer INSTANCE = new TaskTransfer();

	private static final String TYPE_NAME = "task-transfer-format:" + System.currentTimeMillis() + ":"
			+ INSTANCE.hashCode();

	private static final int TYPEID = registerType(TYPE_NAME);

	private TaskTransfer() {
	}

	public static TaskTransfer getInstance() {
		return INSTANCE;
	}

	@Override
	protected int[] getTypeIds() {
		return new int[] { TYPEID };
	}

	@Override
	protected String[] getTypeNames() {
		return new String[] { TYPE_NAME };
	}

	@Override
	protected void javaToNative(Object data, TransferData transferData) {
		if (!(data instanceof AbstractTask[])) {
			return;
		}

		AbstractTask[] tasks = (AbstractTask[]) data;
		int resourceCount = tasks.length;

		try {
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			DataOutputStream dataOut = new DataOutputStream(out);

			//write the number of resources
			dataOut.writeInt(resourceCount);

			//write each resource
			for (int i = 0; i < tasks.length; i++) {
				writeResource(dataOut, tasks[i]);
			}

			//cleanup
			dataOut.close();
			out.close();
			byte[] bytes = out.toByteArray();
			super.javaToNative(bytes, transferData);
		} catch (IOException e) {
			//it's best to send nothing if there were problems
		}
	}

	@Override
	protected Object nativeToJava(TransferData transferData) {
		byte[] bytes = (byte[]) super.nativeToJava(transferData);
		if (bytes == null) {
			return null;
		}
		DataInputStream in = new DataInputStream(new ByteArrayInputStream(bytes));
		try {
			int count = in.readInt();
			AbstractTask[] results = new AbstractTask[count];
			for (int i = 0; i < count; i++) {
				results[i] = readTask(in);
			}
			return results;
		} catch (IOException e) {
			return null;
		}
	}

	private AbstractTask readTask(DataInputStream dataIn) throws IOException {
		String handle = dataIn.readUTF();
		return TasksUiPlugin.getTaskListManager().getTaskList().getTask(handle);
	}

	private void writeResource(DataOutputStream dataOut, AbstractTask task) throws IOException {
		dataOut.writeUTF(task.getHandleIdentifier());
	}
}

Back to the top