FAQ How do I write to the console from a plug-in?
2009-10-12 16:14
831 查看
Many of the people asking this question are confused by the fact that
two Eclipse instances are in use when you are developing plug-ins. One is the
development
platform you are using as your IDE, and the other
is the target
platform—also known as the runtime workbench—consisting
of the plug-ins in the development workbench you are testing against. When a
plug-in in the target platform writes a message to System.out
or
System.err
, the message appears in the Console view of the development platform.
This view emulates the Java console that appears when Eclipse runs under Windows
with java.exe
. You should be writing to the console only in this manner
when in debug mode
(see FAQ_How_do_I_use_the_platform_debug_tracing_facility?
).
In some situations however, a plug-in in the development platform has
a legitimate reason to write to the development platform Console view. Some tools
originally designed for the command line, such as Ant and CVS, traditionally use
console output as a way of communicating results to the tool user. When these
tools are ported for use with an IDE, this console output
is typically replaced with richer forms of feedback, such as views, markers, and decorations.
However, users accustomed to the old command-line output may still want to see this
raw output as an alternative to other visual forms of feedback. Tools in this category
can use the Console view to write this output.
Prior to Eclipse 3.0, each plug-in that wanted console-like output created its own
Console view. Eclipse 3.0 provides a single generic Console view that all plug-ins
can write to. The view can host several console documents at once and allows
the user to switch between different console pages. Each page in the console
is represented by an org.eclipse.ui.console.IConsole
object. To write to the console, you
need to create your own IConsole
instance and connect it to the
Console view. For a console containing a simple text document, you can instantiate
a MessageConsole
instance. Here is a method that locates a console
with a given name and creates a new one if it cannot be found:
Once a console is created, you can write to it either by directly modifying its
IDocument
or by opening an output stream on the console. This
snippet opens a stream and writes some text to a console:
Creating a console and writing to it do not create or reveal the Console view.
If you want to make that sure the Console view is visible, you need to reveal it
using the usual workbench API. Even once the Console view is revealed, keep
in mind that it may contain several pages, each representing a different
IConsole
provided by a plug-in. Additional API asks
the Console view to display your console. This snippet reveals
the Console view and asks it to display a particular console instance:
转自http://wiki.eclipse.org/FAQ_How_do_I_write_to_the_console_from_a_plug-in%3F
two Eclipse instances are in use when you are developing plug-ins. One is the
development
platform you are using as your IDE, and the other
is the target
platform—also known as the runtime workbench—consisting
of the plug-ins in the development workbench you are testing against. When a
plug-in in the target platform writes a message to System.out
or
System.err
, the message appears in the Console view of the development platform.
This view emulates the Java console that appears when Eclipse runs under Windows
with java.exe
. You should be writing to the console only in this manner
when in debug mode
(see FAQ_How_do_I_use_the_platform_debug_tracing_facility?
).
In some situations however, a plug-in in the development platform has
a legitimate reason to write to the development platform Console view. Some tools
originally designed for the command line, such as Ant and CVS, traditionally use
console output as a way of communicating results to the tool user. When these
tools are ported for use with an IDE, this console output
is typically replaced with richer forms of feedback, such as views, markers, and decorations.
However, users accustomed to the old command-line output may still want to see this
raw output as an alternative to other visual forms of feedback. Tools in this category
can use the Console view to write this output.
Prior to Eclipse 3.0, each plug-in that wanted console-like output created its own
Console view. Eclipse 3.0 provides a single generic Console view that all plug-ins
can write to. The view can host several console documents at once and allows
the user to switch between different console pages. Each page in the console
is represented by an org.eclipse.ui.console.IConsole
object. To write to the console, you
need to create your own IConsole
instance and connect it to the
Console view. For a console containing a simple text document, you can instantiate
a MessageConsole
instance. Here is a method that locates a console
with a given name and creates a new one if it cannot be found:
private MessageConsole findConsole(String name) { ConsolePlugin plugin = ConsolePlugin.getDefault(); IConsoleManager conMan = plugin.getConsoleManager(); IConsole[] existing = conMan.getConsoles(); for (int i = 0; i < existing.length; i++) if (name.equals(existing[i].getName())) return (MessageConsole) existing[i]; //no console found, so create a new one MessageConsole myConsole = new MessageConsole(name, null); conMan.addConsoles(new IConsole[]{myConsole}); return myConsole; }
Once a console is created, you can write to it either by directly modifying its
IDocument
or by opening an output stream on the console. This
snippet opens a stream and writes some text to a console:
MessageConsole myConsole = findConsole(CONSOLE_NAME); MessageConsoleStream out = myConsole.newMessageStream(); out.println("Hello from Generic console sample action");
Creating a console and writing to it do not create or reveal the Console view.
If you want to make that sure the Console view is visible, you need to reveal it
using the usual workbench API. Even once the Console view is revealed, keep
in mind that it may contain several pages, each representing a different
IConsole
provided by a plug-in. Additional API asks
the Console view to display your console. This snippet reveals
the Console view and asks it to display a particular console instance:
IConsole myConsole = ...;//your console instance IWorkbenchPage page = ...;//obtain the active page String id = IConsoleConstants.ID_CONSOLE_VIEW; IConsoleView view = (IConsoleView) page.showView(id); view.display(myConsole);
转自http://wiki.eclipse.org/FAQ_How_do_I_write_to_the_console_from_a_plug-in%3F
相关文章推荐
- FAQ How do I write to the console from a plug-in?
- In an iOS 5 Storyboard, how do you push a new scene to the original view controller from a Popover?
- How do you copy the contents of an array to a std::vector in C++ without looping? (From stack over flow)
- Problem 17 - How many letters would be needed to write all the numbers in words from 1 to 1000?
- How do I fix the linker error in project upgraded from VS2010 to VS2013 where the linker is looking
- How do I write a LINQ to Entities query which has the equivalent of the SQL “in” keyword?
- how to do graphics in java(quote from big java, 5e)?
- How to convert from the color camera space to the depth camera space in Kinect For Windows
- In Theano, how to do Reverse-MaxPooling in the Convolutional MaxPooling Auto-Encoder
- How to access Dropbox from the command line in Linux
- [连载]Tutorial series: learning how to write a 3D soft engine from scratch in C#, TypeScript or JavaScript[英]
- How do I change the FROM address to a friendly name?
- From a response document, how do you update a field in the parent document?
- How to do the TM symbol in HTML
- How to change the language of oracle sqlplus from chinese to english in windows(如何将oracle sql plus中的中文变成英文版)
- How to avoid Excel from prompting "The file you are trying to open, , is in a different format than specified by the file extension"? (转)
- [quote] How to determine what target is given in Makefile from the make command-line?
- How to remove the //TODO comments from auto-generated code
- How to use Console.WriteLine in ASP.Net (C#) during debug?
- (Django) How do I set limit_choices_to another class from where the ManyToManyField points to? - django, django-models - TechQues.com