001: /*
002: * @(#)GtkClipboard.java 1.12 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package sun.awt.gtk;
029:
030: import java.awt.datatransfer.*;
031:
032: public class GtkClipboard extends Clipboard implements ClipboardOwner {
033: static {
034: initIDs();
035: }
036:
037: GtkClipboard(String name) {
038: super (name);
039: data = createWidget();
040: }
041:
042: private static native void initIDs();
043:
044: private int data;
045:
046: private native int createWidget();
047:
048: private native void destroyWidget(int data);
049:
050: private native void setNativeClipboard(int data);
051:
052: private native void getNativeClipboard(int data);
053:
054: /* Called from native */
055: private byte[] getStringContents() {
056: String str = null;
057: if (contents != null
058: && contents
059: .isDataFlavorSupported(DataFlavor.stringFlavor)) {
060: try {
061: str = (String) contents
062: .getTransferData(DataFlavor.stringFlavor);
063: } catch (Exception e) {
064: }
065: }
066: return str != null ? str.getBytes() : null;
067: }
068:
069: public synchronized void setContents(Transferable t,
070: ClipboardOwner owner) {
071: super .setContents(t, owner);
072:
073: /* We own the clipboard, this is set so we know we have it when returning
074: * the contents */
075:
076: if (owner == null)
077: this .owner = this ;
078:
079: /* Only strings are copied to native clipboard */
080:
081: if (contents != null
082: && contents
083: .isDataFlavorSupported(DataFlavor.stringFlavor))
084: setNativeClipboard(data);
085: }
086:
087: /* Called from native when the requestee returns the data to be pasted.
088: * Currently only strings are supported...
089: */
090:
091: private synchronized void updateContents(byte[] nativeString) {
092: if (nativeString != null) {
093: contents = new StringSelection(new String(nativeString));
094:
095: notifyAll();
096: }
097: }
098:
099: public synchronized Transferable getContents(Object requester) {
100: /* Return the local contents, if there isn't any then check the native
101: * clipboard and try to return this...This returns immediately,
102: * updateContents will be called when the data is ready...
103: */
104:
105: if (owner == null) {
106: getNativeClipboard(data);
107: try {
108: wait(500);
109: } catch (InterruptedException e) {
110: }
111: }
112:
113: return contents;
114: }
115:
116: /** Owner ship of the GTk+ clipboard has been lost, someother applicaiton
117: * will be providing data to be pasted
118: */
119:
120: public synchronized void lostOwnership(Clipboard c, Transferable t) {
121: owner = null; // Do not know who owns the clipboard
122: contents = null; // Do not know what is in the clipboard yet.
123: }
124:
125: protected void finalize() throws Throwable {
126: destroyWidget(data);
127: data = 0;
128: super.finalize();
129: }
130: }
|