1. /*
  2. * @(#)StringSelection.java 1.18 03/01/23
  3. *
  4. * Copyright 2003 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 <code>Transferable</code> which implements the capability required
  11. * to transfer a <code>String</code>.
  12. *
  13. * This <code>Transferable</code> properly supports
  14. * <code>DataFlavor.stringFlavor</code>
  15. * and all equivalent flavors. Support for
  16. * <code>DataFlavor.plainTextFlavor</code>
  17. * and all equivalent flavors is <b>deprecated</b>. No other
  18. * <code>DataFlavor</code>s are supported.
  19. *
  20. * @see java.awt.datatransfer.DataFlavor#stringFlavor
  21. * @see java.awt.datatransfer.DataFlavor#plainTextFlavor
  22. */
  23. public class StringSelection implements Transferable, ClipboardOwner {
  24. private static final int STRING = 0;
  25. private static final int PLAIN_TEXT = 1;
  26. private static final DataFlavor[] flavors = {
  27. DataFlavor.stringFlavor,
  28. DataFlavor.plainTextFlavor // deprecated
  29. };
  30. private String data;
  31. /**
  32. * Creates a <code>Transferable</code> capable of transferring
  33. * the specified <code>String</code>.
  34. */
  35. public StringSelection(String data) {
  36. this.data = data;
  37. }
  38. /**
  39. * Returns an array of flavors in which this <code>Transferable</code>
  40. * can provide the data. <code>DataFlavor.stringFlavor</code>
  41. * is properly supported.
  42. * Support for <code>DataFlavor.plainTextFlavor</code> is
  43. * <b>deprecated</b>.
  44. *
  45. * @return an array of length two, whose elements are <code>DataFlavor.
  46. * stringFlavor</code> and <code>DataFlavor.plainTextFlavor</code>
  47. */
  48. public DataFlavor[] getTransferDataFlavors() {
  49. // returning flavors itself would allow client code to modify
  50. // our internal behavior
  51. return (DataFlavor[])flavors.clone();
  52. }
  53. /**
  54. * Returns whether the requested flavor is supported by this
  55. * <code>Transferable</code>.
  56. *
  57. * @param flavor the requested flavor for the data
  58. * @return true if <code>flavor</code> is equal to
  59. * <code>DataFlavor.stringFlavor</code> or
  60. * <code>DataFlavor.plainTextFlavor</code> false if <code>flavor</code>
  61. * is not one of the above flavors
  62. * @throws NullPointerException if flavor is <code>null</code>
  63. */
  64. public boolean isDataFlavorSupported(DataFlavor flavor) {
  65. // JCK Test StringSelection0003: if 'flavor' is null, throw NPE
  66. for (int i = 0; i < flavors.length; i++) {
  67. if (flavor.equals(flavors[i])) {
  68. return true;
  69. }
  70. }
  71. return false;
  72. }
  73. /**
  74. * Returns the <code>Transferable</code>'s data in the requested
  75. * <code>DataFlavor</code> if possible. If the desired flavor is
  76. * <code>DataFlavor.stringFlavor</code>, or an equivalent flavor,
  77. * the <code>String</code> representing the selection is
  78. * returned. If the desired flavor is </code>DataFlavor.plainTextFlavor
  79. * </code>, or an equivalent flavor, a <code>Reader</code> is returned.
  80. * <b>Note:<b> The behavior of this method for
  81. * </code>DataFlavor.plainTextFlavor</code>
  82. * and equivalent <code>DataFlavor</code>s is inconsistent with the
  83. * definition of <code>DataFlavor.plainTextFlavor</code>.
  84. *
  85. * @param flavor the requested flavor for the data
  86. * @return the data in the requested flavor, as outlined above
  87. * @throws UnsupportedFlavorException if the requested data flavor is
  88. * not equivalent to either <code>DataFlavor.stringFlavor</code>
  89. * or <code>DataFlavor.plainTextFlavor</code>
  90. * @throws IOException if an IOException occurs while retrieving the data.
  91. * By default, StringSelection never throws this exception, but a
  92. * subclass may.
  93. * @throws NullPointerException if flavor is <code>null</code>
  94. * @see java.io.Reader
  95. */
  96. public Object getTransferData(DataFlavor flavor)
  97. throws UnsupportedFlavorException, IOException
  98. {
  99. // JCK Test StringSelection0007: if 'flavor' is null, throw NPE
  100. if (flavor.equals(flavors[STRING])) {
  101. return (Object)data;
  102. } else if (flavor.equals(flavors[PLAIN_TEXT])) {
  103. return new StringReader(data);
  104. } else {
  105. throw new UnsupportedFlavorException(flavor);
  106. }
  107. }
  108. public void lostOwnership(Clipboard clipboard, Transferable contents) {
  109. }
  110. }