4632N/A/*
4632N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
4632N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4632N/A *
4632N/A * This code is free software; you can redistribute it and/or modify it
4632N/A * under the terms of the GNU General Public License version 2 only, as
4632N/A * published by the Free Software Foundation. Oracle designates this
4632N/A * particular file as subject to the "Classpath" exception as provided
4632N/A * by Oracle in the LICENSE file that accompanied this code.
4632N/A *
4632N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4632N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4632N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4632N/A * version 2 for more details (a copy is included in the LICENSE file that
4632N/A * accompanied this code).
4632N/A *
4632N/A * You should have received a copy of the GNU General Public License version
4632N/A * 2 along with this work; if not, write to the Free Software Foundation,
4632N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4632N/A *
4632N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4632N/A * or visit www.oracle.com if you need additional information or have any
4632N/A * questions.
4632N/A */
4632N/A
4632N/A#import "sun_lwawt_macosx_CDropTargetContextPeer.h"
4632N/A
4632N/A#import <JavaNativeFoundation/JavaNativeFoundation.h>
4632N/A
4632N/A#import "CDataTransferer.h"
4632N/A#import "CDropTarget.h"
4632N/A#import "DnDUtilities.h"
4632N/A#import "ThreadUtilities.h"
4632N/A
4632N/AJNF_CLASS_CACHE(jc_CDropTargetContextPeer, "sun/lwawt/macosx/CDropTargetContextPeer");
4632N/A
4632N/A
4632N/Astatic void TransferFailed(JNIEnv *env, jobject jthis, jlong jdroptarget, jlong jdroptransfer, jlong jformat) {
4632N/A AWT_ASSERT_NOT_APPKIT_THREAD;
4632N/A JNF_MEMBER_CACHE(transferFailedMethod, jc_CDropTargetContextPeer, "transferFailed", "(J)V");
4632N/A JNFCallVoidMethod(env, jthis, transferFailedMethod, jformat); // AWT_THREADING Safe (!appKit)
4632N/A}
4632N/A
4632N/Astatic CDropTarget* GetCDropTarget(jlong jdroptarget) {
4632N/A CDropTarget* dropTarget = (CDropTarget*) jlong_to_ptr(jdroptarget);
4632N/A
4632N/A // Make sure the drop target is of the right kind:
4632N/A if ([dropTarget isKindOfClass:[CDropTarget class]]) {
4632N/A return dropTarget;
4632N/A }
4632N/A
4632N/A return nil;
4632N/A}
4632N/A
4632N/A
4632N/A/*
4632N/A * Class: sun_lwawt_macosx_CDropTargetContextPeer
4632N/A * Method: startTransfer
4632N/A * Signature: (JJ)J
4632N/A */
4632N/AJNIEXPORT jlong JNICALL Java_sun_lwawt_macosx_CDropTargetContextPeer_startTransfer
4632N/A (JNIEnv *env, jobject jthis, jlong jdroptarget, jlong jformat)
4632N/A{
4632N/A AWT_ASSERT_NOT_APPKIT_THREAD;
4632N/A
4632N/A jlong result = (jlong) 0L;
4632N/A
4632N/A // Currently startTransfer and endTransfer are synchronous since [CDropTarget copyDraggingDataForFormat]
4632N/A // works off a data copy and doesn't have to go to the native event thread to get the data.
4632N/A // We can have endTransfer just call startTransfer.
4632N/A
4632N/AJNF_COCOA_ENTER(env);
4632N/A // Get the drop target native object:
4632N/A CDropTarget* dropTarget = GetCDropTarget(jdroptarget);
4632N/A if (dropTarget == nil) {
4632N/A DLog2(@"[CDropTargetContextPeer startTransfer]: GetCDropTarget failed for %d.\n", (NSInteger) jdroptarget);
4632N/A TransferFailed(env, jthis, jdroptarget, (jlong) 0L, jformat);
4632N/A return result;
4632N/A }
4632N/A
4632N/A JNF_MEMBER_CACHE(newDataMethod, jc_CDropTargetContextPeer, "newData", "(J[B)V");
4639N/A if ((*env)->ExceptionOccurred(env) || !newDataMethod) {
4632N/A DLog2(@"[CDropTargetContextPeer startTransfer]: couldn't get newData method for %d.\n", (NSInteger) jdroptarget);
4632N/A TransferFailed(env, jthis, jdroptarget, (jlong) 0L, jformat);
4632N/A return result;
4632N/A }
4632N/A
4632N/A // Get data from drop target:
4632N/A jobject jdropdata = [dropTarget copyDraggingDataForFormat:jformat];
4632N/A if (!jdropdata) {
4632N/A DLog2(@"[CDropTargetContextPeer startTransfer]: copyDraggingDataForFormat failed for %d.\n", (NSInteger) jdroptarget);
4632N/A TransferFailed(env, jthis, jdroptarget, (jlong) 0L, jformat);
4632N/A return result;
4632N/A }
4632N/A
4632N/A // Pass the data to drop target:
4632N/A @try {
4632N/A JNFCallVoidMethod(env, jthis, newDataMethod, jformat, jdropdata); // AWT_THREADING Safe (!appKit)
4632N/A } @catch (NSException *ex) {
4632N/A DLog2(@"[CDropTargetContextPeer startTransfer]: exception in newData() for %d.\n", (NSInteger) jdroptarget);
4632N/A JNFDeleteGlobalRef(env, jdropdata);
4632N/A TransferFailed(env, jthis, jdroptarget, (jlong) 0L, jformat);
4632N/A return result;
4632N/A }
4632N/A
4632N/A // if no error return dropTarget's draggingSequence
4632N/A result = [dropTarget getDraggingSequenceNumber];
4632N/AJNF_COCOA_EXIT(env);
4632N/A
4632N/A return result;
4632N/A}
4632N/A
4632N/A/*
4632N/A * Class: sun_lwawt_macosx_CDropTargetContextPeer
4632N/A * Method: addTransfer
4632N/A * Signature: (JJJ)V
4632N/A */
4632N/AJNIEXPORT void JNICALL Java_sun_lwawt_macosx_CDropTargetContextPeer_addTransfer
4632N/A (JNIEnv *env, jobject jthis, jlong jdroptarget, jlong jdroptransfer, jlong jformat)
4632N/A{
4632N/A // Currently startTransfer and endTransfer are synchronous since [CDropTarget copyDraggingDataForFormat]
4632N/A // works off a data copy and doesn't have to go to the native event thread to get the data.
4632N/A // We can have endTransfer just call startTransfer.
4632N/A
4632N/A Java_sun_lwawt_macosx_CDropTargetContextPeer_startTransfer(env, jthis, jdroptarget, jformat);
4632N/A
4632N/A return;
4632N/A}
4632N/A
4632N/A/*
4632N/A * Class: sun_lwawt_macosx_CDropTargetContextPeer
4632N/A * Method: dropDone
4632N/A * Signature: (JJZZI)V
4632N/A */
4632N/AJNIEXPORT void JNICALL Java_sun_lwawt_macosx_CDropTargetContextPeer_dropDone
4632N/A (JNIEnv *env, jobject jthis, jlong jdroptarget, jlong jdroptransfer, jboolean jislocal, jboolean jsuccess, jint jdropaction)
4632N/A{
4632N/A // Get the drop target native object:
4632N/AJNF_COCOA_ENTER(env);
4632N/A CDropTarget* dropTarget = GetCDropTarget(jdroptarget);
4632N/A if (dropTarget == nil) {
4632N/A DLog2(@"[CDropTargetContextPeer dropDone]: GetCDropTarget failed for %d.\n", (NSInteger) jdroptarget);
4632N/A return;
4632N/A }
4632N/A
4632N/A // Notify drop target Java is all done with this dragging sequence:
4632N/A [dropTarget javaDraggingEnded:(jlong)jdroptransfer success:jsuccess action:jdropaction];
4632N/AJNF_COCOA_EXIT(env);
4632N/A
4632N/A return;
4632N/A}