[tmf] Close event editors on project delete and close
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / project / model / TmfProjectRegistry.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
11 * Bernd Hufmann - Added project creation utility
12 * Patrick Tasse - Refactor resource change listener
13 *******************************************************************************/
14
15 package org.eclipse.linuxtools.tmf.ui.project.model;
16
17 import java.lang.reflect.InvocationTargetException;
18 import java.net.URI;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22
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;
40
41 /**
42 * Factory class storing TMF tracing projects and creating TMF project model elements.
43 * <p>
44 * @version 1.0
45 * @author Francois Chouinard
46 */
47 public class TmfProjectRegistry implements IResourceChangeListener {
48
49 // Create the singleton instance
50 static {
51 new TmfProjectRegistry();
52 }
53
54 // The map of project resource to project model elements
55 private static Map<IProject, TmfProjectElement> registry = new HashMap<>();
56
57 private TmfProjectRegistry() {
58 ResourcesPlugin.getWorkspace().addResourceChangeListener(this);
59 }
60
61 /**
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
65 */
66 public static synchronized TmfProjectElement getProject(IProject project) {
67 return getProject(project, false);
68 }
69
70 /**
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
75 */
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();
83 }
84 return element;
85 }
86
87 /**
88 * Utility method to create a tracing project.
89 *
90 * @param projectName
91 * - A project name
92 * @param projectLocation
93 * - A project location URI. Use null for default location (which is workspace).
94 * @param monitor
95 * - A progress monitor
96 * @return the IProject object or null
97 * @since 2.0
98 */
99 public static IProject createProject(String projectName, final URI projectLocation, IProgressMonitor monitor) {
100
101 final IWorkspace workspace = ResourcesPlugin.getWorkspace();
102 IWorkspaceRoot root = workspace.getRoot();
103 final IProject project = root.getProject(projectName);
104 WorkspaceModifyOperation action = new WorkspaceModifyOperation() {
105 @Override
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);
111 }
112 project.create(description, progressMonitor);
113 }
114
115 if (!project.isOpen()) {
116 project.open(progressMonitor);
117 }
118
119 IProjectDescription description = project.getDescription();
120 description.setNatureIds(new String[] { TmfProjectNature.ID });
121 project.setDescription(description, null);
122
123 IFolder folder = project.getFolder(TmfTracesFolder.TRACES_FOLDER_NAME);
124 if (!folder.exists()) {
125 folder.create(true, true, null);
126 }
127
128 folder = project.getFolder(TmfExperimentFolder.EXPER_FOLDER_NAME);
129 if (!folder.exists()) {
130 folder.create(true, true, null);
131 }
132
133 // create folder for supplementary tracing files
134 folder = project.getFolder(TmfCommonConstants.TRACE_SUPPLEMENTARY_FOLDER_NAME);
135
136 if (!folder.exists()) {
137 folder.create(true, true, null);
138 }
139 }
140 };
141 try {
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) {
146 }
147 return project;
148 }
149
150 // ------------------------------------------------------------------------
151 // IResourceChangeListener
152 // ------------------------------------------------------------------------
153
154 /**
155 * @since 3.0
156 */
157 @Override
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();
162 try {
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() {
169 @Override
170 public void run() {
171 for (TmfTraceElement traceElement : traces) {
172 traceElement.closeEditors();
173 }
174 }
175 });
176 }
177 }
178 } catch (CoreException e) {
179 Activator.getDefault().logError("Error handling resource change event for " + project.getName(), e); //$NON-NLS-1$
180 }
181 }
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();
186 try {
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);
193 }
194 } catch (CoreException e) {
195 Activator.getDefault().logError("Error handling resource change event for " + project.getName(), e); //$NON-NLS-1$
196 }
197 }
198 }
199 }
200 }
201
202 }
This page took 0.077278 seconds and 5 git commands to generate.