Search in sources :

Example 81 with IPackageFragmentRoot

use of org.eclipse.jdt.core.IPackageFragmentRoot in project tdi-studio-se by Talend.

the class EditPropertiesAction method processRename.

protected void processRename(IRepositoryNode node, String originalName) {
    try {
        IRunProcessService runProcessService = CorePlugin.getDefault().getRunProcessService();
        ITalendProcessJavaProject talendProcessJavaProject = runProcessService.getTalendProcessJavaProject();
        if (talendProcessJavaProject == null) {
            return;
        }
        IFolder srcFolder = talendProcessJavaProject.getSrcFolder();
        IPackageFragmentRoot root = talendProcessJavaProject.getJavaProject().getPackageFragmentRoot(srcFolder);
        // add for bug TDI-24379 on August 23, 2013.
        IFolder srcInterFolder = srcFolder.getFolder(JavaUtils.JAVA_INTERNAL_DIRECTORY);
        if (srcInterFolder.exists()) {
            File file = new File(srcInterFolder.getLocationURI());
            for (File f : file.listFiles()) {
                if (f.isFile()) {
                    f.delete();
                }
            }
        }
        // qli modified to fix the bug 5400 and 6185.
        // update for fix [TESB-6784]
        IPackageFragment routinesPkg = getPackageFragment(root, node);
        // ICompilationUnit unit = routinesPkg.getCompilationUnit(originalName +
        // SuffixConstants.SUFFIX_STRING_java);
        //$NON-NLS-1$
        ICompilationUnit unit = routinesPkg.getCompilationUnit(originalName + ".java");
        if (unit == null) {
            return;
        }
        String newName = node.getObject().getProperty().getLabel();
        JavaRenameProcessor processor = new RenameCompilationUnitProcessor(unit);
        // processor.setNewElementName(newName + SuffixConstants.SUFFIX_STRING_java);
        //$NON-NLS-1$
        processor.setNewElementName(newName + ".java");
        RenameRefactoring ref = new RenameRefactoring(processor);
        final PerformRefactoringOperation operation = new PerformRefactoringOperation(ref, CheckConditionsOperation.ALL_CONDITIONS);
        IRunnableWithProgress r = new IRunnableWithProgress() {

            @Override
            public void run(final IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                Display.getDefault().asyncExec(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            operation.run(monitor);
                        } catch (CoreException e) {
                            ExceptionHandler.process(e);
                        }
                    }
                });
            }
        };
        PlatformUI.getWorkbench().getProgressService().run(true, true, r);
        RefactoringStatus conditionStatus = operation.getConditionStatus();
        if (conditionStatus.hasError()) {
            //$NON-NLS-1$
            String errorMessage = Messages.getString("EditPropertiesAction.renameError", unit.getElementName(), newName);
            RefactoringStatusEntry[] entries = conditionStatus.getEntries();
            for (RefactoringStatusEntry entry : entries) {
                //$NON-NLS-1$
                errorMessage += "\n>>>" + entry.getMessage();
            }
            Shell shell = null;
            IRepositoryView viewPart = getViewPart();
            if (viewPart != null) {
                shell = viewPart.getViewSite().getShell();
            } else {
                shell = Display.getCurrent().getActiveShell();
            }
            //$NON-NLS-1$
            MessageDialog.openError(shell, Messages.getString("EditPropertiesAction.warning"), errorMessage);
            return;
        }
        // ICompilationUnit newUnit = routinesPkg.getCompilationUnit(newName + SuffixConstants.SUFFIX_STRING_java);
        //$NON-NLS-1$
        ICompilationUnit newUnit = routinesPkg.getCompilationUnit(newName + ".java");
        if (newUnit == null) {
            return;
        }
        RoutineItem item = (RoutineItem) node.getObject().getProperty().getItem();
        IFile javaFile = (IFile) newUnit.getAdapter(IResource.class);
        try {
            ByteArray byteArray = item.getContent();
            byteArray.setInnerContentFromFile(javaFile);
            IRepositoryService service = CorePlugin.getDefault().getRepositoryService();
            IProxyRepositoryFactory repFactory = service.getProxyRepositoryFactory();
            repFactory.save(item);
        } catch (Exception e) {
            // e.printStackTrace();
            ExceptionHandler.process(e);
        }
    } catch (Exception e) {
        // e.printStackTrace();
        ExceptionHandler.process(e);
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) IRunProcessService(org.talend.designer.runprocess.IRunProcessService) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) PerformRefactoringOperation(org.eclipse.ltk.core.refactoring.PerformRefactoringOperation) IRepositoryView(org.talend.repository.ui.views.IRepositoryView) ITalendProcessJavaProject(org.talend.core.runtime.process.ITalendProcessJavaProject) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) IRepositoryService(org.talend.repository.model.IRepositoryService) Shell(org.eclipse.swt.widgets.Shell) ByteArray(org.talend.core.model.properties.ByteArray) JavaRenameProcessor(org.eclipse.jdt.internal.corext.refactoring.rename.JavaRenameProcessor) IProxyRepositoryFactory(org.talend.repository.model.IProxyRepositoryFactory) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) RenameRefactoring(org.eclipse.ltk.core.refactoring.participants.RenameRefactoring) RefactoringStatusEntry(org.eclipse.ltk.core.refactoring.RefactoringStatusEntry) RoutineItem(org.talend.core.model.properties.RoutineItem) CoreException(org.eclipse.core.runtime.CoreException) PartInitException(org.eclipse.ui.PartInitException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) RenameCompilationUnitProcessor(org.eclipse.jdt.internal.corext.refactoring.rename.RenameCompilationUnitProcessor) CoreException(org.eclipse.core.runtime.CoreException) IFile(org.eclipse.core.resources.IFile) File(java.io.File) IResource(org.eclipse.core.resources.IResource) IFolder(org.eclipse.core.resources.IFolder)

