Search in sources :

Example 36 with IContainer

use of org.eclipse.core.resources.IContainer in project che by eclipse.

the class RenamePackageTest method testPackageRenameWithResource1.

@Test
public void testPackageRenameWithResource1() throws Exception {
    IPackageFragment fragment = getRoot().createPackageFragment("org.test", true, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package org.test;\n");
    buf.append("public class MyClass {\n");
    buf.append("	org.test.MyClass me;\n");
    buf.append("}\n");
    fragment.createCompilationUnit("MyClass.java", buf.toString(), true, null);
    IFile file = ((IFolder) getRoot().getResource()).getFile("x.properties");
    byte[] content = "This is about 'org.test' and more".getBytes();
    file.create(new ByteArrayInputStream(content), true, null);
    file.refreshLocal(IResource.DEPTH_ONE, null);
    RenameJavaElementDescriptor descriptor = RefactoringSignatureDescriptorFactory.createRenameJavaElementDescriptor(IJavaRefactorings.RENAME_PACKAGE);
    descriptor.setJavaElement(fragment);
    descriptor.setNewName("org.test2");
    descriptor.setUpdateReferences(true);
    descriptor.setUpdateQualifiedNames(true);
    descriptor.setFileNamePatterns("*.properties");
    Refactoring refactoring = createRefactoring(descriptor);
    RefactoringStatus status = performRefactoring(refactoring);
    if (status != null)
        assertTrue(status.toString(), status.isOK());
    RefactoringProcessor processor = ((RenameRefactoring) refactoring).getProcessor();
    IResourceMapper rm = (IResourceMapper) processor.getAdapter(IResourceMapper.class);
    IJavaElementMapper jm = (IJavaElementMapper) processor.getAdapter(IJavaElementMapper.class);
    checkMappingUnchanged(jm, rm, new Object[] { getRoot().getJavaProject(), getRoot(), file });
    IFile newFile = ((IContainer) getRoot().getResource()).getFile(new Path("x.properties"));
    assertEquals("This is about 'org.test2' and more", getContents(newFile));
    checkMappingChanged(jm, rm, new Object[][] { { fragment, getRoot().getPackageFragment("org.test2") } });
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IFile(org.eclipse.core.resources.IFile) RenameRefactoring(org.eclipse.ltk.core.refactoring.participants.RenameRefactoring) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IResourceMapper(org.eclipse.ltk.core.refactoring.IResourceMapper) ByteArrayInputStream(java.io.ByteArrayInputStream) RefactoringProcessor(org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor) IJavaElementMapper(org.eclipse.jdt.core.refactoring.IJavaElementMapper) RenameJavaElementDescriptor(org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor) IContainer(org.eclipse.core.resources.IContainer) Refactoring(org.eclipse.ltk.core.refactoring.Refactoring) RenameRefactoring(org.eclipse.ltk.core.refactoring.participants.RenameRefactoring) ProcessorBasedRefactoring(org.eclipse.ltk.core.refactoring.participants.ProcessorBasedRefactoring) IFolder(org.eclipse.core.resources.IFolder) Test(org.junit.Test)

Example 37 with IContainer

use of org.eclipse.core.resources.IContainer in project che by eclipse.

the class RenamePackageTest method helper2.

private RenamePackageProcessor helper2(String[] packageNames, String[][] packageFileNames, String newPackageName) throws Exception {
    ParticipantTesting.reset();
    IPackageFragment[] packages = new IPackageFragment[packageNames.length];
    ICompilationUnit[][] cus = new ICompilationUnit[packageFileNames.length][packageFileNames[0].length];
    for (int i = 0; i < packageNames.length; i++) {
        packages[i] = getRoot().createPackageFragment(packageNames[i], true, null);
        for (int j = 0; j < packageFileNames[i].length; j++) {
            cus[i][j] = createCUfromTestFile(packages[i], packageFileNames[i][j], packageNames[i].replace('.', '/') + "/");
        }
    }
    IPackageFragment thisPackage = packages[0];
    boolean hasSubpackages = thisPackage.hasSubpackages();
    IPath path = thisPackage.getParent().getPath();
    path = path.append(newPackageName.replace('.', '/'));
    IFolder target = ResourcesPlugin.getWorkspace().getRoot().getFolder(path);
    boolean targetExists = target.exists();
    boolean isRename = !targetExists && !thisPackage.hasSubpackages() && thisPackage.getResource().getParent().equals(target.getParent());
    String[] createHandles = null;
    String[] moveHandles = null;
    String[] deleteHandles = null;
    boolean doDelete = true;
    String[] renameHandles = null;
    if (isRename) {
        renameHandles = ParticipantTesting.createHandles(thisPackage, thisPackage.getResource());
    } else {
        renameHandles = ParticipantTesting.createHandles(thisPackage);
        IContainer loop = target;
        List handles = new ArrayList();
        while (loop != null && !loop.exists()) {
            handles.add(ParticipantTesting.createHandles(loop)[0]);
            loop = loop.getParent();
        }
        createHandles = (String[]) handles.toArray(new String[handles.size()]);
        IFolder source = (IFolder) thisPackage.getResource();
        deleteHandles = ParticipantTesting.createHandles(source);
        IResource[] members = source.members();
        List movedObjects = new ArrayList();
        for (int i = 0; i < members.length; i++) {
            if (members[i] instanceof IFolder) {
                doDelete = false;
            } else {
                movedObjects.add(members[i]);
            }
        }
        moveHandles = ParticipantTesting.createHandles(movedObjects.toArray());
    }
    RenameJavaElementDescriptor descriptor = createRefactoringDescriptor(thisPackage, newPackageName);
    descriptor.setUpdateReferences(fUpdateReferences);
    descriptor.setUpdateTextualOccurrences(fUpdateTextualMatches);
    setFilePatterns(descriptor);
    Refactoring refactoring = createRefactoring(descriptor);
    RefactoringStatus result = performRefactoring(refactoring);
    TestCase.assertEquals("preconditions were supposed to pass", null, result);
    if (isRename) {
        ParticipantTesting.testRename(renameHandles, new RenameArguments[] { new RenameArguments(newPackageName, fUpdateReferences), new RenameArguments(target.getName(), fUpdateReferences) });
    } else {
        ParticipantTesting.testRename(renameHandles, new RenameArguments[] { new RenameArguments(newPackageName, fUpdateReferences) });
        ParticipantTesting.testCreate(createHandles);
        List args = new ArrayList();
        for (int i = 0; i < packageFileNames[0].length; i++) {
            args.add(new MoveArguments(target, fUpdateReferences));
        }
        ParticipantTesting.testMove(moveHandles, (MoveArguments[]) args.toArray(new MoveArguments[args.size()]));
        if (doDelete) {
            ParticipantTesting.testDelete(deleteHandles);
        } else {
            ParticipantTesting.testDelete(new String[0]);
        }
    }
    if (hasSubpackages) {
        assertTrue("old package does not exist anymore", getRoot().getPackageFragment(packageNames[0]).exists());
    } else {
        assertTrue("package not renamed", !getRoot().getPackageFragment(packageNames[0]).exists());
    }
    IPackageFragment newPackage = getRoot().getPackageFragment(newPackageName);
    assertTrue("new package does not exist", newPackage.exists());
    for (int i = 0; i < packageFileNames.length; i++) {
        String packageName = (i == 0) ? newPackageName.replace('.', '/') + "/" : packageNames[i].replace('.', '/') + "/";
        for (int j = 0; j < packageFileNames[i].length; j++) {
            String s1 = getFileContents(getOutputTestFileName(packageFileNames[i][j], packageName));
            ICompilationUnit cu = (i == 0) ? newPackage.getCompilationUnit(packageFileNames[i][j] + ".java") : cus[i][j];
            //DebugUtils.dump("cu:" + cu.getElementName());
            String s2 = cu.getSource();
            //DebugUtils.dump("expected:" + s1);
            //DebugUtils.dump("was:" + s2);
            assertEqualLines("invalid update in file " + cu.getElementName(), s1, s2);
        }
    }
    RefactoringProcessor processor = ((ProcessorBasedRefactoring) refactoring).getProcessor();
    return (RenamePackageProcessor) processor;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) RenamePackageProcessor(org.eclipse.jdt.internal.corext.refactoring.rename.RenamePackageProcessor) IPath(org.eclipse.core.runtime.IPath) RenameArguments(org.eclipse.ltk.core.refactoring.participants.RenameArguments) ArrayList(java.util.ArrayList) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) ProcessorBasedRefactoring(org.eclipse.ltk.core.refactoring.participants.ProcessorBasedRefactoring) RefactoringProcessor(org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor) MoveArguments(org.eclipse.ltk.core.refactoring.participants.MoveArguments) List(java.util.List) ArrayList(java.util.ArrayList) IContainer(org.eclipse.core.resources.IContainer) RenameJavaElementDescriptor(org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor) Refactoring(org.eclipse.ltk.core.refactoring.Refactoring) RenameRefactoring(org.eclipse.ltk.core.refactoring.participants.RenameRefactoring) ProcessorBasedRefactoring(org.eclipse.ltk.core.refactoring.participants.ProcessorBasedRefactoring) IResource(org.eclipse.core.resources.IResource) IFolder(org.eclipse.core.resources.IFolder)

Example 38 with IContainer

use of org.eclipse.core.resources.IContainer in project tdi-studio-se by Talend.

the class ImportProjectsUtilities method afterImportAs.

/**
     * DOC smallet Comment method "afterImportAs".
     * 
     * @param newName
     * @param technicalName
     * @throws InvocationTargetException
     */
private static Project afterImportAs(String newName, String technicalName) throws InvocationTargetException {
    // Rename in ".project" and "talendProject" or "talend.project"
    // TODO SML Optimize
    final IWorkspace workspace = org.eclipse.core.resources.ResourcesPlugin.getWorkspace();
    IContainer containers = (IProject) workspace.getRoot().findMember(new Path(technicalName));
    IResource file2 = containers.findMember(IProjectDescription.DESCRIPTION_FILE_NAME);
    try {
        //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        replaceInFile("<name>.*</name>", file2.getLocation().toOSString(), "<name>" + technicalName + "</name>");
        // TDI-19269
        final IProject project = workspace.getRoot().getProject(technicalName);
        XmiResourceManager xmiManager = new XmiResourceManager();
        try {
            final Project loadProject = xmiManager.loadProject(project);
            loadProject.setTechnicalLabel(technicalName);
            loadProject.setLabel(newName);
            loadProject.setLocal(true);
            loadProject.setId(0);
            loadProject.setUrl(null);
            loadProject.setCreationDate(null);
            loadProject.setDescription("");
            loadProject.setType(null);
            // ADD xqliu 2012-03-12 TDQ-4771 clear the list of Folders
            if (loadProject.getFolders() != null) {
                loadProject.getFolders().clear();
            }
            // ~ TDQ-4771
            xmiManager.saveResource(loadProject.eResource());
            return loadProject;
        } catch (PersistenceException e) {
        //
        }
    } catch (IOException e) {
        throw new InvocationTargetException(e);
    }
    return null;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) Project(org.talend.core.model.properties.Project) IProject(org.eclipse.core.resources.IProject) IWorkspace(org.eclipse.core.resources.IWorkspace) PersistenceException(org.talend.commons.exception.PersistenceException) XmiResourceManager(org.talend.core.repository.utils.XmiResourceManager) IOException(java.io.IOException) IContainer(org.eclipse.core.resources.IContainer) IProject(org.eclipse.core.resources.IProject) IResource(org.eclipse.core.resources.IResource) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 39 with IContainer

use of org.eclipse.core.resources.IContainer in project tdi-studio-se by Talend.

the class BusinessNewDiagramFileWizard method addPages.

/**
     * @generated NOT
     */
public void addPages() {
    myFileCreationPage = new WizardNewFileCreationPage(Messages.getString(//$NON-NLS-1$
    "BusinessNewDiagramFileWizard.IntialNewEcoreDiagramFile"), //$NON-NLS-1$
    mySelection) {

        public void createControl(Composite parent) {
            super.createControl(parent);
            IContainer parentContainer = mySelectedModelFile.getParent();
            String originalFileName = mySelectedModelFile.getProjectRelativePath().removeFileExtension().lastSegment();
            //$NON-NLS-1$
            String fileExtension = ".business_diagram";
            String fileName = originalFileName + fileExtension;
            for (int i = 1; parentContainer.getFile(new Path(fileName)).exists(); i++) {
                fileName = originalFileName + i + fileExtension;
            }
            setFileName(fileName);
        }
    };
    //$NON-NLS-1$
    myFileCreationPage.setTitle(Messages.getString("BusinessNewDiagramFileWizard.DiagramFile"));
    myFileCreationPage.setDescription(Messages.getString("BusinessNewDiagramFileWizard.CreateNewDiagram", //$NON-NLS-1$ //$NON-NLS-2$
    BusinessProcessEditPart.MODEL_ID));
    addPage(myFileCreationPage);
    addPage(new RootElementSelectorPage());
}
Also used : Path(org.eclipse.core.runtime.Path) Composite(org.eclipse.swt.widgets.Composite) WizardNewFileCreationPage(org.eclipse.ui.dialogs.WizardNewFileCreationPage) IContainer(org.eclipse.core.resources.IContainer)

Example 40 with IContainer

use of org.eclipse.core.resources.IContainer in project KaiZen-OpenAPI-Editor by RepreZen.

the class Mocks method mockJsonReferenceProposalFile.

public static IFile mockJsonReferenceProposalFile() {
    IFile file = mock(IFile.class);
    IContainer parent = mock(IContainer.class);
    IPath parentPath = mock(IPath.class);
    when(file.getParent()).thenReturn(parent);
    when(parent.getFullPath()).thenReturn(parentPath);
    return file;
}
Also used : IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) IContainer(org.eclipse.core.resources.IContainer)

Aggregations

IContainer (org.eclipse.core.resources.IContainer)199 IResource (org.eclipse.core.resources.IResource)89 IFile (org.eclipse.core.resources.IFile)71 IPath (org.eclipse.core.runtime.IPath)47 CoreException (org.eclipse.core.runtime.CoreException)46 IFolder (org.eclipse.core.resources.IFolder)45 Path (org.eclipse.core.runtime.Path)43 IProject (org.eclipse.core.resources.IProject)28 IWorkspaceRoot (org.eclipse.core.resources.IWorkspaceRoot)26 ArrayList (java.util.ArrayList)25 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)21 IOException (java.io.IOException)13 IStatus (org.eclipse.core.runtime.IStatus)13 ByteArrayInputStream (java.io.ByteArrayInputStream)12 File (java.io.File)12 InvocationTargetException (java.lang.reflect.InvocationTargetException)12 IWorkbenchPage (org.eclipse.ui.IWorkbenchPage)12 Status (org.eclipse.core.runtime.Status)10 PartInitException (org.eclipse.ui.PartInitException)10 InputStream (java.io.InputStream)9