KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > util > LocalSelectionTransfer


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jface.util;
12
13
14 import org.eclipse.core.runtime.IStatus;
15 import org.eclipse.core.runtime.Status;
16 import org.eclipse.jface.resource.JFaceResources;
17 import org.eclipse.jface.viewers.ISelection;
18 import org.eclipse.swt.dnd.ByteArrayTransfer;
19 import org.eclipse.swt.dnd.TransferData;
20
21 /**
22  * A LocalSelectionTransfer may be used for drag and drop operations
23  * within the same instance of Eclipse.
24  * The selection is made available directly for use in the DropTargetListener.
25  * dropAccept method. The DropTargetEvent passed to dropAccept does not contain
26  * the drop data. The selection may be used for validation purposes so that the
27  * drop can be aborted if appropriate.
28  *
29  * This class is not intended to be subclassed.
30  *
31  * @since 3.2
32  */

33 public class LocalSelectionTransfer extends ByteArrayTransfer {
34
35     // First attempt to create a UUID for the type name to make sure that
36
// different Eclipse applications use different "types" of
37
// <code>LocalSelectionTransfer</code>
38
private static final String JavaDoc TYPE_NAME = "local-selection-transfer-format" + (new Long JavaDoc(System.currentTimeMillis())).toString(); //$NON-NLS-1$;
39

40     private static final int TYPEID = registerType(TYPE_NAME);
41
42     private static final LocalSelectionTransfer INSTANCE = new LocalSelectionTransfer();
43
44     private ISelection selection;
45
46     private long selectionSetTime;
47
48     /**
49      * Only the singleton instance of this class may be used.
50      */

51     protected LocalSelectionTransfer() {
52         // do nothing
53
}
54
55     /**
56      * Returns the singleton.
57      *
58      * @return the singleton
59      */

60     public static LocalSelectionTransfer getTransfer() {
61         return INSTANCE;
62     }
63
64     /**
65      * Returns the local transfer data.
66      *
67      * @return the local transfer data
68      */

69     public ISelection getSelection() {
70         return selection;
71     }
72
73     /**
74      * Tests whether native drop data matches this transfer type.
75      *
76      * @param result result of converting the native drop data to Java
77      * @return true if the native drop data does not match this transfer type.
78      * false otherwise.
79      */

80     private boolean isInvalidNativeType(Object JavaDoc result) {
81         return !(result instanceof byte[])
82                 || !TYPE_NAME.equals(new String JavaDoc((byte[]) result));
83     }
84
85     /**
86      * Returns the type id used to identify this transfer.
87      *
88      * @return the type id used to identify this transfer.
89      */

90     protected int[] getTypeIds() {
91         return new int[] { TYPEID };
92     }
93
94     /**
95      * Returns the type name used to identify this transfer.
96      *
97      * @return the type name used to identify this transfer.
98      */

99     protected String JavaDoc[] getTypeNames() {
100         return new String JavaDoc[] { TYPE_NAME };
101     }
102
103     /**
104      * Overrides org.eclipse.swt.dnd.ByteArrayTransfer#javaToNative(Object,
105      * TransferData).
106      * Only encode the transfer type name since the selection is read and
107      * written in the same process.
108      *
109      * @see org.eclipse.swt.dnd.ByteArrayTransfer#javaToNative(java.lang.Object, org.eclipse.swt.dnd.TransferData)
110      */

111     public void javaToNative(Object JavaDoc object, TransferData transferData) {
112         byte[] check = TYPE_NAME.getBytes();
113         super.javaToNative(check, transferData);
114     }
115
116     /**
117      * Overrides org.eclipse.swt.dnd.ByteArrayTransfer#nativeToJava(TransferData).
118      * Test if the native drop data matches this transfer type.
119      *
120      * @see org.eclipse.swt.dnd.ByteArrayTransfer#nativeToJava(TransferData)
121      */

122     public Object JavaDoc nativeToJava(TransferData transferData) {
123         Object JavaDoc result = super.nativeToJava(transferData);
124         if (isInvalidNativeType(result)) {
125             Policy.getLog().log(new Status(
126                             IStatus.ERROR,
127                             Policy.JFACE,
128                             IStatus.ERROR,
129                             JFaceResources.getString("LocalSelectionTransfer.errorMessage"), null)); //$NON-NLS-1$
130
}
131         return selection;
132     }
133
134     /**
135      * Sets the transfer data for local use.
136      *
137      * @param s the transfer data
138      */

139     public void setSelection(ISelection s) {
140         selection = s;
141     }
142
143     /**
144      * Returns the time when the selection operation
145      * this transfer is associated with was started.
146      *
147      * @return the time when the selection operation has started
148      *
149      * @see org.eclipse.swt.events.TypedEvent#time
150      */

151     public long getSelectionSetTime() {
152         return selectionSetTime;
153     }
154
155     /**
156      * Sets the time when the selection operation this
157      * transfer is associated with was started.
158      * If assigning this from an SWT event, be sure to use
159      * <code>setSelectionTime(event.time & 0xFFFF)</code>
160      *
161      * @param time the time when the selection operation was started
162      *
163      * @see org.eclipse.swt.events.TypedEvent#time
164      */

165     public void setSelectionSetTime(long time) {
166         selectionSetTime = time;
167     }
168 }
169
Popular Tags