Java Code Examples for org.eclipse.jdt.core.IJavaElement#isReadOnly()

The following examples show how to use org.eclipse.jdt.core.IJavaElement#isReadOnly() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ReorgPolicyFactory.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected RefactoringStatus verifyDestination(IJavaElement javaElement) throws JavaModelException {
	Assert.isNotNull(javaElement);
	if (!fCheckDestination) {
		return new RefactoringStatus();
	}
	if (!javaElement.exists()) {
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_cannot1);
	}
	if (javaElement instanceof IJavaModel) {
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_jmodel);
	}
	if (!(javaElement instanceof IJavaProject)) {
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_src2proj);
	}
	if (javaElement.isReadOnly()) {
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_src2writable);
	}
	if (ReorgUtils.isPackageFragmentRoot(javaElement.getJavaProject())) {
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_src2nosrc);
	}
	return new RefactoringStatus();
}
 
Example 2
Source File: Checks.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static boolean isAvailable(IJavaElement javaElement) throws JavaModelException {
	if (javaElement == null)
		return false;
	if (! javaElement.exists())
		return false;
	if (javaElement.isReadOnly())
		return false;
	// work around for https://bugs.eclipse.org/bugs/show_bug.cgi?id=48422
	// the Java project is now cheating regarding its children so we shouldn't
	// call isStructureKnown if the project isn't open.
	// see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=52474
	if (!(javaElement instanceof IJavaProject) && !(javaElement instanceof ILocalVariable) && !javaElement.isStructureKnown())
		return false;
	if (javaElement instanceof IMember && ((IMember)javaElement).isBinary())
		return false;
	return true;
}
 
Example 3
Source File: ReorgPolicyFactory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected RefactoringStatus verifyDestination(IJavaElement javaElement) throws JavaModelException {
	Assert.isNotNull(javaElement);
	if (!fCheckDestination)
		return new RefactoringStatus();
	if (!javaElement.exists())
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_cannot1);
	if (javaElement instanceof IJavaModel)
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_jmodel);
	if (!(javaElement instanceof IJavaProject))
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_src2proj);
	if (javaElement.isReadOnly())
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_src2writable);
	if (ReorgUtils.isPackageFragmentRoot(javaElement.getJavaProject()))
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_src2nosrc);
	return new RefactoringStatus();
}
 
Example 4
Source File: PasteAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private boolean canPasteFilesOn(Object target) {
	boolean isPackageFragment= target instanceof IPackageFragment;
	boolean isJavaProject= target instanceof IJavaProject;
	boolean isPackageFragmentRoot= target instanceof IPackageFragmentRoot;
	boolean isContainer= target instanceof IContainer;

	if (!(isPackageFragment || isJavaProject || isPackageFragmentRoot || isContainer))
		return false;

	if (isContainer) {
		if (target instanceof IProject)
			return ((IProject)target).isOpen();

		return true;
	} else {
		if (isJavaProject && !((IJavaProject)target).isOpen())
			return false;

		IJavaElement element= (IJavaElement)target;
		return !element.isReadOnly();
	}
}
 
Example 5
Source File: FindStringsToExternalizeAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private boolean computeEnablementState(IStructuredSelection selection) throws JavaModelException {
	if (selection.isEmpty())
		return false;
	for (Iterator<?> iter= selection.iterator(); iter.hasNext();) {
		Object element= iter.next();
		if (!(element instanceof IJavaElement))
			return false;
		IJavaElement javaElement= (IJavaElement)element;
		if (! javaElement.exists() || javaElement.isReadOnly())
			return false;
		int elementType= javaElement.getElementType();
		if (elementType != IJavaElement.PACKAGE_FRAGMENT &&
			elementType != IJavaElement.PACKAGE_FRAGMENT_ROOT &&
			elementType != IJavaElement.JAVA_PROJECT)
			return false;
		if (elementType == IJavaElement.PACKAGE_FRAGMENT_ROOT){
			IPackageFragmentRoot root= (IPackageFragmentRoot)javaElement;
			if (root.isExternal() || ReorgUtils.isClassFolder(root))
				return false;
		}
	}
	return true;
}
 
Example 6
Source File: FileTransferDropAdapter.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected int determineOperation(Object target, int operation, TransferData transferType, int operations) {

	boolean isPackageFragment= target instanceof IPackageFragment;
	boolean isJavaProject= target instanceof IJavaProject;
	boolean isPackageFragmentRoot= target instanceof IPackageFragmentRoot;
	boolean isContainer= target instanceof IContainer;

	if (!(isPackageFragment || isJavaProject || isPackageFragmentRoot || isContainer))
		return DND.DROP_NONE;

	if (isContainer) {
		IContainer container= (IContainer)target;
		if (container.isAccessible() && !Resources.isReadOnly(container))
			return DND.DROP_COPY;
	} else {
		IJavaElement element= (IJavaElement)target;
		if (!element.isReadOnly())
			return DND.DROP_COPY;
	}

	return DND.DROP_NONE;
}
 
