001: /*
002: * @(#)QtClipboard.java 1.11 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: * @(#)QtClipboard.java 1.2 02/08/09
029: *
030: */
031:
032: package sun.awt.qt;
033:
034: import java.awt.datatransfer.*;
035:
036: // 6185915.
037: // QtClipboard should not implement ClipboardOwner.
038: // Plus cleanup of clipboard contents management code.
039: public class QtClipboard extends Clipboard {
040:
041: static {
042: initIDs();
043: }
044:
045: QtClipboard(String name) {
046: super (name);
047: if (data == 0) {
048: data = initClipboard();
049: }
050: }
051:
052: private static native void initIDs();
053:
054: private static int data = 0;
055:
056: // private Object pasteRequester;
057:
058: private native int initClipboard();
059:
060: private native void destroyClipboard(int data);
061:
062: private native void setNativeClipboard(String contentsString);
063:
064: private native String getNativeClipboardContents();
065:
066: public synchronized void setContents(Transferable t,
067: ClipboardOwner owner) {
068:
069: super .setContents(t, owner);
070:
071: /* Only strings are copied to native clipboard */
072:
073: if (contents != null
074: && contents
075: .isDataFlavorSupported(DataFlavor.stringFlavor)) {
076: try {
077: String contentsString = (String) contents
078: .getTransferData(DataFlavor.stringFlavor);
079: setNativeClipboard(contentsString);
080: } catch (Exception e) {
081:
082: }
083: }
084: }
085:
086: // 6185915
087: // This function is no longer called from the native layer
088: // because we always call getNativeClipboardContents() from
089: // within getContents().
090: /* Called from native when the requestee returns the data to be
091: pasted. Currently only strings are supported.
092: */
093: private void updateContents(String contentsString) {
094:
095: // This could be the place to add functionality to notify
096: // the clipboard listener(s) that the contents have been
097: // updated.
098:
099: if (contentsString == null) {
100: contents = null;
101: } else {
102: contents = new StringSelection(contentsString);
103: }
104: }
105:
106: // 6185915
107: // The parameter Object requester is not currently used.
108: public synchronized Transferable getContents(Object requester) {
109:
110: // Get the native contents of the clipboard and return them.
111:
112: String contentsString = null;
113:
114: try {
115: contentsString = getNativeClipboardContents();
116: } catch (Exception e) {
117:
118: }
119:
120: updateContents(contentsString);
121: //contents = new StringSelection(contentsString);
122:
123: return contents;
124: }
125:
126: // 6185915
127: // This is called from the native layer.
128: // Use invokeLater() to avoid deadlock.
129: protected void lostOwnership() {
130: java.awt.EventQueue.invokeLater(new Runnable() {
131: public void run() {
132: lostOwnership0();
133: }
134: });
135: }
136:
137: // 6185915
138: /** Owner ship of the Qt clipboard has been lost, some other
139: application will be providing data to be pasted
140: */
141: protected synchronized void lostOwnership0() {
142: if (this .owner != null) {
143: this .owner.lostOwnership(this , this .contents);
144: this .owner = null;
145: }
146:
147: this .contents = null; // Do not know what is in the clipboard yet.
148: }
149:
150: protected void finalize() throws Throwable {
151: destroyClipboard(data);
152: data = 0;
153:
154: super.finalize();
155: }
156:
157: }
|