8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.awt.datatransfer;
27
28 import java.awt.EventQueue;
29
30 import java.util.Objects;
31 import java.util.Set;
32 import java.util.HashSet;
33 import java.util.Arrays;
34
35 import java.io.IOException;
36
37 /**
38 * A class that implements a mechanism to transfer data using
39 * cut/copy/paste operations.
40 * <p>
41 * {@link FlavorListener}s may be registered on an instance of the
42 * Clipboard class to be notified about changes to the set of
43 * {@link DataFlavor}s available on this clipboard (see
44 * {@link #addFlavorListener}).
45 *
46 * @see java.awt.Toolkit#getSystemClipboard
47 * @see java.awt.Toolkit#getSystemSelection
48 *
113 * registered on this clipboard.
114 * <p>
115 * The method throws <code>IllegalStateException</code> if the clipboard
116 * is currently unavailable. For example, on some platforms, the system
117 * clipboard is unavailable while it is accessed by another application.
118 *
119 * @param contents the transferable object representing the
120 * clipboard content
121 * @param owner the object which owns the clipboard content
122 * @throws IllegalStateException if the clipboard is currently unavailable
123 * @see java.awt.Toolkit#getSystemClipboard
124 */
125 public synchronized void setContents(Transferable contents, ClipboardOwner owner) {
126 final ClipboardOwner oldOwner = this.owner;
127 final Transferable oldContents = this.contents;
128
129 this.owner = owner;
130 this.contents = contents;
131
132 if (oldOwner != null && oldOwner != owner) {
133 EventQueue.invokeLater(() -> oldOwner.lostOwnership(Clipboard.this, oldContents));
134 }
135 fireFlavorsChanged();
136 }
137
138 /**
139 * Returns a transferable object representing the current contents
140 * of the clipboard. If the clipboard currently has no contents,
141 * it returns <code>null</code>. The parameter Object requestor is
142 * not currently used. The method throws
143 * <code>IllegalStateException</code> if the clipboard is currently
144 * unavailable. For example, on some platforms, the system clipboard is
145 * unavailable while it is accessed by another application.
146 *
147 * @param requestor the object requesting the clip data (not used)
148 * @return the current transferable object on the clipboard
149 * @throws IllegalStateException if the clipboard is currently unavailable
150 * @see java.awt.Toolkit#getSystemClipboard
151 */
152 public synchronized Transferable getContents(Object requestor) {
153 return contents;
307 }
308
309 /**
310 * Checks change of the <code>DataFlavor</code>s and, if necessary,
311 * notifies all listeners that have registered interest for notification
312 * on <code>FlavorEvent</code>s.
313 *
314 * @since 1.5
315 */
316 private void fireFlavorsChanged() {
317 if (flavorListeners == null) {
318 return;
319 }
320
321 Set<DataFlavor> prevDataFlavors = currentDataFlavors;
322 currentDataFlavors = getAvailableDataFlavorSet();
323 if (Objects.equals(prevDataFlavors, currentDataFlavors)) {
324 return;
325 }
326 flavorListeners.forEach(listener ->
327 EventQueue.invokeLater(() ->
328 listener.flavorsChanged(new FlavorEvent(Clipboard.this))));
329 }
330
331 /**
332 * Returns a set of <code>DataFlavor</code>s currently available
333 * on this clipboard.
334 *
335 * @return a set of <code>DataFlavor</code>s currently available
336 * on this clipboard
337 *
338 * @since 1.5
339 */
340 private Set<DataFlavor> getAvailableDataFlavorSet() {
341 Set<DataFlavor> set = new HashSet<>();
342 Transferable contents = getContents(null);
343 if (contents != null) {
344 DataFlavor[] flavors = contents.getTransferDataFlavors();
345 if (flavors != null) {
346 set.addAll(Arrays.asList(flavors));
347 }
|
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.awt.datatransfer;
27
28 import sun.datatransfer.DataFlavorUtil;
29
30 import java.util.Objects;
31 import java.util.Set;
32 import java.util.HashSet;
33 import java.util.Arrays;
34
35 import java.io.IOException;
36
37 /**
38 * A class that implements a mechanism to transfer data using
39 * cut/copy/paste operations.
40 * <p>
41 * {@link FlavorListener}s may be registered on an instance of the
42 * Clipboard class to be notified about changes to the set of
43 * {@link DataFlavor}s available on this clipboard (see
44 * {@link #addFlavorListener}).
45 *
46 * @see java.awt.Toolkit#getSystemClipboard
47 * @see java.awt.Toolkit#getSystemSelection
48 *
113 * registered on this clipboard.
114 * <p>
115 * The method throws <code>IllegalStateException</code> if the clipboard
116 * is currently unavailable. For example, on some platforms, the system
117 * clipboard is unavailable while it is accessed by another application.
118 *
119 * @param contents the transferable object representing the
120 * clipboard content
121 * @param owner the object which owns the clipboard content
122 * @throws IllegalStateException if the clipboard is currently unavailable
123 * @see java.awt.Toolkit#getSystemClipboard
124 */
125 public synchronized void setContents(Transferable contents, ClipboardOwner owner) {
126 final ClipboardOwner oldOwner = this.owner;
127 final Transferable oldContents = this.contents;
128
129 this.owner = owner;
130 this.contents = contents;
131
132 if (oldOwner != null && oldOwner != owner) {
133 DataFlavorUtil.getDesktopService().invokeOnEventThread(() ->
134 oldOwner.lostOwnership(Clipboard.this, oldContents));
135 }
136 fireFlavorsChanged();
137 }
138
139 /**
140 * Returns a transferable object representing the current contents
141 * of the clipboard. If the clipboard currently has no contents,
142 * it returns <code>null</code>. The parameter Object requestor is
143 * not currently used. The method throws
144 * <code>IllegalStateException</code> if the clipboard is currently
145 * unavailable. For example, on some platforms, the system clipboard is
146 * unavailable while it is accessed by another application.
147 *
148 * @param requestor the object requesting the clip data (not used)
149 * @return the current transferable object on the clipboard
150 * @throws IllegalStateException if the clipboard is currently unavailable
151 * @see java.awt.Toolkit#getSystemClipboard
152 */
153 public synchronized Transferable getContents(Object requestor) {
154 return contents;
308 }
309
310 /**
311 * Checks change of the <code>DataFlavor</code>s and, if necessary,
312 * notifies all listeners that have registered interest for notification
313 * on <code>FlavorEvent</code>s.
314 *
315 * @since 1.5
316 */
317 private void fireFlavorsChanged() {
318 if (flavorListeners == null) {
319 return;
320 }
321
322 Set<DataFlavor> prevDataFlavors = currentDataFlavors;
323 currentDataFlavors = getAvailableDataFlavorSet();
324 if (Objects.equals(prevDataFlavors, currentDataFlavors)) {
325 return;
326 }
327 flavorListeners.forEach(listener ->
328 DataFlavorUtil.getDesktopService().invokeOnEventThread(() ->
329 listener.flavorsChanged(new FlavorEvent(Clipboard.this))));
330 }
331
332 /**
333 * Returns a set of <code>DataFlavor</code>s currently available
334 * on this clipboard.
335 *
336 * @return a set of <code>DataFlavor</code>s currently available
337 * on this clipboard
338 *
339 * @since 1.5
340 */
341 private Set<DataFlavor> getAvailableDataFlavorSet() {
342 Set<DataFlavor> set = new HashSet<>();
343 Transferable contents = getContents(null);
344 if (contents != null) {
345 DataFlavor[] flavors = contents.getTransferDataFlavors();
346 if (flavors != null) {
347 set.addAll(Arrays.asList(flavors));
348 }
|