Java Code Examples for org.eclipse.jdt.core.IJavaElement#PACKAGE_FRAGMENT

The following examples show how to use org.eclipse.jdt.core.IJavaElement#PACKAGE_FRAGMENT . 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: NewWizardsActionGroup.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private boolean isNewTarget(Object element) {
	if (element == null)
		return true;
	if (element instanceof IResource) {
		return true;
	}
	if (element instanceof IJavaElement) {
		int type= ((IJavaElement)element).getElementType();
		return type == IJavaElement.JAVA_PROJECT ||
			type == IJavaElement.PACKAGE_FRAGMENT_ROOT ||
			type == IJavaElement.PACKAGE_FRAGMENT ||
			type == IJavaElement.COMPILATION_UNIT ||
			type == IJavaElement.TYPE;
	}
	if (element instanceof IWorkingSet) {
		String workingSetId= ((IWorkingSet)element).getId();
		return
			IWorkingSetIDs.JAVA.equals(workingSetId) ||
			IWorkingSetIDs.RESOURCE.equals(workingSetId) ||
			IWorkingSetIDs.OTHERS.equals(workingSetId);
	}
	return false;
}
 
Example 2
Source File: JavaModelCache.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public IJavaElement getExistingElement(IJavaElement element) {
	switch (element.getElementType()) {
		case IJavaElement.JAVA_MODEL:
			return element;
		case IJavaElement.JAVA_PROJECT:
			return element; // projectCache is a Hashtable and Hashtables don't support getKey(...)
		case IJavaElement.PACKAGE_FRAGMENT_ROOT:
			return (IJavaElement) this.rootCache.getKey(element);
		case IJavaElement.PACKAGE_FRAGMENT:
			return (IJavaElement) this.pkgCache.getKey(element);
		case IJavaElement.COMPILATION_UNIT:
		case IJavaElement.CLASS_FILE:
			return (IJavaElement) this.openableCache.getKey(element);
		case IJavaElement.TYPE:
			return element; // jarTypeCache or childrenCache are Hashtables and Hashtables don't support getKey(...)
		default:
			return element; // childrenCache is a Hashtable and Hashtables don't support getKey(...)
	}
}
 
Example 3
Source File: PackagesView.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected IJavaElement findElementToSelect(IJavaElement je) {
	if (je == null)
		return null;

	switch (je.getElementType()) {
		case IJavaElement.PACKAGE_FRAGMENT:
			return je;
		case IJavaElement.COMPILATION_UNIT:
			return ((ICompilationUnit)je).getParent();
		case IJavaElement.CLASS_FILE:
			return ((IClassFile)je).getParent();
		case IJavaElement.TYPE:
			return ((IType)je).getPackageFragment();
		default:
			return findElementToSelect(je.getParent());
	}
}
 
Example 4
Source File: FindStringsToExternalizeAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private List<NonNLSElement> analyze(IPackageFragmentRoot sourceFolder, IProgressMonitor pm) throws CoreException {
	try{
		IJavaElement[] children= sourceFolder.getChildren();
		pm.beginTask("", children.length); //$NON-NLS-1$
		pm.setTaskName(JavaElementLabels.getElementLabel(sourceFolder, JavaElementLabels.ALL_DEFAULT));
		List<NonNLSElement> result= new ArrayList<NonNLSElement>();
		for (int i= 0; i < children.length; i++) {
			IJavaElement iJavaElement= children[i];
			if (iJavaElement.getElementType() == IJavaElement.PACKAGE_FRAGMENT){
				IPackageFragment pack= (IPackageFragment)iJavaElement;
				if (! pack.isReadOnly())
					result.addAll(analyze(pack, new SubProgressMonitor(pm, 1)));
				else
					pm.worked(1);
			} else
				pm.worked(1);
		}
		return result;
	} finally{
		pm.done();
	}
}
 
