Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ReplacePasteAction.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  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.actions;
  23. import com.dmdirc.util.LogUtils;
  24. import java.awt.datatransfer.Clipboard;
  25. import java.awt.datatransfer.DataFlavor;
  26. import java.awt.datatransfer.UnsupportedFlavorException;
  27. import java.awt.event.ActionEvent;
  28. import java.io.IOException;
  29. import javax.swing.AbstractAction;
  30. import javax.swing.text.JTextComponent;
  31. import org.slf4j.Logger;
  32. import org.slf4j.LoggerFactory;
  33. /**
  34. * Paste action that replaces matching regexes.
  35. */
  36. public final class ReplacePasteAction extends AbstractAction {
  37. private static final Logger LOG = LoggerFactory.getLogger(ReplacePasteAction.class);
  38. /** A version number for this class. */
  39. private static final long serialVersionUID = 1;
  40. /** Clipboard to handle pasting. */
  41. private final Clipboard clipboard;
  42. /** Regex to match for replacement. */
  43. private final String replacementRegex;
  44. /** Replacement string. */
  45. private final String replacementString;
  46. /**
  47. * Creates a new instance of regex replacement paste action.
  48. *
  49. * @param clipboard Clipboard to handle pasting
  50. * @param replacementRegex Regex to match for replacement
  51. * @param replacementString Replacement string
  52. */
  53. public ReplacePasteAction(final Clipboard clipboard,
  54. final String replacementRegex, final String replacementString) {
  55. super("NoSpacesPasteAction");
  56. this.clipboard = clipboard;
  57. this.replacementRegex = replacementRegex;
  58. this.replacementString = replacementString;
  59. }
  60. @Override
  61. public void actionPerformed(final ActionEvent e) {
  62. if (!(e.getSource() instanceof JTextComponent) ||
  63. !clipboard.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
  64. return;
  65. }
  66. try {
  67. //Get clipboard clipboard contents
  68. //Replace spaces with nothing
  69. //Replace the current selection with the contents of the clipboard
  70. ((JTextComponent) e.getSource()).replaceSelection(
  71. ((String) clipboard.getData(DataFlavor.stringFlavor))
  72. .replaceAll(replacementRegex, replacementString));
  73. } catch (IOException | UnsupportedFlavorException ex) {
  74. LOG.info(LogUtils.USER_ERROR, "Unable to get clipboard contents: {}",
  75. ex.getMessage(), ex);
  76. }
  77. }
  78. }