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