1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
13 import java.util.ArrayList;
15 import net.sourceforge.phpdt.core.IJavaElement;
16 import net.sourceforge.phpdt.core.IParent;
17 import net.sourceforge.phpdt.core.IRegion;
23 public class Region implements IRegion {
26 * A collection of the top level elements that have been added to the region
28 protected ArrayList fRootElements;
31 * Creates an empty region.
36 fRootElements = new ArrayList(1);
40 * @see IRegion#add(IJavaElement)
42 public void add(IJavaElement element) {
43 if (!contains(element)) {
44 // "new" element added to region
45 removeAllChildren(element);
46 fRootElements.add(element);
47 fRootElements.trimToSize();
54 public boolean contains(IJavaElement element) {
56 int size = fRootElements.size();
57 ArrayList parents = getAncestors(element);
59 for (int i = 0; i < size; i++) {
60 IJavaElement aTop = (IJavaElement) fRootElements.get(i);
61 if (aTop.equals(element)) {
64 for (int j = 0, pSize = parents.size(); j < pSize; j++) {
65 if (aTop.equals(parents.get(j))) {
66 // an ancestor is already included
75 * Returns a collection of all the parents of this element in bottom-up
79 private ArrayList getAncestors(IJavaElement element) {
80 ArrayList parents = new ArrayList();
81 IJavaElement parent = element.getParent();
82 while (parent != null) {
84 parent = parent.getParent();
93 public IJavaElement[] getElements() {
94 int size = fRootElements.size();
95 IJavaElement[] roots = new IJavaElement[size];
96 for (int i = 0; i < size; i++) {
97 roots[i] = (IJavaElement) fRootElements.get(i);
104 * @see IRegion#remove(IJavaElement)
106 public boolean remove(IJavaElement element) {
108 removeAllChildren(element);
109 return fRootElements.remove(element);
113 * Removes any children of this element that are contained within this
114 * region as this parent is about to be added to the region.
117 * Children are all children, not just direct children.
119 private void removeAllChildren(IJavaElement element) {
120 if (element instanceof IParent) {
121 ArrayList newRootElements = new ArrayList();
122 for (int i = 0, size = fRootElements.size(); i < size; i++) {
123 IJavaElement currentRoot = (IJavaElement) fRootElements.get(i);
124 // walk the current root hierarchy
125 IJavaElement parent = currentRoot.getParent();
126 boolean isChild = false;
127 while (parent != null) {
128 if (parent.equals(element)) {
132 parent = parent.getParent();
135 newRootElements.add(currentRoot);
138 fRootElements = newRootElements;
143 * Returns a printable representation of this region.
145 public String toString() {
146 StringBuffer buffer = new StringBuffer();
147 IJavaElement[] roots = getElements();
149 for (int i = 0; i < roots.length; i++) {
150 buffer.append(roots[i].getElementName());
151 if (i < (roots.length - 1)) {
152 buffer.append(", "); //$NON-NLS-1$
156 return buffer.toString();