| 1 | /******************************************************************************* | 
| 2 |  * Copyright (c) 2008, 2009 IBM Corporation and others. | 
| 3 |  * All rights reserved. This program and the accompanying materials | 
| 4 |  * are made available under the terms of the Eclipse Public License v1.0 | 
| 5 |  * which accompanies this distribution, and is available at | 
| 6 |  * http://www.eclipse.org/legal/epl-v10.html | 
| 7 |  * | 
| 8 |  * Contributors: | 
| 9 |  *     IBM Corporation - initial API and implementation | 
| 10 |  *******************************************************************************/ | 
| 11 | package org.eclipse.pde.api.tools.internal.model; | 
| 12 |   | 
| 13 | import org.eclipse.core.runtime.CoreException; | 
| 14 | import org.eclipse.core.runtime.IStatus; | 
| 15 | import org.eclipse.core.runtime.Status; | 
| 16 | import org.eclipse.pde.api.tools.internal.provisional.ApiPlugin; | 
| 17 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiComponent; | 
| 18 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiElement; | 
| 19 |   | 
| 20 | /** | 
| 21 |  * Abstract description of an API element. | 
| 22 |  * Each {@link IApiElement} has a specific type, name and parent. | 
| 23 |  * <br> | 
| 24 |  * API elements cannot be re-parented. | 
| 25 |  *  | 
| 26 |  * @since 1.0.0 | 
| 27 |  */ | 
| 28 | public abstract class ApiElement implements IApiElement { | 
| 29 |   | 
| 30 |         private int fType = 0; | 
| 31 |         private String fName = null; | 
| 32 |         private IApiElement fParent = null; | 
| 33 |          | 
| 34 |         /** | 
| 35 |          * Constructor | 
| 36 |          * @param parent the parent {@link IApiElement} for this element, may be <code>null</code> | 
| 37 |          * @param type the type of this element. See {@link IApiElement} for values. | 
| 38 |          * @param name the simple name of the element | 
| 39 |          */ | 
| 40 |         protected ApiElement(IApiElement parent, int type, String name) { | 
| 41 |                 fParent = parent; | 
| 42 |                 fType = type; | 
| 43 |                 fName = name; | 
| 44 |         } | 
| 45 |          | 
| 46 |         /** | 
| 47 |          * @see org.eclipse.pde.api.tools.internal.provisional.model.IApiElement#getAncestor(int) | 
| 48 |          */ | 
| 49 |         public IApiElement getAncestor(int ancestorType) { | 
| 50 |                 IApiElement parent = fParent; | 
| 51 |                 while(parent != null && parent.getType() != ancestorType) { | 
| 52 |                         parent = parent.getParent(); | 
| 53 |                 } | 
| 54 |                 return parent; | 
| 55 |         } | 
| 56 |   | 
| 57 |         /** | 
| 58 |          * @see org.eclipse.pde.api.tools.internal.provisional.model.IApiElement#getName() | 
| 59 |          */ | 
| 60 |         public String getName() { | 
| 61 |                 return fName; | 
| 62 |         } | 
| 63 |   | 
| 64 |         /** | 
| 65 |          * @see org.eclipse.pde.api.tools.internal.provisional.model.IApiElement#getParent() | 
| 66 |          */ | 
| 67 |         public IApiElement getParent() { | 
| 68 |                 return fParent; | 
| 69 |         } | 
| 70 |          | 
| 71 |         /** | 
| 72 |          * @see org.eclipse.pde.api.tools.internal.provisional.model.IApiElement#getType() | 
| 73 |          */ | 
| 74 |         public int getType() { | 
| 75 |                 return fType; | 
| 76 |         } | 
| 77 |   | 
| 78 |         /** | 
| 79 |          * Sets the name of this {@link ApiElement} to the new name, iff the new name | 
| 80 |          * is not <code>null</code>, otherwise no change is made. | 
| 81 |          * @param newname | 
| 82 |          */ | 
| 83 |         protected void setName(String newname) { | 
| 84 |                 if(newname != null) { | 
| 85 |                         fName = newname; | 
| 86 |                 } | 
| 87 |         } | 
| 88 |          | 
| 89 |         /** | 
| 90 |          * Throws a core exception. | 
| 91 |          *  | 
| 92 |          * @param message message | 
| 93 |          * @param e underlying exception or <code>null</code> | 
| 94 |          * @throws CoreException | 
| 95 |          */ | 
| 96 |         protected void abort(String message, Throwable e) throws CoreException { | 
| 97 |                 throw new CoreException(new Status(IStatus.ERROR, ApiPlugin.PLUGIN_ID, message, e)); | 
| 98 |         } | 
| 99 |          | 
| 100 |         /* (non-Javadoc) | 
| 101 |          * @see org.eclipse.pde.api.tools.internal.provisional.model.IApiElement#getApiComponent() | 
| 102 |          */ | 
| 103 |         public IApiComponent getApiComponent() { | 
| 104 |                 return (IApiComponent) getAncestor(COMPONENT); | 
| 105 |         } | 
| 106 | } |