Gaurav
Gaurav

Reputation: 329

support multiple instances of views in eclipse e4

In eclipse 3.x, we can able to open multiple instances of view part by providing different secondary id. How can i achieve the same behaviour in eclipse 4, I am not able to find any property of part which support this behaviour.

Other question is I am migrating 3.x application to 4.x using compat layer, i have imported 3.x views in application model and added them in perspectives using placeholders. My problem is if i open first instance of same view, it opens at appropriate partsashcontainer as defined in application model but after that if i open another instance of view, it opens in any area of the perspective instead of defined layout?

So how can i force eclipse 4 to open a view in one layout area if i am opening multiple instances of the view simultaneously?

Upvotes: 0

Views: 1482

Answers (1)

Gaurav
Gaurav

Reputation: 329

The solution is as suggested by @greg-449, I have to create part using EpartService and then attach the part to partstack. As i am using comapt layer so it is not straight forward and have to write some dirty code to achieve that:

                IEclipseContext serviceContext = E4Workbench
                        .getServiceContext();
                final IEclipseContext appContext = (IEclipseContext) serviceContext
                        .getActiveChild(); 

                EModelService modelService = appContext
                        .get(EModelService.class);
                MApplication app = serviceContext.get(MApplication.class);
                EPartService partService = serviceContext
                        .get(EPartService.class);
                MPartStack stack = (MPartStack) modelService.find(
                        "partstack.2", app);
                MPart part = modelService.createModelElement(MPart.class);
                part.setElementId("viewID");
                part.setContributionURI("bundleclass://org.eclipse.ui.workbench/org.eclipse.ui.internal.e4.compatibility.CompatibilityView");
                part.setCloseable(true);
                part.getTags().add(EPartService.REMOVE_ON_HIDE_TAG);

                stack.getChildren().add(part); // Add part to stack
                MPart viewPart = partService.showPart(part,
                        PartState.ACTIVATE); // Show part
                ViewReference ref = ((WorkbenchPage) PlatformUI
                        .getWorkbench().getActiveWorkbenchWindow()
                        .getActivePage()).getViewReference(part);
                IViewPart viewRef = ref.getView(true);

Using this we can open the view using E4 and get the instance of IViewpart to perform other operations of 3.X

Upvotes: 1

Related Questions