tmf: Switch tmf.ui to Java 7 + fix warnings
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / project / model / TmfProjectModelElement.java
1 /*******************************************************************************
2 * Copyright (c) 2010, 2013 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 supplementary files/folder handling
12 *******************************************************************************/
13
14 package org.eclipse.linuxtools.tmf.ui.project.model;
15
16 import java.net.URI;
17 import java.util.ArrayList;
18 import java.util.List;
19
20 import org.eclipse.core.resources.IFolder;
21 import org.eclipse.core.resources.IProject;
22 import org.eclipse.core.resources.IResource;
23 import org.eclipse.core.resources.IResourceChangeEvent;
24 import org.eclipse.core.resources.IResourceChangeListener;
25 import org.eclipse.core.resources.ResourcesPlugin;
26 import org.eclipse.core.runtime.CoreException;
27 import org.eclipse.core.runtime.IPath;
28 import org.eclipse.core.runtime.NullProgressMonitor;
29 import org.eclipse.linuxtools.internal.tmf.ui.Activator;
30 import org.eclipse.linuxtools.tmf.core.TmfCommonConstants;
31
32 /**
33 * The implementation of the base TMF project model element. It provides default implementation
34 * of the <code>ITmfProjectModelElement</code> interface.
35 * <p>
36 * @version 1.0
37 * @author Francois Chouinard
38 */
39 public abstract class TmfProjectModelElement implements ITmfProjectModelElement, IResourceChangeListener {
40
41 // ------------------------------------------------------------------------
42 // Attributes
43 // ------------------------------------------------------------------------
44
45 private final String fName;
46 /**
47 * The project model element resource.
48 */
49 protected final IResource fResource;
50 /**
51 * The project model resource location (URI)
52 */
53 protected final URI fLocation;
54 /**
55 * The project model path of a resource.
56 */
57 protected final IPath fPath;
58 private final ITmfProjectModelElement fParent;
59 /**
60 * The list of children elements.
61 */
62 protected final List<ITmfProjectModelElement> fChildren;
63
64 // ------------------------------------------------------------------------
65 // Constructor
66 // ------------------------------------------------------------------------
67 /**
68 * Constructor.
69 *
70 * Creates a base project model element.
71 * @param name The name of the element.
72 * @param resource The element resource.
73 * @param parent The parent model element.
74 */
75 protected TmfProjectModelElement(String name, IResource resource, ITmfProjectModelElement parent) {
76 fName = name;
77 fResource = resource;
78 fPath = resource.getFullPath();
79 fLocation = resource.getLocationURI();
80 fParent = parent;
81 fChildren = new ArrayList<>();
82 ResourcesPlugin.getWorkspace().addResourceChangeListener(this);
83 }
84
85 private void dispose() {
86 ResourcesPlugin.getWorkspace().removeResourceChangeListener(this);
87 }
88
89 // ------------------------------------------------------------------------
90 // ITmfProjectModelElement
91 // ------------------------------------------------------------------------
92
93 @Override
94 public String getName() {
95 return fName;
96 }
97
98 @Override
99 public IResource getResource() {
100 return fResource;
101 }
102
103 @Override
104 public IPath getPath() {
105 return fPath;
106 }
107
108 @Override
109 public URI getLocation() {
110 return fLocation;
111 }
112
113 @Override
114 public ITmfProjectModelElement getParent() {
115 return fParent;
116 }
117
118 @Override
119 public boolean hasChildren() {
120 return fChildren.size() > 0;
121 }
122
123 @Override
124 public List<ITmfProjectModelElement> getChildren() {
125 return fChildren;
126 }
127
128 @Override
129 public void addChild(ITmfProjectModelElement child) {
130 fChildren.add(child);
131 }
132
133 @Override
134 public void removeChild(ITmfProjectModelElement child) {
135 fChildren.remove(child);
136 if (child instanceof TmfProjectModelElement) {
137 ((TmfProjectModelElement) child).dispose();
138 }
139 refresh();
140 }
141
142 @Override
143 public void refresh() {
144 // Do nothing by default: sub-classes override this on an "as-needed"
145 // basis.
146 }
147
148 // ------------------------------------------------------------------------
149 // IResourceChangeListener
150 // ------------------------------------------------------------------------
151
152 @Override
153 public void resourceChanged(IResourceChangeEvent event) {
154 // Do nothing by default: sub-classes override this on an "as-needed"
155 // basis.
156 }
157
158 // ------------------------------------------------------------------------
159 // Object
160 // ------------------------------------------------------------------------
161
162 @Override
163 public int hashCode() {
164 final int prime = 31;
165 int result = 1;
166 result = prime * result + ((fPath == null) ? 0 : fPath.hashCode());
167 return result;
168 }
169
170 @Override
171 public boolean equals(Object other) {
172 if (this == other) {
173 return true;
174 }
175 if (other == null) {
176 return false;
177 }
178 if (!(other instanceof TmfProjectModelElement)) {
179 return false;
180 }
181 TmfProjectModelElement element = (TmfProjectModelElement) other;
182 return element.fPath.equals(fPath);
183 }
184
185 /**
186 * Returns the trace specific supplementary directory under the project's supplementary folder.
187 * The folder will be created if it doesn't exist.
188 *
189 * @param supplFoldername - folder name.
190 * @return returns the trace specific supplementary directory
191 */
192 public IFolder getTraceSupplementaryFolder(String supplFoldername) {
193 IFolder supplFolderParent = getSupplementaryFolderParent();
194 return supplFolderParent.getFolder(supplFoldername);
195 }
196
197 /**
198 * Returns the supplementary folder for this project
199 *
200 * @return the supplementary folder for this project
201 */
202 public IFolder getSupplementaryFolderParent() {
203 TmfProjectElement project = getProject();
204 IProject projectResource = project.getResource();
205 IFolder supplFolderParent = projectResource.getFolder(TmfCommonConstants.TRACE_SUPPLEMENATARY_FOLDER_NAME);
206
207 if (!supplFolderParent.exists()) {
208 try {
209 supplFolderParent.create(true, true, new NullProgressMonitor());
210 } catch (CoreException e) {
211 Activator.getDefault().logError("Error creating project specific supplementary folder " + supplFolderParent, e); //$NON-NLS-1$
212 }
213 }
214 return supplFolderParent;
215 }
216 }
This page took 0.037801 seconds and 5 git commands to generate.