1
1
/*******************************************************************************
2
- * Copyright (c) 2010, 2016 IBM Corporation and others.
2
+ * Copyright (c) 2010, 2023 IBM Corporation and others.
3
3
*
4
4
* This program and the accompanying materials
5
5
* are made available under the terms of the Eclipse Public License 2.0
12
12
* IBM Corporation - initial API and implementation
13
13
* Lars Vogel <[email protected] > - Bug 395825
14
14
* Simon Scholz <[email protected] > - Bug 486876
15
+ * Christoph Läubrich - make the application service to work in dialog context
15
16
******************************************************************************/
16
17
package org .eclipse .e4 .ui .internal .workbench ;
17
18
18
19
import java .util .Collection ;
20
+ import java .util .List ;
21
+ import java .util .Objects ;
19
22
import java .util .Optional ;
23
+ import java .util .function .Supplier ;
20
24
import javax .inject .Inject ;
21
25
import org .eclipse .e4 .core .contexts .IEclipseContext ;
22
26
import org .eclipse .e4 .ui .model .application .MApplication ;
23
27
import org .eclipse .e4 .ui .model .application .ui .advanced .MPerspective ;
24
28
import org .eclipse .e4 .ui .model .application .ui .advanced .MPlaceholder ;
25
29
import org .eclipse .e4 .ui .model .application .ui .basic .MPart ;
30
+ import org .eclipse .e4 .ui .workbench .UIEvents ;
31
+ import org .eclipse .e4 .ui .workbench .modeling .EModelService ;
26
32
import org .eclipse .e4 .ui .workbench .modeling .EPartService ;
27
33
import org .eclipse .e4 .ui .workbench .modeling .IPartListener ;
28
34
29
35
public class ApplicationPartServiceImpl implements EPartService {
30
36
37
+ 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
+
31
40
private MApplication application ;
32
41
42
+ private EModelService modelService ;
43
+
33
44
@ Inject
34
- ApplicationPartServiceImpl (MApplication application ) {
45
+ ApplicationPartServiceImpl (MApplication application , EModelService modelService ) {
35
46
this .application = application ;
47
+ this .modelService = modelService ;
36
48
}
37
49
38
- private EPartService getActiveWindowService () {
50
+ private Optional < EPartService > getActiveWindowService () {
39
51
IEclipseContext activeWindowContext = application .getContext ().getActiveChild ();
40
52
if (activeWindowContext == null ) {
41
53
throw new IllegalStateException ("Application does not have an active window" ); //$NON-NLS-1$
@@ -45,9 +57,24 @@ private EPartService getActiveWindowService() {
45
57
throw new IllegalStateException ("Active window context is invalid" ); //$NON-NLS-1$
46
58
}
47
59
if (activeWindowPartService == this ) {
48
- throw new IllegalStateException ("Application does not have an active window" ); //$NON-NLS-1$
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
62
+ return Optional .empty ();
49
63
}
50
- return activeWindowPartService ;
64
+ return Optional .of (activeWindowPartService );
65
+ }
66
+
67
+ private Optional <EPartService > getActiveWindowService (MPart part ) {
68
+ return getActiveWindowService ().or (() -> {
69
+ IEclipseContext context = part .getContext ();
70
+ if (context != null ) {
71
+ EPartService partService = context .get (EPartService .class );
72
+ if (partService instanceof PartServiceImpl ) {
73
+ return Optional .of (partService );
74
+ }
75
+ }
76
+ return Optional .empty ();
77
+ });
51
78
}
52
79
53
80
@ Override
@@ -64,107 +91,125 @@ public void removePartListener(IPartListener listener) {
64
91
65
92
@ Override
66
93
public boolean isPartOrPlaceholderInPerspective (String elementId , MPerspective perspective ) {
67
- return getActiveWindowService ().isPartOrPlaceholderInPerspective (elementId , perspective );
94
+ return getActiveWindowService ().orElseThrow (NO_VALID_PARTSERVICE ).isPartOrPlaceholderInPerspective (elementId ,
95
+ perspective );
68
96
}
69
97
70
98
@ Override
71
99
public void switchPerspective (MPerspective perspective ) {
72
- getActiveWindowService ().switchPerspective (perspective );
100
+ getActiveWindowService ().ifPresentOrElse (service -> service .switchPerspective (perspective ),
101
+ () -> switchPerspectiveInternal (perspective ));
73
102
}
74
103
75
104
@ Override
76
105
public Optional <MPerspective > switchPerspective (String perspectiveId ) {
77
- return getActiveWindowService ().switchPerspective (perspectiveId );
106
+ Objects .requireNonNull (perspectiveId );
107
+ Optional <EPartService > windowService = getActiveWindowService ();
108
+ if (windowService .isPresent ()) {
109
+ return windowService .get ().switchPerspective (perspectiveId );
110
+ }
111
+ List <MPerspective > result = modelService .findElements (application , perspectiveId , MPerspective .class , null );
112
+ if (!result .isEmpty ()) {
113
+ MPerspective perspective = result .get (0 );
114
+ switchPerspectiveInternal (perspective );
115
+ return java .util .Optional .of (perspective );
116
+ }
117
+ return Optional .empty ();
118
+ }
119
+
120
+ private void switchPerspectiveInternal (MPerspective perspective ) {
121
+ perspective .getParent ().setSelectedElement (perspective );
122
+ UIEvents .publishEvent (UIEvents .UILifeCycle .PERSPECTIVE_SWITCHED , perspective );
78
123
}
79
124
80
125
@ Override
81
126
public void activate (MPart part ) {
82
- getActiveWindowService ().activate (part );
127
+ getActiveWindowService (part ). orElseThrow ( NO_VALID_PARTSERVICE ).activate (part );
83
128
}
84
129
85
130
@ Override
86
131
public void activate (MPart part , boolean requiresFocus ) {
87
- getActiveWindowService ().activate (part , requiresFocus );
132
+ getActiveWindowService (part ). orElseThrow ( NO_VALID_PARTSERVICE ).activate (part , requiresFocus );
88
133
}
89
134
90
135
@ Override
91
136
public void requestActivation () {
92
- getActiveWindowService ().requestActivation ();
137
+ getActiveWindowService ().orElseThrow ( NO_VALID_PARTSERVICE ). requestActivation ();
93
138
}
94
139
95
140
@ Override
96
141
public void bringToTop (MPart part ) {
97
- getActiveWindowService ().bringToTop (part );
142
+ getActiveWindowService (part ). orElseThrow ( NO_VALID_PARTSERVICE ).bringToTop (part );
98
143
}
99
144
100
145
@ Override
101
146
public MPart findPart (String id ) {
102
- return getActiveWindowService ().findPart (id );
147
+ return getActiveWindowService ().orElseThrow ( NO_VALID_PARTSERVICE ). findPart (id );
103
148
}
104
149
105
150
@ Override
106
151
public Collection <MPart > getParts () {
107
- return getActiveWindowService ().getParts ();
152
+ return getActiveWindowService ().orElseThrow ( NO_VALID_PARTSERVICE ). getParts ();
108
153
}
109
154
110
155
@ Override
111
156
public MPart getActivePart () {
112
- return getActiveWindowService ().getActivePart ();
157
+ return getActiveWindowService ().orElseThrow ( NO_VALID_PARTSERVICE ). getActivePart ();
113
158
}
114
159
115
160
@ Override
116
161
public boolean isPartVisible (MPart part ) {
117
- return getActiveWindowService ().isPartVisible (part );
162
+ return getActiveWindowService (part ). orElseThrow ( NO_VALID_PARTSERVICE ).isPartVisible (part );
118
163
}
119
164
120
165
@ Override
121
166
public MPart createPart (String id ) {
122
- return getActiveWindowService ().createPart (id );
167
+ return getActiveWindowService ().orElseThrow ( NO_VALID_PARTSERVICE ). createPart (id );
123
168
}
124
169
125
170
@ Override
126
171
public MPlaceholder createSharedPart (String id ) {
127
- return getActiveWindowService ().createSharedPart (id );
172
+ return getActiveWindowService ().orElseThrow ( NO_VALID_PARTSERVICE ). createSharedPart (id );
128
173
}
129
174
130
175
@ Override
131
176
public MPlaceholder createSharedPart (String id , boolean force ) {
132
- return getActiveWindowService ().createSharedPart (id , force );
177
+ return getActiveWindowService ().orElseThrow ( NO_VALID_PARTSERVICE ). createSharedPart (id , force );
133
178
}
134
179
135
180
@ Override
136
181
public MPart showPart (String id , PartState partState ) {
137
- return getActiveWindowService ().showPart (id , partState );
182
+ return getActiveWindowService ().orElseThrow ( NO_VALID_PARTSERVICE ). showPart (id , partState );
138
183
}
139
184
140
185
@ Override
141
186
public MPart showPart (MPart part , PartState partState ) {
142
- return getActiveWindowService ().showPart (part , partState );
187
+ return getActiveWindowService (part ). orElseThrow ( NO_VALID_PARTSERVICE ).showPart (part , partState );
143
188
}
144
189
145
190
@ Override
146
191
public void hidePart (MPart part ) {
147
- getActiveWindowService ().hidePart (part );
192
+ getActiveWindowService (part ). orElseThrow ( NO_VALID_PARTSERVICE ).hidePart (part );
148
193
}
149
194
150
195
@ Override
151
196
public void hidePart (MPart part , boolean force ) {
152
- getActiveWindowService ().hidePart (part , force );
197
+ getActiveWindowService (part ). orElseThrow ( NO_VALID_PARTSERVICE ).hidePart (part , force );
153
198
}
154
199
155
200
@ Override
156
201
public Collection <MPart > getDirtyParts () {
157
- return getActiveWindowService ().getDirtyParts ();
202
+ return getActiveWindowService ().orElseThrow ( NO_VALID_PARTSERVICE ). getDirtyParts ();
158
203
}
159
204
160
205
@ Override
161
206
public boolean savePart (MPart part , boolean confirm ) {
162
- return getActiveWindowService ().savePart (part , confirm );
207
+ return getActiveWindowService (part ). orElseThrow ( NO_VALID_PARTSERVICE ).savePart (part , confirm );
163
208
}
164
209
165
210
@ Override
166
211
public boolean saveAll (boolean confirm ) {
167
- return getActiveWindowService ().saveAll (confirm );
212
+ return getActiveWindowService ().orElseThrow ( NO_VALID_PARTSERVICE ). saveAll (confirm );
168
213
}
169
214
170
215
}
0 commit comments