Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1552b1090b90e24e5f2bc81a2866b4f9d8536af2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*******************************************************************************
 * Copyright (c) 2011, 2018 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Lars Vogel <Lars.Vogel@vogella.com> - Bug 483842, 488537
 ******************************************************************************/

package org.eclipse.e4.ui.workbench.addons.cleanupaddon;

import javax.inject.Inject;
import org.eclipse.core.runtime.Assert;
import org.eclipse.e4.core.di.annotations.Optional;
import org.eclipse.e4.core.services.events.IEventBroker;
import org.eclipse.e4.ui.di.UIEventTopic;
import org.eclipse.e4.ui.internal.workbench.swt.AbstractPartRenderer;
import org.eclipse.e4.ui.model.application.MApplication;
import org.eclipse.e4.ui.model.application.ui.MElementContainer;
import org.eclipse.e4.ui.model.application.ui.MUIElement;
import org.eclipse.e4.ui.model.application.ui.advanced.MArea;
import org.eclipse.e4.ui.model.application.ui.advanced.MPerspective;
import org.eclipse.e4.ui.model.application.ui.advanced.MPerspectiveStack;
import org.eclipse.e4.ui.model.application.ui.basic.MCompositePart;
import org.eclipse.e4.ui.model.application.ui.basic.MPartSashContainer;
import org.eclipse.e4.ui.model.application.ui.basic.MPartStack;
import org.eclipse.e4.ui.model.application.ui.basic.MTrimBar;
import org.eclipse.e4.ui.model.application.ui.basic.MWindow;
import org.eclipse.e4.ui.model.application.ui.menu.MMenuElement;
import org.eclipse.e4.ui.model.application.ui.menu.MToolBar;
import org.eclipse.e4.ui.workbench.IPresentationEngine;
import org.eclipse.e4.ui.workbench.UIEvents;
import org.eclipse.e4.ui.workbench.modeling.EModelService;
import org.eclipse.e4.ui.workbench.renderers.swt.SashLayout;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.osgi.service.event.Event;

public class CleanupAddon {

	private static final String DISABLE_CLEANUP_ADDON = "DisableCleanupAddon";

	@Inject
	IEventBroker eventBroker;

	@Inject
	EModelService modelService;

	@Inject
	MApplication app;

	@Inject
	@Optional
	private void subscribeTopicChildren(@UIEventTopic(UIEvents.ElementContainer.TOPIC_CHILDREN) Event event) {

		// Disable the addon if the tag is set on the application
		if (app.getTags().contains(DISABLE_CLEANUP_ADDON)) {
			return;
		}

		Display display = Display.getCurrent();
		Assert.isNotNull(display);

		Object changedObj = event.getProperty(UIEvents.EventTags.ELEMENT);

		// cleanup add-on only cares about removals and if display is not null
		if (!UIEvents.isREMOVE(event)) {
			return;
		}

		final MElementContainer<?> container = (MElementContainer<?>) changedObj;
		MUIElement containerParent = container.getParent();

		// Determine the elements that should *not* ever be auto-destroyed
		if (container instanceof MApplication || container instanceof MPerspectiveStack
				|| container instanceof MMenuElement || container instanceof MTrimBar || container instanceof MToolBar
				|| container instanceof MArea || container.getTags().contains(IPresentationEngine.NO_AUTO_COLLAPSE)) {
			return;
		}

		if (container instanceof MWindow && containerParent instanceof MApplication) {
			return;
		}



		// Stall the removal to handle cases where the container is only
		// transiently empty
		Display.getCurrent().asyncExec(() -> {
			// Remove it from the display if no visible children
			int tbrCount = modelService.toBeRenderedCount(container);

			// Cache the value since setting the TBR may change the
			// result
			boolean lastStack = isLastEditorStack(container);
			if (tbrCount == 0 && !lastStack) {
				container.setToBeRendered(false);
			}

			// Remove it from the model if it has no children at all
			MElementContainer<?> lclContainer = container;
			if (lclContainer.getChildren().isEmpty()) {
				MElementContainer<MUIElement> parent = container.getParent();
				if (parent != null && !lastStack) {
					container.setToBeRendered(false);
					parent.getChildren().remove(container);
				} else if (container instanceof MWindow) {
					// Must be a Detached Window
					MUIElement eParent = (MUIElement) ((EObject) container).eContainer();
					if (eParent instanceof MPerspective) {
						((MPerspective) eParent).getWindows().remove(container);
					} else if (eParent instanceof MWindow) {
						((MWindow) eParent).getWindows().remove(container);
					}
				}
			} else if (container.getChildren().size() == 1 && container instanceof MPartSashContainer) {
				// if a sash container has only one element then remove
				// it and move
				// its child up to where it used to be
				MUIElement theChild = container.getChildren().get(0);
				MElementContainer<MUIElement> parentContainer = container.getParent();
				if (parentContainer != null) {
					int index = parentContainer.getChildren().indexOf(container);

					// Magic check, are we unwrapping a sash container
					if (theChild instanceof MPartSashContainer) {
						if (container.getWidget() instanceof Composite) {
							Composite theComp = (Composite) container.getWidget();
							Object tmp = theChild.getWidget();
							theChild.setWidget(theComp);
							theComp.setLayout(new SashLayout(theComp, theChild));
							theComp.setData(AbstractPartRenderer.OWNING_ME, theChild);
							container.setWidget(tmp);
						}
					}

					theChild.setContainerData(container.getContainerData());
					container.getChildren().remove(theChild);
					parentContainer.getChildren().add(index, theChild);
					container.setToBeRendered(false);
					parentContainer.getChildren().remove(container);
				}
			}
		});
	}

