Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: df68443adebfd186b5efb78dc5d796f57f9120e6 (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
/*****************************************************************************
 * Copyright (c) 2010 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.customization.properties.util;

import java.io.File;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.papyrus.customization.properties.Activator;
import org.eclipse.papyrus.views.properties.contexts.Context;
import org.eclipse.papyrus.views.properties.contexts.Section;

/**
 * The Eclipse Editors can only handle files from the workspace. However, when
 * customizing a Property view, we don't want to pollute the user's workspace.
 * The Customization Editor uses a hidden Project. The files are physically
 * stored in the plugin's preferences folder, in the workspace metadata.
 * These methods cannot be applied on contexts registered via plugin extensions,
 * as they are read-only. For such contexts, you need to first copy them,
 * and edit the copy.
 * 
 * @see CopyContextAction
 * 
 * @author Camille Letavernier
 */
public class ProjectUtil {

	/**
	 * The name of the customization's hidden project
	 */
	public static final String CUSTOM_PROJECT_NAME = "org.eclipse.papyrus.customization.properties.internal"; //$NON-NLS-1$

	/**
	 * 
	 * @param context
	 *        The context we want to edit
	 * @return
	 *         The IFile containing the given context. This IFile can be passed
	 *         to an Eclipse editor
	 * @throws CoreException
	 *         If an error occured
	 */
	public static IFile getContextFile(Context context) throws CoreException {
		IFolder projectFolder = getContextFolder(context);

		IFile contextFile = projectFolder.getFile(context.getName() + ".ctx"); //$NON-NLS-1$

		return contextFile;
	}

	/**
	 * @return the hidden IProject used by the customization plugin
	 * @throws CoreException
	 *         If an error occured
	 */
	public static IProject getContextProject() throws CoreException {
		IWorkspace workspace = ResourcesPlugin.getWorkspace();
		IProject custom = workspace.getRoot().getProject(CUSTOM_PROJECT_NAME);
		if(!custom.exists()) {
			IProjectDescription description = workspace.newProjectDescription(CUSTOM_PROJECT_NAME);
			IPath customProjectPath = Activator.getDefault().getPreferencesPath().append("/" + CUSTOM_PROJECT_NAME); //$NON-NLS-1$
			description.setLocation(customProjectPath);
			custom.create(description, null);
			custom.open(null);
		}

		if(!custom.isOpen()) {
			custom.open(null);
		}

		custom.setHidden(true);
		workspace.getRoot().refreshLocal(IResource.DEPTH_INFINITE, null);

		return custom;
	}

	/**
	 * Return the IFolder containing the given context
	 * 
	 * @param context
	 *        The context for which we want to get its IFolder
	 * @return
	 *         The IFolder containing the given context
	 * @throws CoreException
	 *         If an error occured
	 */
	public static IFolder getContextFolder(Context context) throws CoreException {
		IProject project = getContextProject();

		IFolder projectFolder = project.getFolder(context.getName());

		if(!projectFolder.exists()) {
			String referencedFolderPath = new File(context.eResource().getURI().toFileString()).getParent();
			projectFolder.createLink(new Path(referencedFolderPath), IResource.REPLACE, null);
		}

		return projectFolder;
	}

	/**
	 * Return the IFile containing the section's XWT Resource
	 * 
	 * @param section
	 *        The section we want to edit
	 * @return
	 *         The IFile containing the section's XWT Resource
	 * @throws CoreException
	 *         If an error occured
	 */
	public static IFile getSectionFile(Section section) throws CoreException {
		IFolder projectFolder = getContextFolder((Context)(section.eContainer()).eContainer());
		IFile sectionFile = projectFolder.getFile(section.getSectionFile());
		return sectionFile;
	}
}

Back to the top