eclipse-platform / eclipse.platform.ui

Eclipse Platform
https://projects.eclipse.org/projects/eclipse.platform
Eclipse Public License 2.0
81 stars 184 forks source link

Example handler for an alternative inline display #2093

Closed vogella closed 3 months ago

vogella commented 3 months ago

@HeikoKlare and @Wittmaxi while cleanup up my inbox I found this example code for showing something inline of the current editor. I wanted to share it with you so that you are aware of this option. No error check are included in the example code, it is just a demo which might help you for future developments.

Result:

image

package handler.handlers;

import java.util.List;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.e4.ui.model.application.MApplication;
import org.eclipse.e4.ui.model.application.ui.advanced.MArea;
import org.eclipse.e4.ui.model.application.ui.basic.MPartStack;
import org.eclipse.e4.ui.model.application.ui.basic.MWindow;
import org.eclipse.e4.ui.workbench.modeling.EModelService;
import org.eclipse.e4.ui.workbench.modeling.EPartService;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;

public class SampleHandler extends AbstractHandler {

    MPartStack pipStack = null;
    Composite pipComp = null;

    @Override
    public Object execute(ExecutionEvent event) throws ExecutionException {
        IWorkbenchWindow window = HandlerUtil
                .getActiveWorkbenchWindowChecked(event);
        MApplication theApp = (MApplication) window
                .getService(MApplication.class);
        MWindow win1 = theApp.getChildren().get(0);
        EPartService ps = win1.getContext().get(EPartService.class);
        EModelService ms = win1.getContext().get(EModelService.class);

        if (pipStack == null) {
            // Find the MArea
            List<MArea> areas = ms.findElements(win1, null, MArea.class, null);
            if (areas.size() > 0) {
                MArea area = areas.get(0);
                List<MPartStack> areaStacks = ms.findElements(area, null,
                        MPartStack.class, null);
                if (areaStacks.size()==1) {
                    pipStack =  ms.createModelElement(MPartStack.class);
                    Composite areaComp = (Composite) area.getWidget();
                    Rectangle ar = areaComp.getBounds();

                    pipComp = new Composite(areaComp, SWT.BORDER);
                    pipComp.setSize((ar.width / 2) - 20, (ar.height / 2) - 20);
                    pipComp.setLocation((ar.width / 2) - 25, (ar.height / 2) - 25);
                    pipComp.moveAbove(null);
                    pipComp.setLayout(new FillLayout());

                    pipStack.setVisible(true);
                    Control stackCtrl = (Control) pipStack.getWidget();
                    stackCtrl.setParent(pipComp);
                    pipComp.layout(true);
                }
            }
        } else {
            pipStack.setVisible(true);
            pipComp.dispose();
            pipStack = null;
            pipComp = null;
        }
        return null;
    }
}
vogella commented 3 months ago

Closing as this is only intended for a FYI (and I did not find a better place to put it).

HeikoKlare commented 3 months ago

Thank you for the pointer, @vogella! That's really interesting.

Wittmaxi commented 3 months ago

Thank you @vogella :) Nice to see that we are not the first ones to think of this! What a shame this was never used more widely