001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.core.plugin;
011:
012: import java.util.ArrayList;
013:
014: import org.eclipse.core.runtime.CoreException;
015: import org.eclipse.pde.core.IModelChangedEvent;
016: import org.eclipse.pde.core.plugin.IPluginElement;
017: import org.eclipse.pde.core.plugin.IPluginObject;
018: import org.eclipse.pde.core.plugin.IPluginParent;
019: import org.eclipse.pde.internal.core.PDECoreMessages;
020:
021: public abstract class PluginParent extends IdentifiablePluginObject
022: implements IPluginParent {
023: protected ArrayList fChildren = null;
024:
025: public PluginParent() {
026: }
027:
028: public void add(int index, IPluginObject child)
029: throws CoreException {
030: ensureModelEditable();
031: getChildrenList().add(index, child);
032: postAdd(child);
033: }
034:
035: public void add(IPluginObject child) throws CoreException {
036: ensureModelEditable();
037: getChildrenList().add(child);
038: postAdd(child);
039: }
040:
041: void appendChild(IPluginElement child) {
042: getChildrenList().add(child);
043: }
044:
045: protected void postAdd(IPluginObject child) {
046: ((PluginObject) child).setInTheModel(true);
047: ((PluginObject) child).setParent(this );
048: fireStructureChanged(child, IModelChangedEvent.INSERT);
049: }
050:
051: public int getChildCount() {
052: return getChildrenList().size();
053: }
054:
055: public boolean equals(Object obj) {
056: if (this == obj)
057: return true;
058: if (obj == null)
059: return false;
060: if (obj instanceof IPluginParent) {
061: IPluginParent target = (IPluginParent) obj;
062: if (target.getChildCount() != getChildCount())
063: return false;
064: IPluginObject[] tchildren = target.getChildren();
065: for (int i = 0; i < tchildren.length; i++) {
066: IPluginObject tchild = tchildren[i];
067: IPluginObject child = (IPluginObject) getChildrenList()
068: .get(i);
069: if (child == null || child.equals(tchild) == false)
070: return false;
071: }
072: return true;
073: }
074: return false;
075: }
076:
077: public int getIndexOf(IPluginObject child) {
078: return getChildrenList().indexOf(child);
079: }
080:
081: public void swap(IPluginObject child1, IPluginObject child2)
082: throws CoreException {
083: ensureModelEditable();
084: int index1 = getChildrenList().indexOf(child1);
085: int index2 = getChildrenList().indexOf(child2);
086: if (index1 == -1 || index2 == -1)
087: throwCoreException(PDECoreMessages.PluginParent_siblingsNotFoundException);
088: getChildrenList().set(index2, child1);
089: getChildrenList().set(index1, child2);
090: firePropertyChanged(this , P_SIBLING_ORDER, child1, child2);
091: }
092:
093: public IPluginObject[] getChildren() {
094: return (IPluginObject[]) getChildrenList().toArray(
095: new IPluginObject[getChildrenList().size()]);
096: }
097:
098: public void remove(IPluginObject child) throws CoreException {
099: ensureModelEditable();
100: getChildrenList().remove(child);
101: ((PluginObject) child).setInTheModel(false);
102: fireStructureChanged(child, IModelChangedEvent.REMOVE);
103: }
104:
105: protected ArrayList getChildrenList() {
106: if (fChildren == null)
107: fChildren = new ArrayList(1);
108: return fChildren;
109: }
110:
111: }
|