Registering a console in the Xtext UI? [message #630139] |
Thu, 30 September 2010 18:20 |
No real name 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);
}
}
|
|
|
|
Powered by
FUDForum. Page generated in 0.03693 seconds