Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/classes/sun/awt/X11/XDragSourceProtocol.java
32288 views
/*1* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.awt.X11;2627import java.awt.datatransfer.Transferable;2829import java.awt.dnd.DnDConstants;30import java.awt.dnd.InvalidDnDOperationException;3132import java.util.Map;3334/**35* An abstract class for drag protocols on X11 systems.36* Contains protocol-independent drag source code.37*38* @since 1.539*/40abstract class XDragSourceProtocol {41private final XDragSourceProtocolListener listener;4243private boolean initialized = false;4445private long targetWindow = 0;46private long targetProxyWindow = 0;47private int targetProtocolVersion = 0;48private long targetWindowMask = 0;4950// Always use the XAWT root window as the drag source window.51static long getDragSourceWindow() {52return XWindow.getXAWTRootWindow().getWindow();53}5455protected XDragSourceProtocol(XDragSourceProtocolListener listener) {56if (listener == null) {57throw new NullPointerException("Null XDragSourceProtocolListener");58}59this.listener = listener;60}6162protected final XDragSourceProtocolListener getProtocolListener() {63return listener;64}6566/**67* Returns the protocol name. The protocol name cannot be null.68*/69public abstract String getProtocolName();7071/**72* Initializes a drag operation with the specified supported drop actions,73* contents and data formats.74*75* @param actions a bitwise mask of <code>DnDConstants</code> that represent76* the supported drop actions.77* @param contents the contents for the drag operation.78* @param formats an array of Atoms that represent the supported data formats.79* @param formats an array of Atoms that represent the supported data formats.80* @throws InvalidDnDOperationException if a drag operation is already81* initialized.82* @throws IllegalArgumentException if some argument has invalid value.83* @throws XException if some X call failed.84*/85public final void initializeDrag(int actions, Transferable contents,86Map formatMap, long[] formats)87throws InvalidDnDOperationException,88IllegalArgumentException, XException {89XToolkit.awtLock();90try {91try {92if (initialized) {93throw new InvalidDnDOperationException("Already initialized");94}9596initializeDragImpl(actions, contents, formatMap, formats);9798initialized = true;99} finally {100if (!initialized) {101cleanup();102}103}104} finally {105XToolkit.awtUnlock();106}107}108109/* The caller must hold AWT_LOCK. */110protected abstract void initializeDragImpl(int actions,111Transferable contents,112Map formatMap, long[] formats)113throws InvalidDnDOperationException, IllegalArgumentException, XException;114115/**116* Terminates the current drag operation (if any) and resets the internal117* state of this object.118*119* @throws XException if some X call failed.120*/121public void cleanup() {122initialized = false;123cleanupTargetInfo();124}125126/**127* Clears the information on the current drop target.128*129* @throws XException if some X call failed.130*/131public void cleanupTargetInfo() {132targetWindow = 0;133targetProxyWindow = 0;134targetProtocolVersion = 0;135}136137/**138* Processes the specified client message event.139*140* @returns true if the event was successfully processed.141*/142public abstract boolean processClientMessage(XClientMessageEvent xclient)143throws XException;144145/* The caller must hold AWT_LOCK. */146public final boolean attachTargetWindow(long window, long time) {147assert XToolkit.isAWTLockHeldByCurrentThread();148149TargetWindowInfo info = getTargetWindowInfo(window);150if (info == null) {151return false;152} else {153targetWindow = window;154targetProxyWindow = info.getProxyWindow();155targetProtocolVersion = info.getProtocolVersion();156return true;157}158}159160/* The caller must hold AWT_LOCK. */161public abstract TargetWindowInfo getTargetWindowInfo(long window);162163/* The caller must hold AWT_LOCK. */164public abstract void sendEnterMessage(long[] formats, int sourceAction,165int sourceActions, long time);166/* The caller must hold AWT_LOCK. */167public abstract void sendMoveMessage(int xRoot, int yRoot,168int sourceAction, int sourceActions,169long time);170/* The caller must hold AWT_LOCK. */171public abstract void sendLeaveMessage(long time);172173/* The caller must hold AWT_LOCK. */174protected abstract void sendDropMessage(int xRoot, int yRoot,175int sourceAction, int sourceActions,176long time);177178public final void initiateDrop(int xRoot, int yRoot,179int sourceAction, int sourceActions,180long time) {181XWindowAttributes wattr = new XWindowAttributes();182try {183XErrorHandlerUtil.WITH_XERROR_HANDLER(XErrorHandler.IgnoreBadWindowHandler.getInstance());184int status = XlibWrapper.XGetWindowAttributes(XToolkit.getDisplay(),185targetWindow, wattr.pData);186187XErrorHandlerUtil.RESTORE_XERROR_HANDLER();188189if ((status == 0) ||190((XErrorHandlerUtil.saved_error != null) &&191(XErrorHandlerUtil.saved_error.get_error_code() != XConstants.Success))) {192throw new XException("XGetWindowAttributes failed");193}194195targetWindowMask = wattr.get_your_event_mask();196} finally {197wattr.dispose();198}199200XErrorHandlerUtil.WITH_XERROR_HANDLER(XErrorHandler.IgnoreBadWindowHandler.getInstance());201XlibWrapper.XSelectInput(XToolkit.getDisplay(), targetWindow,202targetWindowMask |203XConstants.StructureNotifyMask);204205XErrorHandlerUtil.RESTORE_XERROR_HANDLER();206207if ((XErrorHandlerUtil.saved_error != null) &&208(XErrorHandlerUtil.saved_error.get_error_code() != XConstants.Success)) {209throw new XException("XSelectInput failed");210}211212sendDropMessage(xRoot, yRoot, sourceAction, sourceActions, time);213}214215protected final void finalizeDrop() {216XErrorHandlerUtil.WITH_XERROR_HANDLER(XErrorHandler.IgnoreBadWindowHandler.getInstance());217XlibWrapper.XSelectInput(XToolkit.getDisplay(), targetWindow,218targetWindowMask);219XErrorHandlerUtil.RESTORE_XERROR_HANDLER();220}221222public abstract boolean processProxyModeEvent(XClientMessageEvent xclient,223long sourceWindow);224225protected final long getTargetWindow() {226return targetWindow;227}228229protected final long getTargetProxyWindow() {230if (targetProxyWindow != 0) {231return targetProxyWindow;232} else {233return targetWindow;234}235}236237protected final int getTargetProtocolVersion() {238return targetProtocolVersion;239}240241public static class TargetWindowInfo {242private final long proxyWindow;243private final int protocolVersion;244public TargetWindowInfo(long proxy, int version) {245proxyWindow = proxy;246protocolVersion = version;247}248public long getProxyWindow() {249return proxyWindow;250}251public int getProtocolVersion() {252return protocolVersion;253}254}255}256257258