Spec-Zone .ru
спецификации, руководства, описания, API
001/*
002 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.  Oracle designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Oracle in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
022 * or visit www.oracle.com if you need additional information or have any
023 * questions.
024 */
025
026package javafx.embed.swt;
027
028import java.nio.ByteBuffer;
029
030import org.eclipse.swt.dnd.ByteArrayTransfer;
031import org.eclipse.swt.dnd.DND;
032import org.eclipse.swt.dnd.TransferData;
033
034public class CustomTransfer extends ByteArrayTransfer {
035    private String name, mime;
036    
037    public CustomTransfer (String name, String mime) {
038        this.name = name;
039        this.mime = mime;
040    }
041    
042    public String getName () {
043        return name;
044    }
045    
046    public String getMime () {
047        return mime;
048    }
049
050    public void javaToNative (Object object, TransferData transferData) {
051        if (!checkCustom(object) || !isSupportedType(transferData)) {
052            DND.error(DND.ERROR_INVALID_DATA);
053        }
054        byte [] bytes = null;
055        if (object instanceof ByteBuffer) {
056            bytes = ((ByteBuffer)object).array();
057        } else {
058            if (object instanceof byte []) bytes = (byte []) object;
059        }
060        if (bytes == null) DND.error(DND.ERROR_INVALID_DATA);
061        super.javaToNative(bytes, transferData);
062    }
063    
064    public Object nativeToJava(TransferData transferData){  
065        if (isSupportedType(transferData)) {
066            return super.nativeToJava(transferData);
067        }
068        return null;
069    }
070    
071    protected String[] getTypeNames(){
072        return new String [] {name};
073    }
074    
075    protected int[] getTypeIds(){
076        return new int [] {registerType(name)};
077    }
078    
079    boolean checkByteArray(Object object) {
080        return (object != null && object instanceof byte[] && ((byte[])object).length > 0);
081    }
082    
083    boolean checkByteBuffer(Object object) {
084        return (object != null && object instanceof ByteBuffer && ((ByteBuffer)object).limit() > 0);
085    }
086    
087    boolean checkCustom(Object object) {
088        return checkByteArray(object) || checkByteBuffer(object);
089    }
090    
091    protected boolean validate(Object object) {
092        return checkCustom(object);
093    }
094}