将 JTextArea 复制为 "text/html" DataFlavor

Copy JTextArea as "text/html" DataFlavor

我有一个 JTextArea,我正在使用 Highlighter 根据下面的 SSCCE 对我的一些文本应用一些语法突出显示:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;

public class SSCCE extends JFrame {
  public SSCCE() {
    final JTextArea aMain = new JTextArea();
    aMain.setFont(new Font("Consolas", Font.PLAIN, 11));
    aMain.setMargin(new Insets(5, 5, 5, 5));
    aMain.setEditable(false);
    add(aMain);

    aMain.setText("The quick brown fox jumped over the lazy dog.");
    Highlighter h = aMain.getHighlighter();
    try {
      h.addHighlight(10, 15, new DefaultHighlighter.DefaultHighlightPainter(new Color(0xFFC800)));
    }
    catch (BadLocationException e) { 
      e.printStackTrace(); 
    }

    aMain.getActionMap().put("Copy", new AbstractAction() {
      public void actionPerformed(ActionEvent e) {
        aMain.copy();
      }
    });
    aMain.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_C, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), "Copy");

    setTitle("SSCCE");
    setSize(350, 150);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setVisible(true);
  }

  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        new SSCCE();
      }
    });
  }
}

当用户选择文本的一部分并按下 CTRL+C 时,我将调用 JTextArea class 的 copy() 方法。这会将文本作为纯文本复制到系统剪贴板上,我会丢失应用到文本的所有突出显示。我正在寻找将样式信息复制为 "text/html" 或 "text/rtf" 的功能。我相信我需要使用 TransferableDataFlavor classes,但我正在努力将一些东西放在一起 - 我不知道如何从 JTextArea 获取数据以正确的格式放置到剪贴板上。

我主要是尝试复制突出显示,然后将其粘贴到 Microsoft Word 或类似应用程序中,突出显示完好无损。样式数据是否以正确的格式提供,或者我必须通过枚举所有亮点手动构建 HTML 标记?

好吧,基本上,因为荧光笔只是 "painted" 在 JTextArea 上,实际上并没有调整文本的样式,所以你需要自己做这一切

基本上你需要:

  • 获取当前所有亮点的列表
  • 从文档中提取文本
  • 包起来html
  • 创建一个合适的 HTML 基础可转让
  • 将 html 标记复制到剪贴板

简单...

import java.awt.Color;
import java.awt.Font;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringBufferInputStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter;

public class SSCCE extends JFrame {

    public SSCCE() {
        final JTextArea aMain = new JTextArea();
        aMain.setFont(new Font("Consolas", Font.PLAIN, 11));
        aMain.setMargin(new Insets(5, 5, 5, 5));
        aMain.setEditable(false);
        add(aMain);

        aMain.setText("The quick brown fox jumped over the lazy dog.");
        Highlighter h = aMain.getHighlighter();
        try {
            h.addHighlight(10, 15, new DefaultHighlighter.DefaultHighlightPainter(new Color(0xFFC800)));
        } catch (BadLocationException e) {
            e.printStackTrace();
        }

        aMain.getActionMap().put("Copy", new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                Highlighter h = aMain.getHighlighter();
                Highlighter.Highlight[] highlights = h.getHighlights();
                StringBuilder sb = new StringBuilder(64);
                sb.append("<html><body>");
                boolean markedUp = false;
                for (Highlighter.Highlight highlight : highlights) {
                    int start = highlight.getStartOffset();
                    int end = highlight.getEndOffset();

                    try {
                        String text = aMain.getDocument().getText(start, end - start);

                        sb.append("<span style = 'background-color: #FFC800'>");
                        sb.append(text);
                        sb.append("</span>");
                        markedUp = true;
                    } catch (BadLocationException ex) {
                        ex.printStackTrace();
                    }
                }
                sb.append("</body></html>");
                if (markedUp) {
                    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
                    clipboard.setContents(new HtmlSelection(sb.toString()), null);
                }
            }
        });
        aMain.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_C, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), "Copy");

        setTitle("SSCCE");
        setSize(350, 150);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new SSCCE();
            }
        });
    }

    private static class HtmlSelection implements Transferable {

        private static List<DataFlavor> htmlFlavors = new ArrayList<>(3);

        static {

            try {
                htmlFlavors.add(new DataFlavor("text/html;class=java.lang.String"));
                htmlFlavors.add(new DataFlavor("text/html;class=java.io.Reader"));
                htmlFlavors.add(new DataFlavor("text/html;charset=unicode;class=java.io.InputStream"));
            } catch (ClassNotFoundException ex) {
                ex.printStackTrace();
            }

        }

        private String html;

        public HtmlSelection(String html) {
            this.html = html;
        }

        public DataFlavor[] getTransferDataFlavors() {
            return (DataFlavor[]) htmlFlavors.toArray(new DataFlavor[htmlFlavors.size()]);
        }

        public boolean isDataFlavorSupported(DataFlavor flavor) {
            return htmlFlavors.contains(flavor);
        }

        public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
            if (String.class.equals(flavor.getRepresentationClass())) {
                return html;
            } else if (Reader.class.equals(flavor.getRepresentationClass())) {
                return new StringReader(html);
            } else if (InputStream.class.equals(flavor.getRepresentationClass())) {
                return new StringBufferInputStream(html);
            }
            throw new UnsupportedFlavorException(flavor);
        }
    }
}

我已将其粘贴到文本编辑器并在浏览器中打开并粘贴到 Word

以上是根据Copy jTable row with its grid lines into excel/word documents

