Skip to content

Commit 0a3c1fd

Browse files
feilimbjukzi
authored andcommitted
Modify the 'Close Active Editors' (plural) handler to add support for parts which represent an Editor and are contributed via eg. PartDescriptors in a Model Fragment. Associated with Issue#2176.
Include JUnit test.
1 parent 9f38380 commit 0a3c1fd

File tree

4 files changed

+372
-1
lines changed

4 files changed

+372
-1
lines changed

bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/CloseAllHandler.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,32 @@
1414

1515
package org.eclipse.ui.internal;
1616

17+
import java.util.Arrays;
18+
import java.util.Collection;
19+
import java.util.Collections;
20+
import java.util.List;
21+
import java.util.stream.Collectors;
1722
import org.eclipse.core.commands.ExecutionEvent;
1823
import org.eclipse.core.commands.ExecutionException;
1924
import org.eclipse.core.expressions.EvaluationResult;
2025
import org.eclipse.core.expressions.Expression;
2126
import org.eclipse.core.expressions.ExpressionInfo;
2227
import org.eclipse.core.expressions.IEvaluationContext;
28+
import org.eclipse.e4.ui.model.application.MApplication;
29+
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
30+
import org.eclipse.e4.ui.workbench.IWorkbench;
31+
import org.eclipse.e4.ui.workbench.modeling.EModelService;
32+
import org.eclipse.e4.ui.workbench.modeling.EPartService;
2333
import org.eclipse.ui.IEditorReference;
2434
import org.eclipse.ui.ISources;
2535
import org.eclipse.ui.IWorkbenchPage;
2636
import org.eclipse.ui.IWorkbenchPart;
2737
import org.eclipse.ui.IWorkbenchWindow;
2838
import org.eclipse.ui.handlers.HandlerUtil;
39+
import org.eclipse.ui.internal.e4.compatibility.CompatibilityEditor;
40+
import org.osgi.framework.BundleContext;
41+
import org.osgi.framework.FrameworkUtil;
42+
import org.osgi.framework.ServiceReference;
2943

3044
/**
3145
* Closes all active editors
@@ -48,6 +62,25 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
4862
IWorkbenchPage page = window.getActivePage();
4963
if (page != null) {
5064
page.closeAllEditors(true);
65+
66+
// close parts representing editors which were contributed via
67+
// eg. model fragment(s)
68+
Collection<MPart> partsTaggedAsEditor = getContributedPartsTaggedAsEditor();
69+
if (!partsTaggedAsEditor.isEmpty()) {
70+
MApplication application = getApplicationModel();
71+
EPartService partService = application.getContext().get(EPartService.class);
72+
if (partService != null) {
73+
for (MPart part : partsTaggedAsEditor) {
74+
if (partService.savePart(part, true)) {
75+
partService.hidePart(part);
76+
}
77+
}
78+
// ensure the EnabledWhenExpression evaluation is performed
79+
// otherwise the 'Close All Editors' will still appear enabled until
80+
// the user clicks/selects a different part
81+
getEvaluationService().requestEvaluation(ISources.ACTIVE_PART_NAME);
82+
}
83+
}
5184
}
5285

5386
return null;
@@ -69,6 +102,12 @@ public EvaluationResult evaluate(IEvaluationContext context) {
69102
if (refArray != null && refArray.length > 0) {
70103
return EvaluationResult.TRUE;
71104
}
105+
106+
// determine if we have any part contributions via model fragment
107+
// which were tagged as being an 'Editor' (and which are to be rendered)
108+
if (!getContributedPartsTaggedAsEditor().isEmpty()) {
109+
return EvaluationResult.TRUE;
110+
}
72111
}
73112
}
74113
return EvaluationResult.FALSE;
@@ -83,4 +122,35 @@ public void collectExpressionInfo(ExpressionInfo info) {
83122
}
84123
return enabledWhen;
85124
}
125+
126+
/**
127+
* Collects part contributions from the application model which are not
128+
* associated with compatibility layer editors, and are instead parts
129+
* contributed via eg. model fragment, and which were tagged as representing an
130+
* Editor, via the {@link Workbench#EDITOR_TAG} tag.
131+
*
132+
* @return a collection of (closable) part contributions from the application
133+
* model, tagged as 'Editor' and not containing the parts associated
134+
* with compatibility layer editors. Returns an empty collection if none
135+
* are found
136+
*/
137+
private Collection<MPart> getContributedPartsTaggedAsEditor() {
138+
MApplication application = getApplicationModel();
139+
EModelService modelService = application.getContext().get(EModelService.class);
140+
141+
List<MPart> partsTaggedAsEditor = modelService != null
142+
? modelService.findElements(application, null, MPart.class, Arrays.asList(Workbench.EDITOR_TAG))
143+
: Collections.emptyList();
144+
145+
// remove parts which we wish to ignore: compatibility layer editors,
146+
// non-closable parts, non-rendered parts
147+
return partsTaggedAsEditor.stream().filter(p -> !CompatibilityEditor.MODEL_ELEMENT_ID.equals(p.getElementId())
148+
&& p.isCloseable() && p.isToBeRendered()).collect(Collectors.toSet());
149+
}
150+
151+
private MApplication getApplicationModel() {
152+
BundleContext bundleContext = FrameworkUtil.getBundle(IWorkbench.class).getBundleContext();
153+
ServiceReference<IWorkbench> reference = bundleContext.getServiceReference(IWorkbench.class);
154+
return bundleContext.getService(reference).getApplication();
155+
}
86156
}

tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/api/ApiTestSuite.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.eclipse.ui.tests.api.workbenchpart.OverriddenTitleTest;
2222
import org.eclipse.ui.tests.api.workbenchpart.RawIViewPartTest;
2323
import org.eclipse.ui.tests.api.workbenchpart.ViewPartTitleTest;
24+
import org.eclipse.ui.tests.e4.CloseAllHandlerTest;
2425
import org.eclipse.ui.tests.ide.api.FileEditorInputTest;
2526
import org.eclipse.ui.tests.ide.api.IDETest;
2627
import org.eclipse.ui.tests.ide.api.IDETest2;
@@ -83,7 +84,8 @@
8384
SaveablesListTest.class,
8485
PerspectiveExtensionReaderTest.class,
8586
ModeledPageLayoutTest.class,
86-
WorkbenchPluginTest.class
87+
WorkbenchPluginTest.class,
88+
CloseAllHandlerTest.class
8789
})
8890
public class ApiTestSuite {
8991

0 commit comments

Comments
 (0)