	/**
	 * Returns true if and only if the given element should make itself visible
	 * when its first child becomes visible and make itself invisible whenever
	 * its last child becomes invisible. Defaults to false for unknown element
	 * types
	 */
	private static boolean shouldReactToChildVisibilityChanges(MUIElement theElement) {
		// TODO: It may be possible to remove the instanceof checks and just use
		// IPresentationEngine.HIDDEN_EXPLICITLY. However, that would require
		// explicitly setting IPresentationEngine.HIDDEN_EXPLICITLY on every
		// object where we want to keep it hidden even if it has a visible child
		// (such as the main toolbar).
		return (theElement instanceof MPartSashContainer || theElement instanceof MPartStack
				|| theElement instanceof MCompositePart)
				&& !theElement.getTags().contains(IPresentationEngine.HIDDEN_EXPLICITLY);
	}

	@Inject
	@Optional
	private void subscribeVisibilityChanged(
			@UIEventTopic(UIEvents.UIElement.TOPIC_VISIBLE) Event event) {

		// Disable the addon if the tag is set on the application
		if (app.getTags().contains(DISABLE_CLEANUP_ADDON)) {
			return;
		}

		MUIElement changedObj = (MUIElement) event.getProperty(UIEvents.EventTags.ELEMENT);
		if (changedObj instanceof MTrimBar || ((Object) changedObj.getParent()) instanceof MToolBar) {
			return;
		}

		if (changedObj.getWidget() instanceof Shell) {
			((Shell) changedObj.getWidget()).setVisible(changedObj.isVisible());
		} else if (changedObj.getWidget() instanceof Rectangle) {
			MElementContainer<MUIElement> parent = changedObj.getParent();
			if (!shouldReactToChildVisibilityChanges(parent)) {
				return;
			}

			if (changedObj.isVisible()) {
				// Make all the parents visible
				if (!parent.isVisible()) {
					parent.setVisible(true);
				}
			} else {
				// If there are no more 'visible' children then make the parent go away too
				boolean makeInvisible = true;
				for (MUIElement kid : parent.getChildren()) {
					if (kid.isToBeRendered() && kid.isVisible()) {
						makeInvisible = false;
						break;
					}
				}
				if (makeInvisible) {
					parent.setVisible(false);
				}
			}
		} else if (changedObj.getWidget() instanceof Control) {
			Control ctrl = (Control) changedObj.getWidget();
			MElementContainer<MUIElement> parent = changedObj.getParent();
			if (parent == null || ((Object) parent) instanceof MToolBar) {
				return;
			}

			if (changedObj.isVisible()) {
				if (parent.getRenderer() != null) {
					Object myParent = ((AbstractPartRenderer) parent.getRenderer())
							.getUIContainer(changedObj);
					if (myParent instanceof Composite) {
						Composite parentComp = (Composite) myParent;
						ctrl.setParent(parentComp);

						Control prevControl = null;
						for (MUIElement childME : parent.getChildren()) {
							if (childME == changedObj) {
								break;
							}
							if (childME.getWidget() instanceof Control && childME.isVisible()) {
								prevControl = (Control) childME.getWidget();
							}
						}
						if (prevControl != null) {
							ctrl.moveBelow(prevControl);
						} else {
							ctrl.moveAbove(null);
						}
						ctrl.requestLayout();
					}

					if (!shouldReactToChildVisibilityChanges(parent)) {
						return;
					}

					// Check if the parent is visible
					if (!parent.isVisible()) {
						parent.setVisible(true);
					}
				}
			} else {
				Shell limbo = (Shell) app.getContext().get("limbo"); //$NON-NLS-1$

				// Reparent the control to 'limbo'
				Composite curParent = ctrl.getParent();
				ctrl.setParent(limbo);
				curParent.requestLayout();

				// Always leave Window's in the presentation
				if ((Object) parent instanceof MWindow) {
					return;
				}

				// If there are no more 'visible' children then make the parent go away too
				boolean makeParentInvisible = true;
				for (MUIElement kid : parent.getChildren()) {
					if (kid.isToBeRendered() && kid.isVisible()) {
						makeParentInvisible = false;
						break;
					}
				}

				if (isLastEditorStack(parent) && makeParentInvisible) {
					return;
				}

				if (!shouldReactToChildVisibilityChanges(parent)) {
					return;
				}

				// Special check: If a perspective goes invisibe we need to make its
				// PerspectiveStack invisible as well...see bug 369528
				if (makeParentInvisible) {
					parent.setVisible(false);
				}
			}
		}
	}

