1414
1515package org .eclipse .ui .internal ;
1616
17+ import java .util .List ;
18+ import org .eclipse .core .commands .AbstractHandler ;
1719import org .eclipse .core .commands .ExecutionEvent ;
1820import org .eclipse .core .commands .ExecutionException ;
19- import org .eclipse .core .expressions . EvaluationResult ;
20- import org .eclipse .core . expressions . Expression ;
21- import org .eclipse .core . expressions . ExpressionInfo ;
22- import org .eclipse .core . expressions . IEvaluationContext ;
21+ import org .eclipse .e4 . core .contexts . IEclipseContext ;
22+ import org .eclipse .e4 . ui . model . application . ui . basic . MPart ;
23+ import org .eclipse .e4 . ui . workbench . IWorkbench ;
24+ import org .eclipse .e4 . ui . workbench . modeling . EPartService ;
2325import org .eclipse .ui .IEditorPart ;
24- import org .eclipse .ui .ISources ;
26+ import org .eclipse .ui .IWorkbenchPart ;
2527import org .eclipse .ui .IWorkbenchWindow ;
2628import org .eclipse .ui .handlers .HandlerUtil ;
29+ import org .osgi .framework .BundleContext ;
30+ import org .osgi .framework .FrameworkUtil ;
31+ import org .osgi .framework .ServiceReference ;
2732
2833/**
2934 * Closes the active editor.
3338 *
3439 * @since 3.3
3540 */
36- public class CloseEditorHandler extends AbstractEvaluationHandler {
37-
38- private Expression enabledWhen ;
39-
40- public CloseEditorHandler () {
41- registerEnablement ();
42- }
41+ public class CloseEditorHandler extends AbstractHandler {
4342
4443 @ Override
4544 public Object execute (ExecutionEvent event ) throws ExecutionException {
46- IWorkbenchWindow window = HandlerUtil .getActiveWorkbenchWindowChecked (event );
47- IEditorPart part = HandlerUtil .getActiveEditorChecked (event );
48- window .getActivePage ().closeEditor (part , true );
4945
50- return null ;
51- }
52-
53- @ Override
54- protected Expression getEnabledWhenExpression () {
55- if (enabledWhen == null ) {
56- enabledWhen = new Expression () {
57- @ Override
58- public EvaluationResult evaluate (IEvaluationContext context ) {
59- IEditorPart part = InternalHandlerUtil .getActiveEditor (context );
60- if (part != null ) {
61- return EvaluationResult .TRUE ;
46+ IWorkbenchPart activePart = HandlerUtil .getActivePart (event );
47+ if (activePart instanceof IEditorPart ) {
48+ IWorkbenchWindow window = HandlerUtil .getActiveWorkbenchWindowChecked (event );
49+ window .getActivePage ().closeEditor ((IEditorPart ) activePart , true );
50+ } else {
51+ // we may have an E4PartWrapper for a part which has been contributed eg. via a
52+ // PartDescriptor in a model fragment, and which has been tagged as
53+ // representing an Editor
54+ if (activePart instanceof E4PartWrapper ) {
55+ // derive the IEclipseContext & EPartService
56+ BundleContext context = FrameworkUtil .getBundle (IWorkbench .class ).getBundleContext ();
57+ ServiceReference <IWorkbench > reference = context .getServiceReference (IWorkbench .class );
58+ IEclipseContext eclipseContext = context .getService (reference ).getApplication ().getContext ();
59+ EPartService partService = eclipseContext .get (EPartService .class );
6260
61+ // access the wrapped part => save & close it
62+ MPart wrappedPart = ((E4PartWrapper ) activePart ).wrappedPart ;
63+ if (wrappedPart != null && partService != null ) {
64+ // ensure the active part does indeed represent an editor
65+ // (and not eg. a view) - checking here is just for extra
66+ // redundancy
67+ if (representsEditor (wrappedPart )) {
68+ if (partService .savePart (wrappedPart , true )) {
69+ partService .hidePart (wrappedPart );
70+ }
6371 }
64- return EvaluationResult .FALSE ;
6572 }
66-
67- @ Override
68- public void collectExpressionInfo (ExpressionInfo info ) {
69- info .addVariableNameAccess (ISources .ACTIVE_EDITOR_NAME );
70- }
71- };
73+ }
7274 }
73- return enabledWhen ;
75+
76+ return null ;
77+ }
78+
79+ /**
80+ * Checks whether the specified part represents an editor instance.
81+ *
82+ * @param part the part to query
83+ * @return true if the specified part represents an editor, false otherwise
84+ */
85+ private boolean representsEditor (MPart part ) {
86+ List <String > partTags = part .getTags ();
87+ return partTags == null || partTags .isEmpty () ? false
88+ : partTags .stream ().anyMatch (tag -> Workbench .EDITOR_TAG .equals (tag ));
7489 }
75- }
90+ }
0 commit comments