Wednesday, 15 August 2012

formatting - Formatted cliboard Java -



formatting - Formatted cliboard Java -

i having issue trying figure out how retain formatting of text in java programme when saving scheme clipboard.

it not work things such microsoft's wordpad or lotus symphony. on contrary, if create formatted string in word , re-create it, works trial cases seek paste into.

i not want utilize external sources such org.eclipse.*.

here links have compiled might help me pointed in proper direction.

i sense if not using proper info flavor? http://docs.oracle.com/javase/1.5.0/docs/api/java/awt/datatransfer/dataflavor.html

i found link talks lot dataflavors, not shed much lite on 1 utilize formatted text. understand though might not same on every os , need check create sure supported on os using.

http://www.javaworld.com/cgi-bin/mailto/x_java.cgi?pagetosend=/export/home/httpd/javaworld/javaworld/javatips/jw-javatip61.html&pagename=/javaworld/javatips/jw-javatip61.html&pageurl=http://www.javaworld.com/javaworld/javatips/jw-javatip61.html&site=jw_core

thanks of help in advanced, appreciate it!

dan

edit

i using code from: http://lists.apple.com/archives/java-dev/2004/jul/msg00359.html few little changes. issue having currently, need transmit info clipboard in 2 different formats. "text/rtf" , "text/plain" seeing programs not back upwards rtf. using within clipboard see within clipboard. can place either rtf or plain text, not both simultaneously. when do, lastly 1 gets added. help appreciated!

in summary, cannot set clipboard 2 different info flavors @ same time.

import java.awt.datatransfer.*; import java.io.*; public class clipboard { public static final string rtf_string = "{\\rtf1\\ansi\\deff0 {\\fonttbl {\\f0 courier;}}\r \n{\\colortbl;\\red0\\green0\\blue0;\\red255\\green0\\blue0;}\r\nthis line default color\\line\r\n\\cf2\r\n\\tab line reddish , has tab before it\\line\r\n\\cf1\r\n\\page line default color , first line on page 2\r\n}\r\n"; public static final dataflavor rtf_flavor = new dataflavor("text/rtf", "rich formatted text"); public static void main(string[] args){ clipboard cb = toolkit.getdefaulttoolkit().getsystemclipboard(); transferable t = new mytransferable( new bytearrayinputstream(rtf_string.getbytes()), rtf_flavor); cb.setcontents(t, null); } static class mytransferable implements transferable { private object info = null; private dataflavor flavor; public mytransferable(object o, dataflavor df) { info = o; flavor = df; } public object gettransferdata (dataflavor df) throws unsupportedflavorexception, ioexception { if (!flavor.ismimetypeequal(flavor)) throw new unsupportedflavorexception(df); homecoming data; } public boolean isdataflavorsupported (dataflavor df) { homecoming flavor.ismimetypeequal(df); } public dataflavor[] gettransferdataflavors() { dataflavor[] ret = {flavor}; homecoming ret; } }

}

after much searching around , trial , error , help friend sebastian , logan, seems figured out. allows multiple formats of info saved clip board @ 1 time in java while retaining styling , formatting of text. helps someone. resource. http://www.pindari.com/rtf1.html

import java.awt.*; import java.awt.datatransfer.*; import java.io.*; public class clipboard{ //creates rtf string private static final string rtf_string = "{\\rtf1\\ansi\\deff0\r\n{\\colortbl;\\red0\\green0\\blue0;\\red255\\green0\\blue0;}\r\nthis line default color\\line\r\n\\cf2\r\nthis line red\\line\r\n\\cf1\r\nthis line default color\r\n}\r\n}"; //creates plain text string private static final string plain_string = "this line default color \n line reddish \n line default color"; //array of info specific flavor private static final object data[] = {new bytearrayinputstream(rtf_string.getbytes()),new bytearrayinputstream(plain_string.getbytes())}; //plain favor private static final dataflavor plain_flavor = new dataflavor("text/plain", "plain flavor"); //rtf flavor private static final dataflavor rtf_flavor = new dataflavor("text/rtf", "rich formatted text"); //array of info flavors private static final dataflavor flavors[] = {rtf_flavor,plain_flavor}; public static void main(string[] args){ //create clip board object clipboard cb = toolkit.getdefaulttoolkit().getsystemclipboard(); //create transferable object transferable p = new mytransferable(data,flavors); //transfer clip board cb.setcontents(p, null); } static class mytransferable implements transferable{ //array of info private object dataa[] = null; //array of flavors private dataflavor flavora[] = null; //transferable class constructor public mytransferable(object data[], dataflavor flavors[]){ //set info passed in local variable dataa = data; //set flavors passes in local variable flavora = flavors; } public object gettransferdata (dataflavor df) throws unsupportedflavorexception, ioexception{ //if text/rtf flavor requested if (df.getmimetype().contains("text/rtf")){ //return text/rtf info homecoming dataa[0]; } //if plain flavor requested else if (df.getmimetype().contains("text/plain")){ //return text/plain info homecoming dataa[1]; } else{ throw new unsupportedflavorexception(df); } } public boolean isdataflavorsupported (dataflavor df){ //if flavor text/rtf or tet/plain homecoming true if(df.getmimetype().contains("text/rtf") || df.getmimetype().contains("text/plain")){ homecoming true; } //return false else{ homecoming false; } } public dataflavor[] gettransferdataflavors(){ //return array of flavors homecoming flavora; } } }

java formatting mime-types clipboard copy-paste

No comments:

Post a Comment