Forum Controls
Spotlight Features

The Rich Engineering Heritage Behind Dependency Injection

Andrew McVeigh takes us on a tour of the rich heritage behind dependency injection, what it represents, and tells us why its here to stay.

Java, the OLPC, and community responsibility

The "One Laptop Per Child" project has a great device ready to ship, but there's no Java on there. Let's think about working together to put Java on OLPC!
Replies: 8 - Pages: 1  
Threads: [ Previous | Next ]
  Click to reply to this thread Reply

Application specific Console

At 11:13 AM on Oct 24, 2005, Venkataramana M DeveloperZone Top 100 wrote:

In addition to my last post regarding printing onto console to debug GEF-code, I would like to share some class which enables to have Application specific console to stream errors, warnings, info etc.
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.text.IDocument;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsoleConstants;
import org.eclipse.ui.console.MessageConsole;
import org.eclipse.ui.console.MessageConsoleStream;
 
/**
 * Create an instance of this class in any of your plugin classes.
 * 
 * Use it as follows ...
 * 
 * ConsoleDisplayMgr.getDefault().println("Some error msg", ConsoleDisplayMgr.MSG_ERROR);
 * ...
 * ...
 * ConsoleDisplayMgr.getDefault().clear();
 * ...  
 */
public class ConsoleDisplayMgr
{
	private static ConsoleDisplayMgr fDefault = null;
	private String fTitle = null;
	private MessageConsole fMessageConsole = null;
	
	public static final int MSG_INFORMATION = 1;
	public static final int MSG_ERROR = 2;
	public static final int MSG_WARNING = 3;
		
	public ConsoleDisplayMgr(String messageTitle)
	{		
		fDefault = this;
		fTitle = messageTitle;
	}
	
	public static ConsoleDisplayMgr getDefault() {
		return fDefault;
	}	
		
	public void println(String msg, int msgKind)
	{		
		if( msg == null ) return;
		
		/* if console-view in Java-perspective is not active, then show it and
		 * then display the message in the console attached to it */		
		if( !displayConsoleView() )
		{
			/*If an exception occurs while displaying in the console, then just diplay atleast the same in a message-box */
			MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "Error", msg);
			return;
		}
		
		/* display message on console */	
		getNewMessageConsoleStream(msgKind).println(msg);				
	}
	
	public void clear()
	{		
		IDocument document = getMessageConsole().getDocument();
		if (document != null) {
			document.set("");
		}			
	}	
		
	private boolean displayConsoleView()
	{
		try
		{
			IWorkbenchWindow activeWorkbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
			if( activeWorkbenchWindow != null )
			{
				IWorkbenchPage activePage = activeWorkbenchWindow.getActivePage();
				if( activePage != null )
					activePage.showView(IConsoleConstants.ID_CONSOLE_VIEW, null, IWorkbenchPage.VIEW_VISIBLE);
			}
			
		} catch (PartInitException partEx) {			
			return false;
		}
		
		return true;
	}
	
	private MessageConsoleStream getNewMessageConsoleStream(int msgKind)
	{		
		int swtColorId = SWT.COLOR_DARK_GREEN;
		
		switch (msgKind)
		{
			case MSG_INFORMATION:
				swtColorId = SWT.COLOR_DARK_GREEN;				
				break;
			case MSG_ERROR:
				swtColorId = SWT.COLOR_DARK_MAGENTA;
				break;
			case MSG_WARNING:
				swtColorId = SWT.COLOR_DARK_BLUE;
				break;
			default:				
		}	
		
		MessageConsoleStream msgConsoleStream = getMessageConsole().newMessageStream();		
		msgConsoleStream.setColor(Display.getCurrent().getSystemColor(swtColorId));
		return msgConsoleStream;
	}
	
	private MessageConsole getMessageConsole()
	{
		if( fMessageConsole == null )
			createMessageConsoleStream(fTitle);	
		
		return fMessageConsole;
	}
		
	private void createMessageConsoleStream(String title)
	{
		fMessageConsole = new MessageConsole(title, null); 
		ConsolePlugin.getDefault().getConsoleManager().addConsoles(new IConsole[]{ fMessageConsole });
	}	
}
 


Just in case if anyone is in need of such a facility.
Eclipse debug plugin makes use of console facilities from org.eclipse.ui.console
  Click to reply to this thread Reply
1. At 5:21 PM on Oct 24, 2005, Haris Peco DeveloperZone Top 100 wrote:

Re: Application specific Console

Is method clear necessary ? - it force usage org.eclipse.jface.text plugin and it is potential problem in rcp application ?
Console view have clear button and i think that isn't necessary.
  Click to reply to this thread Reply
2. At 6:04 AM on Oct 25, 2005, Venkataramana M DeveloperZone Top 100 wrote:

