Source for java.awt.datatransfer.Clipboard

   1: /* Clipboard.java -- Class for transferring data via cut and paste.
   2:    Copyright (C) 1999, 2001, 2005, 2006 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   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)
   9: any later version.
  10: 
  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.
  15: 
  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
  19: 02110-1301 USA.
  20: 
  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
  24: combination.
  25: 
  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. */
  37: 
  38: 
  39: package java.awt.datatransfer;
  40: 
  41: import java.io.IOException;
  42: import java.util.ArrayList;
  43: 
  44: /**
  45:  * This class allows data to be transferred using a cut and paste type
  46:  * mechanism.
  47:  *
  48:  * @author Aaron M. Renn (arenn@urbanophile.com)
  49:  * @author Mark J. Wielaard (mark@klomp.org)
  50:  */
  51: public class Clipboard
  52: {
  53:   /**
  54:    * The data currently on this clipboard.  For use by
  55:    * subclasses. Also returned by the public method getContents().
  56:    */
  57:   protected Transferable contents;
  58: 
  59:   /**
  60:    * The owner of this clipboard.
  61:    */
  62:   protected ClipboardOwner owner;
  63: 
  64:   // The clipboard name
  65:   private final String name;
  66: 
  67:   // The flavor listeners (most likely small).
  68:   private final ArrayList listeners = new ArrayList(3);
  69: 
  70:   /**
  71:    * Initializes a new instance of <code>Clipboard</code> with the
  72:    * specified name.
  73:    *
  74:    * @param name The clipboard name.
  75:    */
  76:   public Clipboard(String name)
  77:   {
  78:     this.name = name;
  79:   }
  80: 
  81:   /**
  82:     * Returns the name of the clipboard.
  83:     */
  84:   public String getName()
  85:   {
  86:     return name;
  87:   }
  88: 
  89:   /**
  90:    * Returns the contents of the clipboard.
  91:    *
  92:    * @param requestor The object requesting the contents. This
  93:    * implementation ignores this parameter.
  94:    *
  95:    * @exception IllegalStateException If the clipboard is currently unavailable
  96:    */
  97:   public synchronized Transferable getContents(Object requestor)
  98:   {
  99:     return contents;
 100:   }
 101: 
 102:   /**
 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
 106:    * clipboard.
 107:    *
 108:    * @param contents The new clipboard contents.
 109:    * @param owner The new clipboard owner
 110:    *
 111:    * @exception IllegalStateException If the clipboard is currently unavailable
 112:    */
 113:   public synchronized void setContents(Transferable contents,
 114:                        ClipboardOwner owner)
 115:   {
 116:     Transferable oldContents = getContents(null);
 117:     this.contents = contents;
 118:     if (this.owner != owner)
 119:       {
 120:     ClipboardOwner oldOwner = this.owner;
 121:     this.owner = owner;
 122:     if (oldOwner != null)
 123:       oldOwner.lostOwnership(this, oldContents);
 124:       }
 125: 
 126:     FlavorListener[] fs = getFlavorListeners();
 127:     if (fs.length > 0)
 128:       {
 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)
 136:       {
 137:         DataFlavor[] df1 = contents.getTransferDataFlavors();
 138:         DataFlavor[] df2 = oldContents.getTransferDataFlavors();
 139:         newFlavors = df1.length != df2.length;
 140:         
 141:         for (int i = 0; !newFlavors && i < df1.length; i++)
 142:           newFlavors = !df1[i].equals(df2[i]);
 143:       }
 144: 
 145:     if (newFlavors)
 146:       {
 147:         FlavorEvent e = new FlavorEvent(this);
 148:         for (int i = 0; i < fs.length; i++)
 149:           fs[i].flavorsChanged(e);
 150:       }
 151:       }
 152:   }
 153: 
 154:   public DataFlavor[] getAvailableDataFlavors()
 155:   {
 156:     Transferable c = getContents(null);
 157:     if (c == null)
 158:       return new DataFlavor[0];
 159:     else
 160:       return c.getTransferDataFlavors();
 161:   }
 162: 
 163:   public boolean isDataFlavorAvailable(DataFlavor flavor)
 164:   {
 165:     DataFlavor[] fs = getAvailableDataFlavors();
 166:     for (int i = 0; i < fs.length; i++)
 167:       if (flavor.equals(fs[i]))
 168:     return true;
 169: 
 170:     return false;
 171:   }
 172: 
 173:   public Object getData(DataFlavor flavor)
 174:     throws UnsupportedFlavorException, IOException
 175:   {
 176:     Transferable c = getContents(null);
 177:     if (c == null)
 178:       throw new UnsupportedFlavorException(flavor);
 179:     else
 180:       return c.getTransferData(flavor);
 181:   }
 182: 
 183:   public void addFlavorListener(FlavorListener listener)
 184:   {
 185:     if (listener == null)
 186:       return;
 187: 
 188:     synchronized(listeners)
 189:       {
 190:     listeners.add(listener);
 191:       }
 192:   }
 193: 
 194:   public void removeFlavorListener(FlavorListener listener)
 195:   {
 196:     if (listener == null)
 197:       return;
 198: 
 199:     synchronized(listeners)
 200:       {
 201:     listeners.remove(listener);
 202:       }
 203:   }
 204: 
 205:   public FlavorListener[] getFlavorListeners()
 206:   {
 207:     synchronized(listeners)
 208:       {
 209:     return (FlavorListener[])
 210:       listeners.toArray(new FlavorListener[listeners.size()]);
 211:       }
 212:   }
 213: }