001: /*
002: * Copyright 1996-2003 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.awt.motif;
027:
028: import java.awt.datatransfer.ClipboardOwner;
029: import java.awt.datatransfer.Transferable;
030:
031: import java.io.IOException;
032:
033: import java.security.AccessController;
034:
035: import sun.awt.datatransfer.SunClipboard;
036: import sun.awt.datatransfer.TransferableProxy;
037: import sun.awt.datatransfer.DataTransferer;
038:
039: import sun.security.action.GetIntegerAction;
040:
041: /**
042: * A class which interfaces with the X11 selection service in order to support
043: * data transfer via Clipboard operations. Most of the work is provided by
044: * sun.awt.datatransfer.DataTransferer.
045: *
046: * @author Amy Fowler
047: * @author Roger Brinkley
048: * @author Danila Sinopalnikov
049: * @author Alexander Gerasimov
050: * @version 1.34, 05/05/07
051: *
052: * @since JDK1.1
053: */
054: public class X11Clipboard extends SunClipboard implements
055: X11SelectionHolder {
056:
057: private final X11Selection clipboardSelection;
058:
059: private static final Object classLock = new Object();
060:
061: private static final int defaultPollInterval = 200;
062:
063: private static int pollInterval;
064:
065: private static int listenedClipboardsCount;
066:
067: /**
068: * Creates a system clipboard object.
069: */
070: public X11Clipboard(String name, String selectionName) {
071: super (name);
072: clipboardSelection = new X11Selection(selectionName, this );
073: }
074:
075: protected void setContentsNative(Transferable contents) {
076: if (!clipboardSelection.getSelectionOwnership(contents, this )) {
077: // Need to figure out how to inform owner the request failed...
078: this .owner = null;
079: this .contents = null;
080: }
081: }
082:
083: public long getID() {
084: return clipboardSelection.atom;
085: }
086:
087: // NOTE: This method may be called by privileged threads.
088: // DO NOT INVOKE CLIENT CODE ON THIS THREAD!
089: public void lostSelectionOwnership() {
090: lostOwnershipImpl();
091: }
092:
093: protected void clearNativeContext() {
094: clipboardSelection.clearNativeContext();
095: }
096:
097: protected long[] getClipboardFormats() {
098: return getClipboardFormats(getID());
099: }
100:
101: private static native long[] getClipboardFormats(long clipboardID);
102:
103: protected byte[] getClipboardData(long format) throws IOException {
104: return getClipboardData(getID(), format);
105: }
106:
107: private static native byte[] getClipboardData(long clipboardID,
108: long format) throws IOException;
109:
110: // Called on the toolkit thread under awtLock.
111: public void checkChange(long[] formats) {
112: if (!clipboardSelection.isOwner()) {
113: super .checkChange(formats);
114: }
115: }
116:
117: void checkChangeHere(Transferable contents) {
118: if (areFlavorListenersRegistered()) {
119: super .checkChange(DataTransferer.getInstance()
120: .getFormatsForTransferableAsArray(contents,
121: flavorMap));
122: }
123: }
124:
125: protected void registerClipboardViewerChecked() {
126: if (pollInterval <= 0) {
127: pollInterval = ((Integer) AccessController
128: .doPrivileged(new GetIntegerAction(
129: "awt.datatransfer.clipboard.poll.interval",
130: defaultPollInterval))).intValue();
131: if (pollInterval <= 0) {
132: pollInterval = defaultPollInterval;
133: }
134: }
135: synchronized (X11Clipboard.classLock) {
136: if (listenedClipboardsCount++ == 0) {
137: registerClipboardViewer(pollInterval);
138: }
139: }
140: }
141:
142: private native void registerClipboardViewer(int pollInterval);
143:
144: protected void unregisterClipboardViewerChecked() {
145: synchronized (X11Clipboard.classLock) {
146: if (--listenedClipboardsCount == 0) {
147: unregisterClipboardViewer();
148: }
149: }
150: }
151:
152: private native void unregisterClipboardViewer();
153:
154: }
|