下面列出了怎么用org.eclipse.ui.console.IOConsoleOutputStream的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void setEncoding(String enc) {
if (enc != null) {
// check if the encoding is supported
try {
new String(new byte[] { 'z' }, 0, 1, enc); // LSA80 : �����, �� ������� �����? ���� ����� - �� ������ ��� �� �����
// � �����-������ EncodingUtils � Core?
} catch (UnsupportedEncodingException e) {
enc = null;
}
}
encoding = enc;
synchronized (rgb2streamMap) {
for (IOConsoleOutputStream cs : rgb2streamMap.values()) {
cs.setEncoding(enc);
}
}
}
@Override
public OutputStream getConsoleStream(ColorStreamType cs) {
final RGB rgb = cs.getRgb();
IOConsoleOutputStream stream;
synchronized (rgb2streamMap) {
stream = rgb2streamMap.get(rgb);
if (stream == null) {
stream = newOutputStream();
stream.setEncoding(encoding);
rgb2streamMap.put(rgb, stream);
}
}
final IOConsoleOutputStream streamParam = stream;
Display.getDefault().syncExec(new Runnable(){
@Override
public void run() {
streamParam.setColor(SharedResourceManager.getColor(rgb));
}
});
return stream;
}
private void outItem(ConsoleTextItem it) {
if (!isLogFiltered || !it.filterIt) {
IOConsoleOutputStream str = (IOConsoleOutputStream) getConsoleStream(it.cs);
if (str != null) {
try {
int len = it.line.length() + crlf.length();
if (it.link != null) {
alLinkInfo.add(new LinkInfo(it.link, printedSize.get(), printedSize.get() + len));
}
printedSize.addAndGet(len);
str.write(it.line);
str.write(crlf);
} catch (Exception e) {
LogHelper.logError(e);
}
}
}
}
/**
* applyTheme
*
* @param name
* @param stream
* @param defaultColor
* @return
*/
private void applyTheme(String name, IOConsoleOutputStream stream, Color defaultColor)
{
Theme theme = ThemePlugin.getDefault().getThemeManager().getCurrentTheme();
Color color = defaultColor;
int style = SWT.NONE;
// grab theme values, if they exist
if (theme.hasEntry(name))
{
TextAttribute attr = theme.getTextAttribute(name);
color = theme.getForeground(name);
style = attr.getStyle();
}
// apply new values
stream.setColor(color);
stream.setFontStyle(style);
}
protected void printMessage(String message, Color c, int style) {
if (message != null) {
IOConsoleOutputStream outputStream = getOutputStream();
outputStream.setActivateOnWrite(true);
if (c != null) {
outputStream.setColor(c);
}
outputStream.setFontStyle(style);
try {
outputStream.write(message);
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static IOConsoleOutputStream getConsoleOutputStream(String name, String iconPath) {
synchronized (lock) {
IOConsoleOutputStream outputStream = consoleOutputs.get(name);
if (outputStream == null) {
MessageConsole console = getConsole(name, iconPath);
HashMap<IOConsoleOutputStream, String> themeConsoleStreamToColor = new HashMap<IOConsoleOutputStream, String>();
outputStream = console.newOutputStream();
themeConsoleStreamToColor.put(outputStream, "console.output");
console.setAttribute("themeConsoleStreamToColor", themeConsoleStreamToColor);
ConsoleColorCache.getDefault().keepConsoleColorsSynched(console);
consoles.put(name, console);
consoleOutputs.put(name, outputStream);
}
return outputStream;
}
}
public FileConsoleMonitorThread(String consoleName, File inputFile, IOConsoleOutputStream output) {
this.inputFile = inputFile;
this.output = output;
setPriority(Thread.MIN_PRIORITY + 1);
setDaemon(true);
setName(consoleName + " MonitorThread"); //$NON-NLS-1$
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public void init(IPageBookViewPage page, IConsole console)
{
if (console instanceof TextConsole)
{
TextConsole textConsole = (TextConsole) console;
Object themeConsoleStreamToColor = textConsole.getAttribute(THEME_CONSOLE_STREAM_TO_COLOR_ATTRIBUTE);
if (themeConsoleStreamToColor instanceof Map<?, ?>)
{
Map m = (Map) themeConsoleStreamToColor;
Set<Map.Entry> entrySet = m.entrySet();
for (Map.Entry entry : entrySet)
{
if (!(entry.getKey() instanceof IOConsoleOutputStream) || !(entry.getValue() instanceof String))
{
return; // Cannot handle it.
}
}
this.extension = new ConsoleThemer(textConsole, (Map) themeConsoleStreamToColor);
}
if (page instanceof TextConsolePage)
{
TextConsolePage tcp = (TextConsolePage) page;
TextViewerThemer themer = new TextViewerThemer(tcp.getViewer());
themer.apply();
}
}
this.page = page;
}
/**
* Constructor - Uses the properties from the JyScriptingPreferencesPage to know if we should write to
* the console or not
*
* @param color the color of the output written
*/
public ScriptOutput(ICallback0<IOConsoleOutputStream> outputStream) {
this(outputStream, JyScriptingPreferencesPage.getShowScriptingOutput());
IPropertyChangeListener listener = new IPropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent event) {
writeToConsole = JyScriptingPreferencesPage.getShowScriptingOutput();
}
};
JythonPlugin.getDefault().getPreferenceStore().addPropertyChangeListener(listener);
}
/**
* OutputStream interface
*/
@Override
public void write(int b) throws IOException {
if (writeToConsole) {
IOConsoleOutputStream out = getOutputStream();
out.write(b);
}
}
/**
* Creates a new Python interpreter (with jython) and returns it.
*
* Note that if the sys is not shared, clients should be in a Thread for it to be really separate).
*/
public static IPythonInterpreter newPythonInterpreter(boolean redirect, boolean shareSys) {
setupJython(); //Important: setup the pythonpath for the jython process.
IPythonInterpreter interpreter;
if (shareSys) {
interpreter = new PythonInterpreterWrapper();
} else {
interpreter = new PythonInterpreterWrapperNotShared();
}
if (redirect) {
interpreter.setOut(new ScriptOutput(new ICallback0<IOConsoleOutputStream>() {
@Override
public IOConsoleOutputStream call() {
getConsole(); //Just to make sure it's initialized.
return fOutputStream;
}
}));
interpreter.setErr(new ScriptOutput(new ICallback0<IOConsoleOutputStream>() {
@Override
public IOConsoleOutputStream call() {
getConsole(); //Just to make sure it's initialized.
return fErrorStream;
}
}));
} else {
interpreter.setErr(NullOutputStream.singleton);
interpreter.setOut(NullOutputStream.singleton);
}
return interpreter;
}
@Override
protected int doActionOnResource(IResource next, IProgressMonitor monitor) {
this.refresh = new ArrayList<IContainer>();
AbstractRunner runner = UniversalRunner.getRunner(natureUsed);
if (next instanceof IContainer) {
this.refresh.add((IContainer) next);
} else {
this.refresh.add(next.getParent());
}
String dir = next.getLocation().toOSString();
File workingDir = new File(dir);
if (!workingDir.exists()) {
Log.log("Received file that does not exist for 2to3: " + workingDir);
return 0;
}
if (!workingDir.isDirectory()) {
workingDir = workingDir.getParentFile();
if (!workingDir.isDirectory()) {
Log.log("Unable to find working dir for 2to3. Found invalid: " + workingDir);
return 0;
}
}
ArrayList<String> parametersWithResource = new ArrayList<String>(parameters);
parametersWithResource.add(0, dir);
Tuple<String, String> tup = runner.runCodeAndGetOutput(RUN_2_TO_3_CODE,
parametersWithResource.toArray(new String[0]), workingDir, monitor);
IOConsoleOutputStream out = MessageConsoles.getConsoleOutputStream("2To3", UIConstants.PY_INTERPRETER_ICON);
try {
out.write(tup.o1);
out.write("\n");
out.write(tup.o2);
} catch (IOException e) {
Log.log(e);
}
return 1;
}
@Override
protected IStatus run(IProgressMonitor monitor) {
console.activate();
IOConsoleOutputStream out = console.newOutputStream();
IOConsoleOutputStream err = console.newOutputStream();
Repl repl = new Repl(console.getInputStream(),
new PrintStream(out), new PrintStream(err));
repl.start();
return Status.OK_STATUS;
}
public void setReportingStream(IOConsoleOutputStream stream) {
this.stream = stream;
}
@SuppressWarnings("boxing")
private void reportExtraData(AnalysisData data) {
SortedBugCollection bugCollection = reporter.getBugCollection();
if (bugCollection == null) {
return;
}
if (FindBugsConsole.getConsole() == null) {
return;
}
IOConsoleOutputStream out = FindBugsConsole.getConsole().newOutputStream();
PrintWriter pw = new PrintWriter(out);
ProjectStats stats = bugCollection.getProjectStats();
Footprint footprint = new Footprint(stats.getBaseFootprint());
Profiler profiler = stats.getProfiler();
Profile profile = profiler.getProfile(ClassDataAnalysisEngine.class);
long totalClassReadTime = TimeUnit.MILLISECONDS.convert(profile.getTotalTime(), TimeUnit.NANOSECONDS);
long totalTime = TimeUnit.MILLISECONDS.convert(footprint.getClockTime(), TimeUnit.MILLISECONDS);
double classReadSpeed = totalClassReadTime > 0 ? data.byteSize * 1000.0 / totalClassReadTime : 0;
double classCountSpeed = totalTime > 0 ? data.classCount * 1000.0 / totalTime : 0;
double classPart = totalTime > 0 ? totalClassReadTime * 100.0 / totalTime : 0;
double appPart = data.byteSize > 0 ? data.byteSizeApp * 100.0 / data.byteSize : 0;
double bytesPerClass = data.classCount > 0 ? ((double) data.byteSize) / data.classCount : 0;
long peakMemory = footprint.getPeakMemory() / (1024 * 1024);
pw.printf("%n");
pw.printf("Total bugs : %1$ 20d %n", stats.getTotalBugs());
pw.printf("Peak memory (MB) : %1$ 20d %n", peakMemory);
pw.printf("Total classes : %1$ 20d %n", data.classCount);
pw.printf("Total time (msec) : %1$ 20d %n", totalTime);
pw.printf("Class read time (msec): %1$ 20d %n", totalClassReadTime);
pw.printf("Class read time (%%) : %1$ 20.0f %n", classPart);
pw.printf("Total bytes read : %1$ 20d %n", data.byteSize);
pw.printf("Application bytes : %1$ 20d %n", data.byteSizeApp);
pw.printf("Application bytes (%%) : %1$ 20.0f %n", appPart);
pw.printf("Avg. bytes per class : %1$ 20.0f %n", bytesPerClass);
pw.printf("Analysis class/sec : %1$ 20.0f %n", classCountSpeed);
pw.printf("Read bytes/sec : %1$ 20.0f %n", classReadSpeed);
pw.printf(" MB/sec : %1$ 20.1f %n", classReadSpeed / (1024 * 1024));
pw.flush();
pw.close();
}
public IOConsoleOutputStream getConsoleStream() {
if (console == null)
createConsole();
return console.getOutStream();
}
public IOConsoleOutputStream getOutStream() {
if (outStream == null)
outStream = newOutputStream();
return outStream;
}
/**
* applyTheme
*/
private void applyTheme()
{
IWorkbench workbench = null;
try
{
workbench = PlatformUI.getWorkbench();
}
catch (IllegalStateException e)
{
IdeLog.logError(ThemePlugin.getDefault(), e);
}
if (workbench != null)
{
final Display display = workbench.getDisplay();
display.syncExec(new Runnable()
{
@SuppressWarnings("unchecked")
public void run()
{
// set colors
ThemePlugin plugin = ThemePlugin.getDefault();
ColorManager colorManager = plugin.getColorManager();
Theme theme = plugin.getThemeManager().getCurrentTheme();
// set background color
// NOTE: we have to force the background color to change; otherwise, even
// with a forced redraw, the background will not be drawn
fConsole.setBackground(null);
fConsole.setBackground(colorManager.getColor(theme.getBackground()));
// set default stream colors
// Note that some colors are repeated because they're used in different scenarios.
HashMap<String, Color> colorNameToDefault = new HashMap<String, Color>();
Color blue = display.getSystemColor(SWT.COLOR_DARK_BLUE);
Color green = display.getSystemColor(SWT.COLOR_DARK_GREEN);
Color yellow = display.getSystemColor(SWT.COLOR_DARK_YELLOW);
Color red = display.getSystemColor(SWT.COLOR_DARK_RED);
colorNameToDefault.put(CONSOLE_ERROR, red);
// Info is the same as input color
colorNameToDefault.put(CONSOLE_INFO, green);
colorNameToDefault.put(CONSOLE_INPUT, green);
// For CONSOLE_OUTPUT stream we should use the foreground color of the theme
colorNameToDefault.put(CONSOLE_OUTPUT, colorManager.getColor(theme.getForeground()));
// Prompt is the same as trace color.
colorNameToDefault.put(CONSOLE_PROMPT, blue);
colorNameToDefault.put(CONSOLE_TRACE, blue);
colorNameToDefault.put(CONSOLE_WARNING, yellow);
Set<Map.Entry> entrySet = fThemeConsoleStreamToColor.entrySet();
for (Map.Entry entry : entrySet)
{
if (entry.getValue() instanceof String && entry.getKey() instanceof IOConsoleOutputStream)
{
String colorName = (String) entry.getValue();
IOConsoleOutputStream stream = (IOConsoleOutputStream) entry.getKey();
applyTheme(colorName, stream, colorNameToDefault.get(colorName));
}
}
refresh();
}
});
}
}
public static IOConsoleOutputStream getOutputStream() {
IOConsoleOutputStream outputStream = findConsole().newOutputStream();
outputStream.setEncoding(System.getProperty("sun.jnu.encoding", "UTF-8"));
return outputStream;
}
private IOConsoleOutputStream getOutputStream() {
return newOutputStream();
}
/**
* @return the output stream to use
*/
private IOConsoleOutputStream getOutputStream() throws MalformedURLException {
return out.call();
}
public IOConsoleOutputStreamExt(IOConsoleOutputStream console) {
this.console = assertNotNull(console);
}
public IOConsoleOutputStream console() {
return console;
}
/**
* Constructor - the user is able to define whether he wants to write to the console or not.
*
* @param color the color of the output written
*/
public ScriptOutput(ICallback0<IOConsoleOutputStream> outputStream, boolean writeToConsole) {
this.writeToConsole = writeToConsole;
out = outputStream;
}