1 /******************************************************************************* 2 * Copyright (c) 2007, 2008 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 module org.eclipse.swt.dnd.URLTransfer; 12 13 import org.eclipse.swt.internal.gtk.OS; 14 15 import org.eclipse.swt.dnd.ByteArrayTransfer; 16 import org.eclipse.swt.dnd.TransferData; 17 import org.eclipse.swt.dnd.DND; 18 19 import java.lang.all; 20 21 /** 22 * The class <code>URLTransfer</code> provides a platform specific mechanism 23 * for converting text in URL format represented as a java <code>String</code> 24 * to a platform specific representation of the data and vice versa. See 25 * <code>Transfer</code> for additional information. The string 26 * 27 * <p>An example of a java <code>String</code> containing a URL is shown below:</p> 28 * 29 * <code><pre> 30 * String url = "http://www.eclipse.org"; 31 * </code></pre> 32 * 33 * @see Transfer 34 */ 35 public class URLTransfer : ByteArrayTransfer { 36 37 static URLTransfer _instance; 38 private static const String TEXT_UNICODE = "text/unicode"; //$NON-NLS-1$ 39 private static const String TEXT_XMOZURL = "text/x-moz-url"; //$NON-NLS-1$ 40 private static int TEXT_UNICODE_ID; 41 private static int TEXT_XMOZURL_ID; 42 43 static this(){ 44 TEXT_UNICODE_ID = registerType(TEXT_UNICODE); 45 TEXT_XMOZURL_ID = registerType(TEXT_XMOZURL); 46 _instance = new URLTransfer(); 47 } 48 49 private this() {} 50 51 /** 52 * Returns the singleton instance of the URLTransfer class. 53 * 54 * @return the singleton instance of the URLTransfer class 55 */ 56 public static URLTransfer getInstance () { 57 return _instance; 58 } 59 60 /** 61 * This implementation of <code>javaToNative</code> converts a URL 62 * represented by a java <code>String</code> to a platform specific representation. 63 * 64 * @param object a java <code>String</code> containing a URL 65 * @param transferData an empty <code>TransferData</code> object that will 66 * be filled in on return with the platform specific format of the data 67 * 68 * @see Transfer#nativeToJava 69 */ 70 override 71 public void javaToNative (Object object, TransferData transferData){ 72 transferData.result = 0; 73 if (!checkURL(object) || !isSupportedType(transferData)) { 74 DND.error(DND.ERROR_INVALID_DATA); 75 } 76 String16 string = stringcast(object).toWCharArray(); 77 auto byteCount = (string.length+1)*2; 78 wchar* pValue = cast(wchar*)OS.g_malloc(byteCount); 79 if (pValue is null) return; 80 pValue[ 0 .. string.length ] = string[]; 81 pValue[ string.length ] = '\0'; 82 transferData.length = cast(int)/*64bit*/byteCount; 83 transferData.format = 8; 84 transferData.pValue = cast(char*)pValue; 85 transferData.result = 1; 86 } 87 88 /** 89 * This implementation of <code>nativeToJava</code> converts a platform 90 * specific representation of a URL to a java <code>String</code>. 91 * 92 * @param transferData the platform specific representation of the data to be converted 93 * @return a java <code>String</code> containing a URL if the conversion was successful; 94 * otherwise null 95 * 96 * @see Transfer#javaToNative 97 */ 98 override 99 public Object nativeToJava(TransferData transferData){ 100 if (!isSupportedType(transferData) || transferData.pValue is null) return null; 101 /* Ensure byteCount is a multiple of 2 bytes */ 102 int size = (transferData.format * transferData.length / 8) / 2 * 2; 103 if (size <= 0) return null; 104 // The string can be terminated with NULL or it is as a maximum of length size 105 for( int i = 0; i < size; i++ ){ 106 if( (cast(wchar*)transferData.pValue)[i] == '\0' ){ 107 size = i; 108 break; 109 } 110 } 111 String string = String_valueOf((cast(wchar*)transferData.pValue)[ 0 .. size ]); 112 return new ArrayWrapperString( string ); 113 } 114 115 override 116 protected int[] getTypeIds(){ 117 return [TEXT_XMOZURL_ID, TEXT_UNICODE_ID]; 118 } 119 120 override 121 protected String[] getTypeNames(){ 122 return [TEXT_XMOZURL, TEXT_UNICODE]; 123 } 124 125 bool checkURL(Object object) { 126 return object !is null && (null !is cast(ArrayWrapperString)object) && (cast(ArrayWrapperString)object).array.length > 0; 127 } 128 129 override 130 protected bool validate(Object object) { 131 return checkURL(object); 132 } 133 }