jdk/src/solaris/classes/sun/awt/X11/XClipboard.java
author darcy
Thu, 13 Mar 2014 12:40:27 -0700
changeset 23647 41d22f2dbeb5
parent 23010 6dadb192ad81
child 23905 51af43e32270
permissions -rw-r--r--
8033712: Fix more serial lint warnings in sun.awt Reviewed-by: alanb, serb, prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 17892
diff changeset
     2
 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. 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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 439
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 439
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 439
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 439
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 439
diff changeset
    23
 * questions.
2
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;
11271
f10f98b24801 7117011: Reduce number of warnings in sun/awt/windows and sun/awt/datatransfer
denis
parents: 5506
diff changeset
    29
import java.awt.datatransfer.DataFlavor;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.SortedMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.AccessController;
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    33
import java.util.HashMap;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    34
import java.util.Map;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    35
import sun.awt.UNIXToolkit;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.awt.datatransfer.DataTransferer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.awt.datatransfer.SunClipboard;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.awt.datatransfer.ClipboardTransferable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.security.action.GetIntegerAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * A class which interfaces with the X11 selection service in order to support
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * data transfer via Clipboard operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    45
public final class XClipboard extends SunClipboard implements OwnershipListener
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    46
{
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private final XSelection selection;
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    48
    // Time of calling XConvertSelection().
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    49
    private long convertSelectionTime;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    50
    // The flag used not to call XConvertSelection() if the previous SelectionNotify
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    51
    // has not been processed by checkChange().
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    52
    private volatile boolean isSelectionNotifyProcessed;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    53
    // The property in which the owner should place requested targets
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    54
    // when tracking changes of available data flavors (practically targets).
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    55
    private volatile XAtom targetsPropertyAtom;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private static final Object classLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private static final int defaultPollInterval = 200;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private static int pollInterval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    63
    private static Map<Long, XClipboard> targetsAtom2Clipboard;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * Creates a system clipboard object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public XClipboard(String name, String selectionName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        super(name);
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    70
        selection = new XSelection(XAtom.get(selectionName));
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    71
        selection.registerOwershipListener(this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    74
    /*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * NOTE: This method may be called by privileged threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *       DO NOT INVOKE CLIENT CODE ON THIS THREAD!
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    78
    public void ownershipChanged(final boolean isOwner) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    79
        if (isOwner) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    80
            checkChangeHere(contents);
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    81
        } else {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    82
            lostOwnershipImpl();
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    83
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    protected synchronized void setContentsNative(Transferable contents) {
11271
f10f98b24801 7117011: Reduce number of warnings in sun/awt/windows and sun/awt/datatransfer
denis
parents: 5506
diff changeset
    87
        SortedMap<Long,DataFlavor> formatMap =
f10f98b24801 7117011: Reduce number of warnings in sun/awt/windows and sun/awt/datatransfer
denis
parents: 5506
diff changeset
    88
            DataTransferer.getInstance().getFormatsForTransferable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                (contents, DataTransferer.adaptFlavorMap(flavorMap));
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    90
        long[] formats = DataTransferer.keysToLongArray(formatMap);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        if (!selection.setOwner(contents, formatMap, formats,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                                XToolkit.getCurrentServerTime())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            this.owner = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            this.contents = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public long getID() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        return selection.getSelectionAtom().getAtom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   103
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public synchronized Transferable getContents(Object requestor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if (contents != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            return contents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        return new ClipboardTransferable(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /* Caller is synchronized on this. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    protected void clearNativeContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        selection.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    protected long[] getClipboardFormats() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        return selection.getTargets(XToolkit.getCurrentServerTime());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    protected byte[] getClipboardData(long format) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        return selection.getData(format, XToolkit.getCurrentServerTime());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   125
    private void checkChangeHere(Transferable contents) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (areFlavorListenersRegistered()) {
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   127
            checkChange(DataTransferer.getInstance().
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                        getFormatsForTransferableAsArray(contents, flavorMap));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   132
    private static int getPollInterval() {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   133
        synchronized (XClipboard.classLock) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   134
            if (pollInterval <= 0) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   135
                pollInterval = AccessController.doPrivileged(
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   136
                        new GetIntegerAction("awt.datatransfer.clipboard.poll.interval",
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   137
                                             defaultPollInterval));
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   138
                if (pollInterval <= 0) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   139
                    pollInterval = defaultPollInterval;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   140
                }
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
            return pollInterval;
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
    }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   145
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   146
    private XAtom getTargetsPropertyAtom() {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   147
        if (null == targetsPropertyAtom) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   148
            targetsPropertyAtom =
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   149
                    XAtom.get("XAWT_TARGETS_OF_SELECTION:" + selection.getSelectionAtom().getName());
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
        return targetsPropertyAtom;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   152
    }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   153
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    protected void registerClipboardViewerChecked() {
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   155
        // for XConvertSelection() to be called for the first time in getTargetsDelayed()
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   156
        isSelectionNotifyProcessed = true;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   157
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   158
        boolean mustSchedule = false;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   159
        synchronized (XClipboard.classLock) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   160
            if (targetsAtom2Clipboard == null) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   161
                targetsAtom2Clipboard = new HashMap<Long, XClipboard>(2);
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   162
            }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   163
            mustSchedule = targetsAtom2Clipboard.isEmpty();
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   164
            targetsAtom2Clipboard.put(getTargetsPropertyAtom().getAtom(), this);
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   165
            if (mustSchedule) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   166
                XToolkit.addEventDispatcher(XWindow.getXAWTRootWindow().getWindow(),
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   167
                                            new SelectionNotifyHandler());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (mustSchedule) {
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   171
            XToolkit.schedule(new CheckChangeTimerTask(), XClipboard.getPollInterval());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    private static class CheckChangeTimerTask implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        public void run() {
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   177
            for (XClipboard clpbrd : targetsAtom2Clipboard.values()) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   178
                clpbrd.getTargetsDelayed();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            synchronized (XClipboard.classLock) {
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   181
                if (targetsAtom2Clipboard != null && !targetsAtom2Clipboard.isEmpty()) {
17892
b330b424a882 8013424: Regression: java.awt.datatransfer.FlavorListeners not notified on Linux/Java 7
ant
parents: 11271
diff changeset
   182
                    // The viewer is still registered, schedule next poll.
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   183
                    XToolkit.schedule(this, XClipboard.getPollInterval());
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
        }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   187
    }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   188
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   189
    private static class SelectionNotifyHandler implements XEventDispatcher {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   190
        public void dispatchEvent(XEvent ev) {
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   191
            if (ev.get_type() == XConstants.SelectionNotify) {
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   192
                final XSelectionEvent xse = ev.get_xselection();
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   193
                XClipboard clipboard = null;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   194
                synchronized (XClipboard.classLock) {
17892
b330b424a882 8013424: Regression: java.awt.datatransfer.FlavorListeners not notified on Linux/Java 7
ant
parents: 11271
diff changeset
   195
                    if (targetsAtom2Clipboard != null && targetsAtom2Clipboard.isEmpty()) {
b330b424a882 8013424: Regression: java.awt.datatransfer.FlavorListeners not notified on Linux/Java 7
ant
parents: 11271
diff changeset
   196
                        // The viewer was unregistered, remove the dispatcher.
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   197
                        XToolkit.removeEventDispatcher(XWindow.getXAWTRootWindow().getWindow(), this);
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   198
                        return;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   199
                    }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   200
                    final long propertyAtom = xse.get_property();
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   201
                    clipboard = targetsAtom2Clipboard.get(propertyAtom);
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   202
                }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   203
                if (null != clipboard) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   204
                    clipboard.checkChange(xse);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    protected void unregisterClipboardViewerChecked() {
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   211
        isSelectionNotifyProcessed = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        synchronized (XClipboard.classLock) {
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   213
            targetsAtom2Clipboard.remove(getTargetsPropertyAtom().getAtom());
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   214
        }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   215
    }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   216
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   217
    // checkChange() will be called on SelectionNotify
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   218
    private void getTargetsDelayed() {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   219
        XToolkit.awtLock();
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   220
        try {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   221
            long curTime = System.currentTimeMillis();
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   222
            if (isSelectionNotifyProcessed || curTime >= (convertSelectionTime + UNIXToolkit.getDatatransferTimeout()))
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   223
            {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   224
                convertSelectionTime = curTime;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   225
                XlibWrapper.XConvertSelection(XToolkit.getDisplay(),
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   226
                                              selection.getSelectionAtom().getAtom(),
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   227
                                              XDataTransferer.TARGETS_ATOM.getAtom(),
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   228
                                              getTargetsPropertyAtom().getAtom(),
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   229
                                              XWindow.getXAWTRootWindow().getWindow(),
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   230
                                              XConstants.CurrentTime);
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   231
                isSelectionNotifyProcessed = false;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   232
            }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   233
        } finally {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   234
            XToolkit.awtUnlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
117
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
     * Tracks changes of available formats.
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   240
     * NOTE: This method may be called by privileged threads.
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   241
     *       DO NOT INVOKE CLIENT CODE ON THIS THREAD!
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   242
     */
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   243
    private void checkChange(XSelectionEvent xse) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   244
        final long propertyAtom = xse.get_property();
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   245
        if (propertyAtom != getTargetsPropertyAtom().getAtom()) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   246
            // wrong atom
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   247
            return;
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
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   250
        final XAtom selectionAtom = XAtom.get(xse.get_selection());
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   251
        final XSelection changedSelection = XSelection.getSelection(selectionAtom);
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
        if (null == changedSelection || changedSelection != selection) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   254
            // unknown selection - do nothing
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   255
            return;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   256
        }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   257
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   258
        isSelectionNotifyProcessed = true;
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
        if (selection.isOwner()) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   261
            // selection is owner - do not need formats
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   262
            return;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   263
        }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   264
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   265
        long[] formats = null;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   266
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   267
        if (propertyAtom == XConstants.None) {
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   268
            // We treat None property atom as "empty selection".
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   269
            formats = new long[0];
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   270
        } else {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   271
            WindowPropertyGetter targetsGetter =
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   272
                new WindowPropertyGetter(XWindow.getXAWTRootWindow().getWindow(),
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   273
                                         XAtom.get(propertyAtom), 0,
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   274
                                         XSelection.MAX_LENGTH, true,
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   275
                                         XConstants.AnyPropertyType);
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   276
            try {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   277
                targetsGetter.execute();
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   278
                formats = XSelection.getFormats(targetsGetter);
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   279
            } finally {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   280
                targetsGetter.dispose();
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   281
            }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   282
        }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   283
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   284
        checkChange(formats);
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   285
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
}