view dwt/dnd/URLTransfer.d @ 8:a9ab4c738ed8

Fix: instanceof
author Frank Benoit <benoit@tionex.de>
date Wed, 27 Aug 2008 14:32:39 +0200
parents e831403a80a9
children d8635bb48c7c
line wrap: on
line source

/*******************************************************************************
 * Copyright (c) 20007 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
module dwt.dnd;

import dwt.internal.carbon.*;

/**
 * The class <code>URLTransfer</code> provides a platform specific mechanism 
 * for converting text in URL format represented as a java <code>String</code> 
 * to a platform specific representation of the data and vice versa.  See 
 * <code>Transfer</code> for additional information. The String  
 * must contain the fully specified url.
 * 
 * <p>An example of a java <code>String[]</code> containing a URL is shown 
 * below:</p>
 * 
 * <code><pre>
 *     String url = "http://www.eclipse.org";
 * </code></pre>
 */
public class URLTransfer : ByteArrayTransfer {

  static URLTransfer _instance = new URLTransfer();
 static final String URL = "url "; //$NON-NLS-1$
   static final int URL_ID = registerType(URL);
  static final String URLN = "urln"; //$NON-NLS-1$
  static final int URLN_ID = registerType(URLN);

private URLTransfer() {}

/**
 * Returns the singleton instance of the URLTransfer class.
 *
 * @return the singleton instance of the URLTransfer class
 */
public static URLTransfer getInstance () {
   return _instance;
}

/**
 * This implementation of <code>javaToNative</code> converts a URL
 * represented by a java <code>String</code> to a platform specific representation.
 * For additional information see <code>Transfer#javaToNative</code>.
 * 
 * @param object a java <code>String[]</code> containing a URL
 * @param transferData an empty <code>TransferData</code> object; this
 *  object will be filled in on return with the platform specific format of the data
 */
public void javaToNative (Object object, TransferData transferData){
 if (!checkURL(object) || !isSupportedType(transferData)) {
        DND.error(DND.ERROR_INVALID_DATA);
    }
 transferData.result = -1;
 String url = cast(String)object;
  int count = url.length();
 char[] chars = new char[count];
   url.getChars(0, count, chars, 0);
 int cfString = OS.CFStringCreateWithCharacters(OS.kCFAllocatorDefault, chars, count);
 if (cfString is 0) return;
    try {
     CFRange range = new CFRange();
        range.length = chars.length;
      int encoding = OS.CFStringGetSystemEncoding();
        int[] size = new int[1];
      int numChars = OS.CFStringGetBytes(cfString, range, encoding, cast(byte)'?', true, null, 0, size);
        if (numChars is 0 || size[0] is 0) return;
        byte[] buffer = new byte[size[0]];
        numChars = OS.CFStringGetBytes(cfString, range, encoding, cast(byte)'?', true, buffer, size [0], size);
       if (numChars is 0) return;
        transferData.data = new byte[][] {buffer};
        transferData.result = 0;
  } finally {
       OS.CFRelease(cfString);
   }
}

/**
 * This implementation of <code>nativeToJava</code> converts a platform specific 
 * representation of a URL to a java <code>String</code>.
 * For additional information see <code>Transfer#nativeToJava</code>.
 * 
 * @param transferData the platform specific representation of the data to be 
 * converted
 * @return a java <code>String[]</code> containing a URL if the 
 * conversion was successful; otherwise null
 */
public Object nativeToJava(TransferData transferData){
  if (!isSupportedType(transferData) || transferData.data is null) return null;
 if (transferData.data.length is 0) return null;
   byte[] buffer = transferData.data[0];
 int encoding = OS.CFStringGetSystemEncoding();
    int cfString = OS.CFStringCreateWithBytes(OS.kCFAllocatorDefault, buffer, buffer.length, encoding, true);
 if (cfString is 0) return null;
   try {
     int length = OS.CFStringGetLength(cfString);
      if (length is 0) return null;
     char[] chars = new char[length];
      CFRange range = new CFRange();
        range.length = length;
        OS.CFStringGetCharacters(cfString, range, chars);
     return new String(chars);
 } finally {
       OS.CFRelease(cfString);
   }
}

protected int[] getTypeIds(){
 return new int[] {URL_ID, URLN_ID};
}

protected String[] getTypeNames(){
  return new String[] {URL, URLN}; 
}

bool checkURL(Object object) {
    return object !is null && ( null !is cast(String)object ) && (cast(String)object).length() > 0;
}

protected bool validate(Object object) {
 return checkURL(object);
}
}