Search in sources :

Example 6 with RenameRefactoringSession

use of org.eclipse.che.ide.ext.java.shared.dto.refactoring.RenameRefactoringSession in project che by eclipse.

the class RefactoringManager method createRenameRefactoring.

/**
     * Create rename refactoring. It can create two rename refactoring types.
     * First is linked mode rename refactoring, second is classic rename refactoring with wizard.
     *
     * @param element
     *         element to rename
     * @param cu
     *         compilation unit which element belongs. null if element is IPackageFragment.
     * @param offset
     *         cursor position inside editor, used only for linked mode
     * @param lightweight
     *         if true try to create linked mode refactoring
     * @return rename refactoring session
     * @throws CoreException
     *         when impossible to create RenameSupport
     * @throws RefactoringException
     *         when we don't support renaming provided element
     */
public RenameRefactoringSession createRenameRefactoring(IJavaElement element, ICompilationUnit cu, int offset, boolean lightweight) throws CoreException, RefactoringException {
    //package fragments are always renamed with wizard
    RenameRefactoringSession session = DtoFactory.newDto(RenameRefactoringSession.class);
    String id = String.format("rename-%s", sessionId.getAndIncrement());
    session.setSessionId(id);
    session.setOldName(element.getElementName());
    session.setWizardType(getWizardType(element));
    if (lightweight && !(element instanceof IPackageFragment)) {
        RenameLinkedModeRefactoringSession refactoringSession = new RenameLinkedModeRefactoringSession(element, cu, offset);
        LinkedModeModel model = refactoringSession.getModel();
        if (model != null) {
            session.setLinkedModeModel(model);
        }
        sessions.put(id, refactoringSession);
        return session;
    } else {
        RenameSupport renameSupport = createRenameSupport(element, null, RenameSupport.UPDATE_REFERENCES);
        if (renameSupport != null && renameSupport.preCheck().isOK()) {
            RenameRefactoring refactoring = renameSupport.getfRefactoring();
            RenameSession renameSession = new RenameSession(refactoring);
            sessions.put(id, renameSession);
            return session;
        }
        throw new RefactoringException("Can't create refactoring session for element: " + element.getElementName());
    }
}
Also used : IPackageFragment(org.eclipse.jdt.core.IPackageFragment) RenameSession(org.eclipse.che.plugin.java.server.refactoring.session.RenameSession) RenameRefactoring(org.eclipse.ltk.core.refactoring.participants.RenameRefactoring) RenameLinkedModeRefactoringSession(org.eclipse.che.plugin.java.server.refactoring.session.RenameLinkedModeRefactoringSession) LinkedModeModel(org.eclipse.che.ide.ext.java.shared.dto.LinkedModeModel) RenameRefactoringSession(org.eclipse.che.ide.ext.java.shared.dto.refactoring.RenameRefactoringSession) RenameSupport(org.eclipse.jdt.ui.refactoring.RenameSupport)

Example 7 with RenameRefactoringSession

use of org.eclipse.che.ide.ext.java.shared.dto.refactoring.RenameRefactoringSession in project che by eclipse.

the class JavaRefactoringRename method createLinkedRenameSession.

private void createLinkedRenameSession() {
    final CreateRenameRefactoring refactoringSession = createRenameRefactoringDto(textEditor, true);
    Promise<RenameRefactoringSession> createRenamePromise = refactoringServiceClient.createRenameRefactoring(refactoringSession);
    createRenamePromise.then(new Operation<RenameRefactoringSession>() {

        @Override
        public void apply(RenameRefactoringSession session) throws OperationException {
            activateLinkedModeIntoEditor(session, textEditor.getDocument());
        }
    }).catchError(new Operation<PromiseError>() {

        @Override
        public void apply(PromiseError arg) throws OperationException {
            isActiveLinkedEditor = false;
            showError();
        }
    });
}
Also used : PromiseError(org.eclipse.che.api.promises.client.PromiseError) Operation(org.eclipse.che.api.promises.client.Operation) CreateRenameRefactoring(org.eclipse.che.ide.ext.java.shared.dto.refactoring.CreateRenameRefactoring) RenameRefactoringSession(org.eclipse.che.ide.ext.java.shared.dto.refactoring.RenameRefactoringSession) OperationException(org.eclipse.che.api.promises.client.OperationException)

