|
Java example source code file (Clipboard.java)
The Clipboard.java Java example source code/* * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package java.awt.datatransfer; import java.awt.EventQueue; import java.util.Set; import java.util.HashSet; import java.util.Arrays; import java.io.IOException; import sun.awt.EventListenerAggregate; /** * A class that implements a mechanism to transfer data using * cut/copy/paste operations. * <p> * {@link FlavorListener}s may be registered on an instance of the * Clipboard class to be notified about changes to the set of * {@link DataFlavor}s available on this clipboard (see * {@link #addFlavorListener}). * * @see java.awt.Toolkit#getSystemClipboard * @see java.awt.Toolkit#getSystemSelection * * @author Amy Fowler * @author Alexander Gerasimov */ public class Clipboard { String name; protected ClipboardOwner owner; protected Transferable contents; /** * An aggregate of flavor listeners registered on this local clipboard. * * @since 1.5 */ private EventListenerAggregate flavorListeners; /** * A set of <code>DataFlavors that is available on * this local clipboard. It is used for tracking changes * of <code>DataFlavors available on this clipboard. * * @since 1.5 */ private Set<DataFlavor> currentDataFlavors; /** * Creates a clipboard object. * * @see java.awt.Toolkit#getSystemClipboard */ public Clipboard(String name) { this.name = name; } /** * Returns the name of this clipboard object. * * @see java.awt.Toolkit#getSystemClipboard */ public String getName() { return name; } /** * Sets the current contents of the clipboard to the specified * transferable object and registers the specified clipboard owner * as the owner of the new contents. * <p> * If there is an existing owner different from the argument * <code>owner, that owner is notified that it no longer * holds ownership of the clipboard contents via an invocation * of <code>ClipboardOwner.lostOwnership() on that owner. * An implementation of <code>setContents() is free not * to invoke <code>lostOwnership() directly from this method. * For example, <code>lostOwnership() may be invoked later on * a different thread. The same applies to <code>FlavorListeners * registered on this clipboard. * <p> * The method throws <code>IllegalStateException if the clipboard * is currently unavailable. For example, on some platforms, the system * clipboard is unavailable while it is accessed by another application. * * @param contents the transferable object representing the * clipboard content * @param owner the object which owns the clipboard content * @throws IllegalStateException if the clipboard is currently unavailable * @see java.awt.Toolkit#getSystemClipboard */ public synchronized void setContents(Transferable contents, ClipboardOwner owner) { final ClipboardOwner oldOwner = this.owner; final Transferable oldContents = this.contents; this.owner = owner; this.contents = contents; if (oldOwner != null && oldOwner != owner) { EventQueue.invokeLater(new Runnable() { public void run() { oldOwner.lostOwnership(Clipboard.this, oldContents); } }); } fireFlavorsChanged(); } /** * Returns a transferable object representing the current contents * of the clipboard. If the clipboard currently has no contents, * it returns <code>null. The parameter Object requestor is * not currently used. The method throws * <code>IllegalStateException if the clipboard is currently * unavailable. For example, on some platforms, the system clipboard is * unavailable while it is accessed by another application. * * @param requestor the object requesting the clip data (not used) * @return the current transferable object on the clipboard * @throws IllegalStateException if the clipboard is currently unavailable * @see java.awt.Toolkit#getSystemClipboard */ public synchronized Transferable getContents(Object requestor) { return contents; } /** * Returns an array of <code>DataFlavors in which the current * contents of this clipboard can be provided. If there are no * <code>DataFlavors available, this method returns a zero-length * array. * * @return an array of <code>DataFlavors in which the current * contents of this clipboard can be provided * * @throws IllegalStateException if this clipboard is currently unavailable * * @since 1.5 */ public DataFlavor[] getAvailableDataFlavors() { Transferable cntnts = getContents(null); if (cntnts == null) { return new DataFlavor[0]; } return cntnts.getTransferDataFlavors(); } /** * Returns whether or not the current contents of this clipboard can be * provided in the specified <code>DataFlavor. * * @param flavor the requested <code>DataFlavor for the contents * * @return <code>true if the current contents of this clipboard * can be provided in the specified <code>DataFlavor; * <code>false otherwise * * @throws NullPointerException if <code>flavor is Other Java examples (source code examples)Here is a short list of links related to this Java Clipboard.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.