jdk/src/solaris/classes/sun/awt/X11/XClipboard.java
author anthony
Tue, 18 Mar 2008 16:19:03 +0300
changeset 129 f995b9c9c5fa
parent 117 766ae458aaf1
child 439 3488710b02f8
permissions -rw-r--r--
6589527: Window and Frame instances can hide their "Applet Warning" Summary: Additional constraints have been added for the setBounds() operation. Reviewed-by: son, art
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
     2
 * Copyright 2003-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.awt.X11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.datatransfer.Transferable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.SortedMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.AccessController;
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    32
import java.util.HashMap;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    33
import java.util.Map;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    34
import sun.awt.UNIXToolkit;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.awt.datatransfer.DataTransferer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.awt.datatransfer.SunClipboard;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.awt.datatransfer.ClipboardTransferable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.security.action.GetIntegerAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * A class which interfaces with the X11 selection service in order to support
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * data transfer via Clipboard operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    44
public final class XClipboard extends SunClipboard implements OwnershipListener
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    45
{
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private final XSelection selection;
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    47
    // Time of calling XConvertSelection().
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    48
    private long convertSelectionTime;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    49
    // The flag used not to call XConvertSelection() if the previous SelectionNotify
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    50
    // has not been processed by checkChange().
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    51
    private volatile boolean isSelectionNotifyProcessed;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    52
    // The property in which the owner should place requested targets
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    53
    // when tracking changes of available data flavors (practically targets).
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    54
    private volatile XAtom targetsPropertyAtom;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private static final Object classLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private static final int defaultPollInterval = 200;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private static int pollInterval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    62
    private static Map<Long, XClipboard> targetsAtom2Clipboard;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * Creates a system clipboard object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public XClipboard(String name, String selectionName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        super(name);
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    69
        selection = new XSelection(XAtom.get(selectionName));
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    70
        selection.registerOwershipListener(this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    73
    /*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * NOTE: This method may be called by privileged threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     *       DO NOT INVOKE CLIENT CODE ON THIS THREAD!
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    77
    public void ownershipChanged(final boolean isOwner) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    78
        if (isOwner) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    79
            checkChangeHere(contents);
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    80
        } else {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    81
            lostOwnershipImpl();
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    82
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    protected synchronized void setContentsNative(Transferable contents) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        SortedMap formatMap = DataTransferer.getInstance().getFormatsForTransferable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                (contents, DataTransferer.adaptFlavorMap(flavorMap));
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    88
        long[] formats = DataTransferer.keysToLongArray(formatMap);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if (!selection.setOwner(contents, formatMap, formats,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                                XToolkit.getCurrentServerTime())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            this.owner = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            this.contents = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public long getID() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        return selection.getSelectionAtom().getAtom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   101
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public synchronized Transferable getContents(Object requestor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        if (contents != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            return contents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        return new ClipboardTransferable(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /* Caller is synchronized on this. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    protected void clearNativeContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        selection.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    protected long[] getClipboardFormats() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        return selection.getTargets(XToolkit.getCurrentServerTime());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    protected byte[] getClipboardData(long format) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        return selection.getData(format, XToolkit.getCurrentServerTime());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   123
    private void checkChangeHere(Transferable contents) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (areFlavorListenersRegistered()) {
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   125
            checkChange(DataTransferer.getInstance().
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                        getFormatsForTransferableAsArray(contents, flavorMap));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   130
    private static int getPollInterval() {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   131
        synchronized (XClipboard.classLock) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   132
            if (pollInterval <= 0) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   133
                pollInterval = AccessController.doPrivileged(
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   134
                        new GetIntegerAction("awt.datatransfer.clipboard.poll.interval",
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   135
                                             defaultPollInterval));
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   136
                if (pollInterval <= 0) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   137
                    pollInterval = defaultPollInterval;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   138
                }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   139
            }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   140
            return pollInterval;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   141
        }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   142
    }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   143
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   144
    private XAtom getTargetsPropertyAtom() {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   145
        if (null == targetsPropertyAtom) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   146
            targetsPropertyAtom =
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   147
                    XAtom.get("XAWT_TARGETS_OF_SELECTION:" + selection.getSelectionAtom().getName());
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   148
        }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   149
        return targetsPropertyAtom;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   150
    }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   151
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    protected void registerClipboardViewerChecked() {
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   153
        // for XConvertSelection() to be called for the first time in getTargetsDelayed()
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   154
        isSelectionNotifyProcessed = true;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   155
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   156
        boolean mustSchedule = false;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   157
        synchronized (XClipboard.classLock) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   158
            if (targetsAtom2Clipboard == null) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   159
                targetsAtom2Clipboard = new HashMap<Long, XClipboard>(2);
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   160
            }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   161
            mustSchedule = targetsAtom2Clipboard.isEmpty();
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   162
            targetsAtom2Clipboard.put(getTargetsPropertyAtom().getAtom(), this);
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   163
            if (mustSchedule) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   164
                XToolkit.addEventDispatcher(XWindow.getXAWTRootWindow().getWindow(),
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   165
                                            new SelectionNotifyHandler());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (mustSchedule) {
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   169
            XToolkit.schedule(new CheckChangeTimerTask(), XClipboard.getPollInterval());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    private static class CheckChangeTimerTask implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        public void run() {
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   175
            for (XClipboard clpbrd : targetsAtom2Clipboard.values()) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   176
                clpbrd.getTargetsDelayed();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            synchronized (XClipboard.classLock) {
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   179
                if (targetsAtom2Clipboard != null && !targetsAtom2Clipboard.isEmpty()) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   180
                    XToolkit.schedule(this, XClipboard.getPollInterval());
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   181
                }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   182
            }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   183
        }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   184
    }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   185
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   186
    private static class SelectionNotifyHandler implements XEventDispatcher {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   187
        public void dispatchEvent(XEvent ev) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   188
            if (ev.get_type() == XlibWrapper.SelectionNotify) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   189
                final XSelectionEvent xse = ev.get_xselection();
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   190
                XClipboard clipboard = null;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   191
                synchronized (XClipboard.classLock) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   192
                    if (targetsAtom2Clipboard != null && !targetsAtom2Clipboard.isEmpty()) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   193
                        XToolkit.removeEventDispatcher(XWindow.getXAWTRootWindow().getWindow(), this);
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   194
                        return;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   195
                    }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   196
                    final long propertyAtom = xse.get_property();
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   197
                    clipboard = targetsAtom2Clipboard.get(propertyAtom);
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   198
                }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   199
                if (null != clipboard) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   200
                    clipboard.checkChange(xse);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    protected void unregisterClipboardViewerChecked() {
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   207
        isSelectionNotifyProcessed = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        synchronized (XClipboard.classLock) {
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   209
            targetsAtom2Clipboard.remove(getTargetsPropertyAtom().getAtom());
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   210
        }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   211
    }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   212
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   213
    // checkChange() will be called on SelectionNotify
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   214
    private void getTargetsDelayed() {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   215
        XToolkit.awtLock();
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   216
        try {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   217
            long curTime = System.currentTimeMillis();
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   218
            if (isSelectionNotifyProcessed || curTime >= (convertSelectionTime + UNIXToolkit.getDatatransferTimeout()))
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   219
            {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   220
                convertSelectionTime = curTime;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   221
                XlibWrapper.XConvertSelection(XToolkit.getDisplay(),
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   222
                                              selection.getSelectionAtom().getAtom(),
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   223
                                              XDataTransferer.TARGETS_ATOM.getAtom(),
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   224
                                              getTargetsPropertyAtom().getAtom(),
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   225
                                              XWindow.getXAWTRootWindow().getWindow(),
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   226
                                              XlibWrapper.CurrentTime);
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   227
                isSelectionNotifyProcessed = false;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   228
            }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   229
        } finally {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   230
            XToolkit.awtUnlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   234
    /*
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   235
     * Tracks changes of available formats.
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   236
     * NOTE: This method may be called by privileged threads.
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   237
     *       DO NOT INVOKE CLIENT CODE ON THIS THREAD!
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   238
     */
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   239
    private void checkChange(XSelectionEvent xse) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   240
        final long propertyAtom = xse.get_property();
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   241
        if (propertyAtom != getTargetsPropertyAtom().getAtom()) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   242
            // wrong atom
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   243
            return;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   244
        }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   245
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   246
        final XAtom selectionAtom = XAtom.get(xse.get_selection());
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   247
        final XSelection changedSelection = XSelection.getSelection(selectionAtom);
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   248
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   249
        if (null == changedSelection || changedSelection != selection) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   250
            // unknown selection - do nothing
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   251
            return;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   252
        }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   253
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   254
        isSelectionNotifyProcessed = true;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   255
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   256
        if (selection.isOwner()) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   257
            // selection is owner - do not need formats
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   258
            return;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   259
        }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   260
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   261
        long[] formats = null;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   262
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   263
        if (propertyAtom == XlibWrapper.None) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   264
            // We treat None property atom as "empty selection".
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   265
            formats = new long[0];
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   266
        } else {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   267
            WindowPropertyGetter targetsGetter =
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   268
                new WindowPropertyGetter(XWindow.getXAWTRootWindow().getWindow(),
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   269
                                         XAtom.get(propertyAtom), 0,
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   270
                                         XSelection.MAX_LENGTH, true,
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   271
                                         XlibWrapper.AnyPropertyType);
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   272
            try {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   273
                targetsGetter.execute();
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   274
                formats = XSelection.getFormats(targetsGetter);
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   275
            } finally {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   276
                targetsGetter.dispose();
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   277
            }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   278
        }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   279
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   280
        checkChange(formats);
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   281
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
}