Search in sources :

Example 16 with Refactoring

use of org.eclipse.ltk.core.refactoring.Refactoring in project che by eclipse.

the class QuickAssistProcessor method getExtractVariableProposal.

private static boolean getExtractVariableProposal(IInvocationContext context, boolean problemsAtLocation, Collection<ICommandAccess> proposals) throws CoreException {
    ASTNode node = context.getCoveredNode();
    if (!(node instanceof Expression)) {
        if (context.getSelectionLength() != 0) {
            return false;
        }
        node = context.getCoveringNode();
        if (!(node instanceof Expression)) {
            return false;
        }
    }
    final Expression expression = (Expression) node;
    ITypeBinding binding = expression.resolveTypeBinding();
    if (binding == null || Bindings.isVoidType(binding)) {
        return false;
    }
    if (proposals == null) {
        return true;
    }
    final ICompilationUnit cu = context.getCompilationUnit();
    ExtractTempRefactoring extractTempRefactoring = new ExtractTempRefactoring(context.getASTRoot(), context.getSelectionOffset(), context.getSelectionLength());
    if (extractTempRefactoring.checkInitialConditions(new NullProgressMonitor()).isOK()) {
        LinkedProposalModel linkedProposalModel = new LinkedProposalModel();
        extractTempRefactoring.setLinkedProposalModel(linkedProposalModel);
        extractTempRefactoring.setCheckResultForCompileProblems(false);
        String label = CorrectionMessages.QuickAssistProcessor_extract_to_local_all_description;
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_LOCAL);
        int relevance;
        if (context.getSelectionLength() == 0) {
            relevance = IProposalRelevance.EXTRACT_LOCAL_ALL_ZERO_SELECTION;
        } else if (problemsAtLocation) {
            relevance = IProposalRelevance.EXTRACT_LOCAL_ALL_ERROR;
        } else {
            relevance = IProposalRelevance.EXTRACT_LOCAL_ALL;
        }
        RefactoringCorrectionProposal proposal = new RefactoringCorrectionProposal(label, cu, extractTempRefactoring, relevance, image) {

            @Override
            protected void init(Refactoring refactoring) throws CoreException {
                ExtractTempRefactoring etr = (ExtractTempRefactoring) refactoring;
                // expensive
                etr.setTempName(etr.guessTempName());
            }
        };
        proposal.setCommandId(EXTRACT_LOCAL_ID);
        proposal.setLinkedProposalModel(linkedProposalModel);
        proposals.add(proposal);
    }
    ExtractTempRefactoring extractTempRefactoringSelectedOnly = new ExtractTempRefactoring(context.getASTRoot(), context.getSelectionOffset(), context.getSelectionLength());
    extractTempRefactoringSelectedOnly.setReplaceAllOccurrences(false);
    if (extractTempRefactoringSelectedOnly.checkInitialConditions(new NullProgressMonitor()).isOK()) {
        LinkedProposalModel linkedProposalModel = new LinkedProposalModel();
        extractTempRefactoringSelectedOnly.setLinkedProposalModel(linkedProposalModel);
        extractTempRefactoringSelectedOnly.setCheckResultForCompileProblems(false);
        String label = CorrectionMessages.QuickAssistProcessor_extract_to_local_description;
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_LOCAL);
        int relevance;
        if (context.getSelectionLength() == 0) {
            relevance = IProposalRelevance.EXTRACT_LOCAL_ZERO_SELECTION;
        } else if (problemsAtLocation) {
            relevance = IProposalRelevance.EXTRACT_LOCAL_ERROR;
        } else {
            relevance = IProposalRelevance.EXTRACT_LOCAL;
        }
        RefactoringCorrectionProposal proposal = new RefactoringCorrectionProposal(label, cu, extractTempRefactoringSelectedOnly, relevance, image) {

            @Override
            protected void init(Refactoring refactoring) throws CoreException {
                ExtractTempRefactoring etr = (ExtractTempRefactoring) refactoring;
                // expensive
                etr.setTempName(etr.guessTempName());
            }
        };
        proposal.setCommandId(EXTRACT_LOCAL_NOT_REPLACE_ID);
        proposal.setLinkedProposalModel(linkedProposalModel);
        proposals.add(proposal);
    }
    ExtractConstantRefactoring extractConstRefactoring = new ExtractConstantRefactoring(context.getASTRoot(), context.getSelectionOffset(), context.getSelectionLength());
    if (extractConstRefactoring.checkInitialConditions(new NullProgressMonitor()).isOK()) {
        LinkedProposalModel linkedProposalModel = new LinkedProposalModel();
        extractConstRefactoring.setLinkedProposalModel(linkedProposalModel);
        extractConstRefactoring.setCheckResultForCompileProblems(false);
        String label = CorrectionMessages.QuickAssistProcessor_extract_to_constant_description;
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_LOCAL);
        int relevance;
        if (context.getSelectionLength() == 0) {
            relevance = IProposalRelevance.EXTRACT_CONSTANT_ZERO_SELECTION;
        } else if (problemsAtLocation) {
            relevance = IProposalRelevance.EXTRACT_CONSTANT_ERROR;
        } else {
            relevance = IProposalRelevance.EXTRACT_CONSTANT;
        }
        RefactoringCorrectionProposal proposal = new RefactoringCorrectionProposal(label, cu, extractConstRefactoring, relevance, image) {

            @Override
            protected void init(Refactoring refactoring) throws CoreException {
                ExtractConstantRefactoring etr = (ExtractConstantRefactoring) refactoring;
                // expensive
                etr.setConstantName(etr.guessConstantName());
            }
        };
        proposal.setCommandId(EXTRACT_CONSTANT_ID);
        proposal.setLinkedProposalModel(linkedProposalModel);
        proposals.add(proposal);
    }
    return false;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) LinkedProposalModel(org.eclipse.jdt.internal.corext.fix.LinkedProposalModel) ExtractTempRefactoring(org.eclipse.jdt.internal.corext.refactoring.code.ExtractTempRefactoring) Image(org.eclipse.swt.graphics.Image) RefactoringCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.RefactoringCorrectionProposal) Refactoring(org.eclipse.ltk.core.refactoring.Refactoring) InlineTempRefactoring(org.eclipse.jdt.internal.corext.refactoring.code.InlineTempRefactoring) ExtractTempRefactoring(org.eclipse.jdt.internal.corext.refactoring.code.ExtractTempRefactoring) PromoteTempToFieldRefactoring(org.eclipse.jdt.internal.corext.refactoring.code.PromoteTempToFieldRefactoring) ExtractMethodRefactoring(org.eclipse.jdt.internal.corext.refactoring.code.ExtractMethodRefactoring) ConvertAnonymousToNestedRefactoring(org.eclipse.jdt.internal.corext.refactoring.code.ConvertAnonymousToNestedRefactoring) ExtractConstantRefactoring(org.eclipse.jdt.internal.corext.refactoring.code.ExtractConstantRefactoring) ExtractConstantRefactoring(org.eclipse.jdt.internal.corext.refactoring.code.ExtractConstantRefactoring)

