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
27 * that have been added to the region
29 protected ArrayList fRootElements;
31 * Creates an empty region.
36 fRootElements = new ArrayList(1);
39 * @see IRegion#add(IJavaElement)
41 public void add(IJavaElement element) {
42 if (!contains(element)) {
43 //"new" element added to region
44 removeAllChildren(element);
45 fRootElements.add(element);
46 fRootElements.trimToSize();
52 public boolean contains(IJavaElement element) {
54 int size = fRootElements.size();
55 ArrayList parents = getAncestors(element);
57 for (int i = 0; i < size; i++) {
58 IJavaElement aTop = (IJavaElement) fRootElements.get(i);
59 if (aTop.equals(element)) {
62 for (int j = 0, pSize = parents.size(); j < pSize; j++) {
63 if (aTop.equals(parents.get(j))) {
64 //an ancestor is already included
72 * Returns a collection of all the parents of this element
76 private ArrayList getAncestors(IJavaElement element) {
77 ArrayList parents = new ArrayList();
78 IJavaElement parent = element.getParent();
79 while (parent != null) {
81 parent = parent.getParent();
89 public IJavaElement[] getElements() {
90 int size= fRootElements.size();
91 IJavaElement[] roots= new IJavaElement[size];
92 for (int i = 0; i < size; i++) {
93 roots[i]= (IJavaElement) fRootElements.get(i);
99 * @see IRegion#remove(IJavaElement)
101 public boolean remove(IJavaElement element) {
103 removeAllChildren(element);
104 return fRootElements.remove(element);
107 * Removes any children of this element that are contained within this
108 * region as this parent is about to be added to the region.
110 * <p>Children are all children, not just direct children.
112 private void removeAllChildren(IJavaElement element) {
113 if (element instanceof IParent) {
114 ArrayList newRootElements = new ArrayList();
115 for (int i = 0, size = fRootElements.size(); i < size; i++) {
116 IJavaElement currentRoot = (IJavaElement)fRootElements.get(i);
117 //walk the current root hierarchy
118 IJavaElement parent = currentRoot.getParent();
119 boolean isChild= false;
120 while (parent != null) {
121 if (parent.equals(element)) {
125 parent = parent.getParent();
128 newRootElements.add(currentRoot);
131 fRootElements= newRootElements;
135 * Returns a printable representation of this region.
137 public String toString() {
138 StringBuffer buffer= new StringBuffer();
139 IJavaElement[] roots= getElements();
141 for (int i= 0; i < roots.length; i++) {
142 buffer.append(roots[i].getElementName());
143 if (i < (roots.length - 1)) {
144 buffer.append(", "); //$NON-NLS-1$
148 return buffer.toString();