During IDE development, we may often need to preview the Java code (maybe a self-provided Java code template). jdt already provides such support and supports syntax highlighting, it is essentially a packaging of textviewer. It may be as follows:
Javapreview is an abstract class. We can use its subclass org. Eclipse. jdt. Internal. UI. preferences. formatter. compilationunitpreview. directly go to the Code:
/*** Create contents of the dialog. ** @ Param parent */@ overrideprotected control createdialogarea (composite parent) {composite container = (composite) super. createdialogarea (parent); getshell (). settext ("preview Java code"); // create a javapreview instance compilationunitpreview Preview = new compilationunitpreview (javacore. getdefaultoptions (), container); preview. getcontrol (). setlayoutdata (New griddata (griddata. fill_both); string filepath = "src/NET/chenxs/jnibindingstestapp. java "; ifile file = resourcesplugin. getworkspace (). getroot (). getproject ("test "). getFile (filepath); string content = ""; try {content = readstring (file, resourcesplugin. getencoding ();} catch (coreexception e) {e. printstacktrace ();} preview. setpreviewtext (content); // sets the Java code Content preview. update (); // display the formatted content return container ;}