1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 Ericsson
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Francois Chouinard - Initial API and implementation
11 * Bernd Hufmann - Added project creation utility
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
;
17 import java
.util
.HashMap
;
20 import org
.eclipse
.core
.resources
.IFolder
;
21 import org
.eclipse
.core
.resources
.IProject
;
22 import org
.eclipse
.core
.resources
.IProjectDescription
;
23 import org
.eclipse
.core
.resources
.IWorkspace
;
24 import org
.eclipse
.core
.resources
.IWorkspaceRoot
;
25 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
26 import org
.eclipse
.core
.runtime
.CoreException
;
27 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
28 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Activator
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.TmfCommonConstants
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.TmfProjectNature
;
33 * Factory class storing TMF tracing projects and creating TMF project model elements.
36 * @author Francois Chouinard
38 public class TmfProjectRegistry
{
40 // The map of project resource to project model elements
41 private static Map
<IProject
, TmfProjectElement
> registry
= new HashMap
<IProject
, TmfProjectElement
>();
44 * Get the project model element for a project resource
45 * @param project the project resource
46 * @return the project model element or null if it does not exist
48 public static synchronized TmfProjectElement
getProject(IProject project
) {
49 return getProject(project
, false);
53 * Get the project model element for a project resource
54 * @param project the project resource
55 * @param force a flag controlling whether a new project should be created if it doesn't exist
56 * @return the project model element
58 public static synchronized TmfProjectElement
getProject(IProject project
, boolean force
) {
59 TmfProjectElement element
= registry
.get(project
);
60 if (element
== null && force
) {
61 registry
.put(project
, new TmfProjectElement(project
.getName(), project
, null));
62 element
= registry
.get(project
);
68 * Utility method to create a tracing project.
72 * @param projectLocation
73 * - A project location URI. Use null for default location (which is workspace).
75 * - A progress monitor
76 * @return the IProject object or null
79 public static IProject
createProject(String projectName
, URI projectLocation
, IProgressMonitor monitor
) {
81 IWorkspace workspace
= ResourcesPlugin
.getWorkspace();
82 IWorkspaceRoot root
= workspace
.getRoot();
83 IProject project
= root
.getProject(projectName
);
85 if (!project
.exists()) {
86 IProjectDescription description
= workspace
.newProjectDescription(project
.getName());
87 if (projectLocation
!= null) {
88 description
.setLocationURI(projectLocation
);
90 project
.create(description
, monitor
);
93 if (!project
.isOpen()) {
94 project
.open(monitor
);
97 IProjectDescription description
= project
.getDescription();
98 description
.setNatureIds(new String
[] { TmfProjectNature
.ID
});
99 project
.setDescription(description
, null);
101 IFolder folder
= project
.getFolder(TmfTraceFolder
.TRACE_FOLDER_NAME
);
102 if (!folder
.exists()) {
103 folder
.create(true, true, null);
106 folder
= project
.getFolder(TmfExperimentFolder
.EXPER_FOLDER_NAME
);
107 if (!folder
.exists()) {
108 folder
.create(true, true, null);
111 // create folder for supplementary tracing files
112 folder
= project
.getFolder(TmfCommonConstants
.TRACE_SUPPLEMENATARY_FOLDER_NAME
);
114 if (!folder
.exists()) {
115 folder
.create(true, true, null);
118 } catch (CoreException e
) {
119 Activator
.getDefault().logError("Error creating TMF project " + project
.getName(), e
); //$NON-NLS-1$