jdk/src/solaris/classes/sun/awt/X11/XClipboard.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 117 766ae458aaf1
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003-2007 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.SortedMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.awt.datatransfer.DataTransferer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.awt.datatransfer.SunClipboard;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.awt.datatransfer.ClipboardTransferable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import sun.security.action.GetIntegerAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * A class which interfaces with the X11 selection service in order to support
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * data transfer via Clipboard operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
public class XClipboard extends SunClipboard implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private final XSelection selection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static final Object classLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private static final int defaultPollInterval = 200;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private static int pollInterval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private static Set listenedClipboards;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Creates a system clipboard object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public XClipboard(String name, String selectionName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        selection = new XSelection(XAtom.get(selectionName), this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * The action to be run when we lose ownership
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * NOTE: This method may be called by privileged threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     *       DO NOT INVOKE CLIENT CODE ON THIS THREAD!
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        lostOwnershipImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    protected synchronized void setContentsNative(Transferable contents) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        SortedMap formatMap = DataTransferer.getInstance().getFormatsForTransferable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                (contents, DataTransferer.adaptFlavorMap(flavorMap));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        long[] formats =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            DataTransferer.getInstance().keysToLongArray(formatMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        if (!selection.setOwner(contents, formatMap, formats,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                                XToolkit.getCurrentServerTime())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            this.owner = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            this.contents = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public long getID() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        return selection.getSelectionAtom().getAtom();
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 synchronized Transferable getContents(Object requestor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        if (contents != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            return contents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        return new ClipboardTransferable(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /* Caller is synchronized on this. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    protected void clearNativeContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        selection.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    protected long[] getClipboardFormats() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        return selection.getTargets(XToolkit.getCurrentServerTime());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    protected byte[] getClipboardData(long format) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        return selection.getData(format, XToolkit.getCurrentServerTime());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    // Called on the toolkit thread under awtLock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public void checkChange(long[] formats) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (!selection.isOwner()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            super.checkChange(formats);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    void checkChangeHere(Transferable contents) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (areFlavorListenersRegistered()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            super.checkChange(DataTransferer.getInstance().
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    protected void registerClipboardViewerChecked() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (pollInterval <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            pollInterval = ((Integer)AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    new GetIntegerAction("awt.datatransfer.clipboard.poll.interval",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                                         defaultPollInterval))).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            if (pollInterval <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                pollInterval = defaultPollInterval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        selection.initializeSelectionForTrackingChanges();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        boolean mustSchedule = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        synchronized (XClipboard.classLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            if (listenedClipboards == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                listenedClipboards = new HashSet(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            mustSchedule = listenedClipboards.isEmpty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            listenedClipboards.add(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        if (mustSchedule) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            XToolkit.schedule(new CheckChangeTimerTask(), pollInterval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    private static class CheckChangeTimerTask implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            for (Iterator iter = listenedClipboards.iterator(); iter.hasNext();) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                XClipboard clpbrd = (XClipboard)iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                clpbrd.selection.getTargetsDelayed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            synchronized (XClipboard.classLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                if (listenedClipboards != null && !listenedClipboards.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    XToolkit.schedule(this, pollInterval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    protected void unregisterClipboardViewerChecked() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        selection.deinitializeSelectionForTrackingChanges();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        synchronized (XClipboard.classLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            listenedClipboards.remove(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
}