	@Inject
	@Optional
	private void subscribeRenderingChanged(
			@UIEventTopic(UIEvents.UIElement.TOPIC_TOBERENDERED) Event event) {

		// Disable the addon if the tag is set on the application
		if (app.getTags().contains(DISABLE_CLEANUP_ADDON)) {
			return;
		}

			MUIElement changedObj = (MUIElement) event.getProperty(UIEvents.EventTags.ELEMENT);
			MElementContainer<MUIElement> container = null;
			if (changedObj.getCurSharedRef() != null) {
				container = changedObj.getCurSharedRef().getParent();
			} else {
				container = changedObj.getParent();
			}

			// this can happen for shared parts that aren't attached to any placeholders
			if (container == null) {
				return;
			}

			// never hide top-level windows
			MUIElement containerElement = container;
			if (containerElement instanceof MWindow && containerElement.getParent() != null) {
				return;
			}

			// These elements should neither be shown nor hidden based on their containment state
			if (isLastEditorStack(containerElement) || containerElement instanceof MPerspective
					|| containerElement instanceof MPerspectiveStack) {
				return;
			}

			Boolean toBeRendered = (Boolean) event.getProperty(UIEvents.EventTags.NEW_VALUE);
			if (toBeRendered) {
				// Bring the container back if one of its children goes visible
				if (!container.isToBeRendered()) {
					container.setToBeRendered(true);
				}
				if (!container.isVisible()
						&& !container.getTags().contains(IPresentationEngine.MINIMIZED)) {
					container.setVisible(true);
				}
			} else {
				// Never hide the container marked as no_close
				if (container.getTags().contains(IPresentationEngine.NO_AUTO_COLLAPSE)) {
					return;
				}

				int visCount = modelService.countRenderableChildren(container);

				// Remove stacks with no visible children from the display (but not the
				// model)
				final MElementContainer<MUIElement> theContainer = container;
				if (visCount == 0) {
				Display.getCurrent().asyncExec(() -> {
					int visCount1 = modelService.countRenderableChildren(theContainer);
					if (!isLastEditorStack(theContainer) && visCount1 == 0) {
						theContainer.setToBeRendered(false);
						}
					});
				} else if (container.getParent() != null) { // omit detached windows
					// if there are rendered elements but none are 'visible' we should
					// make the container invisible as well
					boolean makeInvisible = true;

					// OK, we have rendered children, are they 'visible' ?
					for (MUIElement kid : container.getChildren()) {
						if (!kid.isToBeRendered()) {
							continue;
						}
						if (kid.isVisible()) {
							makeInvisible = false;
							break;
						}
					}

					if (makeInvisible) {
						container.setVisible(false);
					}
				}
			}
	}

	boolean isLastEditorStack(MUIElement element) {
		return modelService.isLastEditorStack(element);
	}
}

Back to the top