Re: Application specific Console

Haris, Many a times some users may demand the console to be cleared programmatically on start of every action to make sure the console-output is only related to the last action. So for that reason clear() method has been exposed. I am not aware of problems using org.eclipse.jface.text in RCP apps. Anyways it is upto the users of ConsoleDisplayMgr whether to have clear or not. As far as non-RCP apps in Eclipse 3.0, that is working quite fine, no problems reported as yet.

Thanks
Venkat
~Venkat + plus magazine ... living mathematics
  Click to reply to this thread Reply
3. At 7:10 AM on Feb 6, 2006, Niraimathi R Javalobby Newcomers wrote:

Re: Application specific Console

hi ,
i am trying to open open the console in my plugin project..i dont have any specific perspective however i am trying open in java perspective only.i used your code but no luck..please guide me to open the console.

Note: i am using eclipse 3.1
  Click to reply to this thread Reply
4. At 3:16 AM on Apr 14, 2006, Mandeep Javalobby Newcomers wrote:

Re: Application specific Console

Hi,

I want to know a way by which I can get the console message loged into a file. Actually to be more specific I want that message of console when the project starts building in eclipse and displays which all files have been compiled.
Or give an idea how can i get an error message (Again in some file) which is displayed on console, if there is any error in the project ,during building of project .


I will be very thankfull .
Thanks
Mandeep
  Click to reply to this thread Reply
5. At 3:32 AM on Apr 14, 2006, Mandeep Javalobby Newcomers wrote:

Re: Application specific Console

Hi,

Although i am just a beginner but i think this is easy task. Try implementing the following code in your plugin wherever you want to highlight the console or open console.

/**************************
runInUIThread(new Runnable() {public void run() {
try{
IViewPart consoleView = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView("org.eclipse.ui.console.ConsoleView");
}catch(Exception e){System.out.println("Console Not Highlighted "+e);
}
}
});
***********************/

Do let me know if you are done with the problem .


Thanks
Mandeep.
  Click to reply to this thread Reply
6. At 8:05 PM on Apr 14, 2006, Venkataramana M DeveloperZone Top 100 wrote:

Re: Application specific Console

>> I want to know a way by which I can get the console
>> message loged into a file. Actually to be more specific I
>> want that message of console when the project starts
>> building in eclipse and displays which all files have
>> been compiled.

First of all, I am not aware of Eclipse-based build writing onto console. Probably you have some external builder. Anyways, let me give some idea of whereall console is used in Eclipse by default... (I am aware of Eclipse 3.0 only)

1. As CONSOLE the term specifies is usually used for STDOUT or STDERR of an application. The CONSOLE-VIEW provided in JAVA-PERSPECTIVE is used only for showing STDERR/STDOUT of java-applications/plugin-applications that are built using Eclipse-based Java Developement Environment. The view is not a generic view where any application can write anything as far as I know, but can be used as a generic view. As you might have observed that when you try to RUN/DEBUG applications, all strings written to System.out/System.err by JAVA/PLUGIN applications get written to the CONSOLE-VIEW. It is also used for reading input from STDIN. You can write a java application to read from standard input...while running with Eclipse, you need to enter input in console.

2. If someone would like to see the strings written to System.out/System.err by an already deployed plugin in Eclipse, then it can be done by running eclipse in debug mode from command prompt as eclipse.exe -debug.
Or else you can hack a lil bit to capture the stderr/stdout of jvm (running the eclipse) as described in http://www.eclipsezone.com/eclipse/forums/t53216.html

3. Finally if a plugin still wants to log some progress/trace information to a console, then this article of creating an application specific console can help. This article makes use of the fact that a console view is already part of probably some console plugin or jdt plugin. What all it does is it creates a new console stream onto which a particular app can write.

So if you have to get the messages written to stderr of a console, then probably you will have to go through of the sources and try a hack. If there is a cleaner way to do that, I would love to hear that.

Thanks
Venkat
~Venkat + plus magazine ... living mathematics
  Click to reply to this thread Reply
7. At 11:19 PM on Dec 10, 2006, Hemavathy Ramesh Javalobby Newcomers wrote:

Re: Application specific Console

Hi,

I have some problem with creating the console. The console view is created, but I am not able to write to the console. It says, "No consoles to display at this time".Any solution?
  Click to reply to this thread Reply
8. At 1:26 AM on Mar 6, 2007, Rogger Javalobby Newcomers wrote:

Re: Application specific Console

Hi,

Is it possible to open a shell in the console view?
For e.g. a command prompt?
Something like cmd /c start run.bat opens an independent command prompt.
Could I in any way link this to the console view?

Thanks,
Rogger.

thread.rss_message