1. /*
  2. * @(#)StringSelection.java 1.8 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.awt.datatransfer;
  8. import java.io.*;
  9. /**
  10. * A class which implements the capability required to transfer a
  11. * simple java String in plain text format.
  12. */
  13. public class StringSelection implements Transferable, ClipboardOwner {
  14. final static int STRING = 0;
  15. final static int PLAIN_TEXT = 1;
  16. DataFlavor flavors[] = {DataFlavor.stringFlavor, DataFlavor.plainTextFlavor};
  17. private String data;
  18. /**
  19. * Creates a transferable object capable of transferring the
  20. * specified string in plain text format.
  21. */
  22. public StringSelection(String data) {
  23. this.data = data;
  24. }
  25. /**
  26. * Returns the array of flavors in which it can provide the data.
  27. */
  28. public synchronized DataFlavor[] getTransferDataFlavors() {
  29. return flavors;
  30. }
  31. /**
  32. * Returns whether the requested flavor is supported by this object.
  33. * @param flavor the requested flavor for the data
  34. */
  35. public boolean isDataFlavorSupported(DataFlavor flavor) {
  36. return (flavor.equals(flavors[STRING]) || flavor.equals(flavors[PLAIN_TEXT]));
  37. }
  38. /**
  39. * If the data was requested in the "java.lang.String" flavor, return the
  40. * String representing the selection.
  41. *
  42. * @param flavor the requested flavor for the data
  43. * @exception UnsupportedFlavorException if the requested data flavor is
  44. * not supported in the "<code>java.lang.String</code>" flavor.
  45. */
  46. public synchronized Object getTransferData(DataFlavor flavor)
  47. throws UnsupportedFlavorException, IOException {
  48. if (flavor.equals(flavors[STRING])) {
  49. return (Object)data;
  50. } else if (flavor.equals(flavors[PLAIN_TEXT])) {
  51. return new StringReader(data);
  52. } else {
  53. throw new UnsupportedFlavorException(flavor);
  54. }
  55. }
  56. public void lostOwnership(Clipboard clipboard, Transferable contents) {
  57. }
  58. }