Example 7
Source File: RenameElementsOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * @see MultiOperation
 */
protected void verify(IJavaElement element) throws JavaModelException {
	if (element == null || !element.exists())
		error(IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST, element);

	if (element.isReadOnly())
		error(IJavaModelStatusConstants.READ_ONLY, element);

	if (!(element instanceof ISourceReference))
		error(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, element);

	int elementType = element.getElementType();
	if (elementType < IJavaElement.TYPE || elementType == IJavaElement.INITIALIZER)
		error(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, element);

	verifyRenaming(element);
}
 
Example 8
Source File: RefactoringAvailabilityTester.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static boolean isExternalizeStringsAvailable(final IStructuredSelection selection) throws JavaModelException {
	for (Iterator<?> iter= selection.iterator(); iter.hasNext();) {
		Object element= iter.next();
		if (element instanceof IJavaElement) {
			IJavaElement javaElement= (IJavaElement)element;
			if (javaElement.exists() && !javaElement.isReadOnly()) {
				int elementType= javaElement.getElementType();
				if (elementType == IJavaElement.PACKAGE_FRAGMENT) {
					return true;
				} else if (elementType == IJavaElement.PACKAGE_FRAGMENT_ROOT) {
					IPackageFragmentRoot root= (IPackageFragmentRoot)javaElement;
					if (!root.isExternal() && !ReorgUtils.isClassFolder(root))
						return true;
				} else if (elementType == IJavaElement.JAVA_PROJECT) {
					return true;
				} else if (elementType == IJavaElement.COMPILATION_UNIT) {
					ICompilationUnit cu= (ICompilationUnit)javaElement;
					if (cu.exists())
						return true;
				} else if (elementType == IJavaElement.TYPE) {
					IJavaElement parent= ((IType) element).getParent();
					if (parent instanceof ICompilationUnit && parent.exists())
						return true;
				}
			}
		} else if (element instanceof IWorkingSet) {
			IWorkingSet workingSet= (IWorkingSet) element;
			return IWorkingSetIDs.JAVA.equals(workingSet.getId());
		}
	}
	return false;
}
 
Example 9
Source File: DeleteElementsOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @see MultiOperation
 */
protected void verify(IJavaElement element) throws JavaModelException {
	IJavaElement[] children = ((IRegion) this.childrenToRemove.get(element)).getElements();
	for (int i = 0; i < children.length; i++) {
		IJavaElement child = children[i];
		if (child.getCorrespondingResource() != null)
			error(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, child);

		if (child.isReadOnly())
			error(IJavaModelStatusConstants.READ_ONLY, child);
	}
}
 
Example 10
Source File: ReorgPolicyFactory.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
@Override
protected RefactoringStatus verifyDestination(IJavaElement javaElement) throws JavaModelException {
	Assert.isNotNull(javaElement);
	if (!fCheckDestination) {
		return new RefactoringStatus();
	}
	if (!javaElement.exists()) {
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_doesnotexist0);
	}
	if (javaElement instanceof IJavaModel) {
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_jmodel);
	}

	if (javaElement.isReadOnly()) {
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_readonly);
	}

	if (!javaElement.isStructureKnown()) {
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_structure);
	}

	if (javaElement instanceof IOpenable) {
		IOpenable openable= (IOpenable) javaElement;
		if (!openable.isConsistent()) {
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_inconsistent);
		}
	}

	if (javaElement instanceof IPackageFragmentRoot) {
		IPackageFragmentRoot root= (IPackageFragmentRoot) javaElement;
		if (root.isArchive()) {
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_archive);
		}
		if (root.isExternal()) {
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_external);
		}
	}

	if (ReorgUtils.isInsideCompilationUnit(javaElement)) {
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_cannot);
	}

	IContainer destinationAsContainer= getDestinationAsContainer();
	if (destinationAsContainer == null || isChildOfOrEqualToAnyFolder(destinationAsContainer)) {
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_not_this_resource);
	}

	if (containsLinkedResources() && !ReorgUtils.canBeDestinationForLinkedResources(javaElement)) {
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_linked);
	}
	return new RefactoringStatus();
}
 
Example 11
Source File: AbstractCodeBuilder.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public boolean isValid() {
  final IJavaElement javaElement = this._iJavaElementFinder.findElementFor(this.owner);
  return (((((javaElement == null) || (!javaElement.isReadOnly())) && (this.ownerSource != null)) && (this.owner != null)) && (this.context != null));
}
 
