1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 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 * Patrick Tasse - Refactor resource change listener
13 *******************************************************************************/
15 package org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
;
17 import java
.lang
.reflect
.InvocationTargetException
;
19 import java
.util
.HashMap
;
20 import java
.util
.List
;
23 import org
.eclipse
.core
.resources
.IFolder
;
24 import org
.eclipse
.core
.resources
.IProject
;
25 import org
.eclipse
.core
.resources
.IProjectDescription
;
26 import org
.eclipse
.core
.resources
.IResourceChangeEvent
;
27 import org
.eclipse
.core
.resources
.IResourceChangeListener
;
28 import org
.eclipse
.core
.resources
.IResourceDelta
;
29 import org
.eclipse
.core
.resources
.IWorkspace
;
30 import org
.eclipse
.core
.resources
.IWorkspaceRoot
;
31 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
32 import org
.eclipse
.core
.runtime
.CoreException
;
33 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
34 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Activator
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.TmfCommonConstants
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.TmfProjectNature
;
37 import org
.eclipse
.swt
.widgets
.Display
;
38 import org
.eclipse
.ui
.PlatformUI
;
39 import org
.eclipse
.ui
.actions
.WorkspaceModifyOperation
;
42 * Factory class storing TMF tracing projects and creating TMF project model elements.
45 * @author Francois Chouinard
47 public class TmfProjectRegistry
implements IResourceChangeListener
{
49 // Create the singleton instance
51 new TmfProjectRegistry();
54 // The map of project resource to project model elements
55 private static Map
<IProject
, TmfProjectElement
> registry
= new HashMap
<>();
57 private TmfProjectRegistry() {
58 ResourcesPlugin
.getWorkspace().addResourceChangeListener(this);
62 * Get the project model element for a project resource
63 * @param project the project resource
64 * @return the project model element or null if it does not exist
66 public static synchronized TmfProjectElement
getProject(IProject project
) {
67 return getProject(project
, false);
71 * Get the project model element for a project resource
72 * @param project the project resource
73 * @param force a flag controlling whether a new project should be created if it doesn't exist
74 * @return the project model element
76 public static synchronized TmfProjectElement
getProject(IProject project
, boolean force
) {
77 TmfProjectElement element
= registry
.get(project
);
78 if (element
== null && force
) {
79 registry
.put(project
, new TmfProjectElement(project
.getName(), project
, null));
80 element
= registry
.get(project
);
81 // force the model to be populated
82 element
.refreshChildren();
88 * Utility method to create a tracing project.
92 * @param projectLocation
93 * - A project location URI. Use null for default location (which is workspace).
95 * - A progress monitor
96 * @return the IProject object or null
99 public static IProject
createProject(String projectName
, final URI projectLocation
, IProgressMonitor monitor
) {
101 final IWorkspace workspace
= ResourcesPlugin
.getWorkspace();
102 IWorkspaceRoot root
= workspace
.getRoot();
103 final IProject project
= root
.getProject(projectName
);
104 WorkspaceModifyOperation action
= new WorkspaceModifyOperation() {
106 protected void execute(IProgressMonitor progressMonitor
) throws CoreException
, InvocationTargetException
, InterruptedException
{
107 if (!project
.exists()) {
108 IProjectDescription description
= workspace
.newProjectDescription(project
.getName());
109 if (projectLocation
!= null) {
110 description
.setLocationURI(projectLocation
);
112 project
.create(description
, progressMonitor
);
115 if (!project
.isOpen()) {
116 project
.open(progressMonitor
);
119 IProjectDescription description
= project
.getDescription();
120 description
.setNatureIds(new String
[] { TmfProjectNature
.ID
});
121 project
.setDescription(description
, null);
123 IFolder folder
= project
.getFolder(TmfTracesFolder
.TRACES_FOLDER_NAME
);
124 if (!folder
.exists()) {
125 folder
.create(true, true, null);
128 folder
= project
.getFolder(TmfExperimentFolder
.EXPER_FOLDER_NAME
);
129 if (!folder
.exists()) {
130 folder
.create(true, true, null);
133 // create folder for supplementary tracing files
134 folder
= project
.getFolder(TmfCommonConstants
.TRACE_SUPPLEMENTARY_FOLDER_NAME
);
136 if (!folder
.exists()) {
137 folder
.create(true, true, null);
142 PlatformUI
.getWorkbench().getProgressService().run(false, false, action
);
143 } catch (InvocationTargetException e
) {
144 Activator
.getDefault().logError("Error creating TMF project " + project
.getName(), e
); //$NON-NLS-1$
145 } catch (InterruptedException e
) {
150 // ------------------------------------------------------------------------
151 // IResourceChangeListener
152 // ------------------------------------------------------------------------
158 public void resourceChanged(IResourceChangeEvent event
) {
159 if (event
.getType() == IResourceChangeEvent
.PRE_DELETE
|| event
.getType() == IResourceChangeEvent
.PRE_CLOSE
) {
160 if (event
.getResource() instanceof IProject
) {
161 IProject project
= (IProject
) event
.getResource();
163 if (project
.hasNature(TmfProjectNature
.ID
)) {
164 TmfProjectElement tmfProjectElement
= registry
.get(project
);
165 final List
<TmfTraceElement
> traces
= tmfProjectElement
.getTracesFolder().getTraces();
166 if (!traces
.isEmpty()) {
167 // Close editors in UI Thread
168 Display
.getDefault().syncExec(new Runnable() {
171 for (TmfTraceElement traceElement
: traces
) {
172 traceElement
.closeEditors();
178 } catch (CoreException e
) {
179 Activator
.getDefault().logError("Error handling resource change event for " + project
.getName(), e
); //$NON-NLS-1$
182 } else if (event
.getType() == IResourceChangeEvent
.POST_CHANGE
) {
183 for (IResourceDelta delta
: event
.getDelta().getAffectedChildren()) {
184 if (delta
.getResource() instanceof IProject
) {
185 IProject project
= (IProject
) delta
.getResource();
187 if (delta
.getKind() == IResourceDelta
.CHANGED
&&
188 project
.isOpen() && project
.hasNature(TmfProjectNature
.ID
)) {
189 TmfProjectElement projectElement
= getProject(project
, true);
190 projectElement
.refresh();
191 } else if (delta
.getKind() == IResourceDelta
.REMOVED
) {
192 registry
.remove(project
);
194 } catch (CoreException e
) {
195 Activator
.getDefault().logError("Error handling resource change event for " + project
.getName(), e
); //$NON-NLS-1$