Example 8 with RenameRefactoringSession

use of org.eclipse.che.ide.ext.java.shared.dto.refactoring.RenameRefactoringSession in project che by eclipse.

the class JavaRefactoringRename method createRenameSession.

private void createRenameSession() {
    final CreateRenameRefactoring refactoringSession = createRenameRefactoringDto(textEditor, false);
    Promise<RenameRefactoringSession> createRenamePromise = refactoringServiceClient.createRenameRefactoring(refactoringSession);
    createRenamePromise.then(new Operation<RenameRefactoringSession>() {

        @Override
        public void apply(RenameRefactoringSession session) throws OperationException {
            renamePresenter.show(session);
            if (mode != null) {
                mode.exitLinkedMode(false);
            }
        }
    }).catchError(new Operation<PromiseError>() {

        @Override
        public void apply(PromiseError arg) throws OperationException {
            showError();
        }
    });
}
Also used : PromiseError(org.eclipse.che.api.promises.client.PromiseError) Operation(org.eclipse.che.api.promises.client.Operation) CreateRenameRefactoring(org.eclipse.che.ide.ext.java.shared.dto.refactoring.CreateRenameRefactoring) RenameRefactoringSession(org.eclipse.che.ide.ext.java.shared.dto.refactoring.RenameRefactoringSession) OperationException(org.eclipse.che.api.promises.client.OperationException)

Example 9 with RenameRefactoringSession

use of org.eclipse.che.ide.ext.java.shared.dto.refactoring.RenameRefactoringSession in project che by eclipse.

the class RenamePresenter method show.

/**
     * Show Rename window with the special information.
     *
     * @param refactorInfo
     *         information about the rename operation
     */
public void show(RefactorInfo refactorInfo) {
    this.refactorInfo = refactorInfo;
    final CreateRenameRefactoring createRenameRefactoring = createRenameRefactoringDto(refactorInfo);
    Promise<RenameRefactoringSession> createRenamePromise = refactorService.createRenameRefactoring(createRenameRefactoring);
    createRenamePromise.then(new Operation<RenameRefactoringSession>() {

        @Override
        public void apply(RenameRefactoringSession session) throws OperationException {
            show(session);
        }
    }).catchError(new Operation<PromiseError>() {

        @Override
        public void apply(PromiseError arg) throws OperationException {
            notificationManager.notify(locale.failedToRename(), arg.getMessage(), FAIL, FLOAT_MODE);
        }
    });
}
Also used : PromiseError(org.eclipse.che.api.promises.client.PromiseError) Operation(org.eclipse.che.api.promises.client.Operation) CreateRenameRefactoring(org.eclipse.che.ide.ext.java.shared.dto.refactoring.CreateRenameRefactoring) RenameRefactoringSession(org.eclipse.che.ide.ext.java.shared.dto.refactoring.RenameRefactoringSession) OperationException(org.eclipse.che.api.promises.client.OperationException)

Example 10 with RenameRefactoringSession

use of org.eclipse.che.ide.ext.java.shared.dto.refactoring.RenameRefactoringSession in project che by eclipse.

the class RenameRefactoringTest method testRenamePreviewChanges.

