JavaPreview类的使用(预览Java代码)

在ide开发中,我们经常可能会需要预览Java代码(可能自己提供的java代码模板),jdt中已经提供了这样的支持,支持语法高亮等,它本质上是对TextViewer的包装。截图可能如下:

JavaPreview是抽象类,我们可以使用其子类 org.eclipse.jdt.internal.ui.preferences.formatter.CompilationUnitPreview.

直接上代码:
/**
	 * Create contents of the dialog.
	 * 
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		getShell().setText("预览Java代码");
		// 创建javapreview实例
		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);// 设置java代码内容
		preview.update();// 显示格式化后内容
		return container;
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值