Skip to main content
summaryrefslogtreecommitdiffstats
blob: 69b922691ced32b4e11542a6eb7a8d9ba136d64c (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
// $codepro.audit.disable com.instantiations.assist.eclipse.analysis.audit.rule.effectivejava.alwaysOverridetoString.alwaysOverrideToString, com.instantiations.assist.eclipse.analysis.deserializeabilitySecurity, com.instantiations.assist.eclipse.analysis.disallowReturnMutable, com.instantiations.assist.eclipse.analysis.enforceCloneableUsageSecurity
/*******************************************************************************
 * Copyright (c) 2011, 2012 Ericsson AB 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
 * 
 * Description:
 * 
 * This class implements the context-sensitive command to add a child element
 * to the navigator view/model
 * 
 * Contributors:
 *   Sebastien Dubois - Created for Mylyn Review R4E project
 *   
 ******************************************************************************/
package org.eclipse.mylyn.reviews.r4e.ui.internal.commands.handlers;

import java.util.List;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.viewers.AbstractTreeViewer;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.mylyn.reviews.core.model.IReviewComponent;
import org.eclipse.mylyn.reviews.r4e.core.model.serial.impl.CompatibilityException;
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.ui.R4EUIPlugin;
import org.eclipse.mylyn.reviews.r4e.ui.internal.model.IR4EUIModelElement;
import org.eclipse.mylyn.reviews.r4e.ui.internal.model.R4EUIModelController;
import org.eclipse.mylyn.reviews.r4e.ui.internal.utils.R4EUIConstants;
import org.eclipse.mylyn.reviews.r4e.ui.internal.utils.UIUtils;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.swt.widgets.Widget;

/**
 * @author Sebastien Dubois
 * @version $Revision: 1.0 $
 */
public class NewChildElementHandler extends AbstractHandler {

	// ------------------------------------------------------------------------
	// Constants
	// ------------------------------------------------------------------------

	/**
	 * Field COMMAND_MESSAGE. (value is ""Adding New Child Element..."")
	 */
	private static final String COMMAND_MESSAGE = "Adding New Child Element...";

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

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

		final IR4EUIModelElement element = getParentElement(aEvent);

		//Get data from user
		if (null != element) {
			try {
				final List<IReviewComponent> tempModelComponents = element.createChildModelDataElement();

				//Create actual model element
				final Job job = new Job(COMMAND_MESSAGE) {
					public String familyName = R4EUIConstants.R4E_UI_JOB_FAMILY;

					@Override
					public boolean belongsTo(Object family) {
						return familyName.equals(family);
					}

					@Override
					public IStatus run(IProgressMonitor monitor) {
						R4EUIModelController.setJobInProgress(true);
						monitor.beginTask(COMMAND_MESSAGE, IProgressMonitor.UNKNOWN);

						for (final IReviewComponent tempModelComponent : tempModelComponents) {
							R4EUIPlugin.Ftracer.traceInfo("Adding child to element " + element.getName());
							IR4EUIModelElement newElement = null;
							try {
								newElement = element.createChildren(tempModelComponent);
								R4EUIModelController.setJobInProgress(false);
								UIUtils.setNavigatorViewFocus(newElement, AbstractTreeViewer.ALL_LEVELS);
							} catch (ResourceHandlingException e) {
								UIUtils.displayResourceErrorDialog(e);

								//Remove object if partially created
								try {
									element.removeChildren(newElement, true);
								} catch (ResourceHandlingException e1) {
									UIUtils.displayResourceErrorDialog(e1);
								} catch (OutOfSyncException e1) {
									UIUtils.displaySyncErrorDialog(e1);
								} catch (CompatibilityException e1) {
									UIUtils.displayCompatibilityErrorDialog(e1);
								}

							} catch (CompatibilityException e) {
								UIUtils.displayCompatibilityErrorDialog(e);

								//Remove object if partially created
								try {
									element.removeChildren(newElement, true);
								} catch (ResourceHandlingException e1) {
									UIUtils.displayResourceErrorDialog(e1);
								} catch (OutOfSyncException e1) {
									UIUtils.displaySyncErrorDialog(e1);
								} catch (CompatibilityException e1) {
									UIUtils.displayCompatibilityErrorDialog(e1);
								}
							} catch (OutOfSyncException e) {
								UIUtils.displaySyncErrorDialog(e);

								//Remove object if partially created
								try {
									element.removeChildren(newElement, true);
								} catch (ResourceHandlingException e1) {
									UIUtils.displayResourceErrorDialog(e1);
								} catch (OutOfSyncException e1) {
									UIUtils.displaySyncErrorDialog(e1);
								} catch (CompatibilityException e1) {
									UIUtils.displayCompatibilityErrorDialog(e1);
								}
							}
						}
						R4EUIModelController.setJobInProgress(false);
						monitor.done();
						return Status.OK_STATUS;
					}
				};
				job.setUser(true);
				job.schedule();
			} catch (ResourceHandlingException e) {
				UIUtils.displayResourceErrorDialog(e);
			}
		}
		return null;
	}

	/**
	 * Method getParentElement.
	 * 
	 * @param event
	 *            ExecutionEvent
	 * @return IR4EUIModelElement
	 */
	private IR4EUIModelElement getParentElement(ExecutionEvent event) {
		final Widget triggerObject = ((Event) event.getTrigger()).widget;
		IR4EUIModelElement element = null;

		if (triggerObject instanceof ToolItem) {
			Object data = ((ToolItem) triggerObject).getParent().getData(R4EUIConstants.ANNOTATION_TOOLBAR);
			if (null == data || !(Boolean) data) {
				//Add element to the root of the tree
				return R4EUIModelController.getRootElement();
			}
		}
		final IStructuredSelection selection = (IStructuredSelection) R4EUIModelController.getNavigatorView()
				.getTreeViewer()
				.getSelection();
		if (!selection.isEmpty()) {
			element = (IR4EUIModelElement) selection.getFirstElement();
		} else {
			//Add element to the root of the tree
			element = R4EUIModelController.getRootElement();
		}
		return element;
	}
}

Back to the top