1 /******************************************************************************* 2 * Copyright (c) 2000, 2017 IBM Corporation and others. 3 * 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/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * IBM Corporation - initial API and implementation 13 *******************************************************************************/ 14 package org.eclipse.swt.dnd; 15 16 import org.eclipse.swt.internal.cocoa.*; 17 18 /** 19 * The class <code>TextTransfer</code> provides a platform specific mechanism 20 * for converting plain text represented as a java <code>String</code> 21 * to a platform specific representation of the data and vice versa. 22 * 23 * <p>An example of a java <code>String</code> containing plain text is shown 24 * below:</p> 25 * 26 * <pre><code> 27 * String textData = "Hello World"; 28 * </code></pre> 29 * 30 * <p>Note the <code>TextTransfer</code> does not change the content of the text 31 * data. For a better integration with the platform, the application should convert 32 * the line delimiters used in the text data to the standard line delimiter used by the 33 * platform. 34 * </p> 35 * 36 * @see Transfer 37 */ 38 public class TextTransfer extends ByteArrayTransfer { 39 40 static TextTransfer _instance = new TextTransfer(); 41 42 static final String ID_NAME = OS.NSPasteboardTypeString.getString(); 43 static final int ID = registerType(ID_NAME); 44 TextTransfer()45TextTransfer() {} 46 47 /** 48 * Returns the singleton instance of the TextTransfer class. 49 * 50 * @return the singleton instance of the TextTransfer class 51 */ getInstance()52public static TextTransfer getInstance () { 53 return _instance; 54 } 55 56 /** 57 * This implementation of <code>javaToNative</code> converts plain text 58 * represented by a java <code>String</code> to a platform specific representation. 59 * 60 * @param object a java <code>String</code> containing text 61 * @param transferData an empty <code>TransferData</code> object that will 62 * be filled in on return with the platform specific format of the data 63 * 64 * @see Transfer#nativeToJava 65 */ 66 @Override javaToNative(Object object, TransferData transferData)67public void javaToNative (Object object, TransferData transferData) { 68 if (!checkText(object) || !isSupportedType(transferData)) { 69 DND.error(DND.ERROR_INVALID_DATA); 70 } 71 transferData.data = NSString.stringWith((String) object); 72 } 73 74 /** 75 * This implementation of <code>nativeToJava</code> converts a platform specific 76 * representation of plain text to a java <code>String</code>. 77 * 78 * @param transferData the platform specific representation of the data to be converted 79 * @return a java <code>String</code> containing text if the conversion was successful; otherwise null 80 * 81 * @see Transfer#javaToNative 82 */ 83 @Override nativeToJava(TransferData transferData)84public Object nativeToJava(TransferData transferData){ 85 if (!isSupportedType(transferData) || transferData.data == null) return null; 86 NSString string = (NSString) transferData.data; 87 return string.getString(); 88 } 89 90 @Override getTypeIds()91protected int[] getTypeIds() { 92 return new int[] {ID}; 93 } 94 95 @Override getTypeNames()96protected String[] getTypeNames() { 97 return new String[] {ID_NAME}; 98 } 99 checkText(Object object)100boolean checkText(Object object) { 101 return (object != null && object instanceof String && ((String)object).length() > 0); 102 } 103 @Override validate(Object object)104protected boolean validate(Object object) { 105 return checkText(object); 106 } 107 } 108