Fix import
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / dnd / LocalSelectionTransfer.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.dnd;
12
13 import net.sourceforge.phpdt.internal.ui.PHPUIMessages;
14 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
15
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.jface.viewers.ISelection;
18 import org.eclipse.swt.dnd.ByteArrayTransfer;
19 import org.eclipse.swt.dnd.TransferData;
20
21 public class LocalSelectionTransfer extends ByteArrayTransfer {
22
23         // First attempt to create a UUID for the type name to make sure that
24         // different Eclipse applications use different "types" of
25         // <code>LocalSelectionTransfer</code>
26         private static final String TYPE_NAME = "local-selection-transfer-format" + (new Long(System.currentTimeMillis())).toString(); //$NON-NLS-1$;
27
28         private static final int TYPEID = registerType(TYPE_NAME);
29
30         private static final LocalSelectionTransfer INSTANCE = new LocalSelectionTransfer();
31
32         private ISelection fSelection;
33
34         private int fSelectionSetTime;
35
36         private LocalSelectionTransfer() {
37         }
38
39         /**
40          * Returns the singleton.
41          */
42         public static LocalSelectionTransfer getInstance() {
43                 return INSTANCE;
44         }
45
46         /**
47          * Sets the transfer data for local use.
48          */
49         public void setSelection(ISelection s) {
50                 fSelection = s;
51         }
52
53         /**
54          * Returns the local transfer data.
55          */
56         public ISelection getSelection() {
57                 return fSelection;
58         }
59
60         public void javaToNative(Object object, TransferData transferData) {
61                 // No encoding needed since this is a hardcoded string read and written
62                 // in the same process.
63                 // See nativeToJava below
64                 byte[] check = TYPE_NAME.getBytes();
65                 super.javaToNative(check, transferData);
66         }
67
68         public Object nativeToJava(TransferData transferData) {
69                 Object result = super.nativeToJava(transferData);
70                 if (isInvalidNativeType(result)) {
71                         PHPeclipsePlugin.log(IStatus.ERROR, PHPUIMessages
72                                         .getString("LocalSelectionTransfer.errorMessage")); //$NON-NLS-1$
73                 }
74                 return fSelection;
75         }
76
77         private boolean isInvalidNativeType(Object result) {
78                 // No encoding needed since this is a hardcoded string read and written
79                 // in the same process.
80                 // See javaToNative above
81                 return !(result instanceof byte[])
82                                 || !TYPE_NAME.equals(new String((byte[]) result));
83         }
84
85         /**
86          * The type id used to identify this transfer.
87          */
88         protected int[] getTypeIds() {
89                 return new int[] { TYPEID };
90         }
91
92         protected String[] getTypeNames() {
93                 return new String[] { TYPE_NAME };
94         }
95
96         public int getSelectionSetTime() {
97                 return fSelectionSetTime;
98         }
99
100         public void setSelectionSetTime(int time) {
101                 fSelectionSetTime = time;
102         }
103
104 }