Example 5
Source File: JavaElementResourceMapping.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static ResourceMapping create(IJavaElement element) {
	switch (element.getElementType()) {
		case IJavaElement.TYPE:
			return create((IType)element);
		case IJavaElement.COMPILATION_UNIT:
			return create((ICompilationUnit)element);
		case IJavaElement.CLASS_FILE:
			return create((IClassFile)element);
		case IJavaElement.PACKAGE_FRAGMENT:
			return create((IPackageFragment)element);
		case IJavaElement.PACKAGE_FRAGMENT_ROOT:
			return create((IPackageFragmentRoot)element);
		case IJavaElement.JAVA_PROJECT:
			return create((IJavaProject)element);
		case IJavaElement.JAVA_MODEL:
			return create((IJavaModel)element);
		default:
			return null;
	}

}
 
Example 6
Source File: ElementChangedListener.java    From JDeodorant with MIT License 5 votes vote down vote up
private void processDelta(IJavaElementDelta delta) {
	IJavaElement javaElement = delta.getElement();
	switch(javaElement.getElementType()) {
	case IJavaElement.JAVA_MODEL:
	case IJavaElement.JAVA_PROJECT:
	case IJavaElement.PACKAGE_FRAGMENT_ROOT:
	case IJavaElement.PACKAGE_FRAGMENT:
		IJavaElementDelta[] affectedChildren = delta.getAffectedChildren();
		for(IJavaElementDelta affectedChild : affectedChildren) {
			processDelta(affectedChild);
		}
		break;
	case IJavaElement.COMPILATION_UNIT:
		ICompilationUnit compilationUnit = (ICompilationUnit)javaElement;
		if(delta.getKind() == IJavaElementDelta.ADDED) {
			CompilationUnitCache.getInstance().compilationUnitAdded(compilationUnit);
		}
		else if(delta.getKind() == IJavaElementDelta.REMOVED) {
			CompilationUnitCache.getInstance().compilationUnitRemoved(compilationUnit);
		}
		else if(delta.getKind() == IJavaElementDelta.CHANGED) {
			if((delta.getFlags() & IJavaElementDelta.F_FINE_GRAINED) != 0) {
				CompilationUnitCache.getInstance().compilationUnitChanged(compilationUnit);
			}
		}
	}
}
 
Example 7
Source File: CleanUpAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private boolean isEnabled(IStructuredSelection selection) {
	Object[] selected= selection.toArray();
	for (int i= 0; i < selected.length; i++) {
		try {
			if (selected[i] instanceof IJavaElement) {
				IJavaElement elem= (IJavaElement)selected[i];
				if (elem.exists()) {
					switch (elem.getElementType()) {
						case IJavaElement.TYPE:
							return elem.getParent().getElementType() == IJavaElement.COMPILATION_UNIT; // for browsing perspective
						case IJavaElement.COMPILATION_UNIT:
							return true;
						case IJavaElement.IMPORT_CONTAINER:
							return true;
						case IJavaElement.PACKAGE_FRAGMENT:
						case IJavaElement.PACKAGE_FRAGMENT_ROOT:
							IPackageFragmentRoot root= (IPackageFragmentRoot)elem.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
							return (root.getKind() == IPackageFragmentRoot.K_SOURCE);
						case IJavaElement.JAVA_PROJECT:
							// https://bugs.eclipse.org/bugs/show_bug.cgi?id=65638
							return true;
					}
				}
			} else if (selected[i] instanceof LogicalPackage) {
				return true;
			} else if (selected[i] instanceof IWorkingSet) {
				IWorkingSet workingSet= (IWorkingSet) selected[i];
				return IWorkingSetIDs.JAVA.equals(workingSet.getId());
			}
		} catch (JavaModelException e) {
			if (!e.isDoesNotExist()) {
				JavaPlugin.log(e);
			}
		}
	}
	return false;
}
 
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 isRenameElementAvailable(IJavaElement element) throws CoreException {
	switch (element.getElementType()) {
		case IJavaElement.JAVA_PROJECT:
			return isRenameAvailable((IJavaProject) element);
		case IJavaElement.PACKAGE_FRAGMENT_ROOT:
			return isRenameAvailable((IPackageFragmentRoot) element);
		case IJavaElement.PACKAGE_FRAGMENT:
			return isRenameAvailable((IPackageFragment) element);
		case IJavaElement.COMPILATION_UNIT:
			return isRenameAvailable((ICompilationUnit) element);
		case IJavaElement.TYPE:
			return isRenameAvailable((IType) element);
		case IJavaElement.METHOD:
			final IMethod method= (IMethod) element;
			if (method.isConstructor())
				return isRenameAvailable(method.getDeclaringType());
			else
				return isRenameAvailable(method);
		case IJavaElement.FIELD:
			final IField field= (IField) element;
			if (Flags.isEnum(field.getFlags()))
				return isRenameEnumConstAvailable(field);
			else
				return isRenameFieldAvailable(field);
		case IJavaElement.TYPE_PARAMETER:
			return isRenameAvailable((ITypeParameter) element);
		case IJavaElement.LOCAL_VARIABLE:
			return isRenameAvailable((ILocalVariable) element);
	}
	return false;
}
 
