Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void setupFilter() {
- final TransferHandler originalHandler = getTransferHandler();
- setTransferHandler(new TransferHandler() {
- @Override
- public boolean canImport(JComponent comp, DataFlavor[] transferFlavors) {
- return originalHandler.canImport(comp, transferFlavors);
- }
- @Override
- public boolean canImport(TransferHandler.TransferSupport support) {
- return originalHandler.canImport(support);
- }
- @Override
- public void exportAsDrag(JComponent comp, InputEvent e, int action) {
- originalHandler.exportAsDrag(comp, e, action);
- }
- @Override
- public void exportToClipboard(JComponent comp, Clipboard clip, int action) {
- originalHandler.exportToClipboard(comp, clip, action);
- }
- @Override
- public Image getDragImage() {
- return originalHandler.getDragImage();
- }
- @Override
- public Point getDragImageOffset() {
- return originalHandler.getDragImageOffset();
- }
- @Override
- public int getSourceActions(JComponent c) {
- return originalHandler.getSourceActions(c);
- }
- @Override
- public Icon getVisualRepresentation(Transferable t) {
- return originalHandler.getVisualRepresentation(t);
- }
- @Override
- public boolean importData(JComponent comp, Transferable t) {
- return originalHandler.importData(comp, t);
- }
- @Override
- public boolean importData(TransferHandler.TransferSupport support) {
- Transferable t = support.getTransferable();
- if (t.isDataFlavorSupported(DataFlavor.getTextPlainUnicodeFlavor())) { // This is good
- String data;
- if (t.isDataFlavorSupported(DataFlavor.allHtmlFlavor)) { // Strip this
- try {
- data = (String) t.getTransferData(DataFlavor.allHtmlFlavor);
- if (data.contains("<body>")) {
- data = data.replaceAll("\r", "").replaceAll("\n", "<br />").replaceAll("(.*<body>)(.*)(</body>.*)", "$2");
- }
- data = data.replaceAll("(<[^>]*>)", "");
- } catch (UnsupportedFlavorException | IOException e) {
- return false;
- }
- } else { // Raw text
- try {
- InputStream inputStream = (InputStream) t.getTransferData(DataFlavor.getTextPlainUnicodeFlavor());
- StringBuilder sb = new StringBuilder();
- while (true) {
- int r = inputStream.read();
- if (r < 0) {
- break;
- } else {
- sb.append((char) r);
- inputStream.read();
- }
- }
- data = sb.toString();
- } catch (UnsupportedFlavorException | IOException e) {
- return false;
- }
- }
- data = EmojiParser.parseToHtmlHexadecimal(data);
- try {
- getDocument().insertString(getCaretPosition(), data, null);
- return true;
- } catch (BadLocationException e) {
- return false;
- }
- }
- return false;
- }
- @Override
- public void setDragImage(Image img) {
- originalHandler.setDragImage(img);
- }
- @Override
- public void setDragImageOffset(Point p) {
- originalHandler.setDragImageOffset(p);
- }
- });
- StyledDocument styledDoc = getStyledDocument();
- if (styledDoc instanceof AbstractDocument) {
- AbstractDocument doc = (AbstractDocument) styledDoc;
- doc.setDocumentFilter(myFilter);
- }
- Document doc = getDocument();
- if (doc instanceof AbstractDocument) {
- AbstractDocument pdoc = (AbstractDocument) doc;
- pdoc.setDocumentFilter(myFilter);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement