How do I access active IWorkbenchWindow in BundleActivator's start method? - eclipse-rcp

In my BundleActivator's start method, I need to access the active IWorkbenchWindow to add an IPartListener to it. However, when the start() method is called,
Workbench.getInstance().getActiveWorkbenchWindow()
returns null.
I tried adding a IWindowListener to Workbench.getInstance(), but a window open event is never fired. Only a window activated event is fired when I switch to another program and back to eclipse.
How do I add the IPartListener properly?

Workbench is an internal class and you should not be using it (Eclipse API Rules of Engagement). Internal classes may be changed without notice (Workbench in fact was completely rewritten between Eclipse 3 or 4).
The official way to get the IWorkbench interface is:
IWorkbench workbench = PlatformUI.getWorkbench();
However this may also return null if it is called too early in the Eclipse startup.
It is not usual to add a part listener in an activator, usually this is done in a view or editor part initialization or in a command handler or action.

I found a way to do it:
final IWorkbench workbench = PlatformUI.getWorkbench();
workbench.getDisplay().asyncExec(new Runnable() {
public void run() {
IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
if (window != null) {
// do something
}
}
});
(from Eclipse documentation)

Related

E4 close a part from an innactive Perspective

I have an e4 application that has two perspective:
Operations
Configuration -> Contains (among other things) a part stack where the configurations are open. Each configuration in a part.
When a new model is loaded all configuration parts are to be closed. This works fine if a load the new model when the configuration perspective is active.
However, if I open some configurations in the Configuration perspective. Switch to the Operations perspective and load a new model.
I can see in the logs that the code to close the parts is called and everything seems to be alright. However, when i switch back to the configuration perspective the parts are still visible an open.
Could somebody tell me how to make sure that the parts are close, regardless of the which is the active perspective?
I found a "workaround" to solve my issue.
I had an event thrown to detect the model load as follows and use it to "close"/hide the parts:
#Inject #Optional
void modelLoadedHandler(#UIEventTopic(STUConstants.UI_TOPIC_CONFIG_LOADED) Object nothing) {
viewer.setInput(sleConfigService);
//Close open config parts
MPartStack stack = (MPartStack) modelService
.find(STUConstants.PART_STACK_ID_CONFIG_VIEW,
application);
List<MStackElement> parts = new ArrayList<>(stack.getChildren());
MPart mpart;
for (MStackElement element : parts) {
mpart = (MPart) element;
log.error("Removing part {} visible {}", mpart.getElementId(), mpart.isVisible());
partService.hidePart(mpart, true);
}
// Adding this make it work regardless of which perspective is
// active.
stack.getChildren().clear();
}
Adding the stack.getChildren().clear(); did the trick. I am not hundred percent whether that would be the right way to deal with this, as i would have though that the PartStack should be emptied automatically when i remove a part.

How to display gef editor?

I am trying to use GEF for displaying and editing a flow diagram in an RCP. I have used GraphicalEditorWithFlyoutPalette as my editor looking at various examples on the internet. I n all those examples I dont find tips on how to show this editor when my RCP application starts first. previously I used a ViewPart to display the flow diagram and it worked fine. Now I am struck without knowing how to open the same on the editor which I designed.
The IDE class has several methods for opening an editor, for example:
IFile file = ... file you want to open
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IEditorInput input = new FileEditorInput(file);
IDE.openEditor(page, input, "editor id");
You can use the org.eclipse.ui.startup extension point to run code early in the Eclipse start up but the code above will not run so early in the start up. But you can schedule a UIJob to run the code:
#Override
public void earlyStartup()
{
new StartJob().schedule();
}
class StartJob extends UIJob
{
public StartJob()
{
super("Start Job");
}
#Override
public IStatus runInUIThread(final IProgressMonitor monitor)
{
.. open editor code
return Status.OK_STATUS;
}
}

Handle Window close event

I'm trying to handle the event when the close button of a Window is clicked:
// View Code
#Override
public void attachWindowListener(WindowListener listener) {
window.addWindowListener(listener);
}
// Presenter code
view.attachWindowListener(new WindowListener(){
public void windowHide(WindowEvent we) {
GWT.log("Window Event - Processing fields");
processFields();
}
});
However, the windowHide function seems to be not executed since I can't see the log I placed there.
How to properly handle that event?
How about
Window.addCloseHandler(
new CloseHandler<Window>()
{
public void onClose( CloseEvent<Window> windowCloseEvent )
{
// Do your worst here
}
} );
I usually put this in onModuleLoad() in my EntryPoint class.
Cheers,
Based on the information provided I would guess that either a.) the events you think are firing do not fire for the Window component (even if it seems like they should) or b.) the events are firing but in a different order than you expect.
For example, it's possible that a BrowserEvent or some other event is firing first as the window is being closed and the Window object's WindowEvent never fires. According to the API docs for GXT 2.x, the WindowEvent will fire on hide and deactivate but it does not specify that it fires on close. The GXT 3.0.x API doc is less clear on this point but I would assume the same behavior. Unfortunately Sencha does not provide good documentation on what events fire for a given component and in what order.
With that said, I have had some luck working through similar issues to this by using a debug class which outputs all the events on a component to which it is attached. This may shed some light on which events are firing and their order of execution, and you may find an optimal event to which you can attach your processFields() method.
For a good example of a debugger class, see this answer from a related post: https://stackoverflow.com/a/2891746/460638. It also includes an example of how to attach the debugger to your component.
API Doc for Window, GXT 2.x: http://dev.sencha.com/deploy/gxt-2.2.5/docs/api/com/extjs/gxt/ui/client/widget/Window.html
API Doc for Window, GXT 3.0.x: http://dev.sencha.com/deploy/gxt-3.0.0/javadoc/gxt/com/sencha/gxt/widget/core/client/Window.html
This worked:
window.addListener(Events.Hide, new Listener<ComponentEvent>() {
#Override
public void handleEvent(ComponentEvent be) {
// Do stuff
}
});

Inconsistent output to Eclipse Console View

I am invoking a compiler command but the compiler messages are not getting displayed in the Eclipse Console View consistently.
I have my launch() method implemented the same way as first code block of
this question; I have the command-line string setup which I use to call DebugPlugin.exec() method. However, unlike the the author of the question above, my output Eclipse console is very inconsistent. T
There is no activity in the console when I invoke the command and the console continues to display the "No console to display at this time." But after invoking the command numerous time and activating different consoles from the drop-down menu, the console occasionally does become active and message is displayed.
I am confused with how the eclipse is behaving and not sure how to resolve this issue. Any comment and/or recommendation would be appreciated.
Thanks!!
--
EDIT
To add some more info, running the external process using External Tools works fine. I add the compiler process c:\path\myprocess.exe in Locations field and the file to compile in the Arguments field within the External Tools Configuration window. When I run it, all the output is displayed fine. It just won't display when I run it programmatically through LaunchConfigurationDelegate class.
Maybe try bringing the console to front programmatically see if it helps:
* Bring the console to front.
*/
public static void showConsole() {
Display.getDefault().asyncExec(new Runnable() {
#Override
public void run() {
IWorkbenchWindow window = CUIPlugin.getActiveWorkbenchWindow();
if (window != null) {
IWorkbenchPage page = window.getActivePage();
if (page != null) {
IViewPart consoleView =
page.findView(IConsoleConstants.ID_CONSOLE_VIEW);
if (consoleView == null) {
IWorkbenchPart activePart = page.getActivePart();
try {
consoleView =
page.showView(IConsoleConstants.ID_CONSOLE_VIEW);
} catch (PartInitException pie) {
CUIPlugin.log(pie);
}
// restore focus stolen by the creation of the
// console
page.activate(activePart);
} else {
boolean bringToTop = true;
if (bringToTop) {
page.bringToTop(consoleView);
}
}
}
}
}
});
}
Finally got it to work. The main change I've made is having my MyLaunchConfigurationDelegate extend LaunchConfigurationDelegate instead of just implementing ILaunchConfigurationDelegate. When observed through the debugger, the launch() method went through similar code path as external process that was launched via External Tools when MyLaunchConfigurationDelegate extended LaunchConfigurationDelegate.
I guess it was lack of information on my part but I wasn't sure which part of the code was more important to share.
Another piece of code that was removed was:
IProcess dbgProcess = DebugPlugin.newProcess(launch, compilerProcess, "XVR Compiler", processAttributes);
...
launch.removeProcess(dbgProcess);
I've added it while attempting different approach in debugging this issue and it actually caused more issues by removing the debugProcess before it has chance to display output to the console.

How do I know when user minimizes / maximizes Eclipse?

I need to respond to the events of minimizing / maximizing Eclipse window. How do I do that?
I can suggest a way: you can write a plugin for it.
For example see this improvized "tutorial", I made it, tried it works on Ganymede. A bit ugly at the final Shell variable, but working. If you know nicer solution just shoot :)
((actually there is a way: to extend your own ControlListener class, but that needs more coding :))
Create a new Plug-in Project, name it as you want, create it from a template named: Hello World Command
Open the SampleHandler class, and then replace the execute() function with this code.
public Object execute(ExecutionEvent event) throws ExecutionException {
IWorkbenchWindow window = HandlerUtil
.getActiveWorkbenchWindowChecked(event);
final Shell s = window.getShell();
window.getShell().addControlListener(new ControlListener() {
#Override
public void controlMoved(ControlEvent e) {
// TODO Auto-generated method stub
}
#Override
public void controlResized(ControlEvent e) {
MessageDialog.openInformation(s,
"WindowEventHandler Plug-in", "RESIZED: "
+ e.toString() + "\nHello, Eclipse world");
}
});
MessageDialog.openInformation(window.getShell(),
"WindowEventHandler Plug-in",
"Hello, Eclipse world, resize will be taken care of.");
return null;
}
now. Start the project (Run As-> Eclipse application), and you'll se an Eclipse button on the toolbar. Click on it! It triggers the running of the above code where the essence is that the window.getShell() returns with the main window component so you can add listeners to it.
If you want it to run automatically, not just for a button, you have to find out a plugin where the entry point is connected to the starting of the application.
Hope this helps.
b
Found a way to do it easily: you have to create a ShellListener or ShellAdapter, which have methods that are called when the shell is iconified, deiconified, activated, deactivated and closed.
After creating it, add it as a listener with the following line:
int i;
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell().addShellListener( yourListenerHere);
If you ever remove it from the shell's listeners list, be sure that Workbench, ActiveWorkbnchWindow and Shell are not null.

Categories