Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4c91be7cddf5eb89c3a75e37a64a1ff4b0c3ed36 (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
/*******************************************************************************
 * Copyright (c) 2014 protos software gmbh (http://www.protos.de).
 * All rights reserved. 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:
 * 		Henrik Rentz-Reichert (initial contribution)
 * 
 *******************************************************************************/

package org.eclipse.etrice.ui.common.base.editor;

import java.util.Collection;
import java.util.Map;

import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature.Setting;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.etrice.core.common.ui.linking.GlobalNonPlatformURIEditorOpener;
import org.eclipse.etrice.core.fsm.fSM.ModelComponent;
import org.eclipse.etrice.core.fsm.ui.FSMUiModule;
import org.eclipse.graphiti.dt.IDiagramTypeProvider;
import org.eclipse.graphiti.mm.pictograms.Diagram;
import org.eclipse.graphiti.services.Graphiti;
import org.eclipse.graphiti.ui.editor.DiagramBehavior;
import org.eclipse.graphiti.ui.editor.DiagramEditor;
import org.eclipse.graphiti.ui.editor.EditorInputAdapter;
import org.eclipse.graphiti.ui.editor.IDiagramEditorInput;
import org.eclipse.help.IContextProvider;
import org.eclipse.jface.dialogs.TrayDialog;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.contexts.IContextService;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.ui.editor.XtextEditor;
import org.eclipse.xtext.util.concurrent.IUnitOfWork;

import static org.eclipse.ui.PlatformUI.getWorkbench;

import com.google.inject.Injector;

/**
 * @author Henrik Rentz-Reichert
 *
 */
public abstract class DiagramEditorBase extends DiagramEditor implements IInputUriHolder {

	private final static String CONTEXT_ID = "org.eclipse.etrice.ui.common.base.context";
	
	protected ModificationTrackingEnabler mte = new ModificationTrackingEnabler();
	protected URI inputUri;
	private Object textEditorClass;
	
	private SuperClassListener superClassListener;

	public DiagramEditorBase(Object textEditorClass) {
		this.textEditorClass = textEditorClass;
		
		Injector injector = FSMUiModule.getInjector();
		injector.injectMembers(this);
		
		TrayDialog.setDialogHelpAvailable(false);
	}
	
	@Override
	protected DiagramBehavior createDiagramBehavior() {
		return new CustomDiagramBehavior(this);
	}

	@Override
	public void init(IEditorSite site, IEditorInput input) throws PartInitException {
		IEditorInput newInput = EditorInputAdapter.adaptToDiagramEditorInput(input);
		if (newInput instanceof IDiagramEditorInput)
			this.inputUri = ((IDiagramEditorInput) newInput).getUri();
		
		super.init(site, input);
	}

	@Override
	public void initializeGraphicalViewer() {
		super.initializeGraphicalViewer();
		
		ResourceSet resourceSet = getEditingDomain().getResourceSet();
		Map<EObject, Collection<Setting>> result = EcoreUtil.UnresolvedProxyCrossReferencer.find(resourceSet);
		if (!result.isEmpty()){
			System.err.println("ERROR in diagram viewer: could not resolve all proxies!");
			//System.out.println(result);
		}
	
		mte.setTarget(getEditingDomain());
	}	
	
	/* (non-Javadoc)
	 * @see org.eclipse.graphiti.ui.editor.DiagramEditor#setFocus()
	 */
	@Override
	public void setFocus() {
		if (getGraphicalViewer() == null) {
			return;
		}
		
		boolean dirtyAlready = isDirty();
		
		// inside this call auto refresh will happen if (and turn the editor dirty)
		super.setFocus();
		
		if (superClassListener != null && superClassListener.isChangeInSuperClass())
			superClassChanged();
		
		if (!dirtyAlready && isDirty())
			doSave(new NullProgressMonitor());
		
		Diagram diagram = getDiagramTypeProvider().getDiagram();
		EObject diagramBo = diagram.getLink().getBusinessObjects().iterator().next();
		if(diagramBo == null || diagramBo.eIsProxy()) {
			handleMissingDiagramBo(diagram);
		}
	}
	
	public ModelComponent getModelComponent() {
		IDiagramTypeProvider diagramTypeProvider = getDiagramTypeProvider();
		if(diagramTypeProvider != null) {
			Diagram diagram = diagramTypeProvider.getDiagram();	
			if(diagram != null) {
				EObject bo = Graphiti.getLinkService().getBusinessObjectForLinkedPictogramElement(diagram);
				if (bo instanceof ModelComponent)
					return (ModelComponent) bo;
			}
		}
		
		return null;
	}

	@Override
	public void dispose() {
		mte.unsetTarget(getEditingDomain());
	
		if(superClassListener != null) {
			PlatformUI.getWorkbench().getActiveWorkbenchWindow().getPartService().removePartListener(superClassListener);
			superClassListener.dispose();
		}
		
		super.dispose();
	}

	@Override
	public void createPartControl(Composite parent) {
		super.createPartControl(parent);
		if(!getDiagramBehavior().isAlive()){
			// something is wrong, avoid further exceptions
			return;
		}
		
		superClassListener = new SuperClassListener(this, textEditorClass);
		getWorkbench().getActiveWorkbenchWindow().getPartService().addPartListener(superClassListener);
		getWorkbench().getService(IContextService.class).activateContext(CONTEXT_ID);
		
		/* we have to save here whether changes have been done or not to get rid of the dirty state
		 * CAUTION: save in
		 * init(IEditorSite site, IEditorInput input)
		 * or
		 * setInput(IEditorInput input)
		 * did not work correctly
		 */
//		if (AutoUpdateFeature.isLastDoneChanges())
			doSave(new NullProgressMonitor());
			
	}

	/**
	 * Check whether the given diagram editor manages a super class
	 * @param editor
	 * @return
	 */
	protected boolean registerSuperClassListener(DiagramEditorBase editor) {
		if (!(this.getClass().equals(editor.getClass()) && this.getModelComponent()!=null))
			return false;
		
		ModelComponent editorSc = editor.getModelComponent();
		if (editorSc!=null) {
			URI editorResURI = GlobalNonPlatformURIEditorOpener.getPlatformURI(editorSc.eResource().getURI());
			
			ModelComponent base = getModelComponent();
			while((base = base.getBase()) != null){
				URI baseResURI = GlobalNonPlatformURIEditorOpener.getPlatformURI(base.eResource().getURI());
				if(editorResURI.equals(baseResURI))
					if(editorSc.getComponentName().equals(base.getComponentName()))
						return true;
			}
		}
		
		return false;
	}

	/**
	 * Check whether the given room editor has a super class
	 * @param editor
	 * @return
	 */
	protected boolean registerSuperClassListener(XtextEditor editor) {
		if (!((Class<?>)textEditorClass).isInstance(editor))
			return false;
		if (this.getModelComponent()==null)
			return false;
		
		return editor.getDocument().readOnly(new IUnitOfWork<Boolean, XtextResource>(){
	
			@Override
			public Boolean exec(XtextResource resource) throws Exception {
				
				URI editorResURI = GlobalNonPlatformURIEditorOpener.getPlatformURI(resource.getURI());
				URI thisScResURI = GlobalNonPlatformURIEditorOpener.getPlatformURI(getModelComponent().eResource().getURI());
				
				// ignore if in same file (handled by graphiti)
				if(thisScResURI == null || thisScResURI.equals(editorResURI))
					return false;
				
				ModelComponent base = getModelComponent();
				while((base = base.getBase()) != null){
					URI baseResURI = GlobalNonPlatformURIEditorOpener.getPlatformURI(base.eResource().getURI());
					if(editorResURI.equals(baseResURI))
						return true;
				}
				
				return false;
			}
		});
	}

	protected abstract void handleMissingDiagramBo(Diagram diagram);
	protected abstract void superClassChanged();
	protected abstract EObject getModel();

	public URI getInputUri() {
		return inputUri;
	}
	
	@Override
	public Object getAdapter(@SuppressWarnings("rawtypes") Class key) {
		if (key.equals(IContextProvider.class)) {
			return new SelectedModelHelpProvider(this);
		}
		return super.getAdapter(key);

	}

}

Back to the top