1 /* Clipboard.java -- Class for transferring data via cut and paste.
2 Copyright (C) 1999, 2001, 2005, 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.awt.datatransfer;
41 import java.io.IOException;
42 import java.util.ArrayList;
45 * This class allows data to be transferred using a cut and paste type
48 * @author Aaron M. Renn (arenn@urbanophile.com)
49 * @author Mark J. Wielaard (mark@klomp.org)
51 public class Clipboard
54 * The data currently on this clipboard. For use by
55 * subclasses. Also returned by the public method getContents().
57 protected Transferable contents;
60 * The owner of this clipboard.
62 protected ClipboardOwner owner;
65 private final String name;
67 // The flavor listeners (most likely small).
68 private final ArrayList listeners = new ArrayList(3);
71 * Initializes a new instance of <code>Clipboard</code> with the
74 * @param name The clipboard name.
76 public Clipboard(String name)
82 * Returns the name of the clipboard.
84 public String getName()
90 * Returns the contents of the clipboard.
92 * @param requestor The object requesting the contents. This
93 * implementation ignores this parameter.
95 * @exception IllegalStateException If the clipboard is currently unavailable
97 public synchronized Transferable getContents(Object requestor)
103 * Sets the content and owner of this clipboard. If the given owner
104 * is different from the current owner then <code>lostOwnership()</code>
105 * is called on the current owner with the old contents of the given
108 * @param contents The new clipboard contents.
109 * @param owner The new clipboard owner
111 * @exception IllegalStateException If the clipboard is currently unavailable
113 public synchronized void setContents(Transferable contents,
114 ClipboardOwner owner)
116 Transferable oldContents = getContents(null);
117 this.contents = contents;
118 if (this.owner != owner)
120 ClipboardOwner oldOwner = this.owner;
122 if (oldOwner != null)
123 oldOwner.lostOwnership(this, oldContents);
126 FlavorListener[] fs = getFlavorListeners();
129 // We are a bit optimistic here. We assume DataFlavors will be
130 // given in the same order. If the number of flavors is
131 // different or the order of the DataFlavors in the list then
132 // fire a change event.
133 boolean newFlavors = ((contents != null && oldContents == null)
134 || (contents == null && oldContents != null));
135 if (!newFlavors && contents != null && oldContents != null)
137 DataFlavor[] df1 = contents.getTransferDataFlavors();
138 DataFlavor[] df2 = oldContents.getTransferDataFlavors();
139 newFlavors = df1.length != df2.length;
141 for (int i = 0; !newFlavors && i < df1.length; i++)
142 newFlavors = !df1[i].equals(df2[i]);
147 FlavorEvent e = new FlavorEvent(this);
148 for (int i = 0; i < fs.length; i++)
149 fs[i].flavorsChanged(e);
154 public DataFlavor[] getAvailableDataFlavors()
156 Transferable c = getContents(null);
158 return new DataFlavor[0];
160 return c.getTransferDataFlavors();
163 public boolean isDataFlavorAvailable(DataFlavor flavor)
165 DataFlavor[] fs = getAvailableDataFlavors();
166 for (int i = 0; i < fs.length; i++)
167 if (flavor.equals(fs[i]))
173 public Object getData(DataFlavor flavor)
174 throws UnsupportedFlavorException, IOException
176 Transferable c = getContents(null);
178 throw new UnsupportedFlavorException(flavor);
180 return c.getTransferData(flavor);
183 public void addFlavorListener(FlavorListener listener)
185 if (listener == null)
188 synchronized(listeners)
190 listeners.add(listener);
194 public void removeFlavorListener(FlavorListener listener)
196 if (listener == null)
199 synchronized(listeners)
201 listeners.remove(listener);
205 public FlavorListener[] getFlavorListeners()
207 synchronized(listeners)
209 return (FlavorListener[])
210 listeners.toArray(new FlavorListener[listeners.size()]);