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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.dnd;
13 import net.sourceforge.phpdt.internal.ui.PHPUIMessages;
14 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
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;
22 public class LocalSelectionTransfer extends ByteArrayTransfer {
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 private static final int TYPEID= registerType(TYPE_NAME);
30 private static final LocalSelectionTransfer INSTANCE= new LocalSelectionTransfer();
32 private ISelection fSelection;
33 private int fSelectionSetTime;
35 private LocalSelectionTransfer() {
39 * Returns the singleton.
41 public static LocalSelectionTransfer getInstance() {
46 * Sets the transfer data for local use.
48 public void setSelection(ISelection s) {
53 * Returns the local transfer data.
55 public ISelection getSelection() {
59 public void javaToNative(Object object, TransferData transferData) {
60 // No encoding needed since this is a hardcoded string read and written in the same process.
61 // See nativeToJava below
62 byte[] check= TYPE_NAME.getBytes();
63 super.javaToNative(check, transferData);
66 public Object nativeToJava(TransferData transferData) {
67 Object result= super.nativeToJava(transferData);
68 if (isInvalidNativeType(result)) {
69 PHPeclipsePlugin.log(IStatus.ERROR, PHPUIMessages.getString("LocalSelectionTransfer.errorMessage")); //$NON-NLS-1$
74 private boolean isInvalidNativeType(Object result) {
75 // No encoding needed since this is a hardcoded string read and written in the same process.
76 // See javaToNative above
77 return !(result instanceof byte[]) || !TYPE_NAME.equals(new String((byte[])result));
81 * The type id used to identify this transfer.
83 protected int[] getTypeIds() {
84 return new int[] {TYPEID};
87 protected String[] getTypeNames(){
88 return new String[] {TYPE_NAME};
91 public int getSelectionSetTime() {
92 return fSelectionSetTime;
95 public void setSelectionSetTime(int time) {
96 fSelectionSetTime= time;