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