49ea330782c9c71739550fe7be45b0f2933d0fbf
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / 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 import net.sourceforge.phpeclipse.ui.WebUI;
16
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.jface.viewers.ISelection;
19 import org.eclipse.swt.dnd.ByteArrayTransfer;
20 import org.eclipse.swt.dnd.TransferData;
21
22 public class LocalSelectionTransfer extends ByteArrayTransfer {
23
24         // First attempt to create a UUID for the type name to make sure that
25         // different Eclipse applications use different "types" of
26         // <code>LocalSelectionTransfer</code>
27         private static final String TYPE_NAME = "local-selection-transfer-format" + (new Long(System.currentTimeMillis())).toString(); //$NON-NLS-1$;
28
29         private static final int TYPEID = registerType(TYPE_NAME);
30
31         private static final LocalSelectionTransfer INSTANCE = new LocalSelectionTransfer();
32
33         private ISelection fSelection;
34
35         private int fSelectionSetTime;
36
37         private LocalSelectionTransfer() {
38         }
39
40         /**
41          * Returns the singleton.
42          */
43         public static LocalSelectionTransfer getInstance() {
44                 return INSTANCE;
45         }
46
47         /**
48          * Sets the transfer data for local use.
49          */
50         public void setSelection(ISelection s) {
51                 fSelection = s;
52         }
53
54         /**
55          * Returns the local transfer data.
56          */
57         public ISelection getSelection() {
58                 return fSelection;
59         }
60
61         public void javaToNative(Object object, TransferData transferData) {
62                 // No encoding needed since this is a hardcoded string read and written
63                 // in the same process.
64                 // See nativeToJava below
65                 byte[] check = TYPE_NAME.getBytes();
66                 super.javaToNative(check, transferData);
67         }
68
69         public Object nativeToJava(TransferData transferData) {
70                 Object result = super.nativeToJava(transferData);
71                 if (isInvalidNativeType(result)) {
72                         WebUI.log(IStatus.ERROR, PHPUIMessages
73                                         .getString("LocalSelectionTransfer.errorMessage")); //$NON-NLS-1$
74                 }
75                 return fSelection;
76         }
77
78         private boolean isInvalidNativeType(Object result) {
79                 // No encoding needed since this is a hardcoded string read and written
80                 // in the same process.
81                 // See javaToNative above
82                 return !(result instanceof byte[])
83                                 || !TYPE_NAME.equals(new String((byte[]) result));
84         }
85
86         /**
87          * The type id used to identify this transfer.
88          */
89         protected int[] getTypeIds() {
90                 return new int[] { TYPEID };
91         }
92
93         protected String[] getTypeNames() {
94                 return new String[] { TYPE_NAME };
95         }
96
97         public int getSelectionSetTime() {
98                 return fSelectionSetTime;
99         }
100
101         public void setSelectionSetTime(int time) {
102                 fSelectionSetTime = time;
103         }
104
105 }