Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2000, 2009 IBM Corporation and others. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html Contributors: IBM Corporation - initial API and implementation /
 
 package org.eclipse.jdt.internal.core;
 
 
 
 
 public class Region implements IRegion {

A collection of the top level elements that have been added to the region
 
 	protected ArrayList rootElements;
Creates an empty region.

 
 public Region() {
 	this. = new ArrayList(1);
 }
 
 public void add(IJavaElement element) {
 	if (!contains(element)) {
 		//"new" element added to region
 		removeAllChildren(element);
 		this..add(element);
 	}
 }
 
 public boolean contains(IJavaElement element) {
 
 	int size = this..size();
 	ArrayList parents = getAncestors(element);
 
 	for (int i = 0; i < sizei++) {
 		IJavaElement aTop = (IJavaElementthis..get(i);
 		if (aTop.equals(element)) {
 			return true;
 		}
 		for (int j = 0, pSize = parents.size(); j < pSizej++) {
 			if (aTop.equals(parents.get(j))) {
 				//an ancestor is already included
 				return true;
 			}
 		}
 	}
 	return false;
 }
Returns a collection of all the parents of this element in bottom-up order.
 
 private ArrayList getAncestors(IJavaElement element) {
 	ArrayList parents = new ArrayList();
 	IJavaElement parent = element.getParent();
 	while (parent != null) {
 		parents.add(parent);
 		parent = parent.getParent();
 	}
 	parents.trimToSize();
 	return parents;
 }
 
 public IJavaElement[] getElements() {
 	int sizethis..size();
 	IJavaElement[] rootsnew IJavaElement[size];
 	for (int i = 0; i < sizei++) {
 		roots[i]= (IJavaElementthis..get(i);
 	}
 
 	return roots;
 }
public boolean remove(IJavaElement element) {
	return this..remove(element);
Removes any children of this element that are contained within this region as this parent is about to be added to the region.

Children are all children, not just direct children.

protected void removeAllChildren(IJavaElement element) {
	if (element instanceof IParent) {
		ArrayList newRootElements = new ArrayList();
		for (int i = 0, size = this..size(); i < sizei++) {
			IJavaElement currentRoot = (IJavaElement)this..get(i);
			//walk the current root hierarchy
			IJavaElement parent = currentRoot.getParent();
			boolean isChildfalse;
			while (parent != null) {
				if (parent.equals(element)) {
					isChildtrue;
					break;
				}
				parent = parent.getParent();
			}
			if (!isChild) {
				newRootElements.add(currentRoot);
			}
		}
		this.newRootElements;
	}
Returns a printable representation of this region.
public String toString() {
	StringBuffer buffernew StringBuffer();
	buffer.append('[');
	for (int i= 0; i < roots.lengthi++) {
		buffer.append(roots[i].getElementName());
		if (i < (roots.length - 1)) {
			buffer.append(", "); //$NON-NLS-1$
		}
	}
	buffer.append(']');
	return buffer.toString();
New to GrepCode? Check out our FAQ X