Example 17 with Refactoring

use of org.eclipse.ltk.core.refactoring.Refactoring in project translationstudio8 by heartsome.

the class ResourceDropAdapterAssistant method performResourceMove.

/**
	 * Performs a resource move
	 */
private IStatus performResourceMove(CommonDropAdapter dropAdapter, IResource[] sources) {
    MultiStatus problems = new MultiStatus(PlatformUI.PLUGIN_ID, 1, WorkbenchNavigatorMessages.resources_ResourceDropAdapterAssistant_problemsMoving, null);
    mergeStatus(problems, validateTarget(dropAdapter.getCurrentTarget(), dropAdapter.getCurrentTransfer(), dropAdapter.getCurrentOperation()));
    IContainer target = getActualTarget((IResource) dropAdapter.getCurrentTarget());
    boolean shouldLinkAutomatically = false;
    if (target.isVirtual()) {
        shouldLinkAutomatically = true;
        for (int i = 0; i < sources.length; i++) {
            if (sources[i].isVirtual() || sources[i].isLinked()) {
                shouldLinkAutomatically = false;
                break;
            }
        }
    }
    if (shouldLinkAutomatically) {
        CopyFilesAndFoldersOperation operation = new CopyFilesAndFoldersOperation(getShell());
        operation.setCreateLinks(true);
        operation.copyResources(sources, target);
    } else {
        ReadOnlyStateChecker checker = new ReadOnlyStateChecker(getShell(), WorkbenchNavigatorMessages.resources_ResourceDropAdapterAssistant_MoveResourceAction_title, WorkbenchNavigatorMessages.resources_ResourceDropAdapterAssistant_MoveResourceAction_checkMoveMessage);
        sources = checker.checkReadOnlyResources(sources);
        try {
            RefactoringContribution contribution = RefactoringCore.getRefactoringContribution(MoveResourcesDescriptor.ID);
            MoveResourcesDescriptor descriptor = (MoveResourcesDescriptor) contribution.createDescriptor();
            descriptor.setResourcesToMove(sources);
            descriptor.setDestination(target);
            refactoringStatus = new RefactoringStatus();
            final Refactoring refactoring = descriptor.createRefactoring(refactoringStatus);
            returnStatus = null;
            IRunnableWithProgress checkOp = new IRunnableWithProgress() {

                public void run(IProgressMonitor monitor) {
                    try {
                        refactoringStatus = refactoring.checkAllConditions(monitor);
                    } catch (CoreException ex) {
                        returnStatus = WorkbenchNavigatorPlugin.createErrorStatus(0, ex.getLocalizedMessage(), ex);
                    }
                }
            };
            if (returnStatus != null)
                return returnStatus;
            try {
                PlatformUI.getWorkbench().getProgressService().run(false, false, checkOp);
            } catch (InterruptedException e) {
                return Status.CANCEL_STATUS;
            } catch (InvocationTargetException e) {
                return WorkbenchNavigatorPlugin.createErrorStatus(0, e.getLocalizedMessage(), e);
            }
            if (refactoringStatus.hasEntries()) {
                RefactoringStatusEntry[] entries = refactoringStatus.getEntries();
                StringBuffer message = new StringBuffer();
                int severity = 0;
                for (RefactoringStatusEntry refactoringStatusEntry : entries) {
                    if (refactoringStatusEntry.getSeverity() > severity) {
                        severity = refactoringStatusEntry.getSeverity();
                        message.replace(0, message.length(), refactoringStatusEntry.getMessage());
                    } else if (refactoringStatusEntry.getSeverity() == severity) {
                        message.append("\n\n").append(refactoringStatusEntry.getMessage());
                    }
                }
                if (severity == RefactoringStatus.INFO) {
                    MessageDialog.openInformation(getShell(), WorkbenchNavigatorMessages.resources_ResourceDropAdapterAssistant_MoveResourceAction_title, message.toString());
                } else if (severity == RefactoringStatus.WARNING) {
                    boolean result = MessageDialog.openConfirm(getShell(), WorkbenchNavigatorMessages.resources_ResourceDropAdapterAssistant_MoveResourceAction_title, message.toString());
                    if (!result) {
                        return Status.CANCEL_STATUS;
                    }
                } else if (severity == RefactoringStatus.ERROR || severity == RefactoringStatus.FATAL) {
                    MessageDialog.openError(getShell(), WorkbenchNavigatorMessages.resources_ResourceDropAdapterAssistant_MoveResourceAction_title, message.toString());
                    return Status.CANCEL_STATUS;
                } else {
                }
            /**
					 * Weachy:
					 * RefactoringUI 类需引入 org.eclipse.ltk.ui.refactoring 插件,
					 * 而 org.eclipse.ltk.ui.refactoring 插件会引入
					 * org.eclipse.compare、org.eclipse.team.core、org.eclipse.team.ui
					 * 三个插件。会导致在导航视图、首选项等出现无用的功能项。因此注释以下4行代码。
					 */
            //					Dialog dialog= RefactoringUI.createLightWeightStatusDialog(refactoringStatus, getShell(), WorkbenchNavigatorMessages.MoveResourceAction_title);
            //					int result = dialog.open();
            //					if (result != IStatus.OK)
            //						return Status.CANCEL_STATUS;
            }
            final PerformRefactoringOperation op = new PerformRefactoringOperation(refactoring, CheckConditionsOperation.ALL_CONDITIONS);
            final IWorkspaceRunnable r = new IWorkspaceRunnable() {

                public void run(IProgressMonitor monitor) throws CoreException {
                    op.run(monitor);
                }
            };
            returnStatus = null;
            IRunnableWithProgress refactorOp = new IRunnableWithProgress() {

                public void run(IProgressMonitor monitor) {
                    try {
                        ResourcesPlugin.getWorkspace().run(r, ResourcesPlugin.getWorkspace().getRoot(), IWorkspace.AVOID_UPDATE, monitor);
                    } catch (CoreException ex) {
                        returnStatus = WorkbenchNavigatorPlugin.createErrorStatus(0, ex.getLocalizedMessage(), ex);
                    }
                }
            };
            if (returnStatus != null)
                return returnStatus;
            try {
                PlatformUI.getWorkbench().getProgressService().run(false, false, refactorOp);
            } catch (InterruptedException e) {
                return Status.CANCEL_STATUS;
            } catch (InvocationTargetException e) {
                return WorkbenchNavigatorPlugin.createErrorStatus(0, e.getLocalizedMessage(), e);
            }
        } catch (CoreException ex) {
            return WorkbenchNavigatorPlugin.createErrorStatus(0, ex.getLocalizedMessage(), ex);
        } catch (OperationCanceledException e) {
        }
    }
    return problems;
}
Also used : IWorkspaceRunnable(org.eclipse.core.resources.IWorkspaceRunnable) RefactoringContribution(org.eclipse.ltk.core.refactoring.RefactoringContribution) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) RefactoringStatusEntry(org.eclipse.ltk.core.refactoring.RefactoringStatusEntry) MultiStatus(org.eclipse.core.runtime.MultiStatus) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) PerformRefactoringOperation(org.eclipse.ltk.core.refactoring.PerformRefactoringOperation) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) MoveResourcesDescriptor(org.eclipse.ltk.core.refactoring.resource.MoveResourcesDescriptor) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) IContainer(org.eclipse.core.resources.IContainer) CopyFilesAndFoldersOperation(org.eclipse.ui.actions.CopyFilesAndFoldersOperation) Refactoring(org.eclipse.ltk.core.refactoring.Refactoring) ReadOnlyStateChecker(org.eclipse.ui.actions.ReadOnlyStateChecker)

Aggregations

Refactoring (org.eclipse.ltk.core.refactoring.Refactoring)17 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)13 RenameRefactoring (org.eclipse.ltk.core.refactoring.participants.RenameRefactoring)11 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)8 RenameJavaElementDescriptor (org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor)8 IType (org.eclipse.jdt.core.IType)7 IMethod (org.eclipse.jdt.core.IMethod)5 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)5 ProcessorBasedRefactoring (org.eclipse.ltk.core.refactoring.participants.ProcessorBasedRefactoring)5 Test (org.junit.Test)5 IContainer (org.eclipse.core.resources.IContainer)4 IFolder (org.eclipse.core.resources.IFolder)3 IPath (org.eclipse.core.runtime.IPath)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IFile (org.eclipse.core.resources.IFile)2 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)2 Path (org.eclipse.core.runtime.Path)2 IJavaElement (org.eclipse.jdt.core.IJavaElement)2 IJavaElementMapper (org.eclipse.jdt.core.refactoring.IJavaElementMapper)2 ChangeSignatureProcessor (org.eclipse.jdt.internal.corext.refactoring.structure.ChangeSignatureProcessor)2