Example 9
Source File: DeleteModifications.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public void delete(IJavaElement element) throws CoreException {
	switch(element.getElementType()) {
		case IJavaElement.JAVA_MODEL:
			return;
		case IJavaElement.JAVA_PROJECT:
			fDelete.add(element);
			if (element.getResource() != null)
				getResourceModifications().addDelete(element.getResource());
			return;
		case IJavaElement.PACKAGE_FRAGMENT_ROOT:
			fDelete.add(element);
			IResource resource= element.getResource();
			// Flag an resource change even if we have an archive. If it is
			// internal (we have a underlying resource then we have a resource
			// change.
			if (resource != null)
				getResourceModifications().addDelete(resource);
			IJavaProject[] referencingProjects= JavaElementUtil.getReferencingProjects((IPackageFragmentRoot) element);
			for (int i= 0; i < referencingProjects.length; i++) {
				IFile classpath= referencingProjects[i].getProject().getFile(".classpath"); //$NON-NLS-1$
				getResourceModifications().addChanged(classpath);
			}
			return;
		case IJavaElement.PACKAGE_FRAGMENT:
			fDelete.add(element);
			fPackagesToDelete.add((IPackageFragment) element);
			return;
		case IJavaElement.COMPILATION_UNIT:
			fDelete.add(element);
			IType[] types= ((ICompilationUnit)element).getTypes();
			fDelete.addAll(Arrays.asList(types));
			if (element.getResource() != null)
				getResourceModifications().addDelete(element.getResource());
			return;
		default:
			fDelete.add(element);
	}

}
 
Example 10
Source File: JavaSearchPage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
final static boolean isSearchableType(IJavaElement element) {
	switch (element.getElementType()) {
		case IJavaElement.PACKAGE_FRAGMENT:
		case IJavaElement.PACKAGE_DECLARATION:
		case IJavaElement.IMPORT_DECLARATION:
		case IJavaElement.TYPE:
		case IJavaElement.FIELD:
		case IJavaElement.METHOD:
			return true;
	}
	return false;
}
 
Example 11
Source File: ContainerFilter.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean select(Viewer viewer, Object parent, Object element) {
	boolean isContainer= element instanceof IContainer;
	if (!isContainer && element instanceof IJavaElement) {
		int type= ((IJavaElement)element).getElementType();
		isContainer= type == IJavaElement.JAVA_MODEL
					|| type == IJavaElement.JAVA_PROJECT
					|| type == IJavaElement.PACKAGE_FRAGMENT
					|| type ==IJavaElement.PACKAGE_FRAGMENT_ROOT;
	}
	return (fFilterContainers && !isContainer) || (!fFilterContainers && isContainer);
}
 
