Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0e8b48937fd38cd81ac406f4f1a900c400486ed1 (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
/*******************************************************************************
 * Copyright (c) 2010 BestSolution.at and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation
 *     Wim Jongman <wim.jongman@remainsoftware.com> - https://bugs.eclipse.org/bugs/show_bug.cgi?id=393150
 ******************************************************************************/
package org.eclipse.e4.tools.emf.ui.internal.wbm;

import javax.annotation.PreDestroy;
import javax.inject.Inject;
import javax.inject.Named;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.runtime.Path;
import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.core.di.annotations.Optional;
import org.eclipse.e4.tools.emf.ui.common.IModelResource;
import org.eclipse.e4.tools.emf.ui.internal.common.ModelEditor;
import org.eclipse.e4.tools.services.IResourcePool;
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
import org.eclipse.e4.ui.workbench.modeling.EPartService;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.swt.widgets.Composite;

public class ApplicationModelEditor extends ModelEditor {

	private static final String EDITORPROJECT = "org.eclipse.e4.tools.emf.ui.editorproject";

	@Inject
	MPart part;

	@Inject
	EPartService partService;

	private Resource resource;

	private IProject project;

	@Inject
	public ApplicationModelEditor(Composite composite, IEclipseContext context, IModelResource modelProvider, @Named(EDITORPROJECT) @Optional IProject project, IResourcePool resourcePool) {
		super(composite, context, modelProvider, project, resourcePool);
		resource = modelProvider.getEditingDomain().getResourceSet().getResources().get(0);
	}

	@Inject
	public void addResourceListener(@Named(EDITORPROJECT) @Optional IProject project) {
		if (project != null) {
			this.project = project;
			project.getWorkspace().addResourceChangeListener(listener);
		}
	}

	@PreDestroy
	private void removeResourceListener() {
		if (project != null) {
			project.getWorkspace().removeResourceChangeListener(listener);
		}
	}

	/**
	 * Listener changes on the resource being edited. Will close the part if the
	 * resource was deleted or the containing project was closed.
	 */
	private IResourceChangeListener listener = new IResourceChangeListener() {
		public void resourceChanged(IResourceChangeEvent event) {

			if (event.getType() == IResourceChangeEvent.PRE_CLOSE || event.getType() == IResourceChangeEvent.PRE_DELETE) {
				if (event.getResource().equals(project)) {
					hidePart(true);
				}
				return;
			}

			if (resource == null)
				return;

			IResourceDelta delta = event.getDelta().findMember(new Path(resource.getURI().toPlatformString(false)));
			if (delta == null) {
				return;
			}

			if (delta.getKind() == IResourceDelta.REMOVED) {
				hidePart(true);
			}
		}

		private void hidePart(boolean force) {
			partService.hidePart(part, force);
		}
	};
}

Back to the top