Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0794a9dcd82b024a3fb9f105f5e3a983fd9ac8e1 (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
385
386
387
388
389
390
391
392
393
394
395
396
/*******************************************************************************
 * Copyright (c) 2011 Formal Mind GmbH and University of Dusseldorf.
 * 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:
 *     Michael Jastram - initial API and implementation
 ******************************************************************************/
package org.eclipse.rmf.pror.reqif10.editor.presentation;

import java.util.Collection;
import java.util.EventObject;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.common.command.BasicCommandStack;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.command.CommandStack;
import org.eclipse.emf.common.command.CommandStackListener;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.impl.AdapterImpl;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.edit.domain.IEditingDomainProvider;
import org.eclipse.emf.edit.provider.ItemProviderAdapter;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.rmf.pror.reqif10.editor.actions.SpecificationWebPrintAction;
import org.eclipse.rmf.pror.reqif10.editor.agilegrid.ProrAgileGridViewer;
import org.eclipse.rmf.pror.reqif10.util.ProrUtil;
import org.eclipse.rmf.reqif10.ReqIf;
import org.eclipse.rmf.reqif10.Reqif10Package;
import org.eclipse.rmf.reqif10.Specification;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.part.EditorPart;
import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
import org.eclipse.ui.views.properties.IPropertySheetPage;

/**
 * @author Lukas Ladenberger
 * @author Michael Jastram
 */
public class SpecificationEditor extends EditorPart implements
		IEditingDomainProvider, IMenuListener, ISelectionProvider {

	public static final String EDITOR_ID = "org.eclipse.rmf.pror.reqif10.SpecificationEditor";

	/**
	 * The {@link Specification} associated with this Editor.
	 */
	private Specification specification;

	/**
	 * The {@link ProrAgileGridViewer} for this Editor.
	 */
	private ProrAgileGridViewer prorAgileGridViewer;

	/**
	 * The {@link Reqif10Editor} of the owning {@link ReqIf} object. We keep a
	 * reference, as we reuse a number of elements from that editor (Property
	 * View, Outline View, EditingDomain). We keep the save status of the
	 * Editors synchronized via the EditingDomain's Command Stack.
	 */
	private Reqif10Editor reqifEditor;

	// A number of Listeners
	private ISelectionChangedListener selectionChangedListener;
	private CommandStackListener commandStackListener;
	private AdapterImpl changeNameListener;
	private AdapterImpl deleteSpecListener;

	/**
	 * Initializes the Editor.
	 */
	@Override
	public void init(IEditorSite site, IEditorInput input) throws PartInitException {
		// Sanity Check
		if (!(input instanceof ReqifSpecificationEditorInput)) {
			throw new IllegalArgumentException("Wrong input type: " + input);
		}

		// Extracting Info from the input
		reqifEditor = ((ReqifSpecificationEditorInput)input).getReqifEditor();
		specification = ((ReqifSpecificationEditorInput)input).getSpec();

		// Part Setup
		setSite(site);
		setInputWithNotify(input);
		setPartName(input.getName());
		site.setSelectionProvider(this);
		
		site.getActionBars().setGlobalActionHandler(
				ActionFactory.PRINT.getId(),
				new SpecificationWebPrintAction(specification, reqifEditor.getEditingDomain()));
	}

	/**
	 * Builds the Part, which is an {@link ProrAgileGridViewer} and registers a
	 * number of Listeners.
	 */
	@Override
	public void createPartControl(final Composite parent) {
		createSpecificationPart(parent);		

		// Order matters!
		registerChangeNameListener();
		registerDeleteListener();
		registerSelectionChangedListener();
		registerCommandStackListener(parent);
	}

	/**
	 * Builds the actual {@link ProrAgileGridViewer}
	 * @param containter
	 */
	private void createSpecificationPart(Composite containter) {
		prorAgileGridViewer = new ProrAgileGridViewer(containter,
				reqifEditor.getAdapterFactory(), getEditingDomain());
		prorAgileGridViewer.setInput(specification);
		prorAgileGridViewer.setContextMenu(buildContextMenu());
	}

	/**
	 * Registers a command stack listener that updates the save state and
	 * updates the selection.
	 */
	private void registerCommandStackListener(final Composite parent) {
		commandStackListener = new CommandStackListener() {
			public void commandStackChanged(final EventObject event) {
				parent.getDisplay().asyncExec(new Runnable() {
					public void run() {
						firePropertyChange(IEditorPart.PROP_DIRTY);
						// Try to select the affected objects.
						Command mostRecentCommand = ((CommandStack) event
								.getSource()).getMostRecentCommand();
						if (mostRecentCommand != null) {
							Collection<?> affectedObjects = mostRecentCommand
									.getAffectedObjects();
							setSelectionToViewer(affectedObjects);
						}
					}
				});
			}
		};
		getEditingDomain().getCommandStack().addCommandStackListener(
				commandStackListener);
	}

	/**
	 * Upon a change of the name of the Specification, the Part must be renamed.
	 */
	private void registerChangeNameListener() {
		changeNameListener = new AdapterImpl() {
			@Override
			public void notifyChanged(Notification notification) {
				if (notification.getFeature() == Reqif10Package.Literals.SPEC_ELEMENT_WITH_ATTRIBUTES__VALUES) {
					ItemProviderAdapter ip = ProrUtil.getItemProvider(reqifEditor.getAdapterFactory(), specification);
					setPartName(ip.getText(specification));
				}
			}
		};
		specification.eAdapters().add(changeNameListener);
	}

	/**
	 * If the Specification is deleted, we must close the editor.
	 */
	private void registerDeleteListener() {
		final EObject container = specification.eContainer();
		deleteSpecListener = new AdapterImpl() {
			@Override
			public void notifyChanged(Notification msg) {
				if (msg.getFeature() == Reqif10Package.Literals.SPECIFICATION__CHILDREN
						|| msg.getEventType() == Notification.REMOVE
						&& msg.getOldValue() == specification) {

					getEditorSite().getPage().closeEditor(
							SpecificationEditor.this, false);
				}
			}
		};
		container.eAdapters().add(deleteSpecListener);
	}

	/**
	 * This sets the selection into whichever viewer is active. Code taken from
	 * the generated {@link Reqif10Editor#setSelectionToViewer(Collection)}
	 */
	private void setSelectionToViewer(Collection<?> collection) {
		final Collection<?> theSelection = collection;
		// Make sure it's okay.
		//
		if (theSelection != null && !theSelection.isEmpty()) {
			Runnable runnable =
				new Runnable() {
					public void run() {

						// Try to select the items in the current content viewer of the editor.
						//
						if (prorAgileGridViewer != null) {
							prorAgileGridViewer.setSelection(new StructuredSelection(theSelection.toArray()), true);
						}

					}
				};
			getSite().getShell().getDisplay().syncExec(runnable);
		}
	}

	private void registerSelectionChangedListener() {
		selectionChangedListener = new ISelectionChangedListener() {
			public void selectionChanged(SelectionChangedEvent event) {
				if(event.getSource() != prorAgileGridViewer){
				SpecificationEditor.this.setSelection(event.getSelection());
			}}
		};
		prorAgileGridViewer.addSelectionChangedListener(selectionChangedListener);
	}

	/**
	 * Delegate populating the context menu to EMF.
	 */
	private MenuManager buildContextMenu() {
		MenuManager contextMenu = new MenuManager("#PopUp");
		contextMenu.add(new Separator("additions"));
		contextMenu.setRemoveAllWhenShown(true);
		contextMenu.addMenuListener(this);
		return contextMenu;
	}

	/**
	 * We use the outline and property view from {@link Reqif10Editor}.
	 */
	@SuppressWarnings("rawtypes")
	@Override
	public Object getAdapter(Class key) {
		if (key.equals(IContentOutlinePage.class)) {
			return reqifEditor.getContentOutlinePage();
		} else if (key.equals(IPropertySheetPage.class)) {
			return reqifEditor.getPropertySheetPage();
		} else {
			return super.getAdapter(key);
		}
	}

	/**
	 * Delegate selection management to {@link #prorAgileGridViewer}.
	 */
	public void addSelectionChangedListener(ISelectionChangedListener listener) {
		prorAgileGridViewer.addSelectionChangedListener(listener);
	}

	/**
	 * Delegate selection management to {@link #prorAgileGridViewer}.
	 */
	public void removeSelectionChangedListener(
			ISelectionChangedListener listener) {
		prorAgileGridViewer.removeSelectionChangedListener(listener);
	}

	/**
	 * Delegate selection management to {@link #prorAgileGridViewer}.
	 */
	public ISelection getSelection() {
		return prorAgileGridViewer.getSelection();
	}

	/**
	 * Delegate selection management to {@link #prorAgileGridViewer}.
	 */
	public void setSelection(ISelection selection) {
		prorAgileGridViewer.setSelection(selection);
		reqifEditor.setStatusLineManager(selection);
}

	/**
	 * This implements {@link org.eclipse.jface.action.IMenuListener} to help
	 * fill the context menus with contributions from the Edit menu.
	 */
	public void menuAboutToShow(IMenuManager menuManager) {
		((IMenuListener)getEditorSite().getActionBarContributor()).menuAboutToShow(menuManager);
	}

	/**
	 * The {@link EditingDomain} from the {@link Reqif10Editor}.
	 */
	public EditingDomain getEditingDomain() {
		return reqifEditor.getEditingDomain();
	}

	/**
	 * Synchronized with {@link Reqif10Editor}
	 */
	@Override
	public void doSave(IProgressMonitor monitor) {
		reqifEditor.doSave(monitor);
		firePropertyChange(IEditorPart.PROP_DIRTY);
	}

	/**
	 * Synchronized with {@link Reqif10Editor}
	 */
	@Override
	public void doSaveAs() {
		reqifEditor.doSaveAs();
		firePropertyChange(IEditorPart.PROP_DIRTY);
	}

	/**
	 * Synchronized with the {@link EditingDomain} from the {@link Reqif10Editor}
	 */
	@Override
	public boolean isDirty() {
		return ((BasicCommandStack)getEditingDomain().getCommandStack()).isSaveNeeded();
	}

	/**
	 * @return true
	 */
	@Override
	public boolean isSaveAsAllowed() {
		return false;
	}

	/**
	 * Delegated to {@link #prorAgileGridViewer}.
	 */
	@Override
	public void setFocus() {
		prorAgileGridViewer.getControl().setFocus();
	}

	/**
	 * Detach all listeners.
	 */
	@Override
	public void dispose() {
		prorAgileGridViewer.dispose();

		if (selectionChangedListener != null) {
			prorAgileGridViewer.removeSelectionChangedListener(selectionChangedListener);
			selectionChangedListener = null;
		}
		if (commandStackListener != null) {
			getEditingDomain().getCommandStack().removeCommandStackListener(commandStackListener);
			commandStackListener = null;
		}
		if (changeNameListener != null) {
			specification.eAdapters().remove(changeNameListener);
			changeNameListener = null;
		}
		if (deleteSpecListener != null) {
			specification.eAdapters().remove(deleteSpecListener);
			deleteSpecListener = null;
		}

		if (reqifEditor.getActionBarContributor().getActiveEditor() == this) {
			reqifEditor.getActionBarContributor().setActiveEditor(null);
		}
	
		super.dispose();
	}

	// TODO I would like to remove this.
	public Reqif10Editor getReqifEditor() {
		return reqifEditor;
	}

	/**
	 * Forward requests to show or hide SpecRelations.
	 */
	public void setShowSpecRelations(boolean checked) {
		prorAgileGridViewer.setShowSpecRelations(checked);
	}

	/**
	 * Only the "Main" Editor ({@link Reqif10Editor}) requires saving.
	 */
	@Override
	public boolean isSaveOnCloseNeeded() {
		return false;
	}
}

Back to the top