Example 12
Source File: FindAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
boolean canOperateOn(IJavaElement element) {
	if (element == null || fValidTypes == null || fValidTypes.length == 0 || !ActionUtil.isOnBuildPath(element))
		return false;

	for (int i= 0; i < fValidTypes.length; i++) {
		if (fValidTypes[i].isInstance(element)) {
			if (element.getElementType() == IJavaElement.PACKAGE_FRAGMENT)
				return hasChildren((IPackageFragment)element);
			else
				return true;
		}
	}
	return false;
}
 
Example 13
Source File: RefactoringAvailabilityTester.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static boolean isRenameElementAvailable(IJavaElement element) throws CoreException {
	switch (element.getElementType()) {
		case IJavaElement.JAVA_PROJECT:
			return isRenameAvailable((IJavaProject) element);
		case IJavaElement.PACKAGE_FRAGMENT_ROOT:
			return isRenameAvailable((IPackageFragmentRoot) element);
		case IJavaElement.PACKAGE_FRAGMENT:
			return isRenameAvailable((IPackageFragment) element);
		case IJavaElement.COMPILATION_UNIT:
			return isRenameAvailable((ICompilationUnit) element);
		case IJavaElement.TYPE:
			return isRenameAvailable((IType) element);
		case IJavaElement.METHOD:
			final IMethod method = (IMethod) element;
			if (method.isConstructor()) {
				return isRenameAvailable(method.getDeclaringType());
			} else {
				return isRenameAvailable(method);
			}
		case IJavaElement.FIELD:
			final IField field = (IField) element;
			if (Flags.isEnum(field.getFlags())) {
				return isRenameEnumConstAvailable(field);
			} else {
				return isRenameFieldAvailable(field);
			}
		case IJavaElement.TYPE_PARAMETER:
			return isRenameAvailable((ITypeParameter) element);
		case IJavaElement.LOCAL_VARIABLE:
			return isRenameAvailable((ILocalVariable) element);
	}
	return false;
}
 
Example 14
Source File: JavadocHover.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static long getHeaderFlags(IJavaElement element) {
	switch (element.getElementType()) {
		case IJavaElement.LOCAL_VARIABLE:
			return LOCAL_VARIABLE_FLAGS;
		case IJavaElement.TYPE_PARAMETER:
			return TYPE_PARAMETER_FLAGS;
		case IJavaElement.PACKAGE_FRAGMENT:
			return PACKAGE_FLAGS;
		default:
			return LABEL_FLAGS;
	}
}
 
Example 15
Source File: RenameSupport.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static RenameSupport create(IJavaElement element, String newName, int flags) throws CoreException {
	switch (element.getElementType()) {
		case IJavaElement.JAVA_PROJECT:
			//return RenameSupport.create((IJavaProject) element, newName, flags);
			return null;
		case IJavaElement.PACKAGE_FRAGMENT_ROOT:
			//return RenameSupport.create((IPackageFragmentRoot) element, newName);
			return null;
		case IJavaElement.PACKAGE_FRAGMENT:
			return RenameSupport.create((IPackageFragment) element, newName, flags);
		case IJavaElement.COMPILATION_UNIT:
			return RenameSupport.create((ICompilationUnit) element, newName, flags);
		case IJavaElement.TYPE:
			return RenameSupport.create((IType) element, newName, flags);
		case IJavaElement.METHOD:
			final IMethod method = (IMethod) element;
			if (method.isConstructor()) {
				return create(method.getDeclaringType(), newName, flags);
			} else {
				return RenameSupport.create((IMethod) element, newName, flags);
			}
		case IJavaElement.FIELD:
			return RenameSupport.create((IField) element, newName, flags);
		case IJavaElement.TYPE_PARAMETER:
			return RenameSupport.create((ITypeParameter) element, newName, flags);
		case IJavaElement.LOCAL_VARIABLE:
			return RenameSupport.create((ILocalVariable) element, newName, flags);
	}
	return null;
}
 