Example 12
Source File: ReorgPolicyFactory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
protected RefactoringStatus verifyDestination(IJavaElement javaElement) throws JavaModelException {
	Assert.isNotNull(javaElement);
	if (!fCheckDestination)
		return new RefactoringStatus();
	if (!javaElement.exists())
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_doesnotexist0);
	if (javaElement instanceof IJavaModel)
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_jmodel);

	if (javaElement.isReadOnly())
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_readonly);

	if (!javaElement.isStructureKnown())
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_structure);

	if (javaElement instanceof IOpenable) {
		IOpenable openable= (IOpenable) javaElement;
		if (!openable.isConsistent())
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_inconsistent);
	}

	if (javaElement instanceof IPackageFragmentRoot) {
		IPackageFragmentRoot root= (IPackageFragmentRoot) javaElement;
		if (root.isArchive())
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_archive);
		if (root.isExternal())
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_external);
	}

	if (ReorgUtils.isInsideCompilationUnit(javaElement)) {
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_cannot);
	}

	IContainer destinationAsContainer= getDestinationAsContainer();
	if (destinationAsContainer == null || isChildOfOrEqualToAnyFolder(destinationAsContainer))
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_not_this_resource);

	if (containsLinkedResources() && !ReorgUtils.canBeDestinationForLinkedResources(javaElement))
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_linked);
	return new RefactoringStatus();
}
 
Example 13
Source File: CopyElementsOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * @see MultiOperation
 *
 * Possible failure codes:
 * <ul>
 *
 *	<li>ELEMENT_DOES_NOT_EXIST - <code>element</code> or its specified destination is
 *		is <code>null</code> or does not exist. If a <code>null</code> element is
 *		supplied, no element is provided in the status, otherwise, the non-existant element
 *		is supplied in the status.
 *	<li>INVALID_ELEMENT_TYPES - <code>element</code> is not contained within a compilation unit.
 *		This operation only operates on elements contained within compilation units.
 *  <li>READ_ONLY - <code>element</code> is read only.
 *	<li>INVALID_DESTINATION - The destination parent specified for <code>element</code>
 *		is of an incompatible type. The destination for a package declaration or import declaration must
 *		be a compilation unit; the destination for a type must be a type or compilation
 *		unit; the destination for any type member (other than a type) must be a type. When
 *		this error occurs, the element provided in the operation status is the <code>element</code>.
 *	<li>INVALID_NAME - the new name for <code>element</code> does not have valid syntax.
 *      In this case the element and name are provided in the status.

 * </ul>
 */
protected void verify(IJavaElement element) throws JavaModelException {
	if (element == null || !element.exists())
		error(IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST, element);

	if (element.getElementType() < IJavaElement.TYPE)
		error(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, element);

	if (element.isReadOnly())
		error(IJavaModelStatusConstants.READ_ONLY, element);

	IJavaElement dest = getDestinationParent(element);
	verifyDestination(element, dest);
	verifySibling(element, dest);
	if (this.renamingsList != null) {
		verifyRenaming(element);
	}
}
 
Example 14
Source File: Checks.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Checks whether it is possible to modify the given <code>IJavaElement</code>.
 * The <code>IJavaElement</code> must exist and be non read-only to be modifiable.
 * Moreover, if it is a <code>IMember</code> it must not be binary.
 * The returned <code>RefactoringStatus</code> has <code>ERROR</code> severity if
 * it is not possible to modify the element.
 * @param javaElement
 * @return the status
 * @throws JavaModelException
 *
 * @see IJavaElement#exists
 * @see IJavaElement#isReadOnly
 * @see IMember#isBinary
 * @see RefactoringStatus
 */
public static RefactoringStatus checkAvailability(IJavaElement javaElement) throws JavaModelException{
	RefactoringStatus result= new RefactoringStatus();
	if (! javaElement.exists())
		result.addFatalError(Messages.format(RefactoringCoreMessages.Refactoring_not_in_model, getJavaElementName(javaElement)));
	if (javaElement.isReadOnly())
		result.addFatalError(Messages.format(RefactoringCoreMessages.Refactoring_read_only, getJavaElementName(javaElement)));
	if (javaElement.exists() && !javaElement.isStructureKnown())
		result.addFatalError(Messages.format(RefactoringCoreMessages.Refactoring_unknown_structure, getJavaElementName(javaElement)));
	if (javaElement instanceof IMember && ((IMember)javaElement).isBinary())
		result.addFatalError(Messages.format(RefactoringCoreMessages.Refactoring_binary, getJavaElementName(javaElement)));
	return result;
}