| 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.IResource; |
| 20 | import org.eclipse.core.resources.IResourceChangeEvent; |
| 21 | import org.eclipse.core.resources.IResourceChangeListener; |
| 22 | import org.eclipse.core.resources.ResourcesPlugin; |
| 23 | import org.eclipse.core.runtime.IPath; |
| 24 | |
| 25 | /** |
| 26 | * <b><u>TmfProjectModelElement</u></b> |
| 27 | * <p> |
| 28 | */ |
| 29 | public abstract class TmfProjectModelElement implements ITmfProjectModelElement, IResourceChangeListener { |
| 30 | |
| 31 | // ------------------------------------------------------------------------ |
| 32 | // Attributes |
| 33 | // ------------------------------------------------------------------------ |
| 34 | |
| 35 | private final String fName; |
| 36 | protected final IResource fResource; |
| 37 | protected final URI fLocation; |
| 38 | protected final IPath fPath; |
| 39 | private final ITmfProjectModelElement fParent; |
| 40 | protected final List<ITmfProjectModelElement> fChildren; |
| 41 | |
| 42 | // ------------------------------------------------------------------------ |
| 43 | // Constructor |
| 44 | // ------------------------------------------------------------------------ |
| 45 | |
| 46 | protected TmfProjectModelElement(String name, IResource resource, ITmfProjectModelElement parent) { |
| 47 | fName = name; |
| 48 | fResource = resource; |
| 49 | fPath = resource.getFullPath(); |
| 50 | fLocation = resource.getLocationURI(); |
| 51 | fParent = parent; |
| 52 | fChildren = new ArrayList<ITmfProjectModelElement>(); |
| 53 | ResourcesPlugin.getWorkspace().addResourceChangeListener(this); |
| 54 | } |
| 55 | |
| 56 | // ------------------------------------------------------------------------ |
| 57 | // ITmfProjectModelElement |
| 58 | // ------------------------------------------------------------------------ |
| 59 | |
| 60 | @Override |
| 61 | public String getName() { |
| 62 | return fName; |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public IResource getResource() { |
| 67 | return fResource; |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public IPath getPath() { |
| 72 | return fPath; |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public URI getLocation() { |
| 77 | return fLocation; |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public ITmfProjectModelElement getParent() { |
| 82 | return fParent; |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public boolean hasChildren() { |
| 87 | return fChildren.size() > 0; |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public List<ITmfProjectModelElement> getChildren() { |
| 92 | return fChildren; |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public void addChild(ITmfProjectModelElement child) { |
| 97 | fChildren.add(child); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public void removeChild(ITmfProjectModelElement child) { |
| 102 | fChildren.remove(child); |
| 103 | refresh(); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public void refresh() { |
| 108 | // Do nothing by default: sub-classes override this on an "as-needed" |
| 109 | // basis. |
| 110 | } |
| 111 | |
| 112 | // ------------------------------------------------------------------------ |
| 113 | // IResourceChangeListener |
| 114 | // ------------------------------------------------------------------------ |
| 115 | |
| 116 | @Override |
| 117 | public void resourceChanged(IResourceChangeEvent event) { |
| 118 | // Do nothing by default: sub-classes override this on an "as-needed" |
| 119 | // basis. |
| 120 | } |
| 121 | |
| 122 | // ------------------------------------------------------------------------ |
| 123 | // Object |
| 124 | // ------------------------------------------------------------------------ |
| 125 | |
| 126 | @Override |
| 127 | public boolean equals(Object other) { |
| 128 | if (this == other) |
| 129 | return true; |
| 130 | if (!(other instanceof TmfProjectModelElement)) |
| 131 | return false; |
| 132 | TmfProjectModelElement o = (TmfProjectModelElement) other; |
| 133 | return o.fName.equals(fName) && o.fLocation.equals(fLocation); |
| 134 | } |
| 135 | |
| 136 | } |