1 /* |
|
2 * Copyright 1996-2003 Sun Microsystems, Inc. All Rights Reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Sun designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Sun in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 * have any questions. |
|
24 */ |
|
25 |
|
26 package sun.awt.motif; |
|
27 |
|
28 import java.awt.datatransfer.ClipboardOwner; |
|
29 import java.awt.datatransfer.Transferable; |
|
30 |
|
31 import java.io.IOException; |
|
32 |
|
33 import java.security.AccessController; |
|
34 |
|
35 import sun.awt.datatransfer.SunClipboard; |
|
36 import sun.awt.datatransfer.TransferableProxy; |
|
37 import sun.awt.datatransfer.DataTransferer; |
|
38 |
|
39 import sun.security.action.GetIntegerAction; |
|
40 |
|
41 |
|
42 /** |
|
43 * A class which interfaces with the X11 selection service in order to support |
|
44 * data transfer via Clipboard operations. Most of the work is provided by |
|
45 * sun.awt.datatransfer.DataTransferer. |
|
46 * |
|
47 * @author Amy Fowler |
|
48 * @author Roger Brinkley |
|
49 * @author Danila Sinopalnikov |
|
50 * @author Alexander Gerasimov |
|
51 * |
|
52 * @since JDK1.1 |
|
53 */ |
|
54 public class X11Clipboard extends SunClipboard implements X11SelectionHolder { |
|
55 |
|
56 private final X11Selection clipboardSelection; |
|
57 |
|
58 private static final Object classLock = new Object(); |
|
59 |
|
60 private static final int defaultPollInterval = 200; |
|
61 |
|
62 private static int pollInterval; |
|
63 |
|
64 private static int listenedClipboardsCount; |
|
65 |
|
66 /** |
|
67 * Creates a system clipboard object. |
|
68 */ |
|
69 public X11Clipboard(String name, String selectionName) { |
|
70 super(name); |
|
71 clipboardSelection = new X11Selection(selectionName, this); |
|
72 } |
|
73 |
|
74 protected void setContentsNative(Transferable contents) { |
|
75 if (!clipboardSelection.getSelectionOwnership(contents, this)) { |
|
76 // Need to figure out how to inform owner the request failed... |
|
77 this.owner = null; |
|
78 this.contents = null; |
|
79 } |
|
80 } |
|
81 |
|
82 public long getID() { |
|
83 return clipboardSelection.atom; |
|
84 } |
|
85 |
|
86 // NOTE: This method may be called by privileged threads. |
|
87 // DO NOT INVOKE CLIENT CODE ON THIS THREAD! |
|
88 public void lostSelectionOwnership() { |
|
89 lostOwnershipImpl(); |
|
90 } |
|
91 |
|
92 protected void clearNativeContext() { |
|
93 clipboardSelection.clearNativeContext(); |
|
94 } |
|
95 |
|
96 protected long[] getClipboardFormats() { |
|
97 return getClipboardFormats(getID()); |
|
98 } |
|
99 private static native long[] getClipboardFormats(long clipboardID); |
|
100 |
|
101 protected byte[] getClipboardData(long format) |
|
102 throws IOException { |
|
103 return getClipboardData(getID(), format); |
|
104 } |
|
105 private static native byte[] getClipboardData(long clipboardID, long format) |
|
106 throws IOException; |
|
107 |
|
108 |
|
109 // Called on the toolkit thread under awtLock. |
|
110 public void checkChange(long[] formats) { |
|
111 if (!clipboardSelection.isOwner()) { |
|
112 super.checkChange(formats); |
|
113 } |
|
114 } |
|
115 |
|
116 void checkChangeHere(Transferable contents) { |
|
117 if (areFlavorListenersRegistered()) { |
|
118 super.checkChange(DataTransferer.getInstance(). |
|
119 getFormatsForTransferableAsArray(contents, flavorMap)); |
|
120 } |
|
121 } |
|
122 |
|
123 protected void registerClipboardViewerChecked() { |
|
124 if (pollInterval <= 0) { |
|
125 pollInterval = ((Integer)AccessController.doPrivileged( |
|
126 new GetIntegerAction("awt.datatransfer.clipboard.poll.interval", |
|
127 defaultPollInterval))).intValue(); |
|
128 if (pollInterval <= 0) { |
|
129 pollInterval = defaultPollInterval; |
|
130 } |
|
131 } |
|
132 synchronized (X11Clipboard.classLock) { |
|
133 if (listenedClipboardsCount++ == 0) { |
|
134 registerClipboardViewer(pollInterval); |
|
135 } |
|
136 } |
|
137 } |
|
138 |
|
139 private native void registerClipboardViewer(int pollInterval); |
|
140 |
|
141 protected void unregisterClipboardViewerChecked() { |
|
142 synchronized (X11Clipboard.classLock) { |
|
143 if (--listenedClipboardsCount == 0) { |
|
144 unregisterClipboardViewer(); |
|
145 } |
|
146 } |
|
147 } |
|
148 |
|
149 private native void unregisterClipboardViewer(); |
|
150 |
|
151 } |
|