Eclipse插件:如何输出字符串到控制台

首先定义一个控制台类(继承自MessageConsole),以及一个输出带链接的字符串的帮助类。

 

package com.shansun.easycmd.controls;

import java.io.IOException;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocumentListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsoleManager;
import org.eclipse.ui.console.MessageConsole;
import org.eclipse.ui.console.MessageConsoleStream;

public class MavenConsole extends MessageConsole {

	final public static String MAVEN_CONSOLE_NAME = "Maven Tools";

	int addStrLen;

	static class DocChangeListener implements IDocumentListener {

		int lenBeforeChange;

		int expectedlen;

		final MavenConsole mvnConsole;

		final MavenHyperLink mvnLink;

		public DocChangeListener(MavenConsole console, MavenHyperLink link, int lenWanted) {
			mvnConsole = console;
			mvnLink = link;
			expectedlen = lenWanted;
		}

		public void documentAboutToBeChanged(DocumentEvent event) {}

		public void documentChanged(final DocumentEvent event) {
			int strLenAfterChange = event.getDocument().getLength();
			if (strLenAfterChange >= expectedlen) {
				Job job = new Job("MavenMessageConsole.addLinkToConsole") {
					public IStatus run(IProgressMonitor monitor) {
						try {
							mvnConsole.addHyperlink(mvnLink, expectedlen - mvnLink.getText().length(), mvnLink.getText().length());
						} catch (Exception e) {}

						return Status.OK_STATUS;
					}
				};
				job.schedule();
				event.getDocument().removeDocumentListener(this);
			}
		}
	}

	public MavenConsole() {
		super(MAVEN_CONSOLE_NAME, null);
		addStrLen = 0;
	}

	public void clearConsole() {
		super.clearConsole();
		this.addStrLen = 0;
	}

	public void printToConsole(final String msg, final Color color) {
		final MessageConsoleStream stream = this.newMessageStream();
		this.activate();
		if (color != null) stream.setColor(color);
		addStrLen += msg.length();
		stream.println(msg); // "\n" will take length 1, so use print, not println
		try {
	        stream.flush();
        } catch (IOException e) {
	        e.printStackTrace();
        }
	}

	public void addLinkToConsole(final MavenHyperLink link, Color color) {
		if (link.valid()) {
			try {
				final MavenConsole console = this;
				final int currentStrLen = addStrLen;
				printToConsole(link.getText(), color);
				console.getDocument().addDocumentListener(new DocChangeListener(console, link, currentStrLen + link.getText().length()));

			} catch (Exception e) {}
		}
	}

	public static MavenConsole getMavenConsole() {
		IConsoleManager manager = ConsolePlugin.getDefault().getConsoleManager();
		IConsole[] curConsoles = manager.getConsoles();
		for (IConsole co : curConsoles) {
			if (co.getName().equals(MAVEN_CONSOLE_NAME))
			    return (MavenConsole) co;
		}
		MavenConsole myConsole = new MavenConsole();
		manager.addConsoles(new IConsole[] { myConsole });
		return myConsole;
	}
}

 

package com.shansun.easycmd.controls;

import java.net.URL;

import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.console.IHyperlink;

public class MavenHyperLink implements IHyperlink {

	String text;

	URL url;

	public MavenHyperLink(String text, String urlStr) {
		try {
			this.text = text;
			this.url = new URL(urlStr);
		} catch (Exception e) {
			this.url = null;
		}
	}

	public boolean valid() {
		return this.text != null && this.url != null;
	}

	public String getText() {
		return this.text;
	}

	public void linkActivated() {
		try {
			if (url != null)
			    PlatformUI.getWorkbench().getBrowserSupport().createBrowser("TestConsole").openURL(url);
		} catch (Exception e) {}
	}

	public void linkEntered() {}

	public void linkExited() {}

}

 下面介绍如何在代码中使用这两个类:

private void doMavenInWindows(boolean isDir, String osPath) {
		final Color colorBlue = Display.getDefault().getSystemColor(SWT.COLOR_BLUE);
		Color colorRed = Display.getDefault().getSystemColor(SWT.COLOR_RED);
		Color colorBlack = Display.getDefault().getSystemColor(SWT.COLOR_BLACK);

		final MavenConsole myConsole = MavenConsole.getMavenConsole();
		myConsole.printToConsole(osPath, colorBlue);
		try {
			final String mavenExePath = Activator.getMavenExe();
			if (mavenExePath == null) return;
			try {
				myConsole.clearConsole();
				myConsole.setBackground(colorBlack);
				String cmd = mavenExePath + " clean deploy -Dmaven.test.skip=true";
				Process proc = null;
				if (isDir) {
					proc = Runtime.getRuntime().exec(cmd, null, new File(osPath));
				} else {
					int index1 = osPath.lastIndexOf('/');
					int index2 = osPath.lastIndexOf('\\');
					int index = Math.max(index1, index2);
					String realPath = osPath.substring(0, index);
					proc = Runtime.getRuntime().exec(cmd, null, new File(realPath));
				}
				InputStream is = proc.getInputStream();
				InputStreamReader isr = new InputStreamReader(is, "GBK");
				final BufferedReader br = new BufferedReader(isr);
				myConsole.printToConsole("Maven Tools v1.0, Made By Lanbo.xj", colorRed);
				Job job = new Job("MavenMessageConsole.addLinkToConsole") {
					public IStatus run(IProgressMonitor monitor) {
						String line = null;
						try {
							while ((line = br.readLine()) != null) {
								final String tmpLine = line;
								myConsole.printToConsole(tmpLine, colorBlue);
							}
							return Status.OK_STATUS;
						} catch (IOException e) {
							e.printStackTrace();
						}
						return Status.CANCEL_STATUS;
					}
				};
				job.schedule();
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值