Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2000, 2008 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;
 
 import java.util.Map;
 
 import  org.eclipse.core.resources.*;
 import  org.eclipse.core.runtime.IProgressMonitor;
 import  org.eclipse.core.runtime.IStatus;
 import  org.eclipse.core.runtime.OperationCanceledException;
 import  org.eclipse.core.runtime.PerformanceStats;
Abstract class for implementations of java elements which are IOpenable.

See also:
IJavaElement
IOpenable
 
 public abstract class Openable extends JavaElement implements IOpenableIBufferChangedListener {
 
 protected Openable(JavaElement parent) {
 	super(parent);
 }
The buffer associated with this element has changed. Registers this element as being out of synch with its buffer's contents. If the buffer has been closed, this element is set as NOT out of synch with the contents.

See also:
IBufferChangedListener
 
 public void bufferChanged(BufferChangedEvent event) {
 	if (event.getBuffer().isClosed()) {
 	} else {
 	}
 }
Builds this element's structure and properties in the given info object, based on this element's current contents (reuse buffer contents if this element has an open buffer, or resource contents if this element does not have an open buffer). Children are placed in the given newElements table (note, this element has already been placed in the newElements table). Returns true if successful, or false if an error is encountered while determining the structure of this element.
 
 protected abstract boolean buildStructure(OpenableElementInfo info, IProgressMonitor pmMap newElements, IResource underlyingResourcethrows JavaModelException;
 /*
  * Returns whether this element can be removed from the Java model cache to make space.
  */
 public boolean canBeRemovedFromCache() {
 	try {
 		return !hasUnsavedChanges();
 	} catch (JavaModelException e) {
 		return false;
 	}
 }
 /*
  * Returns whether the buffer of this element can be removed from the Java model cache to make space.
  */
 public boolean canBufferBeRemovedFromCache(IBuffer buffer) {
 	return !buffer.hasUnsavedChanges();
 }
Close the buffer associated with this element, if any.
 
 protected void closeBuffer() {
 	if (!hasBuffer()) return// nothing to do
 	IBuffer buffer = getBufferManager().getBuffer(this);
 	if (buffer != null) {
 		buffer.close();
 	}
 }
This element is being closed. Do any necessary cleanup.
 
 protected void closing(Object info) {
 }
 protected void codeComplete(
		int positionCompletionRequestor requestor,
		ITypeRoot typeRoot,
		IProgressMonitor monitorthrows JavaModelException {
	if (requestor == null) {
		throw new IllegalArgumentException("Completion requestor cannot be null"); //$NON-NLS-1$
	}
	PerformanceStats performanceStats = .
		? PerformanceStats.getStats(.this)
null;
	if(performanceStats != null) {
		performanceStats.startRun(new String(cu.getFileName()) + " at " + position); //$NON-NLS-1$
	}
	IBuffer buffer = getBuffer();
	if (buffer == null) {
		return;
	}
	if (position < -1 || position > buffer.getLength()) {
	}
	SearchableEnvironment environment = project.newSearchableNameEnvironment(owner);
	// set unit to skip
	environment.unitToSkip = unitToSkip;
	// code complete
	CompletionEngine engine = new CompletionEngine(environmentrequestorproject.getOptions(true), projectownermonitor);
	engine.complete(cuposition, 0, typeRoot);
	if(performanceStats != null) {
		performanceStats.endRun();
	}
		..println(Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInSourcePackage: " + environment.nameLookup.timeSpentInSeekTypesInSourcePackage + "ms");  //$NON-NLS-1$ //$NON-NLS-2$
		..println(Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInBinaryPackage: " + environment.nameLookup.timeSpentInSeekTypesInBinaryPackage + "ms");  //$NON-NLS-1$ //$NON-NLS-2$
	}
protected IJavaElement[] codeSelect(org.eclipse.jdt.internal.compiler.env.ICompilationUnit cuint offsetint lengthWorkingCopyOwner ownerthrows JavaModelException {
	PerformanceStats performanceStats = .
		? PerformanceStats.getStats(.this)
null;
	if(performanceStats != null) {
		performanceStats.startRun(new String(cu.getFileName()) + " at [" + offset + "," + length + "]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
	}
	SearchableEnvironment environment = project.newSearchableNameEnvironment(owner);
	SelectionRequestor requestornew SelectionRequestor(environment.nameLookupthis);
	IBuffer buffer = getBuffer();
	if (buffer == null) {
		return requestor.getElements();
	}
	int endbuffer.getLength();
	if (offset < 0 || length < 0 || offset + length > end ) {
	}
	// fix for 1FVXGDK
	SelectionEngine engine = new SelectionEngine(environmentrequestorproject.getOptions(true), owner);
	engine.select(cuoffsetoffset + length - 1);
	if(performanceStats != null) {
		performanceStats.endRun();
	}
		..println(Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInSourcePackage: " + environment.nameLookup.timeSpentInSeekTypesInSourcePackage + "ms");  //$NON-NLS-1$ //$NON-NLS-2$
		..println(Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInBinaryPackage: " + environment.nameLookup.timeSpentInSeekTypesInBinaryPackage + "ms");  //$NON-NLS-1$ //$NON-NLS-2$
	}
	return requestor.getElements();
/*
 * Returns a new element info for this element.
 */
protected Object createElementInfo() {
	return new OpenableElementInfo();

See also:
IJavaElement
public boolean exists() {
	if (JavaModelManager.getJavaModelManager().getInfo(this) != null)
		return true;
	switch (getElementType()) {
			if (root.isArchive()) {
				// pkg in a jar -> need to open root to know if this pkg exists
				try {
catch (JavaModelException e) {
					return false;
				}
				return rootInfo.rawPackageInfo.containsKey(((PackageFragmentthis).);
			}
			break;
				// class file in a jar -> need to open this class file to know if it exists
				return super.exists();
			}
			break;
	}
	return validateExistence(resource()).isOK();
	IBuffer buffer = getBuffer();
	String source = buffer == null ? null : buffer.getContents();
	return Util.getLineSeparator(sourcegetJavaProject());
protected void generateInfos(Object infoHashMap newElements, IProgressMonitor monitorthrows JavaModelException {
		String element;
		switch (getElementType()) {
			case JAVA_PROJECT:
				element = "project"//$NON-NLS-1$
				break;
			case PACKAGE_FRAGMENT_ROOT:
				element = "root"//$NON-NLS-1$
				break;
			case PACKAGE_FRAGMENT:
				element = "package"//$NON-NLS-1$
				break;
			case CLASS_FILE:
				element = "class file"//$NON-NLS-1$
				break;
			case COMPILATION_UNIT:
				element = "compilation unit"//$NON-NLS-1$
				break;
			default:
				element = "element"//$NON-NLS-1$
		}
		..println(Thread.currentThread() +" OPENING " + element + " " + this.toStringWithAncestors()); //$NON-NLS-1$//$NON-NLS-2$
	}
	// open its ancestors if needed
	openAncestors(newElementsmonitor);
	// validate existence
	IResource underlResource = resource();
	IStatus status = validateExistence(underlResource);
	if (!status.isOK())
		throw newJavaModelException(status);
	if (monitor != null && monitor.isCanceled())
		throw new OperationCanceledException();
	 // puts the info before building the structure so that questions to the handle behave as if the element existed
	 // (case of compilation units becoming working copies)
	newElements.put(thisinfo);
	// build the structure of the openable (this will open the buffer if needed)
	try {
		OpenableElementInfo openableElementInfo = (OpenableElementInfo)info;
		boolean isStructureKnown = buildStructure(openableElementInfomonitornewElementsunderlResource);
		openableElementInfo.setIsStructureKnown(isStructureKnown);
catch (JavaModelException e) {
		newElements.remove(this);
		throw e;
	}
	// remove out of sync buffer for this element
		..println(JavaModelManager.getJavaModelManager().cacheToString("-> ")); //$NON-NLS-1$
	}
Note: a buffer with no unsaved changes can be closed by the Java Model since it has a finite number of buffers allowed open at one time. If this is the first time a request is being made for the buffer, an attempt is made to create and fill this element's buffer. If the buffer has been closed since it was first opened, the buffer is re-created.

See also:
IOpenable
public IBuffer getBuffer() throws JavaModelException {
	if (hasBuffer()) {
		// ensure element is open
		IBuffer buffer = getBufferManager().getBuffer(this);
		if (buffer == null) {
			// try to (re)open a buffer
			buffer = openBuffer(nullinfo);
		}
		if (buffer instanceof NullBuffer) {
			return null;
		}
		return buffer;
else {
		return null;
	}
Answers the buffer factory to use for creating new buffers

Deprecated:
Returns the buffer manager for this element.
	return BufferManager.getDefaultBufferManager();
Return my underlying resource. Elements that may not have a corresponding resource must override this method.

See also:
IJavaElement
public IResource getCorrespondingResource() throws JavaModelException {
/*
 * @see IJavaElement
 */
	return this;

See also:
IJavaElement
public IResource getUnderlyingResource() throws JavaModelException {
	IResource parentResource = this..getUnderlyingResource();
	if (parentResource == null) {
		return null;
	}
	int type = parentResource.getType();
	if (type == IResource.FOLDER || type == IResource.PROJECT) {
		IContainer folder = (IContainer) parentResource;
		IResource resource = folder.findMember(getElementName());
		if (resource == null) {
else {
			return resource;
		}
else {
		return parentResource;
	}
Returns true if this element may have an associated source buffer, otherwise false. Subclasses must override as required.
protected boolean hasBuffer() {
	return false;

See also:
IOpenable
public boolean hasUnsavedChanges() throws JavaModelException{
	if (isReadOnly() || !isOpen()) {
		return false;
	}
	IBuffer buf = getBuffer();
	if (buf != null && buf.hasUnsavedChanges()) {
		return true;
	}
	// for package fragments, package fragment roots, and projects must check open buffers
	// to see if they have an child with unsaved changes
	int elementType = getElementType();
	if (elementType == PACKAGE_FRAGMENT ||
		elementType == PACKAGE_FRAGMENT_ROOT ||
		elementType == JAVA_PROJECT ||
		elementType == JAVA_MODEL) { // fix for 1FWNMHH
		while (openBuffers.hasMoreElements()) {
			IBuffer buffer= (IBuffer)openBuffers.nextElement();
			if (buffer.hasUnsavedChanges()) {
				if (isAncestorOf(owner)) {
					return true;
				}
			}
		}
	}
	return false;
Subclasses must override as required.

See also:
IOpenable
public boolean isConsistent() {
	return true;

See also:
IOpenable
public boolean isOpen() {
	return JavaModelManager.getJavaModelManager().getInfo(this) != null;
Returns true if this represents a source element. Openable source elements have an associated buffer created when they are opened.
protected boolean isSourceElement() {
	return false;

See also:
IJavaElement
public boolean isStructureKnown() throws JavaModelException {

See also:
IOpenable
public void makeConsistent(IProgressMonitor monitorthrows JavaModelException {
	// only compilation units can be inconsistent
	// other openables cannot be inconsistent so default is to do nothing

See also:
IOpenable
public void open(IProgressMonitor pmthrows JavaModelException {
	getElementInfo(pm);
Opens a buffer on the contents of this element, and returns the buffer, or returns null if opening fails. By default, do nothing - subclasses that have buffers must override as required.
protected IBuffer openBuffer(IProgressMonitor pmObject infothrows JavaModelException {
	return null;
public IResource getResource() {
	if (root != null) {
		if (root.isExternal())
			return null;
		if (root.isArchive())
			return root.resource(root);
	}
	return resource(root);
public IResource resource() {
	if (root != null && root.isArchive())
		return root.resource(root);
	return resource(root);
protected abstract IResource resource(PackageFragmentRoot root);

Returns whether the corresponding resource or associated file exists
protected boolean resourceExists(IResource underlyingResource) {
	return underlyingResource.isAccessible();

See also:
IOpenable
public void save(IProgressMonitor pmboolean forcethrows JavaModelException {
	if (isReadOnly()) {
	}
	IBuffer buf = getBuffer();
	if (buf != null) { // some Openables (like a JavaProject) don't have a buffer
		buf.save(pmforce);
		makeConsistent(pm); // update the element info of this element
	}
Find enclosing package fragment root if any
/*
 * Validates the existence of this openable. Returns a non ok status if it doesn't exist.
 */
abstract protected IStatus validateExistence(IResource underlyingResource);
/*
 * Opens the ancestors of this openable that are not yet opened, validating their existence.
 */
protected void openAncestors(HashMap newElements, IProgressMonitor monitorthrows JavaModelException {
	Openable openableParent = (Openable)getOpenableParent();
	if (openableParent != null && !openableParent.isOpen()) {
		openableParent.generateInfos(openableParent.createElementInfo(), newElementsmonitor);
	}
New to GrepCode? Check out our FAQ X