001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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.jdt.internal.core;
011:
012: import java.util.ArrayList;
013:
014: import org.eclipse.jdt.core.IJavaElement;
015: import org.eclipse.jdt.core.IParent;
016: import org.eclipse.jdt.core.IRegion;
017:
018: /**
019: * @see IRegion
020: */
021:
022: public class Region implements IRegion {
023:
024: /**
025: * A collection of the top level elements
026: * that have been added to the region
027: */
028: protected ArrayList fRootElements;
029:
030: /**
031: * Creates an empty region.
032: *
033: * @see IRegion
034: */
035: public Region() {
036: fRootElements = new ArrayList(1);
037: }
038:
039: /**
040: * @see IRegion#add(IJavaElement)
041: */
042: public void add(IJavaElement element) {
043: if (!contains(element)) {
044: //"new" element added to region
045: removeAllChildren(element);
046: fRootElements.add(element);
047: fRootElements.trimToSize();
048: }
049: }
050:
051: /**
052: * @see IRegion
053: */
054: public boolean contains(IJavaElement element) {
055:
056: int size = fRootElements.size();
057: ArrayList parents = getAncestors(element);
058:
059: for (int i = 0; i < size; i++) {
060: IJavaElement aTop = (IJavaElement) fRootElements.get(i);
061: if (aTop.equals(element)) {
062: return true;
063: }
064: for (int j = 0, pSize = parents.size(); j < pSize; j++) {
065: if (aTop.equals(parents.get(j))) {
066: //an ancestor is already included
067: return true;
068: }
069: }
070: }
071: return false;
072: }
073:
074: /**
075: * Returns a collection of all the parents of this element
076: * in bottom-up order.
077: *
078: */
079: private ArrayList getAncestors(IJavaElement element) {
080: ArrayList parents = new ArrayList();
081: IJavaElement parent = element.getParent();
082: while (parent != null) {
083: parents.add(parent);
084: parent = parent.getParent();
085: }
086: parents.trimToSize();
087: return parents;
088: }
089:
090: /**
091: * @see IRegion
092: */
093: public IJavaElement[] getElements() {
094: int size = fRootElements.size();
095: IJavaElement[] roots = new IJavaElement[size];
096: for (int i = 0; i < size; i++) {
097: roots[i] = (IJavaElement) fRootElements.get(i);
098: }
099:
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>Children are all children, not just direct children.
117: */
118: protected void removeAllChildren(IJavaElement element) {
119: if (element instanceof IParent) {
120: ArrayList newRootElements = new ArrayList();
121: for (int i = 0, size = fRootElements.size(); i < size; i++) {
122: IJavaElement currentRoot = (IJavaElement) fRootElements
123: .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: }
|