Example usage for org.eclipse.jdt.core IJavaModel rename

List of usage examples for org.eclipse.jdt.core IJavaModel rename

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaModel rename.

Prototype

void rename(IJavaElement[] elements, IJavaElement[] destinations, String[] names, boolean replace,
        IProgressMonitor monitor) throws JavaModelException;

Source Link

Document

Renames the given elements as specified.

Usage

From source file:fr.imag.adele.cadse.cadseg.operation.MigrateCodePagesAction.java

License:Apache License

/**
 * Finish1.//from w ww .j a va2 s.  c o  m
 * 
 * @param pmo
 *            the pmo
 * 
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 * @throws JAXBException
 *             the JAXB exception
 * @throws CoreException
 *             the core exception
 */
private void finish1(IProgressMonitor pmo) throws IOException, JAXBException, CoreException {
    CadseCore.getCadseDomain().beginOperation("Import binary cadse");
    List<IJavaElement> destinations = new ArrayList<IJavaElement>();
    List<IJavaElement> elements = new ArrayList<IJavaElement>();
    List<String> names = new ArrayList<String>();
    try {
        pmo.beginTask("migrate code from " + oldCadse.getName() + " to " + newCadse.getName(),
                oldCadse.getItemType().size() * 2 + 1);

        its_old = new HashMap<String, CItemType>();
        its_new = new HashMap<String, CItemType>();
        IFolder oldsources = oldProject.getFolder("sources");
        IFolder newsources = newProject.getFolder("sources");
        IJavaProject oldjp = JavaCore.create(oldProject);
        IJavaProject newjp = JavaCore.create(newProject);

        IPackageFragmentRoot joldsources = oldjp.getPackageFragmentRoot(oldsources);
        IPackageFragmentRoot jnewsources = newjp.getPackageFragmentRoot(newsources);

        for (CItemType cit : oldCadse.getItemType()) {
            its_old.put(cit.getId(), cit);
        }
        for (CItemType cit : newCadse.getItemType()) {
            its_new.put(cit.getId(), cit);
        }
        pmo.worked(1);

        for (CItemType cit : oldCadse.getItemType()) {
            pmo.worked(1);

            String qclazz = cit.getManagerClass();
            if (qclazz == null)
                continue;

            String oldpn = getPackage(qclazz);
            String oldcn = getClassName(qclazz);

            CItemType newcit = its_new.get(cit.getId());
            if (newcit == null) {
                System.out.println("Cannot migrate " + qclazz);
                continue;
            }
            String newqclazz = newcit.getManagerClass();
            if (newqclazz == null) {
                System.out.println("Cannot migrate " + qclazz);
                continue;
            }
            String newpn = getPackage(newqclazz);
            String newcn = getClassName(newqclazz);
            IFile fo = oldsources.getFile(new Path(oldpn.replace('.', '/')).append(oldcn + ".java"));
            IFile fn = newsources.getFile(new Path(newpn.replace('.', '/')).append(newcn + ".java"));
            if (!fo.exists() || !fn.exists()) {
                System.out.println("Cannot migrate " + qclazz);
                continue;
            }

            ICompilationUnit oldcu = JavaCore.createCompilationUnitFrom(fo);
            ICompilationUnit newcu = JavaCore.createCompilationUnitFrom(fn);
            if (oldcu == null || newcu == null || !oldcu.exists() || !newcu.exists()) {
                System.out.println("Cannot migrate " + qclazz);
                continue;
            }

            IJavaElement findelement = oldcu;
            IJavaElement newdestination = joldsources.createPackageFragment(oldpn, true, pmo);
            ;
            String newname = oldcn + ".java";

            if (!oldpn.equals(newpn)) {
                // move
                System.out.println("move " + oldpn + " to " + newpn);
                newdestination = joldsources.createPackageFragment(newpn, true, pmo);
            }
            if (!oldcn.equals(newcn)) {
                System.out.println("rename " + oldcn + " to " + newcn);
                newname = newcn + ".java";
            }
            elements.add(findelement);
            destinations.add(newdestination);
            names.add(newname);

            cit.setManagerClass(oldCadse.getName() + "/" + newpn + "." + newcn);
            System.out.println("change manager to " + cit.getManagerClass());
        }
        IJavaModel jm = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot());
        jm.rename((IJavaElement[]) elements.toArray(new IJavaElement[elements.size()]),
                (IJavaElement[]) destinations.toArray(new IJavaElement[destinations.size()]),
                (String[]) names.toArray(new String[names.size()]), false, pmo);

        writeCadse(oldProject);
        //         
        // for (CItemType cit : cadse.getItemType()) {
        // Item sourceItemType = its.get(cit.getId());
        // if (sourceItemType == null) continue;
        // for( CLinkType clt : cit.getOutgoingLink()) {
        //               
        // }
        // pmo.worked(1);
        // }

    } catch (JavaModelException e) {
        System.out.println(e.getStatus());
        e.printStackTrace();
    } finally {
        CadseCore.getCadseDomain().endOperation();
    }
}