Skip to content

Commit

Permalink
Added support for pasting styled comparison text into WordPad.
Browse files Browse the repository at this point in the history
  • Loading branch information
MauveCloud committed Apr 11, 2019
1 parent 73ad434 commit 271c403
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions src/Ic2ExpReactorPlanner/HtmlSelection.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.rtf.RTFEditorKit;

/**
* Represents a clipboard selection of html text, to allow pasting into editors that support it, such as OpenOffice Writer.
Expand All @@ -22,6 +29,7 @@ public class HtmlSelection implements Transferable, ClipboardOwner {

private final DataFlavor htmlFlavor = new DataFlavor("text/html; charset=utf-8", null);
private final DataFlavor plainFlavor = new DataFlavor("text/plain; charset=utf-8", null);
private final DataFlavor rtfFlavor = new DataFlavor("text/rtf", null);

private final String data;

Expand All @@ -33,13 +41,14 @@ public HtmlSelection(final String data) {
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[] {
htmlFlavor,
plainFlavor
plainFlavor,
rtfFlavor
};
}

@Override
public boolean isDataFlavorSupported(final DataFlavor flavor) {
return flavor.equals(htmlFlavor) || flavor.equals(plainFlavor);
return flavor.equals(htmlFlavor) || flavor.equals(plainFlavor) || flavor.equals(rtfFlavor);
}

@Override
Expand All @@ -48,6 +57,8 @@ public Object getTransferData(final DataFlavor flavor) throws UnsupportedFlavorE
return new ByteArrayInputStream(data.getBytes("utf-8"));
} else if (flavor.equals(plainFlavor)) {
return new ByteArrayInputStream(data.replace("<br>", "\n").replaceAll("<[^>]+>", "").getBytes("utf-8"));
} else if (flavor.equals(rtfFlavor)) {
return new ByteArrayInputStream(convertToRTF(data).getBytes("us-ascii"));
} else {
throw new UnsupportedFlavorException(flavor);
}
Expand All @@ -58,4 +69,28 @@ public void lostOwnership(final Clipboard clipboard, final Transferable contents
// no-op
}

// modified from https://stackoverflow.com/questions/2091803/how-to-convert-html-to-rtf-in-java
// only tested to handle html tags expected to be output by the planner's comparison feature.
private static String convertToRTF(final String htmlStr) {

OutputStream os = new ByteArrayOutputStream();
HTMLEditorKit htmlEditorKit = new HTMLEditorKit();
RTFEditorKit rtfEditorKit = new RTFEditorKit();
String rtfStr = null;

String tempStr = htmlStr.replace("</font>", "#END_FONT#").replace("<br>", "#NEW_LINE#");
InputStream is = new ByteArrayInputStream(tempStr.getBytes());
try {
Document doc = htmlEditorKit.createDefaultDocument();
htmlEditorKit.read(is, doc, 0);
rtfEditorKit.write(os, doc, 0, doc.getLength());
rtfStr = os.toString();
rtfStr = rtfStr.replace("#NEW_LINE#", "\\line ");
rtfStr = rtfStr.replace("#END_FONT#", "\\cf0 ");
} catch (IOException | BadLocationException e) {
e.printStackTrace();
}
return rtfStr;
}

}

0 comments on commit 271c403

Please sign in to comment.