Skip to content

Commit a595b86

Browse files
committed
Handle the case when application has no focus in ApplicationPartService
Currently when the application has no focus there is also no active window child and in this case several actions still fail even though a part is available for perform actions. This now handles the case of an application without focus with an empty optional to allow fall back to the part context. If that context has still no suitable part service implementation as a last resort the containing window context is used.
1 parent e7ae380 commit a595b86

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

bundles/org.eclipse.e4.ui.workbench/src/org/eclipse/e4/ui/internal/workbench/ApplicationPartServiceImpl.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.eclipse.e4.ui.model.application.ui.advanced.MPerspective;
2828
import org.eclipse.e4.ui.model.application.ui.advanced.MPlaceholder;
2929
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
30+
import org.eclipse.e4.ui.model.application.ui.basic.MWindow;
3031
import org.eclipse.e4.ui.workbench.UIEvents;
3132
import org.eclipse.e4.ui.workbench.modeling.EModelService;
3233
import org.eclipse.e4.ui.workbench.modeling.EPartService;
@@ -35,7 +36,7 @@
3536
public class ApplicationPartServiceImpl implements EPartService {
3637

3738
private static final Supplier<RuntimeException> NO_VALID_PARTSERVICE = () -> new IllegalStateException(
38-
"No valid PartService can be aquired from the current context"); //$NON-NLS-1$
39+
"No valid PartService can be acquired from the current context"); //$NON-NLS-1$
3940

4041
private MApplication application;
4142

@@ -50,15 +51,17 @@ public class ApplicationPartServiceImpl implements EPartService {
5051
private Optional<EPartService> getActiveWindowService() {
5152
IEclipseContext activeWindowContext = application.getContext().getActiveChild();
5253
if (activeWindowContext == null) {
53-
throw new IllegalStateException("Application does not have an active window"); //$NON-NLS-1$
54+
// in this case the application has no focus so we can't determine the active
55+
// child.
56+
return Optional.empty();
5457
}
5558
EPartService activeWindowPartService = activeWindowContext.get(EPartService.class);
5659
if (activeWindowPartService == null) {
5760
throw new IllegalStateException("Active window context is invalid"); //$NON-NLS-1$
5861
}
5962
if (activeWindowPartService == this) {
60-
// in this cas we would run into an infinite recursion, so from the current
61-
// active window we can't aquire another part service
63+
// in this case we would run into an infinite recursion, so from the current
64+
// active window we can't acquire another part service
6265
return Optional.empty();
6366
}
6467
return Optional.of(activeWindowPartService);
@@ -68,10 +71,20 @@ private Optional<EPartService> getActiveWindowService(MPart part) {
6871
return getActiveWindowService().or(() -> {
6972
IEclipseContext context = part.getContext();
7073
if (context != null) {
74+
// First try the context of the part
7175
EPartService partService = context.get(EPartService.class);
7276
if (partService instanceof PartServiceImpl) {
7377
return Optional.of(partService);
7478
}
79+
// Otherwise use the context of the contained window
80+
MWindow window = modelService.getTopLevelWindowFor(part);
81+
if (window != null) {
82+
IEclipseContext windowContext = window.getContext();
83+
EPartService windowPartService = windowContext.get(EPartService.class);
84+
if (windowPartService instanceof PartServiceImpl) {
85+
return Optional.of(windowPartService);
86+
}
87+
}
7588
}
7689
return Optional.empty();
7790
});

0 commit comments

Comments
 (0)