Skip to content

Commit

Permalink
Fix for #943: defer check for action enablement
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Aug 22, 2019
1 parent 05c77a6 commit cfc61ef
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 81 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* Copyright 2009-2017 the original author or authors.
* Copyright 2009-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -17,9 +17,9 @@

import org.codehaus.groovy.eclipse.refactoring.core.convert.AssignStatementToNewLocalRefactoring;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Display;

/**
* Assign a statement to a new local variable.
Expand All @@ -36,27 +36,24 @@ public class AssignStatementToNewLocalAction extends GroovyRefactoringAction {

@Override
public void run(IAction action) {
if (initRefactoring() && assignStatementRefactoring.isApplicable()) {
if (checkPreconditions() && assignStatementRefactoring.isApplicable()) {
assignStatementRefactoring.applyRefactoring(getDocument());

// update the selection
Point p = assignStatementRefactoring.getNewSelection();
Point p = assignStatementRefactoring.getNewSelection();
getEditor().getViewer().setSelectedRange(p.x, p.y);
}
}

private IDocument getDocument() {
return getEditor().getDocumentProvider().getDocument(getEditor().getEditorInput());
}

@Override
public void selectionChanged(IAction action, ISelection selection) {
super.selectionChanged(action, selection);
if (action.isEnabled() && getSelection() != null && getUnit() != null) {
assignStatementRefactoring = new AssignStatementToNewLocalRefactoring(getUnit(), getSelection().getOffset());
if (!assignStatementRefactoring.isApplicable()) {
action.setEnabled(false);
}

if (action.isEnabled()) {
Display.getDefault().asyncExec(() -> {
if (getSelection() != null && getUnit() != null) {
assignStatementRefactoring = new AssignStatementToNewLocalRefactoring(getUnit(), getSelection().getOffset());
action.setEnabled(assignStatementRefactoring.isApplicable());
}
});
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* Copyright 2009-2017 the original author or authors.
* Copyright 2009-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -17,8 +17,8 @@

import org.codehaus.groovy.eclipse.refactoring.core.convert.ConvertToClosureRefactoring;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.widgets.Display;

/**
* Action to convert a method to a closure.
Expand All @@ -29,26 +29,26 @@ public class ConvertToClosureAction extends GroovyRefactoringAction {

@Override
public void run(IAction action) {
if (initRefactoring() && convertToClosureRefactoring.isApplicable()) {
if (checkPreconditions() && convertToClosureRefactoring.isApplicable()) {
convertToClosureRefactoring.applyRefactoring(getDocument());

// force updating of the action's state after running
int length = getSelection().getLength();
getEditor().getViewer().setSelectedRange(getSelection().getOffset(), length > 1 ? length -1 : length + 1);
int offset = getSelection().getOffset(), length = getSelection().getLength();
getEditor().getViewer().setSelectedRange(offset, length > 1 ? length - 1 : length + 1);
}
}

private IDocument getDocument() {
return getEditor().getDocumentProvider().getDocument(getEditor().getEditorInput());
}

@Override
public void selectionChanged(IAction action, ISelection selection) {
super.selectionChanged(action, selection);
if (action.isEnabled() && getSelection() != null && getUnit() != null) {
convertToClosureRefactoring = new ConvertToClosureRefactoring(getUnit(), getSelection().getOffset());
if (!convertToClosureRefactoring.isApplicable()) {
action.setEnabled(false);
}

if (action.isEnabled()) {
Display.getDefault().asyncExec(() -> {
if (getSelection() != null && getUnit() != null) {
convertToClosureRefactoring = new ConvertToClosureRefactoring(getUnit(), getSelection().getOffset());
action.setEnabled(convertToClosureRefactoring.isApplicable());
}
});
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* Copyright 2009-2017 the original author or authors.
* Copyright 2009-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -17,8 +17,8 @@

import org.codehaus.groovy.eclipse.refactoring.core.convert.ConvertToMethodRefactoring;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.widgets.Display;

/**
* Action to convert a method to a closure.
Expand All @@ -29,26 +29,26 @@ public class ConvertToMethodAction extends GroovyRefactoringAction {

@Override
public void run(IAction action) {
if (initRefactoring() && convertToMethodRefactoring.isApplicable()) {
if (checkPreconditions() && convertToMethodRefactoring.isApplicable()) {
convertToMethodRefactoring.applyRefactoring(getDocument());

// force updating of the action's state after running
int length = getSelection().getLength();
getEditor().getViewer().setSelectedRange(getSelection().getOffset(), length > 1 ? length -1 : length + 1);
int offset = getSelection().getOffset(), length = getSelection().getLength();
getEditor().getViewer().setSelectedRange(offset, length > 1 ? length - 1 : length + 1);
}
}

private IDocument getDocument() {
return getEditor().getDocumentProvider().getDocument(getEditor().getEditorInput());
}

@Override
public void selectionChanged(IAction action, ISelection selection) {
super.selectionChanged(action, selection);
if (action.isEnabled() && getSelection() != null && getUnit() != null) {
convertToMethodRefactoring = new ConvertToMethodRefactoring(getUnit(), getSelection().getOffset());
if (!convertToMethodRefactoring.isApplicable()) {
action.setEnabled(false);
}

if (action.isEnabled()) {
Display.getDefault().asyncExec(() -> {
if (getSelection() != null && getUnit() != null) {
convertToMethodRefactoring = new ConvertToMethodRefactoring(getUnit(), getSelection().getOffset());
action.setEnabled(convertToMethodRefactoring.isApplicable());
}
});
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* Copyright 2009-2017 the original author or authors.
* Copyright 2009-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -23,9 +23,9 @@
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ltk.ui.refactoring.RefactoringWizard;
import org.eclipse.ui.IEditorActionDelegate;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchWindow;
Expand All @@ -35,48 +35,46 @@ public abstract class GroovyRefactoringAction implements IWorkbenchWindowActionD

private GroovyEditor editor;
private ITextSelection selection;
private GroovyCompilationUnit gcu;
private GroovyCompilationUnit groovyUnit;

protected boolean initRefactoring() {
if (editor == null || selection == null || gcu == null) {
return false;
}
if (gcu != null) {
if (gcu.getModuleNode() == null) {
displayErrorDialog("Cannot find ModuleNode for " + gcu.getElementName());
return false;
protected boolean checkPreconditions() {
if (editor != null && selection != null && groovyUnit != null) {
if (groovyUnit.getModuleNode() != null) {
return true;
}
return true;
// return PlatformUI.getWorkbench().saveAllEditors(true);
displayErrorDialog("Cannot find ModuleNode for " + groovyUnit.getElementName());
}
return false;
}

protected void displayErrorDialog(String message) {
ErrorDialog error = new ErrorDialog(
editor.getSite().getShell(), "Groovy Refactoring error", message,
new Status(IStatus.ERROR, GroovyPlugin.PLUGIN_ID, message),
IStatus.ERROR | IStatus.WARNING);
editor.getSite().getShell(), "Groovy Refactoring error", message,
new Status(IStatus.ERROR, GroovyPlugin.PLUGIN_ID, message), IStatus.ERROR | IStatus.WARNING);
error.open();
}

@Override
public void dispose() {
editor = null;
gcu = null;
selection = null;
groovyUnit = null;
}

protected final IDocument getDocument() {
return editor.getDocumentProvider().getDocument(editor.getEditorInput());
}

protected GroovyEditor getEditor() {
protected final GroovyEditor getEditor() {
return editor;
}

protected ITextSelection getSelection() {
protected final ITextSelection getSelection() {
return selection;
}

protected GroovyCompilationUnit getUnit() {
return gcu;
protected final GroovyCompilationUnit getUnit() {
return groovyUnit;
}

@Override
Expand All @@ -87,27 +85,25 @@ public void init(IWorkbenchWindow window) {
public void selectionChanged(IAction action, ISelection selection) {
if (selection instanceof ITextSelection) {
this.selection = (ITextSelection) selection;
action.setEnabled(true);
} else {
this.selection = null;
action.setEnabled(false);
}
updateEnabled(action);
}

@Override
public void setActiveEditor(IAction action, IEditorPart targetEditor) {
if (targetEditor instanceof GroovyEditor) {
this.editor = (GroovyEditor) targetEditor;
this.gcu = editor.getGroovyCompilationUnit();
action.setEnabled(true);
this.groovyUnit = editor.getGroovyCompilationUnit();
} else {
this.editor = null;
this.gcu = null;
action.setEnabled(false);
this.groovyUnit = null;
}
updateEnabled(action);
}

public static int getUIFlags() {
return RefactoringWizard.DIALOG_BASED_USER_INTERFACE | RefactoringWizard.PREVIEW_EXPAND_FIRST_NODE;
private void updateEnabled(IAction action) {
action.setEnabled(editor != null && selection != null && groovyUnit != null);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* Copyright 2009-2017 the original author or authors.
* Copyright 2009-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
Expand Down Expand Up @@ -33,13 +33,12 @@
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;

public class RenameDispatcherAction extends GroovyRefactoringAction {

@Override
public void run(IAction action) {
if (initRefactoring()) {
if (checkPreconditions()) {
GroovyCompilationUnit unit = getUnit();
ITextSelection selection = getSelection();
CandidateCollector dispatcher = new CandidateCollector(unit, selection);
Expand Down Expand Up @@ -102,6 +101,6 @@ private void openJavaRefactoringWizard(IJavaElement element) throws CoreExceptio
}

private Shell getShell() {
return PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
return getEditor().getSite().getShell();
}
}

0 comments on commit cfc61ef

Please sign in to comment.