java - Copy JTextArea as "text/html" DataFlavor -


i have jtextarea , using highlighter apply syntax highlighting of text per sscce below:

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 on 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();       }     });   } } 

when user selects portion of text , presses ctrl+c calling copy() method of jtextarea class. copies text onto system clipboard plain text , lose highlighting have applied text. looking ability copy style information includes highlights either "text/html" or "text/rtf". believe need use transferable , dataflavor classes, struggling putting - don't know how data jtextarea in correct format place onto clipboard.

i trying copy highlighting , paste microsoft word or similar application highlighting intact. style data available in correct format or manually have construct html markup enumerating highlights?

okay, basically, because highlighter "painted" on jtextarea , doesn't adjust style of text, kind of need yourself

basically need to:

  • get list of current highlights
  • extract text document
  • wrap html
  • create suitable html based transferable
  • copy html markup clipboard

easy...

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 on 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;                 (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);         }     } } 

i've pasted text editor , opened in browser , pasted word

the above based on copy jtable row grid lines excel/word documents


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -