Question

I am making an application based on Eclipse e4 framework. I was wondering how the minimal size of the application window can be controlled. There seems no properties can be defined in e4xmi file for this purpose.

Does anyone know how to do it?

I found a thread in Eclipse Community Forum (http://www.eclipse.org/forums/index.php/t/244875/) saying it can be achieved by creating my own renderer. How can I do that exactly?

Thank you very much :)

Was it helpful?

Solution

Assuming you are using the built-in SWT Renderers, you can also listen for the creation of your E4 MWindow elements and gain access to the underlying SWT Shell. In this example the listener is registered in an AddOn, which you can add to your e4xmi.

import javax.annotation.PostConstruct;

import org.eclipse.e4.core.services.events.IEventBroker;
import org.eclipse.e4.ui.model.application.ui.basic.MWindow;
import org.eclipse.e4.ui.workbench.UIEvents;
import org.eclipse.swt.widgets.Shell;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;

public class MinSizeAddon {
    @PostConstruct
    public void init(final IEventBroker eventBroker) {
        EventHandler handler = new EventHandler() {
            @Override
            public void handleEvent(Event event) {
                if (!UIEvents.isSET(event))
                    return;

                Object objElement = event.getProperty(UIEvents.EventTags.ELEMENT);
                if (!(objElement instanceof MWindow))
                    return;

                MWindow windowModel = (MWindow)objElement;
                Shell theShell = (Shell)windowModel.getWidget();
                if (theShell == null)
                    return;

                theShell.setMinimumSize(400, 300);
            }
        };
        eventBroker.subscribe(UIEvents.UIElement.TOPIC_WIDGET, handler);
    }
}

Note, that this will be executed for any MWindow in your application, and there can be more of them (i.e. when an MPart is detached from the MPartStack into a seperate window). If you want to limit the execution to specific MWindows, I recommend to add a tag to the window in the e4xmi and check for this tag before setting the minimum size.

OTHER TIPS

If anyone is still looking to do this in an e4 application and doesn't want to roll their own renderer, you can simply do the following in the post-construct of your part class:

@PostConstruct
public void postConstruct(Composite parent) {
  parent.getShell().setMinimumSize(300, 300);
  //...
}

The parent Composite passed in by the framework gives you access to the Shell, which lets you set the minimum size. This stops the application from being resized to less than the specified minimum size (in pixels).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top