summaryrefslogtreecommitdiffstats
path: root/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/builder/ToggleNatureAction.java
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/builder/ToggleNatureAction.java')
-rw-r--r--plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/builder/ToggleNatureAction.java106
1 files changed, 106 insertions, 0 deletions
diff --git a/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/builder/ToggleNatureAction.java b/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/builder/ToggleNatureAction.java
new file mode 100644
index 0000000..f25148d
--- /dev/null
+++ b/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/builder/ToggleNatureAction.java
@@ -0,0 +1,106 @@
1/*****************************************************************************
2 * Copyright (c) 2009 Ken Gilmer
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * Ken Gilmer - initial API and implementation
10 *******************************************************************************/
11package org.yocto.bc.ui.builder;
12
13import java.util.Iterator;
14
15import org.eclipse.core.resources.IProject;
16import org.eclipse.core.resources.IProjectDescription;
17import org.eclipse.core.runtime.CoreException;
18import org.eclipse.core.runtime.IAdaptable;
19import org.eclipse.jface.action.IAction;
20import org.eclipse.jface.viewers.ISelection;
21import org.eclipse.jface.viewers.IStructuredSelection;
22import org.eclipse.ui.IObjectActionDelegate;
23import org.eclipse.ui.IWorkbenchPart;
24
25public class ToggleNatureAction implements IObjectActionDelegate {
26
27 private ISelection selection;
28
29 /*
30 * (non-Javadoc)
31 *
32 * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
33 */
34 public void run(IAction action) {
35 if (selection instanceof IStructuredSelection) {
36 for (Iterator it = ((IStructuredSelection) selection).iterator(); it
37 .hasNext();) {
38 Object element = it.next();
39 IProject project = null;
40 if (element instanceof IProject) {
41 project = (IProject) element;
42 } else if (element instanceof IAdaptable) {
43 project = (IProject) ((IAdaptable) element)
44 .getAdapter(IProject.class);
45 }
46 if (project != null) {
47 toggleNature(project);
48 }
49 }
50 }
51 }
52
53 /*
54 * (non-Javadoc)
55 *
56 * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction,
57 * org.eclipse.jface.viewers.ISelection)
58 */
59 public void selectionChanged(IAction action, ISelection selection) {
60 this.selection = selection;
61 }
62
63 /*
64 * (non-Javadoc)
65 *
66 * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction,
67 * org.eclipse.ui.IWorkbenchPart)
68 */
69 public void setActivePart(IAction action, IWorkbenchPart targetPart) {
70 }
71
72 /**
73 * Toggles sample nature on a project
74 *
75 * @param project
76 * to have sample nature added or removed
77 */
78 private void toggleNature(IProject project) {
79 try {
80 IProjectDescription description = project.getDescription();
81 String[] natures = description.getNatureIds();
82
83 for (int i = 0; i < natures.length; ++i) {
84 if (BitbakeCommanderNature.NATURE_ID.equals(natures[i])) {
85 // Remove the nature
86 String[] newNatures = new String[natures.length - 1];
87 System.arraycopy(natures, 0, newNatures, 0, i);
88 System.arraycopy(natures, i + 1, newNatures, i,
89 natures.length - i - 1);
90 description.setNatureIds(newNatures);
91 project.setDescription(description, null);
92 return;
93 }
94 }
95
96 // Add the nature
97 String[] newNatures = new String[natures.length + 1];
98 System.arraycopy(natures, 0, newNatures, 0, natures.length);
99 newNatures[natures.length] = BitbakeCommanderNature.NATURE_ID;
100 description.setNatureIds(newNatures);
101 project.setDescription(description, null);
102 } catch (CoreException e) {
103 }
104 }
105
106}