Skip to content

Commit

Permalink
Make some changes inspired by code review feedback. Change the handle…
Browse files Browse the repository at this point in the history
…r to derive the active part from the event context - performing the default/previous logic when the active part is an IEditorPart, and perform 'new' behaviour otherwise.
  • Loading branch information
Feilim Breatnach committed Oct 1, 2024
1 parent b556ea3 commit 1c4a9da
Showing 1 changed file with 32 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@
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.core.contexts.IEclipseContext;
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
import org.eclipse.e4.ui.workbench.IWorkbench;
import org.eclipse.e4.ui.workbench.modeling.EPartService;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.ServiceReference;
Expand All @@ -36,22 +41,33 @@
public class CloseEditorHandler extends AbstractHandler {

@Override
public Object execute(ExecutionEvent event) {
// derive the IEclipseContext instance
BundleContext context = FrameworkUtil.getBundle(IWorkbench.class).getBundleContext();
ServiceReference<IWorkbench> reference = context.getServiceReference(IWorkbench.class);
IEclipseContext eclipseContext = context.getService(reference).getApplication().getContext();
if (eclipseContext != null) {
// derive the active part via the context
MPart activePart = eclipseContext.getActiveLeaf().get(MPart.class);
EPartService partService = eclipseContext.get(EPartService.class);
if (activePart != null && partService != null) {
// ensure the active part does indeed represent an editor
// (and not eg. a view) - checking here is just for extra
// redundancy
if (representsEditor(activePart)) {
if (partService.savePart(activePart, true)) {
partService.hidePart(activePart);
public Object execute(ExecutionEvent event) throws ExecutionException {

IWorkbenchPart activePart = HandlerUtil.getActivePart(event);
if (activePart instanceof IEditorPart) {
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
window.getActivePage().closeEditor((IEditorPart) activePart, true);
} else {
// we may have an E4PartWrapper for a part which has been contributed eg. via a
// PartDescriptor in a model fragment, and which has been tagged as
// representing an Editor
if (activePart instanceof E4PartWrapper) {
// derive the IEclipseContext & EPartService
BundleContext context = FrameworkUtil.getBundle(IWorkbench.class).getBundleContext();
ServiceReference<IWorkbench> reference = context.getServiceReference(IWorkbench.class);
IEclipseContext eclipseContext = context.getService(reference).getApplication().getContext();
EPartService partService = eclipseContext.get(EPartService.class);

// access the wrapped part => save & close it
MPart wrappedPart = ((E4PartWrapper) activePart).wrappedPart;
if (wrappedPart != null && partService != null) {
// ensure the active part does indeed represent an editor
// (and not eg. a view) - checking here is just for extra
// redundancy
if (representsEditor(wrappedPart)) {
if (partService.savePart(wrappedPart, true)) {
partService.hidePart(wrappedPart);
}
}
}
}
Expand Down

0 comments on commit 1c4a9da

Please sign in to comment.