Example 82 with IPackageFragmentRoot

use of org.eclipse.jdt.core.IPackageFragmentRoot in project AutoRefactor by JnRouvignac.

the class JavaCoreHelper method addSourceContainer.

private static IPackageFragmentRoot addSourceContainer(IJavaProject javaProject, String containerName) throws Exception {
    final IProject project = javaProject.getProject();
    final IFolder folder = project.getFolder(containerName);
    createFolder(folder);
    IPackageFragmentRoot root = javaProject.getPackageFragmentRoot(folder);
    IClasspathEntry cpe = JavaCore.newSourceEntry(root.getPath(), EMPTY_PATHS, EMPTY_PATHS, null);
    addToClasspath(javaProject, Arrays.asList(cpe));
    return root;
}
Also used : IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) IProject(org.eclipse.core.resources.IProject) IFolder(org.eclipse.core.resources.IFolder) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Example 83 with IPackageFragmentRoot

use of org.eclipse.jdt.core.IPackageFragmentRoot in project AutoRefactor by JnRouvignac.

the class PrepareApplyRefactoringsJob method addAll.

private void addAll(Queue<RefactoringUnit> results, List<IJavaElement> javaElements) throws JavaModelException {
    for (IJavaElement javaElement : javaElements) {
        final JavaProjectOptions options = getJavaProjectOptions(javaElement);
        if (javaElement instanceof ICompilationUnit) {
            add(results, (ICompilationUnit) javaElement, options);
        } else if (javaElement instanceof IPackageFragment) {
            final IPackageFragment pf = (IPackageFragment) javaElement;
            addAll(results, pf.getCompilationUnits(), options);
        } else if (javaElement instanceof IPackageFragmentRoot) {
            final IPackageFragmentRoot pfr = (IPackageFragmentRoot) javaElement;
            addAll(results, Arrays.asList(pfr.getChildren()));
        } else if (javaElement instanceof IJavaProject) {
            IJavaProject javaProject = (IJavaProject) javaElement;
            for (IPackageFragment pf : javaProject.getPackageFragments()) {
                addAll(results, pf.getCompilationUnits(), options);
            }
        }
    }
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IJavaProject(org.eclipse.jdt.core.IJavaProject) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Example 84 with IPackageFragmentRoot

use of org.eclipse.jdt.core.IPackageFragmentRoot in project sling by apache.

the class JcrContentContentProvider method getPipelinedChildren.

@Override
public void getPipelinedChildren(Object aParent, Set theCurrentChildren) {
    if (aParent instanceof IProject) {
        IProject project = (IProject) aParent;
        if (ProjectHelper.isContentProject(project)) {
            for (Iterator<?> it = theCurrentChildren.iterator(); it.hasNext(); ) {
                Object aChild = (Object) it.next();
                if (aChild instanceof IPackageFragmentRoot) {
                    IPackageFragmentRoot ipfr = (IPackageFragmentRoot) aChild;
                    IResource res = ipfr.getResource();
                    IFolder syncDir = getSyncDir(project);
                    if (res != null && syncDir != null && res.equals(syncDir)) {
                        // then remove this one folder provided via j2ee content provider
                        // reason: we are showing it too via the sling content provider
                        it.remove();
                        // and we can break here since there's only one syncdir currently
                        break;
                    }
                }
            }
        }
        Object[] children = projectGetChildren(project);
        if (children != null && children.length > 0) {
            theCurrentChildren.addAll(Arrays.asList(children));
        }
        return;
    } else if (aParent instanceof SyncDir) {
        theCurrentChildren.clear();
        Object[] children = getChildren(aParent);
        if (children != null) {
            theCurrentChildren.addAll(Arrays.asList(children));
        }
    }
}
Also used : SyncDir(org.apache.sling.ide.eclipse.ui.nav.model.SyncDir) IProject(org.eclipse.core.resources.IProject) IResource(org.eclipse.core.resources.IResource) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) IFolder(org.eclipse.core.resources.IFolder)

