Based on the answer by MadProgrammer, which from a concept basis is spot on and has enabled me to come up with the following. The only difference between the two answers is my support of multiple DataFlavor's so a text/plain
is copied as well as a text/html
flavor. I have also provided an improved HTML markup routine which includes text which is outside a highlight but within the selection.
UPDATE 1: My original answer didn't handle situations where you have nested highlights - the solution can now be used to export any highlighted text within a JTextArea
to any application which supports text/html
or text/plain
.
UPDATE 2: Now added support for all the different classes of DataFlavor's as per MadProgrammer's suggestion. I have also fixed an issue if we have overlapping highlights which start at the same location.
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("&", "&").replace("<", "<").replace(">", ">").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();
}
});
}
}