Example 16
Source File: PackageExplorerActionGroup.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private boolean isGoIntoTarget(Object element) {
	if (element == null)
		return false;
	if (element instanceof IJavaElement) {
		int type= ((IJavaElement)element).getElementType();
		return type == IJavaElement.JAVA_PROJECT ||
			type == IJavaElement.PACKAGE_FRAGMENT_ROOT ||
			type == IJavaElement.PACKAGE_FRAGMENT;
	}
	if (element instanceof IWorkingSet) {
		return true;
	}
	return false;
}
 
Example 17
Source File: TypeHierarchyLifeCycle.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private void processDelta(IJavaElementDelta delta, ArrayList<IType> changedTypes) {
	IJavaElement element= delta.getElement();
	switch (element.getElementType()) {
		case IJavaElement.TYPE:
			processTypeDelta((IType) element, changedTypes);
			processChildrenDelta(delta, changedTypes); // (inner types)
			break;
		case IJavaElement.JAVA_MODEL:
		case IJavaElement.JAVA_PROJECT:
		case IJavaElement.PACKAGE_FRAGMENT_ROOT:
		case IJavaElement.PACKAGE_FRAGMENT:
			processChildrenDelta(delta, changedTypes);
			break;
		case IJavaElement.COMPILATION_UNIT:
			ICompilationUnit cu= (ICompilationUnit)element;
			if (!JavaModelUtil.isPrimary(cu)) {
				return;
			}

			if (delta.getKind() == IJavaElementDelta.CHANGED && isPossibleStructuralChange(delta.getFlags())) {
				try {
					if (cu.exists()) {
						IType[] types= cu.getAllTypes();
						for (int i= 0; i < types.length; i++) {
							processTypeDelta(types[i], changedTypes);
						}
					}
				} catch (JavaModelException e) {
					JavaPlugin.log(e);
				}
			} else {
				processChildrenDelta(delta, changedTypes);
			}
			break;
		case IJavaElement.CLASS_FILE:
			if (delta.getKind() == IJavaElementDelta.CHANGED) {
				IType type= ((IClassFile) element).getType();
				processTypeDelta(type, changedTypes);
			} else {
				processChildrenDelta(delta, changedTypes);
			}
			break;
	}
}
 
Example 18
Source File: CompilationUnitContextType.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public Package() {
	super("enclosing_package", JavaTemplateMessages.CompilationUnitContextType_variable_description_enclosing_package, IJavaElement.PACKAGE_FRAGMENT);  //$NON-NLS-1$
}
 
Example 19
Source File: JavaElementLabelComposer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Appends the label for a Java element with the flags as defined by this class.
 *
 * @param element the element to render
 * @param flags the rendering flags.
 */
