Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 253339a1b4ff2307d4f28ba12839de256727ed9c (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
/*****************************************************************************
 * 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:
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.eclipse.project.editors.project;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Set;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.papyrus.eclipse.project.editors.Activator;
import org.eclipse.papyrus.eclipse.project.editors.interfaces.IProjectEditor;

/**
 *
 * This class allows to manage the eclipse project
 *
 */
public class ProjectEditor extends AbstractProjectEditor implements IProjectEditor {


	/**
	 *
	 * Constructor.
	 *
	 * @param project
	 *            the eclipse project
	 * @throws CoreException
	 */
	public ProjectEditor(final IProject project) throws CoreException {
		super(project);
	}

	/**
	 *
	 * Constructor.
	 *
	 * @param folder
	 *            a folder
	 * @throws CoreException
	 */
	public ProjectEditor(final IFolder folder) throws CoreException {
		super(null);
		// TODO : create an action to import a folder as a project!
		// this will allow to test the create method
		throw new UnsupportedOperationException();
	}



	/**
	 * Create the project file
	 * TODO NOT TESTED
	 *
	 * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IProjectEditor#createFiles(Set)
	 *
	 *      {@inheritDoc}
	 */
	public void createFiles(final Set<String> files) {
		if (files.contains(PROJECT_FILE)) {
			final IFile file = getProject().getFile(PROJECT_FILE);
			if (!file.exists()) {
				String input = ""; //$NON-NLS-1$
				input += AbstractProjectEditor.XML_HEADER;
				input += "<" + NAME + ">"; //$NON-NLS-1$ //$NON-NLS-2$
				input += getProject().getName();
				input += "</" + NAME + ">"; //$NON-NLS-1$ //$NON-NLS-2$
				input += "<" + COMMENT + ">"; //$NON-NLS-1$ //$NON-NLS-2$
				input += "</" + COMMENT + ">"; //$NON-NLS-1$ //$NON-NLS-2$
				input += "<" + BUILD_SPEC + ">"; //$NON-NLS-1$ //$NON-NLS-2$
				input += "</" + BUILD_SPEC + ">"; //$NON-NLS-1$ //$NON-NLS-2$
				input += "<" + NATURE + ">"; //$NON-NLS-1$ //$NON-NLS-2$
				input += "</" + NATURE + ">"; //$NON-NLS-1$ //$NON-NLS-2$

				try {
					file.create(getInputStream(input), true, null);
				} catch (final CoreException e) {
					Activator.log.error(e);
				}
			}
		}
	}


	/**
	 *
	 * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IProjectEditor#getMissingFiles()
	 *
	 *      {@inheritDoc}
	 */
	@Override
	public Set<String> getMissingFiles() {
		final Set<String> missingFile = super.getMissingFiles();
		final IFile projectFile = getProject().getFile(PROJECT_FILE);
		if (!projectFile.exists()) {
			missingFile.add(IProjectEditor.PROJECT_FILE);
		}
		return missingFile;
	}

	/**
	 *
	 * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IProjectEditor#addFile(java.net.URL, java.lang.String)
	 *
	 * @param url
	 * @param fileDestinationPath
	 * @param eraseExitingFile
	 */
	public void addFile(final URL url, final String fileDestinationPath, final boolean eraseExitingFile) {
		final IFile targetFile = getProject().getFile(new Path(fileDestinationPath));
		if (targetFile.exists()) {
			if (eraseExitingFile) {
				try {
					targetFile.delete(true, new NullProgressMonitor());
				} catch (final CoreException e) {
					Activator.log.error(e);
				}
			} else {
				return;
			}
		}
		try {
			final InputStream is = url.openStream();
			;
			targetFile.create(is, false, new NullProgressMonitor());
			is.close();
			targetFile.refreshLocal(IResource.DEPTH_ZERO, new NullProgressMonitor());
		} catch (final CoreException e) {
			Activator.log.error(e);
		} catch (final IOException e) {
			Activator.log.error(e);
		}


	}
}

Back to the top