| 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<ITmfProjectModelElement>(); |
| 82 | ResourcesPlugin.getWorkspace().addResourceChangeListener(this); |
| 83 | } |
| 84 | |
| 85 | private void dispose() { |
| 86 | ResourcesPlugin.getWorkspace().removeResourceChangeListener(this); |
| 87 | } |
| 88 | |
| 89 | // ------------------------------------------------------------------------ |
| 90 | // ITmfProjectModelElement |
| 91 | // ------------------------------------------------------------------------ |
| 92 | /* |
| 93 | * (non-Javadoc) |
| 94 | * @see org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement#getName() |
| 95 | */ |
| 96 | @Override |
| 97 | public String getName() { |
| 98 | return fName; |
| 99 | } |
| 100 | /* |
| 101 | * (non-Javadoc) |
| 102 | * @see org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement#getResource() |
| 103 | */ |
| 104 | @Override |
| 105 | public IResource getResource() { |
| 106 | return fResource; |
| 107 | } |
| 108 | /* |
| 109 | * (non-Javadoc) |
| 110 | * @see org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement#getPath() |
| 111 | */ |
| 112 | @Override |
| 113 | public IPath getPath() { |
| 114 | return fPath; |
| 115 | } |
| 116 | /* |
| 117 | * (non-Javadoc) |
| 118 | * @see org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement#getLocation() |
| 119 | */ |
| 120 | @Override |
| 121 | public URI getLocation() { |
| 122 | return fLocation; |
| 123 | } |
| 124 | /* |
| 125 | * (non-Javadoc) |
| 126 | * @see org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement#getParent() |
| 127 | */ |
| 128 | @Override |
| 129 | public ITmfProjectModelElement getParent() { |
| 130 | return fParent; |
| 131 | } |
| 132 | /* |
| 133 | * (non-Javadoc) |
| 134 | * @see org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement#hasChildren() |
| 135 | */ |
| 136 | @Override |
| 137 | public boolean hasChildren() { |
| 138 | return fChildren.size() > 0; |
| 139 | } |
| 140 | /* |
| 141 | * (non-Javadoc) |
| 142 | * @see org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement#getChildren() |
| 143 | */ |
| 144 | @Override |
| 145 | public List<ITmfProjectModelElement> getChildren() { |
| 146 | return fChildren; |
| 147 | } |
| 148 | /* |
| 149 | * (non-Javadoc) |
| 150 | * @see org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement#addChild(org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement) |
| 151 | */ |
| 152 | @Override |
| 153 | public void addChild(ITmfProjectModelElement child) { |
| 154 | fChildren.add(child); |
| 155 | } |
| 156 | /* |
| 157 | * (non-Javadoc) |
| 158 | * @see org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement#removeChild(org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement) |
| 159 | */ |
| 160 | @Override |
| 161 | public void removeChild(ITmfProjectModelElement child) { |
| 162 | fChildren.remove(child); |
| 163 | if (child instanceof TmfProjectModelElement) { |
| 164 | ((TmfProjectModelElement) child).dispose(); |
| 165 | } |
| 166 | refresh(); |
| 167 | } |
| 168 | /* |
| 169 | * (non-Javadoc) |
| 170 | * @see org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement#refresh() |
| 171 | */ |
| 172 | @Override |
| 173 | public void refresh() { |
| 174 | // Do nothing by default: sub-classes override this on an "as-needed" |
| 175 | // basis. |
| 176 | } |
| 177 | |
| 178 | // ------------------------------------------------------------------------ |
| 179 | // IResourceChangeListener |
| 180 | // ------------------------------------------------------------------------ |
| 181 | /* |
| 182 | * (non-Javadoc) |
| 183 | * @see org.eclipse.core.resources.IResourceChangeListener#resourceChanged(org.eclipse.core.resources.IResourceChangeEvent) |
| 184 | */ |
| 185 | @Override |
| 186 | public void resourceChanged(IResourceChangeEvent event) { |
| 187 | // Do nothing by default: sub-classes override this on an "as-needed" |
| 188 | // basis. |
| 189 | } |
| 190 | |
| 191 | // ------------------------------------------------------------------------ |
| 192 | // Object |
| 193 | // ------------------------------------------------------------------------ |
| 194 | /* |
| 195 | * (non-Javadoc) |
| 196 | * @see java.lang.Object#hashCode() |
| 197 | */ |
| 198 | @Override |
| 199 | public int hashCode() { |
| 200 | final int prime = 31; |
| 201 | int result = 1; |
| 202 | result = prime * result + ((fPath == null) ? 0 : fPath.hashCode()); |
| 203 | return result; |
| 204 | } |
| 205 | /* |
| 206 | * (non-Javadoc) |
| 207 | * @see java.lang.Object#equals(java.lang.Object) |
| 208 | */ |
| 209 | @Override |
| 210 | public boolean equals(Object other) { |
| 211 | if (this == other) { |
| 212 | return true; |
| 213 | } |
| 214 | if (other == null) { |
| 215 | return false; |
| 216 | } |
| 217 | if (!(other instanceof TmfProjectModelElement)) { |
| 218 | return false; |
| 219 | } |
| 220 | TmfProjectModelElement element = (TmfProjectModelElement) other; |
| 221 | return element.fPath.equals(fPath); |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Returns the trace specific supplementary directory under the project's supplementary folder. |
| 226 | * The folder will be created if it doesn't exist. |
| 227 | * |
| 228 | * @param supplFoldername - folder name. |
| 229 | * @return returns the trace specific supplementary directory |
| 230 | */ |
| 231 | public IFolder getTraceSupplementaryFolder(String supplFoldername) { |
| 232 | IFolder supplFolderParent = getSupplementaryFolderParent(); |
| 233 | return supplFolderParent.getFolder(supplFoldername); |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Returns the supplementary folder for this project |
| 238 | * |
| 239 | * @return the supplementary folder for this project |
| 240 | */ |
| 241 | public IFolder getSupplementaryFolderParent() { |
| 242 | TmfProjectElement project = getProject(); |
| 243 | IProject projectResource = project.getResource(); |
| 244 | IFolder supplFolderParent = projectResource.getFolder(TmfCommonConstants.TRACE_SUPPLEMENATARY_FOLDER_NAME); |
| 245 | |
| 246 | if (!supplFolderParent.exists()) { |
| 247 | try { |
| 248 | supplFolderParent.create(true, true, new NullProgressMonitor()); |
| 249 | } catch (CoreException e) { |
| 250 | Activator.getDefault().logError("Error creating project specific supplementary folder " + supplFolderParent, e); //$NON-NLS-1$ |
| 251 | } |
| 252 | } |
| 253 | return supplFolderParent; |
| 254 | } |
| 255 | } |