Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0763da119ebaefa56fa8edd86bc75e6ba5b54c52 (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
// $codepro.audit.disable com.instantiations.assist.eclipse.analysis.audit.rule.effectivejava.alwaysOverridetoString.alwaysOverrideToString, com.instantiations.assist.eclipse.analysis.deserializeabilitySecurity, com.instantiations.assist.eclipse.analysis.enforceCloneableUsageSecurity
/*******************************************************************************
 * Copyright (c) 2010 Ericsson Research Canada
 * 
 * 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
 * 
 * Description:
 * 
 * This class implements the context-sensitive command to add an anomaly on 
 * a review item
 * 
 * Contributors:
 *   Sebastien Dubois - Created for Mylyn Review R4E project
 *   
 ******************************************************************************/

package org.eclipse.mylyn.reviews.r4e.ui.internal.commands;

import java.util.Iterator;
import java.util.List;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.ISourceReference;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.text.TextSelection;
import org.eclipse.jface.viewers.AbstractTreeViewer;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ITreeSelection;
import org.eclipse.mylyn.reviews.r4e.core.model.R4EAnomaly;
import org.eclipse.mylyn.reviews.r4e.core.model.R4EFileVersion;
import org.eclipse.mylyn.reviews.r4e.core.model.serial.impl.OutOfSyncException;
import org.eclipse.mylyn.reviews.r4e.core.model.serial.impl.ResourceHandlingException;
import org.eclipse.mylyn.reviews.r4e.core.rfs.spi.ReviewsFileStorageException;
import org.eclipse.mylyn.reviews.r4e.ui.R4EUIPlugin;
import org.eclipse.mylyn.reviews.r4e.ui.internal.model.IR4EUIPosition;
import org.eclipse.mylyn.reviews.r4e.ui.internal.model.R4EUIAnomalyBasic;
import org.eclipse.mylyn.reviews.r4e.ui.internal.model.R4EUIAnomalyContainer;
import org.eclipse.mylyn.reviews.r4e.ui.internal.model.R4EUIComment;
import org.eclipse.mylyn.reviews.r4e.ui.internal.model.R4EUIFileContext;
import org.eclipse.mylyn.reviews.r4e.ui.internal.model.R4EUIModelController;
import org.eclipse.mylyn.reviews.r4e.ui.internal.model.R4EUIReviewBasic;
import org.eclipse.mylyn.reviews.r4e.ui.internal.model.R4EUIReviewItem;
import org.eclipse.mylyn.reviews.r4e.ui.internal.model.R4EUITextPosition;
import org.eclipse.mylyn.reviews.r4e.ui.internal.utils.CommandUtils;
import org.eclipse.mylyn.reviews.r4e.ui.internal.utils.R4EUIConstants;
import org.eclipse.mylyn.reviews.r4e.ui.internal.utils.UIUtils;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.texteditor.ITextEditor;

/**
 * @author lmcdubo
 * @version $Revision: 1.0 $
 */
public class NewAnomalyHandler extends AbstractHandler {

	// ------------------------------------------------------------------------
	// Methods
	// ------------------------------------------------------------------------

	/**
	 * Method execute.
	 * 
	 * @param event
	 *            ExecutionEvent
	 * @return Object
	 * @throws ExecutionException
	 * @see org.eclipse.core.commands.IHandler#execute(ExecutionEvent)
	 */
	public Object execute(final ExecutionEvent event) {

		final IEditorPart editorPart = PlatformUI.getWorkbench()
				.getActiveWorkbenchWindow()
				.getActivePage()
				.getActiveEditor(); // $codepro.audit.disable methodChainLength

		//Act differently depending on the type of selection we get
		final ISelection selection = HandlerUtil.getCurrentSelection(event);
		if (selection instanceof ITextSelection) {
			addAnomalyFromText((ITextSelection) selection);

		} else if (selection instanceof ITreeSelection) {

			//First remove any editor selection (if open) if we execute the command from the review navigator view
			if (null != editorPart && editorPart instanceof ITextEditor) {
				((ITextEditor) editorPart).getSelectionProvider().setSelection(null);
			}

			//Then iterate through all selections	
			for (final Iterator<?> iterator = ((ITreeSelection) selection).iterator(); iterator.hasNext();) {
				addAnomalyFromTree(iterator.next());
			}
		} else if (selection.isEmpty()) {
			//Try to get the active editor highlighted range and set it as the editor's selection
			if (null != editorPart) {
				if (editorPart instanceof ITextEditor) {
					final IRegion region = ((ITextEditor) editorPart).getHighlightRange();
					final TextSelection selectedText = new TextSelection(
							((ITextEditor) editorPart).getDocumentProvider().getDocument(editorPart.getEditorInput()),
							region.getOffset(), region.getLength());
					((ITextEditor) editorPart).getSelectionProvider().setSelection(selectedText);
					addAnomalyFromText(selectedText);
				}
			}
		}
		return null;
	}

	/**
	 * Method addAnomalyFromText.
	 * 
	 * @param aSelection
	 *            ITextSelection
	 */
	private void addAnomalyFromText(ITextSelection aSelection) {
		//This is a text selection in a text editor, we need to get the file path and
		//the position of the selection within the file
		try {
			final R4EUITextPosition position = CommandUtils.getPosition(aSelection);
			final R4EFileVersion baseVersion = CommandUtils.getBaseFileData();
			final R4EFileVersion targetVersion = CommandUtils.getTargetFileData();

			//Add anomaly to model
			if (null != targetVersion) {
				addAnomaly(baseVersion, targetVersion, position);
			} else {
				R4EUIPlugin.Ftracer.traceWarning("Trying to add review item to base file");
				final ErrorDialog dialog = new ErrorDialog(null, R4EUIConstants.DIALOG_TITLE_ERROR,
						"Add Anomaly Error", new Status(IStatus.ERROR, R4EUIPlugin.PLUGIN_ID, 0,
								"No Target File present to Add Anomaly", null), IStatus.ERROR);
				dialog.open();
			}
		} catch (CoreException e) {
			UIUtils.displayCoreErrorDialog(e);
		} catch (ReviewsFileStorageException e) {
			UIUtils.displayReviewsFileStorageErrorDialog(e);
		}
	}

	/**
	 * Method addAnomalyFromTree.
	 * 
	 * @param aSelection
	 *            ITreeSelection
	 */
	private void addAnomalyFromTree(Object aSelection) {

		//This is a selection from the tree view (e.g. Review Navigator, Package Explorer etc...)
		//We will need to get the parent file path and the position of the element in a text editor
		//If the selection is on the File itself, then the selection will include all the lines
		//in the file.  Otherwise it will include all the lines corresponding to the currently 
		//selected element	
		try {

			R4EUITextPosition position = null;
			IFile workspaceFile = null;

			if (aSelection instanceof IFile) {
				position = CommandUtils.getPosition((IFile) aSelection);
				workspaceFile = (IFile) aSelection;
			} else if (R4EUIPlugin.isJDTAvailable() && aSelection instanceof ISourceReference) {
				//NOTE:  This is always true because all elements that implement ISourceReference
				//       also implement IJavaElement.  The resource is always an IFile
				workspaceFile = (IFile) ((IJavaElement) aSelection).getResource();
				//TODO is that the right file to get the position???
				position = CommandUtils.getPosition((ISourceReference) aSelection, workspaceFile);
			} else if (R4EUIPlugin.isCDTAvailable()
					&& aSelection instanceof org.eclipse.cdt.core.model.ISourceReference) {
				//NOTE:  This is always true because all elements that implement ISourceReference
				//       also implement ICElement.  The resource is always an IFile
				if (aSelection instanceof org.eclipse.cdt.core.model.ITranslationUnit) {
					workspaceFile = (IFile) ((org.eclipse.cdt.core.model.ICElement) aSelection).getResource();
				} else if (aSelection instanceof org.eclipse.cdt.core.model.ICElement) {
					workspaceFile = (IFile) ((org.eclipse.cdt.core.model.ICElement) aSelection).getParent()
							.getResource();
				}
				//TODO is that the right file to get the position???
				position = CommandUtils.getPosition((org.eclipse.cdt.core.model.ISourceReference) aSelection,
						workspaceFile);
			} else {
				//This should never happen
				R4EUIPlugin.Ftracer.traceWarning("Invalid selection " + aSelection.getClass().toString()
						+ ".  Ignoring");
				return;
			}

			//Add anomaly to model
			final R4EFileVersion baseVersion = CommandUtils.updateBaseFile(workspaceFile);
			final R4EFileVersion targetVersion = CommandUtils.updateTargetFile(workspaceFile);

			//Add anomaly to model
			if (null != targetVersion) {
				addAnomaly(baseVersion, targetVersion, position);
			} else {
				R4EUIPlugin.Ftracer.traceWarning("Trying to add review item to base file");
				final ErrorDialog dialog = new ErrorDialog(null, R4EUIConstants.DIALOG_TITLE_ERROR,
						"Add Anomaly Error", new Status(IStatus.ERROR, R4EUIPlugin.PLUGIN_ID, 0,
								"No Target File present to Add Anomaly", null), IStatus.ERROR);
				dialog.open();
			}
		} catch (CoreException e) {
			UIUtils.displayCoreErrorDialog(e);
		} catch (ReviewsFileStorageException e) {
			UIUtils.displayReviewsFileStorageErrorDialog(e);
		}
	}

	/**
	 * Method AddAnomaly. Adds an anomaly to the model based on user input
	 * 
	 * @param aBaseFileVersion
	 *            R4EFileVersion
	 * @param aTargetFileVersion
	 *            R4EFileVersion
	 * @param aUIPosition
	 *            IR4EUIPosition
	 */
	private void addAnomaly(R4EFileVersion aBaseFileVersion, R4EFileVersion aTargetFileVersion,
			IR4EUIPosition aUIPosition) {

		try {

			//Check if the file element and/or anomaly already exist
			//If file exists, add anomaly element to it
			//if anomaly element already exist, add a new comment to it
			//for all other cases, create the parent elements as needed as well.
			final List<R4EUIReviewItem> reviewItems = R4EUIModelController.getActiveReview().getReviewItems();

			boolean isNewAnomaly = true;
			for (R4EUIReviewItem reviewItem : reviewItems) {
				R4EUIFileContext[] files = (R4EUIFileContext[]) reviewItem.getChildren();
				for (R4EUIFileContext file : files) {
					if (null != file.getFileContext().getTarget()
							&& aTargetFileVersion.getLocalVersionID().equals(
									file.getFileContext().getTarget().getLocalVersionID())) {

						//File already exists, check if anomaly also exists
						R4EUIAnomalyContainer anomalyContainer = (R4EUIAnomalyContainer) file.getAnomalyContainerElement();
						if (null != anomalyContainer) {
							R4EUIAnomalyBasic[] anomalies = (R4EUIAnomalyBasic[]) anomalyContainer.getChildren();
							for (R4EUIAnomalyBasic uiAnomaly : anomalies) {
								if (uiAnomaly.getPosition().isSameAs(aUIPosition)) {
									isNewAnomaly = false;
									addCommentToExistingAnomaly(uiAnomaly);
									R4EUIPlugin.Ftracer.traceInfo("Added comment to existing anomaly: Target = "
											+ file.getFileContext().getTarget().getName()
											+ ((null != file.getFileContext().getBase()) ? "Base = "
													+ file.getFileContext().getBase().getName() : "") + " Position = "
											+ aUIPosition.toString());
								}
							}
						} else {
							anomalyContainer = new R4EUIAnomalyContainer(file, R4EUIConstants.ANOMALIES_LABEL);
							file.addChildren(anomalyContainer);
						}
						if (isNewAnomaly) {
							addAnomalyToExistingFileContext(aTargetFileVersion, anomalyContainer, aUIPosition);
							R4EUIPlugin.Ftracer.traceInfo("Added anomaly: Target = "
									+ file.getFileContext().getTarget().getName()
									+ ((null != file.getFileContext().getBase()) ? "Base = "
											+ file.getFileContext().getBase().getName() : "") + " Position = "
									+ aUIPosition.toString());
						}
						return; //We found the file so we are done here	
					}
				}
			}

			//This is a new file create it (and its parent reviewItem) and all its children
			addAnomalyToNewFileContext(aBaseFileVersion, aTargetFileVersion, aUIPosition);
			R4EUIPlugin.Ftracer.traceInfo("Added Anomaly: Target = "
					+ aTargetFileVersion.getName()
					+ "_"
					+ aTargetFileVersion.getVersionID()
					+ ((null != aBaseFileVersion) ? "Base = " + aBaseFileVersion.getName() + "_"
							+ aBaseFileVersion.getVersionID() : "") + " Position = " + aUIPosition.toString());

		} catch (ResourceHandlingException e) {
			UIUtils.displayResourceErrorDialog(e);

		} catch (OutOfSyncException e) {
			UIUtils.displaySyncErrorDialog(e);
		}
	}

	/**
	 * Method addCommentToExistingAnomaly.
	 * 
	 * @param aUIAnomaly
	 *            R4EUIAnomaly
	 * @throws ResourceHandlingException
	 * @throws OutOfSyncException
	 */
	private void addCommentToExistingAnomaly(R4EUIAnomalyBasic aUIAnomaly) throws ResourceHandlingException,
			OutOfSyncException {

		final R4EUIComment uiComment = aUIAnomaly.createComment();
		UIUtils.setNavigatorViewFocus(uiComment, AbstractTreeViewer.ALL_LEVELS);
	}

	/**
	 * Method addAnomalyToExistingFileContext.
	 * 
	 * @param aTargetFileVersion
	 *            R4EFileVersion
	 * @param aContainer
	 *            R4EUIAnomalyContainer
	 * @param aUIPosition
	 *            IR4EUIPosition
	 * @throws ResourceHandlingException
	 * @throws OutOfSyncException
	 */
	private void addAnomalyToExistingFileContext(R4EFileVersion aTargetFileVersion, R4EUIAnomalyContainer aContainer,
			IR4EUIPosition aUIPosition) throws ResourceHandlingException, OutOfSyncException {

		final R4EUIAnomalyBasic uiAnomaly = aContainer.createAnomaly(aTargetFileVersion,
				(R4EUITextPosition) aUIPosition);
		UIUtils.setNavigatorViewFocus(uiAnomaly, AbstractTreeViewer.ALL_LEVELS);
	}

	/**
	 * Method addAnomalyToNewFileContext.
	 * 
	 * @param aBaseFileVersion
	 *            R4EFileVersion
	 * @param aTargetFileVersion
	 *            R4EFileVersion
	 * @param aUIPosition
	 *            IR4EUIPosition
	 * @throws ResourceHandlingException
	 * @throws OutOfSyncException
	 */
	private void addAnomalyToNewFileContext(R4EFileVersion aBaseFileVersion, R4EFileVersion aTargetFileVersion,
			IR4EUIPosition aUIPosition) throws ResourceHandlingException, OutOfSyncException {

		final R4EAnomaly tempAnomaly = R4EUIAnomalyContainer.createDetachedAnomaly();

		if (null != tempAnomaly) {
			final R4EUIReviewBasic uiReview = R4EUIModelController.getActiveReview();
			final R4EUIReviewItem uiReviewItem = uiReview.createResourceReviewItem(aTargetFileVersion.getName());
			if (null == uiReviewItem) {
				return;
			}
			final R4EUIFileContext uiFileContext = uiReviewItem.createFileContext(aBaseFileVersion, aTargetFileVersion,
					null);
			if (null == uiFileContext) {
				uiReview.removeChildren(uiReviewItem, false);
				return;
			}

			final R4EUIAnomalyContainer uiAnomalyContainer = new R4EUIAnomalyContainer(uiFileContext,
					R4EUIConstants.ANOMALIES_LABEL);
			uiFileContext.addChildren(uiAnomalyContainer);

			final R4EUIAnomalyBasic uiAnomaly = uiAnomalyContainer.createAnomalyFromDetached(aTargetFileVersion,
					tempAnomaly, (R4EUITextPosition) aUIPosition);
			UIUtils.setNavigatorViewFocus(uiAnomaly, AbstractTreeViewer.ALL_LEVELS);
		}
	}
}

Back to the top