Commit | Line | Data |
---|---|---|
12c155f5 | 1 | /******************************************************************************* |
60ae41e1 | 2 | * Copyright (c) 2011, 2014 Ericsson |
889da47d | 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 |
f537c959 | 12 | * Patrick Tasse - Refactor resource change listener |
12c155f5 FC |
13 | *******************************************************************************/ |
14 | ||
15 | package org.eclipse.linuxtools.tmf.ui.project.model; | |
16 | ||
f537c959 | 17 | import java.lang.reflect.InvocationTargetException; |
889da47d | 18 | import java.net.URI; |
12c155f5 FC |
19 | import java.util.HashMap; |
20 | import java.util.Map; | |
21 | ||
889da47d | 22 | import org.eclipse.core.resources.IFolder; |
12c155f5 | 23 | import org.eclipse.core.resources.IProject; |
889da47d | 24 | import org.eclipse.core.resources.IProjectDescription; |
f537c959 PT |
25 | import org.eclipse.core.resources.IResourceChangeEvent; |
26 | import org.eclipse.core.resources.IResourceChangeListener; | |
27 | import org.eclipse.core.resources.IResourceDelta; | |
889da47d BH |
28 | import org.eclipse.core.resources.IWorkspace; |
29 | import org.eclipse.core.resources.IWorkspaceRoot; | |
30 | import org.eclipse.core.resources.ResourcesPlugin; | |
31 | import org.eclipse.core.runtime.CoreException; | |
32 | import org.eclipse.core.runtime.IProgressMonitor; | |
33 | import org.eclipse.linuxtools.internal.tmf.ui.Activator; | |
34 | import org.eclipse.linuxtools.tmf.core.TmfCommonConstants; | |
35 | import org.eclipse.linuxtools.tmf.core.TmfProjectNature; | |
f537c959 PT |
36 | import org.eclipse.ui.PlatformUI; |
37 | import org.eclipse.ui.actions.WorkspaceModifyOperation; | |
12c155f5 FC |
38 | |
39 | /** | |
b544077e | 40 | * Factory class storing TMF tracing projects and creating TMF project model elements. |
12c155f5 | 41 | * <p> |
b544077e BH |
42 | * @version 1.0 |
43 | * @author Francois Chouinard | |
12c155f5 | 44 | */ |
f537c959 PT |
45 | public class TmfProjectRegistry implements IResourceChangeListener { |
46 | ||
47 | // Create the singleton instance | |
48 | static { | |
49 | new TmfProjectRegistry(); | |
50 | } | |
12c155f5 | 51 | |
828e5592 | 52 | // The map of project resource to project model elements |
507b1336 | 53 | private static Map<IProject, TmfProjectElement> registry = new HashMap<>(); |
12c155f5 | 54 | |
f537c959 PT |
55 | private TmfProjectRegistry() { |
56 | ResourcesPlugin.getWorkspace().addResourceChangeListener(this); | |
57 | } | |
58 | ||
828e5592 PT |
59 | /** |
60 | * Get the project model element for a project resource | |
61 | * @param project the project resource | |
62 | * @return the project model element or null if it does not exist | |
63 | */ | |
12c155f5 | 64 | public static synchronized TmfProjectElement getProject(IProject project) { |
828e5592 PT |
65 | return getProject(project, false); |
66 | } | |
67 | ||
68 | /** | |
69 | * Get the project model element for a project resource | |
70 | * @param project the project resource | |
71 | * @param force a flag controlling whether a new project should be created if it doesn't exist | |
72 | * @return the project model element | |
73 | */ | |
74 | public static synchronized TmfProjectElement getProject(IProject project, boolean force) { | |
12c155f5 | 75 | TmfProjectElement element = registry.get(project); |
828e5592 | 76 | if (element == null && force) { |
12c155f5 FC |
77 | registry.put(project, new TmfProjectElement(project.getName(), project, null)); |
78 | element = registry.get(project); | |
f537c959 PT |
79 | // force the model to be populated |
80 | element.refreshChildren(); | |
12c155f5 FC |
81 | } |
82 | return element; | |
83 | } | |
84 | ||
889da47d BH |
85 | /** |
86 | * Utility method to create a tracing project. | |
87 | * | |
88 | * @param projectName | |
89 | * - A project name | |
90 | * @param projectLocation | |
91 | * - A project location URI. Use null for default location (which is workspace). | |
92 | * @param monitor | |
93 | * - A progress monitor | |
94 | * @return the IProject object or null | |
95 | * @since 2.0 | |
96 | */ | |
f537c959 | 97 | public static IProject createProject(String projectName, final URI projectLocation, IProgressMonitor monitor) { |
889da47d | 98 | |
f537c959 | 99 | final IWorkspace workspace = ResourcesPlugin.getWorkspace(); |
889da47d | 100 | IWorkspaceRoot root = workspace.getRoot(); |
f537c959 PT |
101 | final IProject project = root.getProject(projectName); |
102 | WorkspaceModifyOperation action = new WorkspaceModifyOperation() { | |
103 | @Override | |
104 | protected void execute(IProgressMonitor progressMonitor) throws CoreException, InvocationTargetException, InterruptedException { | |
105 | if (!project.exists()) { | |
106 | IProjectDescription description = workspace.newProjectDescription(project.getName()); | |
107 | if (projectLocation != null) { | |
108 | description.setLocationURI(projectLocation); | |
109 | } | |
110 | project.create(description, progressMonitor); | |
889da47d | 111 | } |
889da47d | 112 | |
f537c959 PT |
113 | if (!project.isOpen()) { |
114 | project.open(progressMonitor); | |
115 | } | |
889da47d | 116 | |
f537c959 PT |
117 | IProjectDescription description = project.getDescription(); |
118 | description.setNatureIds(new String[] { TmfProjectNature.ID }); | |
119 | project.setDescription(description, null); | |
889da47d | 120 | |
f537c959 PT |
121 | IFolder folder = project.getFolder(TmfTraceFolder.TRACE_FOLDER_NAME); |
122 | if (!folder.exists()) { | |
123 | folder.create(true, true, null); | |
124 | } | |
889da47d | 125 | |
f537c959 PT |
126 | folder = project.getFolder(TmfExperimentFolder.EXPER_FOLDER_NAME); |
127 | if (!folder.exists()) { | |
128 | folder.create(true, true, null); | |
129 | } | |
889da47d | 130 | |
f537c959 PT |
131 | // create folder for supplementary tracing files |
132 | folder = project.getFolder(TmfCommonConstants.TRACE_SUPPLEMENATARY_FOLDER_NAME); | |
889da47d | 133 | |
f537c959 PT |
134 | if (!folder.exists()) { |
135 | folder.create(true, true, null); | |
136 | } | |
889da47d | 137 | } |
f537c959 PT |
138 | }; |
139 | try { | |
140 | PlatformUI.getWorkbench().getProgressService().run(false, false, action); | |
141 | } catch (InvocationTargetException e) { | |
889da47d | 142 | Activator.getDefault().logError("Error creating TMF project " + project.getName(), e); //$NON-NLS-1$ |
f537c959 PT |
143 | } catch (InterruptedException e) { |
144 | } | |
145 | return project; | |
146 | } | |
147 | ||
148 | // ------------------------------------------------------------------------ | |
149 | // IResourceChangeListener | |
150 | // ------------------------------------------------------------------------ | |
151 | ||
152 | /** | |
153 | * @since 3.0 | |
154 | */ | |
155 | @Override | |
156 | public void resourceChanged(IResourceChangeEvent event) { | |
157 | if (event.getType() == IResourceChangeEvent.POST_CHANGE) { | |
158 | for (IResourceDelta delta : event.getDelta().getAffectedChildren()) { | |
159 | if (delta.getResource() instanceof IProject) { | |
160 | IProject project = (IProject) delta.getResource(); | |
161 | try { | |
162 | if (delta.getKind() == IResourceDelta.CHANGED && | |
163 | project.isOpen() && project.hasNature(TmfProjectNature.ID)) { | |
164 | TmfProjectElement projectElement = getProject(project, true); | |
165 | projectElement.refresh(); | |
166 | } else if (delta.getKind() == IResourceDelta.REMOVED) { | |
167 | registry.remove(project); | |
168 | } | |
169 | } catch (CoreException e) { | |
170 | Activator.getDefault().logError("Error handling resource change event for " + project.getName(), e); //$NON-NLS-1$ | |
171 | } | |
172 | } | |
173 | } | |
889da47d | 174 | } |
889da47d BH |
175 | } |
176 | ||
12c155f5 | 177 | } |