public void appendElementLabel(IJavaElement element, long flags) {
	int type= element.getElementType();
	IPackageFragmentRoot root= null;

	if (type != IJavaElement.JAVA_MODEL && type != IJavaElement.JAVA_PROJECT && type != IJavaElement.PACKAGE_FRAGMENT_ROOT) {
		root= JavaModelUtil.getPackageFragmentRoot(element);
	}
	if (root != null && getFlag(flags, JavaElementLabels.PREPEND_ROOT_PATH)) {
		appendPackageFragmentRootLabel(root, JavaElementLabels.ROOT_QUALIFIED);
		fBuilder.append(JavaElementLabels.CONCAT_STRING);
	}

	switch (type) {
	case IJavaElement.METHOD:
		appendMethodLabel((IMethod) element, flags);
		break;
	case IJavaElement.FIELD:
		appendFieldLabel((IField) element, flags);
		break;
	case IJavaElement.LOCAL_VARIABLE:
		appendLocalVariableLabel((ILocalVariable) element, flags);
		break;
	case IJavaElement.TYPE_PARAMETER:
		appendTypeParameterLabel((ITypeParameter) element, flags);
		break;
	case IJavaElement.INITIALIZER:
		appendInitializerLabel((IInitializer) element, flags);
		break;
	case IJavaElement.TYPE:
		appendTypeLabel((IType) element, flags);
		break;
	case IJavaElement.CLASS_FILE:
		appendClassFileLabel((IClassFile) element, flags);
		break;
	case IJavaElement.COMPILATION_UNIT:
		appendCompilationUnitLabel((ICompilationUnit) element, flags);
		break;
	case IJavaElement.PACKAGE_FRAGMENT:
		appendPackageFragmentLabel((IPackageFragment) element, flags);
		break;
	case IJavaElement.PACKAGE_FRAGMENT_ROOT:
		appendPackageFragmentRootLabel((IPackageFragmentRoot) element, flags);
		break;
	case IJavaElement.IMPORT_CONTAINER:
	case IJavaElement.IMPORT_DECLARATION:
	case IJavaElement.PACKAGE_DECLARATION:
		appendDeclarationLabel(element, flags);
		break;
	case IJavaElement.JAVA_PROJECT:
	case IJavaElement.JAVA_MODEL:
		fBuilder.append(element.getElementName());
		break;
	default:
		fBuilder.append(element.getElementName());
	}

	if (root != null && getFlag(flags, JavaElementLabels.APPEND_ROOT_PATH)) {
		fBuilder.append(JavaElementLabels.CONCAT_STRING);
		appendPackageFragmentRootLabel(root, JavaElementLabels.ROOT_QUALIFIED);
	}
}
 
Example 20
Source File: OpenTypeHierarchyAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private static IStatus compileCandidates(List<IJavaElement> result, List<IJavaElement> elements) {
	IStatus ok= Status.OK_STATUS;
	boolean onlyContainers= true;
	for (Iterator<IJavaElement> iter= elements.iterator(); iter.hasNext();) {
		IJavaElement elem= iter.next();
		try {
			switch (elem.getElementType()) {
				case IJavaElement.INITIALIZER:
				case IJavaElement.METHOD:
				case IJavaElement.FIELD:
				case IJavaElement.TYPE:
					onlyContainers= false;
					//$FALL-THROUGH$
				case IJavaElement.PACKAGE_FRAGMENT_ROOT:
				case IJavaElement.JAVA_PROJECT:
					result.add(elem);
					break;
				case IJavaElement.PACKAGE_FRAGMENT:
					if (((IPackageFragment)elem).containsJavaResources())
						result.add(elem);
					break;
				case IJavaElement.PACKAGE_DECLARATION:
					result.add(elem.getAncestor(IJavaElement.PACKAGE_FRAGMENT));
					break;
				case IJavaElement.IMPORT_DECLARATION:
					IImportDeclaration decl= (IImportDeclaration)elem;
					if (decl.isOnDemand())
						elem= JavaModelUtil.findTypeContainer(elem.getJavaProject(), Signature.getQualifier(elem.getElementName()));
					else
						elem= elem.getJavaProject().findType(elem.getElementName());
					if (elem != null) {
						onlyContainers= false;
						result.add(elem);
					}
					break;
				case IJavaElement.CLASS_FILE:
					onlyContainers= false;
					result.add(((IClassFile)elem).getType());
					break;
				case IJavaElement.COMPILATION_UNIT:
					ICompilationUnit cu= (ICompilationUnit)elem;
					IType[] types= cu.getTypes();
					if (types.length > 0) {
						onlyContainers= false;
						result.addAll(Arrays.asList(types));
					}
			}
		} catch (JavaModelException e) {
			return e.getStatus();
		}
	}
	int size= result.size();
	if (size == 0 || (size > 1 && !onlyContainers))
		return createStatus(ActionMessages.OpenTypeHierarchyAction_messages_no_valid_java_element);
	return ok;
}