首先定义一个控制台类(继承自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();
}
}