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