Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e16d91c449fcf20652f5f453c8a60b6960f608ed (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
/*****************************************************************************
 * Copyright (c) 2011 CEA LIST.
 *
 *
 * 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
 *
 * Contributors:
 *  Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr
 *
 *****************************************************************************/
package org.eclipse.papyrus.eclipse.project.editors.file;


import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.AssertionFailedException;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IAccessRule;
import org.eclipse.jdt.core.IClasspathAttribute;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.internal.core.ClasspathEntry;
import org.eclipse.papyrus.eclipse.project.editors.Activator;
import org.eclipse.papyrus.eclipse.project.editors.interfaces.IClasspathEditor;

/**
 *
 * The editor for the classpath file
 *
 */
public class ClasspathEditor extends AbstractFileEditor implements IClasspathEditor {

	/**
	 * the edited java project
	 */
	private final IJavaProject javaProject;


	/**
	 *
	 * Constructor.
	 *
	 * @param project
	 */
	public ClasspathEditor(final IProject project) throws AssertionFailedException {
		super(project);
		this.javaProject = JavaCore.create(project);
	}

	/**
	 *
	 * Constructor.
	 *
	 * @param javaProject
	 *            a java project
	 */
	public ClasspathEditor(final IJavaProject javaProject) {
		super(javaProject.getProject());
		this.javaProject = javaProject;
	}

	@Override
	public void init() {
		// nothing to do here
	}

	/**
	 * save the modification
	 *
	 * @throws Throwable
	 */
	public void save() {
		try {
			this.javaProject.save(new NullProgressMonitor(), true);
		} catch (JavaModelException ex) {
			Activator.log.error(ex);
		}
	}

	/**
	 *
	 * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IClasspathEditor#addSourceFolderToClasspath(java.lang.String)
	 *
	 *      {@inheritDoc}
	 */
	public void addSourceFolderToClasspath(final String folderPath) {

		if (exists() && !isSourceFolderRegistered(folderPath)) {

			// parameters for the new ClasspathEntry
			boolean isExported = false;
			IPath[] exclusionPatterns = new IPath[0];
			IPath sourceAttachmentPath = null;
			IPath specificOutputLocation = null;
			boolean combineAccessRules = false;
			IClasspathAttribute[] extraAttributes = new IClasspathAttribute[0];
			IPath sourceAttachmentRootPath = null;
			IAccessRule[] accessRules = null;
			int contentKind = IPackageFragmentRoot.K_SOURCE;
			IPath[] inclusionPatterns = new IPath[0];
			int entryKind = IClasspathEntry.CPE_SOURCE;
			IPath path2 = new Path(folderPath);

			IClasspathEntry[] classpathes = null;
			try {
				classpathes = this.javaProject.getRawClasspath();
			} catch (JavaModelException e) {
				e.printStackTrace();
			}

			IClasspathEntry[] entries = new IClasspathEntry[classpathes.length + 1];
			for (int i = 0; i < classpathes.length; i++) {
				entries[i] = classpathes[i];
			}
			entries[classpathes.length] = new ClasspathEntry(contentKind, entryKind, path2, inclusionPatterns, exclusionPatterns, sourceAttachmentPath, sourceAttachmentRootPath, specificOutputLocation, isExported, accessRules, combineAccessRules,
					extraAttributes);
			try {
				this.javaProject.setRawClasspath(entries, new NullProgressMonitor());
			} catch (JavaModelException e) {
				Activator.log.error(e);
			}
		}
	}

	/**
	 *
	 * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IClasspathEditor#isSourceFolderRegistered(java.lang.String)
	 *
	 *      {@inheritDoc}
	 */
	public boolean isSourceFolderRegistered(final String folderPath) {
		IClasspathEntry[] entries = null;
		try {
			entries = this.javaProject.getRawClasspath();
		} catch (JavaModelException e) {
			Activator.log.error(e);
		}

		for (int i = 0; i < entries.length; i++) {
			IClasspathEntry entry = entries[i];
			if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
				if (entry.getPath().equals(new Path(folderPath))) {
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * Tests if the classpath file exists
	 *
	 * @see org.eclipse.papyrus.eclipse.project.editors.project.AbstractProjectEditor.plugin.AbstractEditor#exists()
	 *
	 *      {@inheritDoc}
	 */
	@Override
	public boolean exists() {
		IFile classpath = getProject().getFile(CLASSPATH_FILE);
		return classpath.exists();
	}

	/**
	 *
	 * @see org.eclipse.papyrus.eclipse.project.editors.project.ProjectEditor#getMissingFiles()
	 *
	 *      {@inheritDoc}
	 */
	@Override
	public Set<String> getMissingFiles() {
		Set<String> files = super.getMissingFiles();
		IFile classpath = getProject().getFile(CLASSPATH_FILE);
		if (!classpath.exists()) {
			files.add(CLASSPATH_FILE);
		}
		return files;
	}

	/**
	 *
	 * @see org.eclipse.papyrus.eclipse.project.editors.project.ProjectEditor#createFiles(Set)
	 *
	 *      {@inheritDoc}
	 */
	public void createFiles(final Set<String> files) {
		if (files.contains(CLASSPATH_FILE)) {
			IFile classpath = getProject().getFile(CLASSPATH_FILE);
			if (!classpath.exists()) {
				InputStream is = getInputStream("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<?eclipse version=\"3.4\"?>\n" + "<classpath>\n" + "</classpath>\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$

				try {
					classpath.create(is, true, null);
				} catch (CoreException ex) {
					Activator.log.error(ex);
				}
			}
		}
	}

	public String[] getSourceFolders() {
		List<String> sourceFolders = new LinkedList<String>();
		IClasspathEntry[] entries = null;
		try {
			entries = this.javaProject.getRawClasspath();
		} catch (JavaModelException e) {
			Activator.log.error(e);
		}

		for (int i = 0; i < entries.length; i++) {
			IClasspathEntry entry = entries[i];
			if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
				sourceFolders.add(entry.getPath().makeRelativeTo(javaProject.getPath()).toString());
			}
		}

		return sourceFolders.toArray(new String[sourceFolders.size()]);
	}

	public String[] getBinFolders() {
		List<String> binFolders = new LinkedList<String>();
		try {
			// General bin folder
			binFolders.add(javaProject.getOutputLocation().makeRelativeTo(javaProject.getPath()).toString());
		} catch (JavaModelException ex) {
			Activator.log.error(ex);
		}
		IClasspathEntry[] entries = null;
		try {
			entries = this.javaProject.getRawClasspath();
		} catch (JavaModelException e) {
			Activator.log.error(e);
		}

		for (int i = 0; i < entries.length; i++) {
			IClasspathEntry entry = entries[i];
			if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
				if (entry.getOutputLocation() != null) {
					// Bin folder associated to each source folder
					binFolders.add(entry.getOutputLocation().makeRelativeTo(javaProject.getPath()).toString());
				}
			}
		}

		return binFolders.toArray(new String[binFolders.size()]);
	}
}

Back to the top