Skip to content

Package: ProjectNature

ProjectNature

nameinstructionbranchcomplexitylinemethod
ProjectNature(String, String)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
configure()
M: 2 C: 62
97%
M: 2 C: 4
67%
M: 2 C: 2
50%
M: 2 C: 13
87%
M: 0 C: 1
100%
deconfigure()
M: 62 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 14 C: 0
0%
M: 1 C: 0
0%
getProject()
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%
hasNature(IProject)
M: 3 C: 5
63%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 1
33%
M: 0 C: 1
100%
setProject(IProject)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
toggleNature(IProject, String)
M: 33 C: 46
58%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 6 C: 10
63%
M: 0 C: 1
100%

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 org.eclipse.core.resources.ICommand;
17: import org.eclipse.core.resources.IProject;
18: import org.eclipse.core.resources.IProjectDescription;
19: import org.eclipse.core.resources.IProjectNature;
20: import org.eclipse.core.runtime.CoreException;
21:
22: /**
23: * Common project nature implementation.
24: */
25: public abstract class ProjectNature implements IProjectNature {
26:
27:         private final String id;
28:         private final String builder;
29:         private IProject project;
30:
31:         /**
32:          * Initializes me with my unique identifier and builder ID.
33:          *
34:          * @param id my unique identifier
35:          * @param builder my builder
36:          */
37:         protected ProjectNature(String id, String builder) {
38:                 super();
39:
40:                 this.id = id;
41:                 this.builder = builder;
42:         }
43:
44:         @Override
45:         public void configure() throws CoreException {
46:•                if (builder == null) {
47:                         // Nothing to configure;
48:                         return;
49:                 }
50:
51:                 final IProjectDescription desc = project.getDescription();
52:                 final ICommand[] commands = desc.getBuildSpec();
53:
54:•                for (int i = 0; i < commands.length; ++i) {
55:•                        if (commands[i].getBuilderName().equals(builder)) {
56:                                 return;
57:                         }
58:                 }
59:
60:                 final ICommand[] newCommands = new ICommand[commands.length + 1];
61:                 System.arraycopy(commands, 0, newCommands, 0, commands.length);
62:                 final ICommand command = desc.newCommand();
63:                 command.setBuilderName(builder);
64:                 newCommands[newCommands.length - 1] = command;
65:                 desc.setBuildSpec(newCommands);
66:                 project.setDescription(desc, null);
67:         }
68:
69:         @Override
70:         public void deconfigure() throws CoreException {
71:•                if (builder == null) {
72:                         // Nothing to deconfigure;
73:                         return;
74:                 }
75:
76:                 final IProjectDescription description = getProject().getDescription();
77:                 final ICommand[] commands = description.getBuildSpec();
78:•                for (int i = 0; i < commands.length; ++i) {
79:•                        if (commands[i].getBuilderName().equals(builder)) {
80:                                 final ICommand[] newCommands = new ICommand[commands.length - 1];
81:                                 System.arraycopy(commands, 0, newCommands, 0, i);
82:                                 System.arraycopy(commands, i + 1, newCommands, i,
83:                                         commands.length - i - 1);
84:                                 description.setBuildSpec(newCommands);
85:                                 project.setDescription(description, null);
86:                                 return;
87:                         }
88:                 }
89:         }
90:
91:         @Override
92:         public IProject getProject() {
93:                 return project;
94:         }
95:
96:         @Override
97:         public void setProject(IProject project) {
98:                 this.project = project;
99:         }
100:
101:         /**
102:          * Query whether a {@code project} has my nature.
103:          *
104:          * @param project a project
105:          * @return whether it has my nature
106:          */
107:         public boolean hasNature(IProject project) {
108:                 try {
109:                         return project.hasNature(id);
110:                 } catch (final CoreException e) {
111:                         // It doesn't have our nature, then
112:                         return false;
113:                 }
114:         }
115:
116:         /**
117:          * Toggles a nature on a project.
118:          *
119:          * @param project
120:          * to have sample nature added or removed
121:          * @param natureID the nature ID to toggle
122:          * @throws CoreException issue while toggling nature
123:          */
124:         public static void toggleNature(IProject project, String natureID) throws CoreException {
125:                 final IProjectDescription description = project.getDescription();
126:                 final String[] natures = description.getNatureIds();
127:
128:•                for (int i = 0; i < natures.length; ++i) {
129:•                        if (natureID.equals(natures[i])) {
130:                                 // Remove the nature
131:                                 final String[] newNatures = new String[natures.length - 1];
132:                                 System.arraycopy(natures, 0, newNatures, 0, i);
133:                                 System.arraycopy(natures, i + 1, newNatures, i, natures.length - i - 1);
134:                                 description.setNatureIds(newNatures);
135:                                 project.setDescription(description, null);
136:                                 return;
137:                         }
138:                 }
139:
140:                 // Add the nature
141:                 final String[] newNatures = new String[natures.length + 1];
142:                 System.arraycopy(natures, 0, newNatures, 0, natures.length);
143:                 newNatures[natures.length] = natureID;
144:                 description.setNatureIds(newNatures);
145:                 project.setDescription(description, null);
146:         }
147:
148: }