Commit | Line | Data |
---|---|---|
12c155f5 | 1 | /******************************************************************************* |
889da47d BH |
2 | * Copyright (c) 2011, 2013 Ericsson |
3 | * | |
12c155f5 FC |
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 | |
889da47d | 8 | * |
12c155f5 FC |
9 | * Contributors: |
10 | * Francois Chouinard - Initial API and implementation | |
889da47d | 11 | * Bernd Hufmann - Added project creation utility |
12c155f5 FC |
12 | *******************************************************************************/ |
13 | ||
14 | package org.eclipse.linuxtools.tmf.ui.project.model; | |
15 | ||
889da47d | 16 | import java.net.URI; |
12c155f5 FC |
17 | import java.util.HashMap; |
18 | import java.util.Map; | |
19 | ||
889da47d | 20 | import org.eclipse.core.resources.IFolder; |
12c155f5 | 21 | import org.eclipse.core.resources.IProject; |
889da47d BH |
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; | |
12c155f5 FC |
31 | |
32 | /** | |
b544077e | 33 | * Factory class storing TMF tracing projects and creating TMF project model elements. |
12c155f5 | 34 | * <p> |
b544077e BH |
35 | * @version 1.0 |
36 | * @author Francois Chouinard | |
12c155f5 FC |
37 | */ |
38 | public class TmfProjectRegistry { | |
39 | ||
828e5592 | 40 | // The map of project resource to project model elements |
507b1336 | 41 | private static Map<IProject, TmfProjectElement> registry = new HashMap<>(); |
12c155f5 | 42 | |
828e5592 PT |
43 | /** |
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 | |
47 | */ | |
12c155f5 | 48 | public static synchronized TmfProjectElement getProject(IProject project) { |
828e5592 PT |
49 | return getProject(project, false); |
50 | } | |
51 | ||
52 | /** | |
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 | |
57 | */ | |
58 | public static synchronized TmfProjectElement getProject(IProject project, boolean force) { | |
12c155f5 | 59 | TmfProjectElement element = registry.get(project); |
828e5592 | 60 | if (element == null && force) { |
12c155f5 FC |
61 | registry.put(project, new TmfProjectElement(project.getName(), project, null)); |
62 | element = registry.get(project); | |
63 | } | |
64 | return element; | |
65 | } | |
66 | ||
889da47d BH |
67 | /** |
68 | * Utility method to create a tracing project. | |
69 | * | |
70 | * @param projectName | |
71 | * - A project name | |
72 | * @param projectLocation | |
73 | * - A project location URI. Use null for default location (which is workspace). | |
74 | * @param monitor | |
75 | * - A progress monitor | |
76 | * @return the IProject object or null | |
77 | * @since 2.0 | |
78 | */ | |
79 | public static IProject createProject(String projectName, URI projectLocation, IProgressMonitor monitor) { | |
80 | ||
81 | IWorkspace workspace = ResourcesPlugin.getWorkspace(); | |
82 | IWorkspaceRoot root = workspace.getRoot(); | |
83 | IProject project = root.getProject(projectName); | |
84 | try { | |
85 | if (!project.exists()) { | |
86 | IProjectDescription description = workspace.newProjectDescription(project.getName()); | |
87 | if (projectLocation != null) { | |
88 | description.setLocationURI(projectLocation); | |
89 | } | |
90 | project.create(description, monitor); | |
91 | } | |
92 | ||
93 | if (!project.isOpen()) { | |
94 | project.open(monitor); | |
95 | } | |
96 | ||
97 | IProjectDescription description = project.getDescription(); | |
98 | description.setNatureIds(new String[] { TmfProjectNature.ID }); | |
99 | project.setDescription(description, null); | |
100 | ||
101 | IFolder folder = project.getFolder(TmfTraceFolder.TRACE_FOLDER_NAME); | |
102 | if (!folder.exists()) { | |
103 | folder.create(true, true, null); | |
104 | } | |
105 | ||
106 | folder = project.getFolder(TmfExperimentFolder.EXPER_FOLDER_NAME); | |
107 | if (!folder.exists()) { | |
108 | folder.create(true, true, null); | |
109 | } | |
110 | ||
111 | // create folder for supplementary tracing files | |
112 | folder = project.getFolder(TmfCommonConstants.TRACE_SUPPLEMENATARY_FOLDER_NAME); | |
113 | ||
114 | if (!folder.exists()) { | |
115 | folder.create(true, true, null); | |
116 | } | |
117 | return project; | |
118 | } catch (CoreException e) { | |
119 | Activator.getDefault().logError("Error creating TMF project " + project.getName(), e); //$NON-NLS-1$ | |
120 | } | |
121 | return null; | |
122 | } | |
123 | ||
12c155f5 | 124 | } |