Skip to content

Commit 8279c72

Browse files
committed
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.
1 parent 2cab7ac commit 8279c72

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

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

Lines changed: 68 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,21 @@ 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+
}
79+
}
5180
}
5281

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

0 commit comments

Comments
 (0)