1 /* |
|
2 * Copyright 1996-2005 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.Toolkit; |
|
29 |
|
30 import java.awt.datatransfer.Transferable; |
|
31 import java.awt.datatransfer.FlavorMap; |
|
32 import java.awt.datatransfer.DataFlavor; |
|
33 import java.awt.datatransfer.SystemFlavorMap; |
|
34 |
|
35 import java.util.Map; |
|
36 import java.util.SortedMap; |
|
37 import java.util.Vector; |
|
38 |
|
39 import sun.awt.AppContext; |
|
40 import sun.awt.SunToolkit; |
|
41 |
|
42 import sun.awt.datatransfer.DataTransferer; |
|
43 |
|
44 /* |
|
45 * Implements a general interface to the X11 selection mechanism. |
|
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 X11Selection { |
|
55 |
|
56 static FlavorMap flavorMap = SystemFlavorMap.getDefaultFlavorMap(); |
|
57 |
|
58 static Vector selections; |
|
59 |
|
60 long atom; |
|
61 |
|
62 private X11Clipboard clipboard; |
|
63 private X11SelectionHolder holder; |
|
64 private Transferable contents; |
|
65 |
|
66 private boolean disposed = false; |
|
67 private byte[] data = null; |
|
68 private boolean dataAvailable = false; |
|
69 private static final Object source = new Object(); |
|
70 |
|
71 static { |
|
72 // 4154170: Need to ensure the the toolkit is initialized prior |
|
73 // to executing this initializer |
|
74 Toolkit toolkit = Toolkit.getDefaultToolkit(); |
|
75 |
|
76 selections = new Vector(); |
|
77 |
|
78 initIDs(); |
|
79 init(); |
|
80 |
|
81 } |
|
82 |
|
83 private static native void initIDs(); |
|
84 static native void init(); |
|
85 |
|
86 public X11Selection(String name, X11Clipboard clipboard) { |
|
87 atom = ((MDataTransferer)DataTransferer.getInstance()).getAtomForTarget(name); |
|
88 selections.addElement(this); |
|
89 this.clipboard = clipboard; |
|
90 } |
|
91 |
|
92 private static Object[] getSelectionsArray() { |
|
93 return selections.toArray(); |
|
94 } |
|
95 |
|
96 /* |
|
97 * methods for acting as selection provider |
|
98 */ |
|
99 native boolean pGetSelectionOwnership(Object source, |
|
100 Transferable transferable, |
|
101 long[] formats, |
|
102 Map formatMap, |
|
103 X11SelectionHolder holder); |
|
104 |
|
105 boolean getSelectionOwnership(Transferable contents, |
|
106 X11SelectionHolder holder) { |
|
107 SortedMap formatMap = |
|
108 DataTransferer.getInstance().getFormatsForTransferable |
|
109 (contents, DataTransferer.adaptFlavorMap(flavorMap)); |
|
110 long[] formats = |
|
111 DataTransferer.getInstance().keysToLongArray(formatMap); |
|
112 SunToolkit.insertTargetMapping(source, AppContext.getAppContext()); |
|
113 |
|
114 /* |
|
115 * Update 'contents' and 'holder' fields in the native code under |
|
116 * AWTLock protection to prevent race with lostSelectionOwnership(). |
|
117 */ |
|
118 SunToolkit.awtLock(); |
|
119 try { |
|
120 boolean isOwnerSet = pGetSelectionOwnership(source, contents, formats, formatMap, |
|
121 holder); |
|
122 if (isOwnerSet) { |
|
123 clipboard.checkChangeHere(contents); |
|
124 } |
|
125 return isOwnerSet; |
|
126 } finally { |
|
127 SunToolkit.awtUnlock(); |
|
128 } |
|
129 } |
|
130 |
|
131 // To be MT-safe this method should be called under awtLock. |
|
132 boolean isOwner() { |
|
133 return holder != null; |
|
134 } |
|
135 |
|
136 // NOTE: This method may be called by privileged threads. |
|
137 // DO NOT INVOKE CLIENT CODE ON THIS THREAD! |
|
138 private void lostSelectionOwnership() { |
|
139 if (holder != null) { |
|
140 holder.lostSelectionOwnership(); |
|
141 holder = null; |
|
142 } |
|
143 contents = null; |
|
144 } |
|
145 |
|
146 native void clearNativeContext(); |
|
147 |
|
148 /* |
|
149 * Subclasses should override disposeImpl() instead of dispose(). Client |
|
150 * code should always invoke dispose(), never disposeImpl(). |
|
151 */ |
|
152 protected void disposeImpl() { |
|
153 selections.removeElement(this); |
|
154 } |
|
155 |
|
156 public final void dispose() { |
|
157 boolean call_disposeImpl = false; |
|
158 |
|
159 if (!disposed) { |
|
160 synchronized (this) { |
|
161 if (!disposed) { |
|
162 disposed = call_disposeImpl = true; |
|
163 } |
|
164 } |
|
165 } |
|
166 |
|
167 if (call_disposeImpl) { |
|
168 disposeImpl(); |
|
169 } |
|
170 } |
|
171 |
|
172 /** |
|
173 * Finds out all selections that have flavor listeners registered |
|
174 * and returns their atoms. |
|
175 * Upcall from native code. |
|
176 * |
|
177 * @return an array of selection atoms |
|
178 */ |
|
179 private static long[] getSelectionAtomsToCheckChange() { |
|
180 Object[] sels = getSelectionsArray(); |
|
181 long[] idArray = new long[sels.length]; |
|
182 int count = 0; |
|
183 |
|
184 for (int i = 0; i < sels.length; i++) { |
|
185 X11Clipboard clipboard = ((X11Selection)sels[i]).clipboard; |
|
186 if (clipboard.areFlavorListenersRegistered()) { |
|
187 idArray[count++] = clipboard.getID(); |
|
188 } |
|
189 } |
|
190 |
|
191 long[] atomArray = new long[count]; |
|
192 System.arraycopy(idArray, 0, atomArray, 0, atomArray.length); |
|
193 |
|
194 return atomArray; |
|
195 } |
|
196 |
|
197 /** |
|
198 * Upcall from native code. |
|
199 */ |
|
200 private void checkChange(long[] formats) { |
|
201 clipboard.checkChange(formats); |
|
202 } |
|
203 } |
|