1) Added missing strings for italic, underline and strike through.
[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 that have been added to the region
27          */
28         protected ArrayList fRootElements;
29
30         /**
31          * Creates an empty region.
32          * 
33          * @see IRegion
34          */
35         public Region() {
36                 fRootElements = new ArrayList(1);
37         }
38
39         /**
40          * @see IRegion#add(IJavaElement)
41          */
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();
48                 }
49         }
50
51         /**
52          * @see IRegion
53          */
54         public boolean contains(IJavaElement element) {
55
56                 int size = fRootElements.size();
57                 ArrayList parents = getAncestors(element);
58
59                 for (int i = 0; i < size; i++) {
60                         IJavaElement aTop = (IJavaElement) fRootElements.get(i);
61                         if (aTop.equals(element)) {
62                                 return true;
63                         }
64                         for (int j = 0, pSize = parents.size(); j < pSize; j++) {
65                                 if (aTop.equals(parents.get(j))) {
66                                         // an ancestor is already included
67                                         return true;
68                                 }
69                         }
70                 }
71                 return false;
72         }
73
74         /**
75          * Returns a collection of all the parents of this element in bottom-up
76          * order.
77          * 
78          */
79         private ArrayList getAncestors(IJavaElement element) {
80                 ArrayList parents = new ArrayList();
81                 IJavaElement parent = element.getParent();
82                 while (parent != null) {
83                         parents.add(parent);
84                         parent = parent.getParent();
85                 }
86                 parents.trimToSize();
87                 return parents;
88         }
89
90         /**
91          * @see IRegion
92          */
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);
98                 }
99
100                 return roots;
101         }
102
103         /**
104          * @see IRegion#remove(IJavaElement)
105          */
106         public boolean remove(IJavaElement element) {
107
108                 removeAllChildren(element);
109                 return fRootElements.remove(element);
110         }
111
112         /**
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.
115          * 
116          * <p>
117          * Children are all children, not just direct children.
118          */
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)) {
129                                                 isChild = true;
130                                                 break;
131                                         }
132                                         parent = parent.getParent();
133                                 }
134                                 if (!isChild) {
135                                         newRootElements.add(currentRoot);
136                                 }
137                         }
138                         fRootElements = newRootElements;
139                 }
140         }
141
142         /**
143          * Returns a printable representation of this region.
144          */
145         public String toString() {
146                 StringBuffer buffer = new StringBuffer();
147                 IJavaElement[] roots = getElements();
148                 buffer.append('[');
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$
153                         }
154                 }
155                 buffer.append(']');
156                 return buffer.toString();
157         }
158 }