@Test
public void testRenamePreviewChanges() throws Exception {
    StringBuilder b = new StringBuilder();
    b.append("package p;\n");
    b.append("public class A{\n private A a; \n}\n");
    ICompilationUnit unit = getPackageP().createCompilationUnit("A.java", b.toString(), false, null);
    IType type = unit.getAllTypes()[0];
    RenameRefactoringSession refactoring = manager.createRenameRefactoring(type, unit, b.indexOf("A"), false);
    DtoServerImpls.ValidateNewNameImpl validateNewName = new DtoServerImpls.ValidateNewNameImpl();
    validateNewName.setSessionId(refactoring.getSessionId());
    validateNewName.setNewName("MyClass");
    RefactoringStatus status = manager.renameValidateNewName(validateNewName);
    manager.createChange(refactoring.getSessionId());
    RefactoringPreview preview = manager.getRefactoringPreview(refactoring.getSessionId());
    RefactoringChange change1 = new DtoServerImpls.ChangeEnabledStateImpl();
    change1.setSessionId(refactoring.getSessionId());
    change1.setChangeId(preview.getChildrens().get(0).getId());
    ChangePreview changePreview = manager.getChangePreview(change1);
    assertThat(changePreview).isNotNull();
    assertThat(changePreview.getFileName()).isNotNull().isNotEmpty();
    assertThat(changePreview.getOldContent()).isNotNull().isNotEmpty();
    assertThat(changePreview.getNewContent()).isNotNull().isNotEmpty();
    assertThat(changePreview.getNewContent()).isNotEqualTo(changePreview.getOldContent());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) RefactoringChange(org.eclipse.che.ide.ext.java.shared.dto.refactoring.RefactoringChange) ChangePreview(org.eclipse.che.ide.ext.java.shared.dto.refactoring.ChangePreview) DtoServerImpls(org.eclipse.che.plugin.java.server.dto.DtoServerImpls) RefactoringStatus(org.eclipse.che.ide.ext.java.shared.dto.refactoring.RefactoringStatus) RefactoringPreview(org.eclipse.che.ide.ext.java.shared.dto.refactoring.RefactoringPreview) RenameRefactoringSession(org.eclipse.che.ide.ext.java.shared.dto.refactoring.RenameRefactoringSession) IType(org.eclipse.jdt.core.IType) Test(org.junit.Test)

Aggregations

RenameRefactoringSession (org.eclipse.che.ide.ext.java.shared.dto.refactoring.RenameRefactoringSession)13 Test (org.junit.Test)9 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)8 IType (org.eclipse.jdt.core.IType)7 RefactoringStatus (org.eclipse.che.ide.ext.java.shared.dto.refactoring.RefactoringStatus)6 DtoServerImpls (org.eclipse.che.plugin.java.server.dto.DtoServerImpls)5 Operation (org.eclipse.che.api.promises.client.Operation)3 OperationException (org.eclipse.che.api.promises.client.OperationException)3 PromiseError (org.eclipse.che.api.promises.client.PromiseError)3 CreateRenameRefactoring (org.eclipse.che.ide.ext.java.shared.dto.refactoring.CreateRenameRefactoring)3 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)2 LinkedModeModel (org.eclipse.che.ide.ext.java.shared.dto.LinkedModeModel)1 ChangeCreationResult (org.eclipse.che.ide.ext.java.shared.dto.refactoring.ChangeCreationResult)1 ChangePreview (org.eclipse.che.ide.ext.java.shared.dto.refactoring.ChangePreview)1 LinkedRenameRefactoringApply (org.eclipse.che.ide.ext.java.shared.dto.refactoring.LinkedRenameRefactoringApply)1 RefactoringChange (org.eclipse.che.ide.ext.java.shared.dto.refactoring.RefactoringChange)1 RefactoringPreview (org.eclipse.che.ide.ext.java.shared.dto.refactoring.RefactoringPreview)1 RenameSettings (org.eclipse.che.ide.ext.java.shared.dto.refactoring.RenameSettings)1 ValidateNewName (org.eclipse.che.ide.ext.java.shared.dto.refactoring.ValidateNewName)1 RenameLinkedModeRefactoringSession (org.eclipse.che.plugin.java.server.refactoring.session.RenameLinkedModeRefactoringSession)1