Skip to content

Package: AddRemoveProjectNatureHandler

AddRemoveProjectNatureHandler

nameinstructionbranchcomplexitylinemethod
AddRemoveProjectNatureHandler()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
execute(ExecutionEvent)
M: 61 C: 0
0%
M: 12 C: 0
0%
M: 7 C: 0
0%
M: 20 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2019 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * EclipseSource - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emfforms.ide.internal.builder;
15:
16: import java.util.Iterator;
17:
18: import org.eclipse.core.commands.AbstractHandler;
19: import org.eclipse.core.commands.ExecutionEvent;
20: import org.eclipse.core.commands.ExecutionException;
21: import org.eclipse.core.resources.IProject;
22: import org.eclipse.core.runtime.CoreException;
23: import org.eclipse.core.runtime.IAdaptable;
24: import org.eclipse.jface.viewers.ISelection;
25: import org.eclipse.jface.viewers.IStructuredSelection;
26: import org.eclipse.ui.handlers.HandlerUtil;
27:
28: /**
29: * Add/Remove handler for the View model projects nature.
30: */
31: public class AddRemoveProjectNatureHandler extends AbstractHandler {
32:
33:         @Override
34:         public Object execute(ExecutionEvent event) throws ExecutionException {
35:                 final ISelection selection = HandlerUtil.getCurrentSelection(event);
36:                 String natureID = event.getParameter("natureID");
37:•                if (natureID == null) {
38:                         // Default to view model nature
39:                         natureID = ViewModelNature.NATURE_ID;
40:                 }
41:
42:                 //
43:•                if (selection instanceof IStructuredSelection) {
44:                         for (final Iterator<?> it = ((IStructuredSelection) selection).iterator(); it
45:•                                .hasNext();) {
46:                                 final Object element = it.next();
47:                                 IProject project = null;
48:•                                if (element instanceof IProject) {
49:                                         project = (IProject) element;
50:•                                } else if (element instanceof IAdaptable) {
51:                                         project = ((IAdaptable) element).getAdapter(IProject.class);
52:                                 }
53:•                                if (project != null) {
54:                                         try {
55:                                                 ProjectNature.toggleNature(project, natureID);
56:                                         } catch (final CoreException e) {
57:                                                 Activator.log("Failed to toggle nature", e);
58:                                                 throw new ExecutionException("Failed to toggle nature",
59:                                                         e);
60:                                         }
61:                                 }
62:                         }
63:                 }
64:
65:                 return null;
66:         }
67:
68: }