Search in sources :

Example 1 with LinkedModeModel

use of org.eclipse.che.ide.ext.java.shared.dto.LinkedModeModel 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 2 with LinkedModeModel

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

the class JavaRefactoringRename method activateLinkedModeIntoEditor.

private void activateLinkedModeIntoEditor(final RenameRefactoringSession session, final Document document) {
    mode = linkedEditor.getLinkedMode();
    LinkedModel model = linkedEditor.createLinkedModel();
    LinkedModeModel linkedModeModel = session.getLinkedModeModel();
    List<LinkedModelGroup> groups = new ArrayList<>();
    for (LinkedPositionGroup positionGroup : linkedModeModel.getGroups()) {
        LinkedModelGroup group = linkedEditor.createLinkedGroup();
        LinkedData data = positionGroup.getData();
        if (data != null) {
            LinkedModelData modelData = linkedEditor.createLinkedModelData();
            modelData.setType("link");
            modelData.setValues(data.getValues());
            group.setData(modelData);
        }
        List<Position> positions = new ArrayList<>();
        for (Region region : positionGroup.getPositions()) {
            positions.add(new Position(region.getOffset(), region.getLength()));
        }
        group.setPositions(positions);
        groups.add(group);
    }
    model.setGroups(groups);
    disableAutoSave();
    mode.enterLinkedMode(model);
    mode.addListener(new LinkedMode.LinkedModeListener() {

        @Override
        public void onLinkedModeExited(boolean successful, int start, int end) {
            boolean isSuccessful = false;
            try {
                if (successful) {
                    isSuccessful = true;
                    newName = document.getContentRange(start, end - start);
                    performRename(session);
                }
            } finally {
                mode.removeListener(this);
                isActiveLinkedEditor = false;
                if (!isSuccessful && linkedEditor instanceof EditorWithAutoSave) {
                    ((EditorWithAutoSave) linkedEditor).enableAutoSave();
                }
            }
        }
    });
}
Also used : LinkedModel(org.eclipse.che.ide.api.editor.link.LinkedModel) Position(org.eclipse.che.ide.api.editor.text.Position) HasLinkedMode(org.eclipse.che.ide.api.editor.link.HasLinkedMode) LinkedMode(org.eclipse.che.ide.api.editor.link.LinkedMode) ArrayList(java.util.ArrayList) LinkedModeModel(org.eclipse.che.ide.ext.java.shared.dto.LinkedModeModel) LinkedModelGroup(org.eclipse.che.ide.api.editor.link.LinkedModelGroup) LinkedPositionGroup(org.eclipse.che.ide.ext.java.shared.dto.LinkedPositionGroup) EditorWithAutoSave(org.eclipse.che.ide.api.editor.EditorWithAutoSave) LinkedData(org.eclipse.che.ide.ext.java.shared.dto.LinkedData) Region(org.eclipse.che.ide.ext.java.shared.dto.Region) LinkedModelData(org.eclipse.che.ide.api.editor.link.LinkedModelData)

Aggregations

LinkedModeModel (org.eclipse.che.ide.ext.java.shared.dto.LinkedModeModel)2 ArrayList (java.util.ArrayList)1 EditorWithAutoSave (org.eclipse.che.ide.api.editor.EditorWithAutoSave)1 HasLinkedMode (org.eclipse.che.ide.api.editor.link.HasLinkedMode)1 LinkedMode (org.eclipse.che.ide.api.editor.link.LinkedMode)1 LinkedModel (org.eclipse.che.ide.api.editor.link.LinkedModel)1 LinkedModelData (org.eclipse.che.ide.api.editor.link.LinkedModelData)1 LinkedModelGroup (org.eclipse.che.ide.api.editor.link.LinkedModelGroup)1 Position (org.eclipse.che.ide.api.editor.text.Position)1 LinkedData (org.eclipse.che.ide.ext.java.shared.dto.LinkedData)1 LinkedPositionGroup (org.eclipse.che.ide.ext.java.shared.dto.LinkedPositionGroup)1 Region (org.eclipse.che.ide.ext.java.shared.dto.Region)1 RenameRefactoringSession (org.eclipse.che.ide.ext.java.shared.dto.refactoring.RenameRefactoringSession)1 RenameLinkedModeRefactoringSession (org.eclipse.che.plugin.java.server.refactoring.session.RenameLinkedModeRefactoringSession)1 RenameSession (org.eclipse.che.plugin.java.server.refactoring.session.RenameSession)1 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)1 RenameSupport (org.eclipse.jdt.ui.refactoring.RenameSupport)1 RenameRefactoring (org.eclipse.ltk.core.refactoring.participants.RenameRefactoring)1