Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Registering a console in the Xtext UI?
Registering a console in the Xtext UI? [message #630139] Thu, 30 September 2010 18:20 Go to next message
No real name is currently offline No real nameFriend
Messages: 101
Registered: August 2010
Senior Member
Hi,

Is there a way to register a console class that I've created with the Xtext UI of my DSL? I know i'd have to add the code in the My<DSL>UiModule.java but what class does it extend and what does it bind to?

Right now, the class is a singleton, but I'm thinking maybe it shouldn't be. Or is there already some class present in Xtext that I can extend for the Console?

Thanks.

/**
 * Provides a singleton console object for writing messages.
 *
 */
public class MyHLMConsole {

	private MessageConsole console = null;
	private MessageConsoleStream msgStream = null;
	private MessageConsoleStream infoStream = null;
	private MessageConsoleStream warnStream = null;
	private MessageConsoleStream errorStream = null;
	private static final String consoleName = "HLM-Console";
	
	public enum StreamCode {
		MESSAGE, INFO, WARNING, ERROR
	};

	// Singleton object.
	private static MyHLMConsole ref;
	
	/**
	 * Private Constructor.
	 *
	 */
	private MyHLMConsole() {
		// Create a console and register it.
		console = new MessageConsole(consoleName, null);
		ConsolePlugin.getDefault().getConsoleManager().addConsoles(new IConsole[] {console});
		
		// Create the message streams for this console.
		msgStream = createMessageConsoleStream(SWT.COLOR_BLACK);
		infoStream = createMessageConsoleStream(SWT.COLOR_DARK_GREEN);
		warnStream = createMessageConsoleStream(SWT.COLOR_DARK_YELLOW);
		errorStream = createMessageConsoleStream(SWT.COLOR_RED);
	}

	/**
	 * Get singleton object.
	 * @return Singleton object.
	 */
	public MyHLMConsole getDefault() {
		if (ref == null) {
			ref = new MyHLMConsole();
		}
		return ref;
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#clone()
	 */
	public Object clone() throws CloneNotSupportedException
	{
		// Do not allow cloning!
		throw new CloneNotSupportedException("MyHLMConsole is singleton");
	}
	
	/**
	 * Clear the console.
	 */
	public void clear() {
		console.clearConsole();
	}
	
	/**
	 * Write a message to the console.
	 * @param message - The message.
	 * @param type - The type of message (normal, info, warning, error).
	 */
	public void print(String message, StreamCode code)
	{
		// Write the message and flush the stream.
		switch (code) {
		case MESSAGE:
			msgStream.print(message + "\n");
			try { msgStream.flush(); } catch (IOException e) { e.printStackTrace(); }
			break;
		case INFO:
			infoStream.print("[INFO] :: " + message + "\n");
			try { infoStream.flush(); } catch (IOException e) { e.printStackTrace(); }
			break;
		case WARNING:
			warnStream.print("[WARN] :: " + message + "\n");
			try { warnStream.flush(); } catch (IOException e) { e.printStackTrace(); }
			break;
		case ERROR:
			errorStream.print("[ERROR] :: " + message + "\n");
			try { errorStream.flush(); } catch (IOException e) { e.printStackTrace(); }
			break;
		default:
			break;
		}
	}
	
	/**
	 * Display (activate) the console.
	 * @return True if successful, false otherwise.
	 */
	public boolean display() {
		// Get active workbench, page, part.
		IWorkbenchWindow activeWorkbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
		if (activeWorkbenchWindow != null) {
			IWorkbenchPage activePage = activeWorkbenchWindow.getActivePage();
			if (activePage != null) {
				// Keep current active part before displaying the console.
				IWorkbenchPart activePart = activePage.getActivePart();
				try {
					// Display console.
					IConsoleView view = (IConsoleView) activePage.showView(IConsoleConstants.ID_CONSOLE_VIEW);
					view.display(console);
				} catch (PartInitException e) {
					popupError("Error", "Opening console view failed");
					return false;
				}
				// Restore the previous active part. Otehrwise, focus will move to console view.
				activePage.activate(activePart);
			}
		}
		return true;
	}
	
	/**
	 * Create a new MessageConsoleStream tied to this console.
	 * @param color - The color to associate with stream.
	 * @return A MessageConsoleStream
	 */
	private MessageConsoleStream createMessageConsoleStream(int color) {
		MessageConsoleStream stream = console.newMessageStream();
		stream.setColor(Display.getCurrent().getSystemColor(color));
		stream.setActivateOnWrite(false);
		return stream;
	}
		
	/**
	 * Display a message dialog box indicating a error.
	 * @param title - Title of dialog box.
	 * @param message - Message to display.
	 */
	public void popupError(String title, String message)
	{
		MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), title, message);
	}
	
	/**
	 * Display a message dialog box asking a question.
	 * @param title - Title of dialog box.
	 * @param message - Message to display.
	 * @return True if user presses 'yes', false otherwise.
	 */
	public boolean popupQuestion(String title, String message)
	{
		return MessageDialog.openQuestion(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), title, message);
	}
}

Re: Registering a console in the Xtext UI? [message #630210 is a reply to message #630139] Fri, 01 October 2010 07:52 Go to previous message
Meinte Boersma is currently offline Meinte BoersmaFriend
Messages: 434
Registered: July 2009
Location: Leiden, Netherlands
Senior Member
This is more of a Guice question in combination with how the Xtext runtime modules work. As you want it to be a singleton, you'll definitely have to bind it, with Scope.SINGLETON as scope. Have a look at the OO hierarchy of the UI runtime module to see where/how you have to do the binding.

Previous Topic:Default search URIs for importURI
Next Topic:Linking doesn't work after deployment
Goto Forum:
  


Current Time: Fri Apr 26 14:49:20 GMT 2024

Powered by FUDForum. Page generated in 0.03693 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top