Java Code Examples for org.eclipse.jdt.core.IPackageFragment#getParent()
The following examples show how to use
org.eclipse.jdt.core.IPackageFragment#getParent() .
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: JavaElementUtil.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
/** * @param pack the package fragment; may not be null * @return the parent package fragment, or null if the given package fragment is the default package or a top level package */ public static IPackageFragment getParentSubpackage(IPackageFragment pack) { if (pack.isDefaultPackage()) { return null; } final int index= pack.getElementName().lastIndexOf('.'); if (index == -1) { return null; } final IPackageFragmentRoot root= (IPackageFragmentRoot) pack.getParent(); final String newPackageName= pack.getElementName().substring(0, index); final IPackageFragment parent= root.getPackageFragment(newPackageName); if (parent.exists()) { return parent; } else { return null; } }
Example 2
Source File: PackagesView.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
private String formatLogicalPackageMessage(LogicalPackage logicalPackage) { IPackageFragment[] fragments= logicalPackage.getFragments(); StringBuffer buf= new StringBuffer(logicalPackage.getElementName()); buf.append(JavaElementLabels.CONCAT_STRING); String message= ""; //$NON-NLS-1$ boolean firstTime= true; for (int i= 0; i < fragments.length; i++) { IPackageFragment fragment= fragments[i]; IJavaElement element= fragment.getParent(); if (element instanceof IPackageFragmentRoot) { IPackageFragmentRoot root= (IPackageFragmentRoot) element; String label= JavaElementLabels.getElementLabel(root, JavaElementLabels.DEFAULT_QUALIFIED | JavaElementLabels.ROOT_QUALIFIED); if (firstTime) { buf.append(label); firstTime= false; } else message= Messages.format(JavaBrowsingMessages.StatusBar_concat, new String[] {message, label}); } } buf.append(message); return buf.toString(); }
Example 3
Source File: PackagesViewHierarchicalContentProvider.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
private Object getHierarchicalParent(IPackageFragment fragment) { IJavaElement parent= fragment.getParent(); if ((parent instanceof IPackageFragmentRoot) && parent.exists()) { IPackageFragmentRoot root= (IPackageFragmentRoot) parent; if (root.isArchive() || root.isExternal() || !fragment.exists()) { return findNextLevelParentByElementName(fragment); } else { IResource resource= fragment.getResource(); if ((resource != null) && (resource instanceof IFolder)) { IFolder folder= (IFolder) resource; IResource res= folder.getParent(); IJavaElement el= JavaCore.create(res); if (el != null) { return el; } else { return res; } } } } return parent; }
Example 4
Source File: PackageExplorerContentProvider.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
/** * Returns the hierarchical packages inside a given folder. * @param folder The parent folder * @param result Collection where the resulting elements are added * @throws CoreException thrown when elements could not be accessed */ private void getHierarchicalPackagesInFolder(IFolder folder, Collection<Object> result) throws CoreException { IResource[] resources= folder.members(); for (int i= 0; i < resources.length; i++) { IResource resource= resources[i]; if (resource instanceof IFolder) { IFolder curr= (IFolder) resource; IJavaElement element= JavaCore.create(curr); if (element instanceof IPackageFragment) { if (fFoldPackages) { IPackageFragment fragment= (IPackageFragment) element; IPackageFragmentRoot root= (IPackageFragmentRoot) fragment.getParent(); element= getFolded(root.getChildren(), fragment); } result.add(element); } } } }
Example 5
Source File: NewNameQueries.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
private static IInputValidator createPackageNameValidator(final IPackageFragment pack) { IInputValidator validator= new IInputValidator(){ public String isValid(String newText) { if (newText == null || "".equals(newText)) //$NON-NLS-1$ return INVALID_NAME_NO_MESSAGE; IStatus status= JavaConventionsUtil.validatePackageName(newText, pack); if (status.getSeverity() == IStatus.ERROR) return status.getMessage(); IJavaElement parent= pack.getParent(); try { if (parent instanceof IPackageFragmentRoot){ if (! RenamePackageProcessor.isPackageNameOkInRoot(newText, (IPackageFragmentRoot)parent)) return ReorgMessages.ReorgQueries_packagewithThatNameexistsMassage; } } catch (CoreException e) { return INVALID_NAME_NO_MESSAGE; } if (pack.getElementName().equalsIgnoreCase(newText)) return ReorgMessages.ReorgQueries_resourceExistsWithDifferentCaseMassage; return null; } }; return validator; }
Example 6
Source File: JavaElementUtil.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
/** * @param pack a package fragment * @return an array containing the given package and all subpackages * @throws JavaModelException if getting the all sibling packages fails */ public static IPackageFragment[] getPackageAndSubpackages(IPackageFragment pack) throws JavaModelException { if (pack.isDefaultPackage()) return new IPackageFragment[] { pack }; IPackageFragmentRoot root= (IPackageFragmentRoot) pack.getParent(); IJavaElement[] allPackages= root.getChildren(); ArrayList<IPackageFragment> subpackages= new ArrayList<IPackageFragment>(); subpackages.add(pack); String prefix= pack.getElementName() + '.'; for (int i= 0; i < allPackages.length; i++) { IPackageFragment currentPackage= (IPackageFragment) allPackages[i]; if (currentPackage.getElementName().startsWith(prefix)) subpackages.add(currentPackage); } return subpackages.toArray(new IPackageFragment[subpackages.size()]); }
Example 7
Source File: ReorgPolicyFactory.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
private IPackageFragmentRoot getDestinationAsPackageFragmentRoot(IJavaElement javaElement) throws JavaModelException { if (javaElement == null) return null; if (javaElement instanceof IPackageFragmentRoot) return (IPackageFragmentRoot) javaElement; if (javaElement instanceof IPackageFragment) { IPackageFragment pack= (IPackageFragment) javaElement; if (pack.getParent() instanceof IPackageFragmentRoot) return (IPackageFragmentRoot) pack.getParent(); } if (javaElement instanceof IJavaProject) return ReorgUtils.getCorrespondingPackageFragmentRoot((IJavaProject) javaElement); return null; }
Example 8
Source File: RegionBasedHierarchyBuilder.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
/** * Adds all of the openables defined within this package fragment to the * list. */ private void injectAllOpenablesForPackageFragment( IPackageFragment packFrag, ArrayList openables) { try { IPackageFragmentRoot root = (IPackageFragmentRoot) packFrag.getParent(); int kind = root.getKind(); if (kind != 0) { boolean isSourcePackageFragment = (kind == IPackageFragmentRoot.K_SOURCE); if (isSourcePackageFragment) { ICompilationUnit[] cus = packFrag.getCompilationUnits(); for (int i = 0, length = cus.length; i < length; i++) { openables.add(cus[i]); } } else { IClassFile[] classFiles = packFrag.getClassFiles(); for (int i = 0, length = classFiles.length; i < length; i++) { openables.add(classFiles[i]); } } } } catch (JavaModelException e) { // ignore } }
Example 9
Source File: RenameModifications.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
private IPackageFragment[] getSubpackages(IPackageFragment pack) throws CoreException { IPackageFragmentRoot root= (IPackageFragmentRoot) pack.getParent(); IJavaElement[] allPackages= root.getChildren(); if (pack.isDefaultPackage()) { return new IPackageFragment[0]; } ArrayList<IPackageFragment> result= new ArrayList<>(); String prefix= pack.getElementName() + '.'; for (int i= 0; i < allPackages.length; i++) { IPackageFragment currentPackage= (IPackageFragment) allPackages[i]; if (currentPackage.getElementName().startsWith(prefix)) { result.add(currentPackage); } } return result.toArray(new IPackageFragment[result.size()]); }
Example 10
Source File: JavaElementUtil.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
/** * @param pack a package fragment * @return an array containing the given package and all subpackages * @throws JavaModelException if getting the all sibling packages fails */ public static IPackageFragment[] getPackageAndSubpackages(IPackageFragment pack) throws JavaModelException { if (pack.isDefaultPackage()) { return new IPackageFragment[] { pack }; } IPackageFragmentRoot root= (IPackageFragmentRoot) pack.getParent(); IJavaElement[] allPackages= root.getChildren(); ArrayList<IPackageFragment> subpackages= new ArrayList<>(); subpackages.add(pack); String prefix= pack.getElementName() + '.'; for (int i= 0; i < allPackages.length; i++) { IPackageFragment currentPackage= (IPackageFragment) allPackages[i]; if (currentPackage.getElementName().startsWith(prefix)) { subpackages.add(currentPackage); } } return subpackages.toArray(new IPackageFragment[subpackages.size()]); }
Example 11
Source File: ReorgUtils.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
public static boolean isParentInWorkspaceOrOnDisk(IPackageFragment pack, IPackageFragmentRoot root){ if (pack == null) return false; IJavaElement packParent= pack.getParent(); if (packParent == null) return false; if (packParent.equals(root)) return true; IResource packageResource= ResourceUtil.getResource(pack); IResource packageRootResource= ResourceUtil.getResource(root); return isParentInWorkspaceOrOnDisk(packageResource, packageRootResource); }
Example 12
Source File: RenameModifications.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private IPackageFragment[] getSubpackages(IPackageFragment pack) throws CoreException { IPackageFragmentRoot root= (IPackageFragmentRoot) pack.getParent(); IJavaElement[] allPackages= root.getChildren(); if (pack.isDefaultPackage()) return new IPackageFragment[0]; ArrayList<IPackageFragment> result= new ArrayList<IPackageFragment>(); String prefix= pack.getElementName() + '.'; for (int i= 0; i < allPackages.length; i++) { IPackageFragment currentPackage= (IPackageFragment) allPackages[i]; if (currentPackage.getElementName().startsWith(prefix)) result.add(currentPackage); } return result.toArray(new IPackageFragment[result.size()]); }
Example 13
Source File: ReorgUtils.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
public static boolean isParentInWorkspaceOrOnDisk(IPackageFragment pack, IPackageFragmentRoot root){ if (pack == null) { return false; } IJavaElement packParent= pack.getParent(); if (packParent == null) { return false; } if (packParent.equals(root)) { return true; } IResource packageResource= ResourceUtil.getResource(pack); IResource packageRootResource= ResourceUtil.getResource(root); return isParentInWorkspaceOrOnDisk(packageResource, packageRootResource); }
Example 14
Source File: PackageExplorerContentProvider.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
public Object getHierarchicalPackageParent(IPackageFragment child) { String name= child.getElementName(); IPackageFragmentRoot parent= (IPackageFragmentRoot) child.getParent(); int index= name.lastIndexOf('.'); if (index != -1) { String realParentName= name.substring(0, index); IPackageFragment element= parent.getPackageFragment(realParentName); if (element.exists()) { try { if (fFoldPackages && isEmpty(element) && findSinglePackageChild(element, parent.getChildren()) != null) { return getHierarchicalPackageParent(element); } } catch (JavaModelException e) { // ignore } return element; } else { // bug 65240 IResource resource= element.getResource(); if (resource != null) { return resource; } } } if (parent.getResource() instanceof IProject) { return parent.getJavaProject(); } return parent; }
Example 15
Source File: PackagesViewFlatContentProvider.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private IPackageFragment[] getPackageFragments(IPackageFragment[] iPackageFragments) { List<IPackageFragment> list= new ArrayList<IPackageFragment>(); for (int i= 0; i < iPackageFragments.length; i++) { IPackageFragment fragment= iPackageFragments[i]; IJavaElement el= fragment.getParent(); if (el instanceof IPackageFragmentRoot) { IPackageFragmentRoot root= (IPackageFragmentRoot) el; if (root.isExternal()) continue; } list.add(fragment); } return list.toArray(new IPackageFragment[list.size()]); }
Example 16
Source File: PackagesViewFlatContentProvider.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private Object findElementToRefresh(IPackageFragment fragment) { if (fViewer.testFindItem(fragment) == null) { if(fInputIsProject) return fragment.getJavaProject(); else return fragment.getParent(); } return fragment; }
Example 17
Source File: PackagesViewHierarchicalContentProvider.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private Object findNextLevelParentByElementName(IPackageFragment child) { String name= child.getElementName(); int index= name.lastIndexOf('.'); if (index != -1) { String realParentName= name.substring(0, index); IPackageFragment element= ((IPackageFragmentRoot) child.getParent()).getPackageFragment(realParentName); if (element.exists()) { return element; } } return child.getParent(); }
Example 18
Source File: MoveCompilationUnitChange.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 4 votes |
@Override Change doPerformReorg(IProgressMonitor pm) throws CoreException, OperationCanceledException { String name; String newName= getNewName(); if (newName == null) { name= getCu().getElementName(); } else { name= newName; } // get current modification stamp long currentStamp= IResource.NULL_STAMP; IResource resource= getCu().getResource(); if (resource != null) { currentStamp= resource.getModificationStamp(); } IPackageFragment destination= getDestinationPackage(); fUndoable= !destination.exists() || !destination.getCompilationUnit(name).exists(); IPackageFragment[] createdPackages= null; if (!destination.exists()) { IPackageFragmentRoot packageFragmentRoot= (IPackageFragmentRoot) destination.getParent(); createdPackages= createDestination(packageFragmentRoot, destination, pm); } // perform the move and restore modification stamp getCu().move(destination, null, newName, true, pm); if (fStampToRestore != IResource.NULL_STAMP) { ICompilationUnit moved= destination.getCompilationUnit(name); IResource movedResource= moved.getResource(); if (movedResource != null) { movedResource.revertModificationStamp(fStampToRestore); } } if (fDeletePackages != null) { for (int i= fDeletePackages.length - 1; i >= 0; i--) { fDeletePackages[i].delete(true, pm); } } if (fUndoable) { return new MoveCompilationUnitChange(destination, getCu().getElementName(), getOldPackage(), currentStamp, createdPackages); } else { return null; } }
Example 19
Source File: NameLookup.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
/** * Notifies the given requestor of all types (classes and interfaces) in the * given package fragment with the given (unqualified) name. * Checks the requestor at regular intervals to see if the requestor * has canceled. If the given package fragment is <code>null</code>, all types in the * project whose simple name matches the given name are found. * * @param name The name to search * @param pkg The corresponding package fragment * @param partialMatch partial name matches qualify when <code>true</code>; * only exact name matches qualify when <code>false</code> * @param acceptFlags a bit mask describing if classes, interfaces or both classes and interfaces * are desired results. If no flags are specified, all types are returned. * @param requestor The requestor that collects the result * * @see #ACCEPT_CLASSES * @see #ACCEPT_INTERFACES * @see #ACCEPT_ENUMS * @see #ACCEPT_ANNOTATIONS */ public void seekTypes(String name, IPackageFragment pkg, boolean partialMatch, int acceptFlags, IJavaElementRequestor requestor, boolean considerSecondaryTypes) { /* if (VERBOSE) { Util.verbose(" SEEKING TYPES"); //$NON-NLS-1$ Util.verbose(" -> name: " + name); //$NON-NLS-1$ Util.verbose(" -> pkg: " + ((JavaElement) pkg).toStringWithAncestors()); //$NON-NLS-1$ Util.verbose(" -> partial match:" + partialMatch); //$NON-NLS-1$ } */ String matchName= partialMatch ? name.toLowerCase() : name; if (pkg == null) { findAllTypes(matchName, partialMatch, acceptFlags, requestor); return; } PackageFragmentRoot root= (PackageFragmentRoot) pkg.getParent(); try { // look in working copies first int firstDot = -1; String topLevelTypeName = null; int packageFlavor= root.internalKind(); if (this.typesInWorkingCopies != null || packageFlavor == IPackageFragmentRoot.K_SOURCE) { firstDot = matchName.indexOf('.'); if (!partialMatch) topLevelTypeName = firstDot == -1 ? matchName : matchName.substring(0, firstDot); } if (this.typesInWorkingCopies != null) { if (seekTypesInWorkingCopies(matchName, pkg, firstDot, partialMatch, topLevelTypeName, acceptFlags, requestor, considerSecondaryTypes)) return; } // look in model switch (packageFlavor) { case IPackageFragmentRoot.K_BINARY : matchName= matchName.replace('.', '$'); seekTypesInBinaryPackage(matchName, pkg, partialMatch, acceptFlags, requestor); break; case IPackageFragmentRoot.K_SOURCE : seekTypesInSourcePackage(matchName, pkg, firstDot, partialMatch, topLevelTypeName, acceptFlags, requestor); if (matchName.indexOf('$') != -1) { matchName= matchName.replace('$', '.'); firstDot = matchName.indexOf('.'); if (!partialMatch) topLevelTypeName = firstDot == -1 ? matchName : matchName.substring(0, firstDot); seekTypesInSourcePackage(matchName, pkg, firstDot, partialMatch, topLevelTypeName, acceptFlags, requestor); } break; default : return; } } catch (JavaModelException e) { return; } }
Example 20
Source File: MoveCompilationUnitChange.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
@Override Change doPerformReorg(IProgressMonitor pm) throws CoreException, OperationCanceledException { String name; String newName= getNewName(); if (newName == null) name= getCu().getElementName(); else name= newName; // get current modification stamp long currentStamp= IResource.NULL_STAMP; IResource resource= getCu().getResource(); if (resource != null) { currentStamp= resource.getModificationStamp(); } IPackageFragment destination= getDestinationPackage(); fUndoable= !destination.exists() || !destination.getCompilationUnit(name).exists(); IPackageFragment[] createdPackages= null; if (!destination.exists()) { IPackageFragmentRoot packageFragmentRoot= (IPackageFragmentRoot) destination.getParent(); createdPackages= createDestination(packageFragmentRoot, destination, pm); } // perform the move and restore modification stamp getCu().move(destination, null, newName, true, pm); if (fStampToRestore != IResource.NULL_STAMP) { ICompilationUnit moved= destination.getCompilationUnit(name); IResource movedResource= moved.getResource(); if (movedResource != null) { movedResource.revertModificationStamp(fStampToRestore); } } if (fDeletePackages != null) { for (int i= fDeletePackages.length - 1; i >= 0; i--) { fDeletePackages[i].delete(true, pm); } } if (fUndoable) { return new MoveCompilationUnitChange(destination, getCu().getElementName(), getOldPackage(), currentStamp, createdPackages); } else { return null; } }