Example 85 with IPackageFragmentRoot

use of org.eclipse.jdt.core.IPackageFragmentRoot in project bndtools by bndtools.

the class PackageDecorator method updateDecoration.

public static void updateDecoration(IProject project, Project model) throws Exception {
    if (!project.isOpen()) {
        return;
    }
    IJavaProject javaProject = JavaCore.create(project);
    if (javaProject == null) {
        // project is not a java project
        return;
    }
    boolean changed = false;
    for (IClasspathEntry cpe : javaProject.getRawClasspath()) {
        if (cpe.getEntryKind() != IClasspathEntry.CPE_SOURCE) {
            continue;
        }
        for (IPackageFragmentRoot pkgRoot : javaProject.findPackageFragmentRoots(cpe)) {
            assert pkgRoot.getKind() == IPackageFragmentRoot.K_SOURCE;
            IResource pkgRootResource = pkgRoot.getCorrespondingResource();
            if (pkgRootResource == null) {
                continue;
            }
            File pkgRootFile = pkgRootResource.getLocation().toFile();
            boolean pkgInSourcePath = model.getSourcePath().contains(pkgRootFile);
            for (IJavaElement child : pkgRoot.getChildren()) {
                IPackageFragment pkg = (IPackageFragment) child;
                assert pkg.getKind() == IPackageFragmentRoot.K_SOURCE;
                IResource pkgResource = pkg.getCorrespondingResource();
                if (pkgResource == null) {
                    continue;
                }
                String text = pkgResource.getPersistentProperty(packageDecoratorKey);
                if (pkgInSourcePath) {
                    String pkgName = pkg.getElementName();
                    // Decorate if exported package
                    Attrs pkgAttrs = model.getExports().getByFQN(pkgName);
                    if (pkgAttrs != null) {
                        StringBuilder sb = new StringBuilder(" ").append(Version.parseVersion(pkgAttrs.getVersion()));
                        pkgAttrs = model.getImports().getByFQN(pkgName);
                        if (pkgAttrs != null) {
                            String versionRange = pkgAttrs.getVersion();
                            if (versionRange != null) {
                                sb.append('↔').append(versionRange);
                            }
                        }
                        String version = sb.toString();
                        if (!version.equals(text)) {
                            pkgResource.setPersistentProperty(packageDecoratorKey, version);
                            changed = true;
                        }
                        continue;
                    }
                    // Decorate if non-empty, non-contained package
                    if (pkg.containsJavaResources() && !model.getContained().containsFQN(pkgName)) {
                        if (!excluded.equals(text)) {
                            pkgResource.setPersistentProperty(packageDecoratorKey, excluded);
                            changed = true;
                        }
                        continue;
                    }
                }
                // Clear decoration
                if (text != null) {
                    pkgResource.setPersistentProperty(packageDecoratorKey, null);
                    changed = true;
                }
            }
        }
    }
    // If decoration change, update display
    if (changed) {
        Display display = PlatformUI.getWorkbench().getDisplay();
        SWTConcurrencyUtil.execForDisplay(display, true, new Runnable() {

            @Override
            public void run() {
                PlatformUI.getWorkbench().getDecoratorManager().update(packageDecoratorId);
            }
        });
    }
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) Attrs(aQute.bnd.header.Attrs) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) IJavaProject(org.eclipse.jdt.core.IJavaProject) File(java.io.File) IResource(org.eclipse.core.resources.IResource) Display(org.eclipse.swt.widgets.Display)

Aggregations

IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)179 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)96 Test (org.junit.Test)95 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)78 OrganizeImportsOperation (org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation)67 IChooseImportQuery (org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation.IChooseImportQuery)67 IJavaProject (org.eclipse.jdt.core.IJavaProject)37 File (java.io.File)26 IJavaElement (org.eclipse.jdt.core.IJavaElement)22 IResource (org.eclipse.core.resources.IResource)17 JarEntry (org.eclipse.che.ide.ext.java.shared.JarEntry)16 IPath (org.eclipse.core.runtime.IPath)15 ArrayList (java.util.ArrayList)13 IType (org.eclipse.jdt.core.IType)12 CoreException (org.eclipse.core.runtime.CoreException)11 Path (org.eclipse.core.runtime.Path)11 IClasspathEntry (org.eclipse.jdt.core.IClasspathEntry)11 JavaModelException (org.eclipse.jdt.core.JavaModelException)10 IFile (org.eclipse.core.resources.IFile)9 IProject (org.eclipse.core.resources.IProject)9