1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 IBM Corporation and others.
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
9 * SPDX-License-Identifier: EPL-2.0
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.swt.dnd;
17 import org.eclipse.swt.internal.ole.win32.*;
18 import org.eclipse.swt.internal.win32.*;
21 * <code>Transfer</code> provides a mechanism for converting between a java
22 * representation of data and a platform specific representation of data and
23 * vice versa. It is used in data transfer operations such as drag and drop and
24 * clipboard copy/paste.
26 * <p>You should only need to become familiar with this class if you are
27 * implementing a Transfer subclass and you are unable to subclass the
28 * ByteArrayTransfer class.</p>
30 * @see ByteArrayTransfer
31 * @see <a href="http://www.eclipse.org/swt/snippets/#dnd">Drag and Drop snippets</a>
32 * @see <a href="http://www.eclipse.org/swt/examples.php">SWT Example: DNDExample</a>
33 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
35 public abstract class Transfer {
37 private static final int RETRY_LIMIT = 10;
39 * Feature in Windows. When another application has control
40 * of the clipboard, the clipboard is locked and it's not
41 * possible to retrieve data until the other application is
42 * finished. To allow other applications to get the
43 * data, use PeekMessage() to enable cross thread
46 int getData(IDataObject dataObject, FORMATETC pFormatetc, STGMEDIUM pmedium) {
47 if (dataObject.GetData(pFormatetc, pmedium) == COM.S_OK) return COM.S_OK;
48 try {Thread.sleep(50);} catch (Throwable t) {}
49 int result = dataObject.GetData(pFormatetc, pmedium);
51 while (result != COM.S_OK && retryCount++ < RETRY_LIMIT) {
53 OS.PeekMessage(msg, 0, 0, 0, OS.PM_NOREMOVE | OS.PM_NOYIELD);
54 try {Thread.sleep(50);} catch (Throwable t) {}
55 result = dataObject.GetData(pFormatetc, pmedium);
61 * Returns a list of the platform specific data types that can be converted using
62 * this transfer agent.
64 * <p>Only the data type fields of the <code>TransferData</code> objects are filled
67 * @return a list of the data types that can be converted using this transfer agent
69 abstract public TransferData[] getSupportedTypes();
72 * Returns true if the <code>TransferData</code> data type can be converted
73 * using this transfer agent, or false otherwise (including if transferData is
76 * @param transferData a platform specific description of a data type; only the data
77 * type fields of the <code>TransferData</code> object need to be filled in
79 * @return true if the transferData data type can be converted using this transfer
82 abstract public boolean isSupportedType(TransferData transferData);
85 * Returns the platform specific ids of the data types that can be converted using
86 * this transfer agent.
88 * @return the platform specific ids of the data types that can be converted using
91 abstract protected int[] getTypeIds();
94 * Returns the platform specific names of the data types that can be converted
95 * using this transfer agent.
97 * @return the platform specific names of the data types that can be converted
98 * using this transfer agent.
100 abstract protected String[] getTypeNames();
103 * Converts a java representation of data to a platform specific representation of
106 * <p>On a successful conversion, the transferData.result field will be set as follows:</p>
108 * <li>Windows: COM.S_OK
112 * <p>If this transfer agent is unable to perform the conversion, the transferData.result
113 * field will be set to a failure value as follows:</p>
115 * <li>Windows: COM.DV_E_TYMED or COM.E_FAIL
119 * @param object a java representation of the data to be converted; the type of
120 * Object that is passed in is dependent on the <code>Transfer</code> subclass.
122 * @param transferData an empty TransferData object; this object will be
123 * filled in on return with the platform specific representation of the data
125 * @exception org.eclipse.swt.SWTException <ul>
126 * <li>ERROR_INVALID_DATA - if object does not contain data in a valid format or is <code>null</code></li>
129 abstract protected void javaToNative (Object object, TransferData transferData);
132 * Converts a platform specific representation of data to a java representation.
134 * @param transferData the platform specific representation of the data to be
137 * @return a java representation of the converted data if the conversion was
138 * successful; otherwise null. If transferData is <code>null</code> then
139 * <code>null</code> is returned. The type of Object that is returned is
140 * dependent on the <code>Transfer</code> subclass.
142 abstract protected Object nativeToJava(TransferData transferData);
145 * Registers a name for a data type and returns the associated unique identifier.
147 * <p>You may register the same type more than once, the same unique identifier
148 * will be returned if the type has been previously registered.</p>
150 * <p>Note: On windows, do <b>not</b> call this method with pre-defined
151 * Clipboard Format types such as CF_TEXT or CF_BITMAP because the
152 * pre-defined identifier will not be returned</p>
154 * @param formatName the name of a data type
156 * @return the unique identifier associated with this data type
158 public static int registerType(String formatName) {
159 // Look name up in the registry
160 // If name is not in registry, add it and return assigned value.
161 // If name already exists in registry, return its assigned value
162 TCHAR chFormatName = new TCHAR(0, formatName, true);
163 return OS.RegisterClipboardFormat(chFormatName);
167 * Test that the object is of the correct format for this Transfer class.
169 * @param object a java representation of the data to be converted
171 * @return true if object is of the correct form for this transfer type
175 protected boolean validate(Object object) {