Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

StringTransferable.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.ui_swing.dialogs.actioneditor;
  23. import com.dmdirc.logger.ErrorLevel;
  24. import com.dmdirc.logger.Logger;
  25. import java.awt.datatransfer.DataFlavor;
  26. import java.awt.datatransfer.Transferable;
  27. import java.awt.datatransfer.UnsupportedFlavorException;
  28. /**
  29. * String transfer handler.
  30. */
  31. public class StringTransferable implements Transferable {
  32. /** Local tranfer flavour. */
  33. private DataFlavor localStringFlavor;
  34. /** Serial transfer flavour. */
  35. private final DataFlavor serialStringFlavor;
  36. /** Transferred string. */
  37. private final String data;
  38. /**
  39. * Creates a new string transferable object.
  40. *
  41. * @param data String to transger
  42. */
  43. public StringTransferable(final String data) {
  44. super();
  45. this.data = data;
  46. try {
  47. localStringFlavor = new DataFlavor(
  48. DataFlavor.javaJVMLocalObjectMimeType +
  49. ";class=java.lang.String");
  50. } catch (ClassNotFoundException e) {
  51. Logger.userError(ErrorLevel.LOW, "unable to create data flavor: " +
  52. e.getMessage());
  53. }
  54. serialStringFlavor = new DataFlavor(String.class, "String");
  55. }
  56. /** {@inheritDoc} */
  57. @Override
  58. public DataFlavor[] getTransferDataFlavors() {
  59. return new DataFlavor[]{localStringFlavor, serialStringFlavor,};
  60. }
  61. /** {@inheritDoc} */
  62. @Override
  63. public boolean isDataFlavorSupported(final DataFlavor flavor) {
  64. return localStringFlavor.equals(flavor) || serialStringFlavor.equals(flavor);
  65. }
  66. /**
  67. * {@inheritDoc}
  68. *
  69. * @return String to transfer
  70. */
  71. @Override
  72. public Object getTransferData(final DataFlavor flavor) throws UnsupportedFlavorException {
  73. if (!isDataFlavorSupported(flavor)) {
  74. throw new UnsupportedFlavorException(flavor);
  75. }
  76. return data;
  77. }
  78. }