eclipsewindowsizeminimume4

How to restrict the minimum size of the window for Eclipse e4


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 :)


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.