Changes:
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / Region.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
12
13 import java.util.ArrayList;
14
15 import net.sourceforge.phpdt.core.IJavaElement;
16 import net.sourceforge.phpdt.core.IParent;
17 import net.sourceforge.phpdt.core.IRegion;
18
19 /**
20  * @see IRegion
21  */
22  
23 public class Region implements IRegion {
24
25         /**
26          * A collection of the top level elements
27          * that have been added to the region
28          */
29         protected ArrayList fRootElements;
30 /**
31  * Creates an empty region.
32  *
33  * @see IRegion
34  */
35 public Region() {
36         fRootElements = new ArrayList(1);
37 }
38 /**
39  * @see IRegion#add(IJavaElement)
40  */
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();
47         }
48 }
49 /**
50  * @see IRegion
51  */
52 public boolean contains(IJavaElement element) {
53         
54         int size = fRootElements.size();
55         ArrayList parents = getAncestors(element);
56         
57         for (int i = 0; i < size; i++) {
58                 IJavaElement aTop = (IJavaElement) fRootElements.get(i);
59                 if (aTop.equals(element)) {
60                         return true;
61                 }
62                 for (int j = 0, pSize = parents.size(); j < pSize; j++) {
63                         if (aTop.equals(parents.get(j))) {
64                                 //an ancestor is already included
65                                 return true;
66                         }
67                 }
68         }
69         return false;
70 }
71 /**
72  * Returns a collection of all the parents of this element
73  * in bottom-up order.
74  *
75  */
76 private ArrayList getAncestors(IJavaElement element) {
77         ArrayList parents = new ArrayList();
78         IJavaElement parent = element.getParent();
79         while (parent != null) {
80                 parents.add(parent);
81                 parent = parent.getParent();
82         }
83         parents.trimToSize();
84         return parents;
85 }
86 /**
87  * @see IRegion
88  */
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);
94         }
95
96         return roots;
97 }
98 /**
99  * @see IRegion#remove(IJavaElement)
100  */
101 public boolean remove(IJavaElement element) {
102
103         removeAllChildren(element);
104         return fRootElements.remove(element);
105 }
106 /**
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.
109  *
110  * <p>Children are all children, not just direct children.
111  */
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)) {
122                                         isChild= true;
123                                         break;
124                                 }
125                                 parent = parent.getParent();
126                         }
127                         if (!isChild) {
128                                 newRootElements.add(currentRoot);
129                         }
130                 }
131                 fRootElements= newRootElements;
132         }
133 }
134 /**
135  * Returns a printable representation of this region.
136  */
137 public String toString() {
138         StringBuffer buffer= new StringBuffer();
139         IJavaElement[] roots= getElements();
140         buffer.append('[');
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$
145                 }
146         }
147         buffer.append(']');
148         return buffer.toString();
149 }
150 }