1 /*******************************************************************************
2 * Copyright (c) 2000, 2012 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;
16 import org.eclipse.swt.internal.ole.win32.*;
17 import org.eclipse.swt.internal.win32.*;
20 * The class <code>TextTransfer</code> provides a platform specific mechanism
21 * for converting plain text represented as a java <code>String</code>
22 * to a platform specific representation of the data and vice versa.
24 * <p>An example of a java <code>String</code> containing plain text is shown
28 * String textData = "Hello World";
31 * <p>Note the <code>TextTransfer</code> does not change the content of the text
32 * data. For a better integration with the platform, the application should convert
33 * the line delimiters used in the text data to the standard line delimiter used by the
39 public class TextTransfer extends ByteArrayTransfer {
41 private static TextTransfer _instance = new TextTransfer();
42 private static final String CF_UNICODETEXT = "CF_UNICODETEXT"; //$NON-NLS-1$
43 private static final String CF_TEXT = "CF_TEXT"; //$NON-NLS-1$
44 private static final int CF_UNICODETEXTID = COM.CF_UNICODETEXT;
45 private static final int CF_TEXTID = COM.CF_TEXT;
47 private TextTransfer() {}
50 * Returns the singleton instance of the TextTransfer class.
52 * @return the singleton instance of the TextTransfer class
54 public static TextTransfer getInstance () {
59 * This implementation of <code>javaToNative</code> converts plain text
60 * represented by a java <code>String</code> to a platform specific representation.
62 * @param object a java <code>String</code> containing text
63 * @param transferData an empty <code>TransferData</code> object that will
64 * be filled in on return with the platform specific format of the data
66 * @see Transfer#nativeToJava
69 public void javaToNative (Object object, TransferData transferData){
70 if (!checkText(object) || !isSupportedType(transferData)) {
71 DND.error(DND.ERROR_INVALID_DATA);
73 transferData.result = COM.E_FAIL;
74 String string = (String)object;
75 switch (transferData.type) {
76 case COM.CF_UNICODETEXT: {
77 int charCount = string.length ();
78 char[] chars = new char[charCount+1];
79 string.getChars (0, charCount, chars, 0);
80 int byteCount = chars.length * 2;
81 long newPtr = OS.GlobalAlloc(COM.GMEM_FIXED | COM.GMEM_ZEROINIT, byteCount);
82 OS.MoveMemory(newPtr, chars, byteCount);
83 transferData.stgmedium = new STGMEDIUM();
84 transferData.stgmedium.tymed = COM.TYMED_HGLOBAL;
85 transferData.stgmedium.unionField = newPtr;
86 transferData.stgmedium.pUnkForRelease = 0;
87 transferData.result = COM.S_OK;
91 int count = string.length();
92 char[] chars = new char[count + 1];
93 string.getChars(0, count, chars, 0);
94 int codePage = OS.GetACP();
95 int cchMultiByte = OS.WideCharToMultiByte(codePage, 0, chars, -1, null, 0, null, null);
96 if (cchMultiByte == 0) {
97 transferData.stgmedium = new STGMEDIUM();
98 transferData.result = COM.DV_E_STGMEDIUM;
101 long lpMultiByteStr = OS.GlobalAlloc(COM.GMEM_FIXED | COM.GMEM_ZEROINIT, cchMultiByte);
102 OS.WideCharToMultiByte(codePage, 0, chars, -1, lpMultiByteStr, cchMultiByte, null, null);
103 transferData.stgmedium = new STGMEDIUM();
104 transferData.stgmedium.tymed = COM.TYMED_HGLOBAL;
105 transferData.stgmedium.unionField = lpMultiByteStr;
106 transferData.stgmedium.pUnkForRelease = 0;
107 transferData.result = COM.S_OK;
115 * This implementation of <code>nativeToJava</code> converts a platform specific
116 * representation of plain text to a java <code>String</code>.
118 * @param transferData the platform specific representation of the data to be converted
119 * @return a java <code>String</code> containing text if the conversion was successful; otherwise null
121 * @see Transfer#javaToNative
124 public Object nativeToJava(TransferData transferData){
125 if (!isSupportedType(transferData) || transferData.pIDataObject == 0) return null;
127 IDataObject data = new IDataObject(transferData.pIDataObject);
129 FORMATETC formatetc = transferData.formatetc;
130 STGMEDIUM stgmedium = new STGMEDIUM();
131 stgmedium.tymed = COM.TYMED_HGLOBAL;
132 transferData.result = getData(data, formatetc, stgmedium);
134 if (transferData.result != COM.S_OK) return null;
135 long hMem = stgmedium.unionField;
137 switch (transferData.type) {
138 case CF_UNICODETEXTID: {
139 /* Ensure byteCount is a multiple of 2 bytes */
140 int size = OS.GlobalSize(hMem) / 2 * 2;
141 if (size == 0) return null;
142 char[] chars = new char[size/2];
143 long ptr = OS.GlobalLock(hMem);
144 if (ptr == 0) return null;
146 OS.MoveMemory(chars, ptr, size);
147 int length = chars.length;
148 for (int i=0; i<chars.length; i++) {
149 if (chars [i] == '\0') {
154 return new String (chars, 0, length);
156 OS.GlobalUnlock(hMem);
160 long lpMultiByteStr = OS.GlobalLock(hMem);
161 if (lpMultiByteStr == 0) return null;
163 int codePage = OS.GetACP();
164 int cchWideChar = OS.MultiByteToWideChar (codePage, OS.MB_PRECOMPOSED, lpMultiByteStr, -1, null, 0);
165 if (cchWideChar == 0) return null;
166 char[] lpWideCharStr = new char [cchWideChar - 1];
167 OS.MultiByteToWideChar (codePage, OS.MB_PRECOMPOSED, lpMultiByteStr, -1, lpWideCharStr, lpWideCharStr.length);
168 return new String(lpWideCharStr);
170 OS.GlobalUnlock(hMem);
181 protected int[] getTypeIds(){
182 return new int[] {CF_UNICODETEXTID, CF_TEXTID};
186 protected String[] getTypeNames(){
187 return new String[] {CF_UNICODETEXT, CF_TEXT};
190 boolean checkText(Object object) {
191 return (object != null && object instanceof String && ((String)object).length() > 0);
195 protected boolean validate(Object object) {
196 return checkText(object);