基于 MadProgrammer 的回答,从概念的角度来看,这个回答是正确的,使我能够提出以下内容。两个答案之间的唯一区别是我支持多个 DataFlavor,因此复制 text/plain 以及 text/html 风格。我还提供了一个改进的 HTML 标记例程,其中包括突出显示之外但在选择范围内的文本。

更新 1:我的原始答案没有处理嵌套高亮的情况 - 该解决方案现在可用于导出 JTextArea 中的任何高亮文本任何支持 text/htmltext/plain.

的应用程序

更新 2:现在根据 MadProgrammer 的建议添加了对 DataFlavor 的所有不同 类 的支持。我还解决了一个问题,如果我们有从同一位置开始的重叠高光。

import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.event.*;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
import javax.swing.*;
import javax.swing.text.*;

@SuppressWarnings("serial")

public class SSCCE extends JFrame {
  public SSCCE() {
    final JTextArea aMain = new JTextArea();
    aMain.setFont(new Font("Consolas", Font.PLAIN, 11));
    aMain.setMargin(new Insets(5, 5, 5, 5));
    aMain.setEditable(false);
    add(aMain);

    aMain.setText("The quick brown fox jumped over the lazy dog.");
    Highlighter h = aMain.getHighlighter();
    try {
      h.addHighlight(10, 15, new DefaultHighlighter.DefaultHighlightPainter(new Color(0xFFC800)));
    }
    catch (BadLocationException e) { 
      e.printStackTrace(); 
    }

    aMain.getActionMap().put("Copy", new AbstractAction() {
      public void actionPerformed(ActionEvent e) {
        Highlighter h = aMain.getHighlighter();
        Highlighter.Highlight[] hls = h.getHighlights();
        int start = aMain.getSelectionStart();
        int end = aMain.getSelectionEnd();
        Document d = aMain.getDocument();

        Arrays.sort(hls, new Comparator<Highlighter.Highlight>() {
          @Override
          public int compare(Highlighter.Highlight a, Highlighter.Highlight b) {
            int r = a.getStartOffset() - b.getStartOffset();
            if (r == 0) {
              r = (b.getEndOffset() - b.getStartOffset()) - (a.getEndOffset() - a.getStartOffset());
            }
            return r;
          }
        });

        try {
          StringBuilder sb = new StringBuilder();
          sb.append("<html><body>");
          sb.append("<pre style='font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 10pt'>");

          String s = d.getText(start, end - start);
          String as[] = s.replaceAll("\r?\n", "\n").split("(?!^)");
          Color ac[] = new Color[as.length];

          for (Highlighter.Highlight hl : hls) { 
            int hs = hl.getStartOffset();
            int he = hl.getEndOffset();

            if ((he > start) && (hs < end)) {
              Color c = ((DefaultHighlighter.DefaultHighlightPainter)hl.getPainter()).getColor();
              if ((c != null) && (aMain.getSelectionColor() != c)) {
                hs = (hs < start) ? start : hs;
                he = (he > end) ? end : he;

                for (int i = (hs - start); i < (he - start); i++) {
                  ac[i] = c;
                }
              }
            }
            else if (hs > end) {
              break;
            }
          }

          Color pc = null;
          for (int i = 0; i < as.length; i++) {
            if (ac[i] != pc) {
              if (pc != null) {
                sb.append("</span>");
              }
              if (ac[i] != null) {
                sb.append("<span style='background-color: " + String.format("#%02x%02x%02x", ac[i].getRed(), ac[i].getGreen(), ac[i].getBlue()) + "'>");
              }
              pc = ac[i];
            }
            sb.append(as[i].replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;").replace("\n", "<br>"));
          }
          if (pc != null) {
            sb.append("</span>");
          }

          sb.append("</pre>");
          sb.append("</body></html>");

          Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
          c.setContents(new MyTransferable(s, sb.toString()), null);
        }
        catch (BadLocationException ex) {
          ex.printStackTrace();
          aMain.copy();
        }
      }
    });
    aMain.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_C, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), "Copy");

    setTitle("SSCCE");
    setSize(350, 150);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setVisible(true);
  }

  private static class MyTransferable implements Transferable {
    private static ArrayList<DataFlavor> MyFlavors = new ArrayList<DataFlavor>();
    private String plain = null;
    private String html = null;

    static {
      try {
        for (String m : new String[]{"text/plain", "text/html"}) {
          MyFlavors.add(new DataFlavor(m + ";class=java.lang.String"));
          MyFlavors.add(new DataFlavor(m + ";class=java.io.Reader"));
          MyFlavors.add(new DataFlavor(m + ";class=java.io.InputStream;charset=utf-8"));
        }
      }
      catch (ClassNotFoundException e) {
        e.printStackTrace();
      }
    }

    public MyTransferable(String plain, String html) {
      this.plain = plain;
      this.html = html;
    }

    public DataFlavor[] getTransferDataFlavors() {
      return MyFlavors.toArray(new DataFlavor[MyFlavors.size()]);
    }

    public boolean isDataFlavorSupported(DataFlavor flavor) {
      return MyFlavors.contains(flavor);
    }

    public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
      String s = null;
      if (flavor.getMimeType().contains("text/plain")) {
        s = plain;
      }
      else if (flavor.getMimeType().contains("text/html")) {
        s = html;
      }
      if (s != null) {
        if (String.class.equals(flavor.getRepresentationClass())) {
          return s;
        }
        else if (Reader.class.equals(flavor.getRepresentationClass())) {
          return new StringReader(s);
        }
        else if (InputStream.class.equals(flavor.getRepresentationClass())) {
          return new ByteArrayInputStream(s.getBytes(StandardCharsets.UTF_8));
        }
      }
      throw new UnsupportedFlavorException(flavor);
    }
  }

  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        new SSCCE();
      }
    });
  }
}