Skip to content

Commit

Permalink
TEIIDDES-1917: Addresses refactoring issues with read-only resources
Browse files Browse the repository at this point in the history
* Rather than disabling refactoring actions if resource is read-only, let
  the refactor wizards handle it.

* If running rename or move, a dialog should be displayed stopping the user
  going any further due to the resource being read-only

* If running any refactoring and a related resource is read-only then the
  dialog will also be displayed

* AbstractRefactorAction
 * Removes restriction on enablement of action due to read-only resources

* RefactorResourceUtils
 * For check methods, pass a level of error status that should be returned.
   Since related resource errors in delete are errors, the deletion of a
   read-only resource is allowed so a warning should be returned
 * In finding related resources, avoid testing their statuses since this is
   checking whether read-only and is unnecessary since these resources are
   tested anyway

* [Delete|Move|Rename]ResourcesRefactoring
 * Better handle distinction between status values returning as warnings
   and errors

* MoveResourcesWizard
 * Corrects extra call to getString() when such strings have already been
   fetched, since the createErrorStatus method is also used for handling
   exception messages
  • Loading branch information
Paul Richardson committed Nov 15, 2013
1 parent 60d3954 commit ef27042
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1563,7 +1563,7 @@ RefreshModelAction.updateNotAllowed.msg=Automatic update of the source is curren
ResourcesRefactoring.noResourceError = Resource has not been specified
ResourcesRefactoring.saveEditorsError = Cannot save the open editors
ResourcesRefactoring.resourceNoExistError = Resource {0} does not exist
ResourcesRefactoring.readOnlyResourceError = The {0} is read-only
ResourcesRefactoring.readOnlyResourceError = The resource {0} is read-only
ResourcesRefactoring.readOnlyRelatedResourceError = The related resource {0} is read-only
ResourcesRefactoring.refactorProjectError = Cannot refactor the project {0}
ResourcesRefactoring.projectClosedError = Cannot refactor the closed project {0}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.teiid.designer.core.workspace.ModelUtil;
import org.teiid.designer.ui.common.eventsupport.SelectionUtilities;

/**
Expand Down Expand Up @@ -78,13 +77,6 @@ public void selectionChanged(IAction action, ISelection selection) {

List<IResource> resources = SelectionUtilities.getSelectedIResourceObjects(selection);

for (IResource resource : resources) {
if (ModelUtil.isIResourceReadOnly(resource)) {
action.setEnabled(false);
return;
}
}

action.setEnabled(true);
selectedResources = resources;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public boolean visit(IResource visitedResource) {
* implementation of createInitialConditions and when testing dependencies.
*
* @param resource
* @param progressMonitor
* @param status
*/
protected abstract void checkResource(IResource resource, IProgressMonitor progressMonitor, RefactoringStatus status);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,9 @@
import org.teiid.designer.core.refactor.RefactorModelExtensionManager;
import org.teiid.designer.core.refactor.RelatedResourceFinder;
import org.teiid.designer.core.refactor.RelatedResourceFinder.Relationship;
import org.teiid.designer.core.refactor.ResourceStatusList;
import org.teiid.designer.core.workspace.ModelResource;
import org.teiid.designer.core.workspace.ModelResourceImpl;
import org.teiid.designer.core.workspace.ModelUtil;
import org.teiid.designer.core.workspace.ModelWorkspaceException;
import org.teiid.designer.core.workspace.WorkspaceResourceFinderUtil;
import org.teiid.designer.ui.UiConstants;
import org.teiid.designer.ui.common.util.UiUtil;
Expand Down Expand Up @@ -820,11 +818,22 @@ public static void checkResourceSynched(IResource resource, RefactoringStatus st
*
* @param resource
* @param status
* @param statusLevel
* @param statusMsg
*
*/
public static void checkResourceWritable(IResource resource, RefactoringStatus status) {
public static void checkResourceWritable(IResource resource, RefactoringStatus status, int statusLevel, String statusMsg) {
if (ModelUtil.isIResourceReadOnly(resource)) {
status.merge(RefactoringStatus.createFatalErrorStatus(getString("ResourcesRefactoring.readOnlyResourceError", resource.getName()))); //$NON-NLS-1$
switch (statusLevel) {
case IStatus.INFO:
status.merge(RefactoringStatus.createInfoStatus(statusMsg));
break;
case IStatus.WARNING:
status.merge(RefactoringStatus.createWarningStatus(statusMsg));
break;
default:
status.merge(RefactoringStatus.createFatalErrorStatus(statusMsg));
}
}
}

Expand All @@ -836,9 +845,11 @@ public static void checkResourceWritable(IResource resource, RefactoringStatus s
*
* @param resource
* @param status
* @param statusLevel
* @param statusMsg
*
*/
public static void checkModelResourceWritable(IResource resource, RefactoringStatus status) {
public static void checkModelResourceWritable(IResource resource, RefactoringStatus status, int statusLevel, String statusMsg) {
try {
ModelResource modelResource = ModelUtil.getModel(resource);
if (modelResource == null) {
Expand All @@ -847,21 +858,15 @@ public static void checkModelResourceWritable(IResource resource, RefactoringSta
}

if (modelResource.isReadOnly()) {
status.merge(RefactoringStatus.createFatalErrorStatus(getString("ResourcesRefactoring.readOnlyResourceError", resource.getName()))); //$NON-NLS-1$
return;
}

RelatedResourceFinder finder = new RelatedResourceFinder(resource);
Collection<IFile> relatedFiles = finder.findRelatedResources(Relationship.ALL);

for (IFile relatedFile : relatedFiles) {
try {
modelResource = ModelUtil.getModel(relatedFile);
if (modelResource != null && modelResource.isReadOnly()) {
status.merge(RefactoringStatus.createWarningStatus(getString("ResourcesRefactoring.readOnlyRelatedResourceError", modelResource.getItemName()))); //$NON-NLS-1$
}
} catch (ModelWorkspaceException err) {
ModelerCore.Util.log(IStatus.ERROR, err, err.getMessage());
switch (statusLevel) {
case IStatus.INFO:
status.merge(RefactoringStatus.createInfoStatus(statusMsg));
break;
case IStatus.WARNING:
status.merge(RefactoringStatus.createWarningStatus(statusMsg));
break;
default:
status.merge(RefactoringStatus.createFatalErrorStatus(statusMsg));
}
}
} catch (Exception err) {
Expand Down Expand Up @@ -938,17 +943,13 @@ public static void calculateRelatedResources(IResource resource, RefactoringStat

// Determine dependent resources
Collection<IFile> searchResults = finder.findRelatedResources(relationship);
ResourceStatusList statusList = new ResourceStatusList(searchResults);

for (IStatus problem : statusList.getProblems()) {
status.merge(RefactoringStatus.create(problem));
}
if (searchResults == null)
return;

for (IFile file : statusList.getResourceList()) {
for (IFile file : searchResults) {
try {

callback.checkValidFile(file, status);
if (! status.isOK()) {
if (status.getSeverity() > IStatus.WARNING) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,24 +117,37 @@ private void checkProjectReadOnly(IProject project, RefactoringStatus status) {

@Override
protected void checkResource(IResource resource, IProgressMonitor progressMonitor, RefactoringStatus status) {
int readOnlyStatusLevel;
String readOnlyStatusMsg;

if (getResources().contains(resource)) {
readOnlyStatusLevel = IStatus.WARNING;
readOnlyStatusMsg = RefactorResourcesUtils.getString("ResourcesRefactoring.readOnlyResourceError", //$NON-NLS-1$
resource.getName());
} else {
readOnlyStatusLevel = IStatus.ERROR;
readOnlyStatusMsg = RefactorResourcesUtils.getString("ResourcesRefactoring.readOnlyRelatedResourceError", //$NON-NLS-1$
resource.getName());
}

RefactorResourcesUtils.checkResourceExists(resource, status);
if (!status.isOK()) return;
if (status.getSeverity() > IStatus.WARNING) return;

RefactorResourcesUtils.checkResourceSynched(resource, status);
if (!status.isOK()) return;
if (status.getSeverity() > IStatus.WARNING) return;

if (resource instanceof IProject)
checkProjectReadOnly((IProject) resource, status);
else
RefactorResourcesUtils.checkResourceWritable(resource, status);
RefactorResourcesUtils.checkResourceWritable(resource, status, readOnlyStatusLevel, readOnlyStatusMsg);

if (!status.isOK()) return;
if (status.getSeverity() > IStatus.WARNING) return;

RefactorResourcesUtils.checkExtensionManager(resource, RefactorType.DELETE, progressMonitor, status);
if (!status.isOK()) return;
if (status.getSeverity() > IStatus.WARNING) return;

RefactorResourcesUtils.checkModelResourceWritable(resource, status);
if (!status.isOK()) return;
RefactorResourcesUtils.checkModelResourceWritable(resource, status, readOnlyStatusLevel, readOnlyStatusMsg);
if (status.getSeverity() > IStatus.WARNING) return;

RefactorResourcesUtils.checkSavedResource(resource, status);
}
Expand All @@ -157,7 +170,7 @@ public RefactoringStatus checkInitialConditions(final IProgressMonitor progressM

for (IResource resource : getResources()) {
checkResource(resource, progressMonitor, status);
if (! status.isOK()) break;
if (status.getSeverity() > IStatus.WARNING) break;

// Check validity of related resources
IResourceCallback callback = new AbstractResourceCallback() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,23 @@ public void setDestination(IContainer destination) {

@Override
protected void checkResource(IResource resource, IProgressMonitor progressMonitor, RefactoringStatus status) {
String readOnlyStatusMsg;

if (getResources().contains(resource)) {
readOnlyStatusMsg = RefactorResourcesUtils.getString("ResourcesRefactoring.readOnlyResourceError", //$NON-NLS-1$
resource.getName());
} else {
readOnlyStatusMsg = RefactorResourcesUtils.getString("ResourcesRefactoring.readOnlyRelatedResourceError", //$NON-NLS-1$
resource.getName());
}

RefactorResourcesUtils.checkResourceExists(resource, status);
if (!status.isOK()) return;

RefactorResourcesUtils.checkResourceSynched(resource, status);
if (!status.isOK()) return;

RefactorResourcesUtils.checkResourceWritable(resource, status);
RefactorResourcesUtils.checkResourceWritable(resource, status, IStatus.ERROR, readOnlyStatusMsg);
if (!status.isOK()) return;

RefactorResourcesUtils.checkResourceIsNotProject(resource, status);
Expand All @@ -143,7 +153,7 @@ protected void checkResource(IResource resource, IProgressMonitor progressMonito
RefactorResourcesUtils.checkExtensionManager(resource, RefactorType.MOVE, progressMonitor, status);
if (!status.isOK()) return;

RefactorResourcesUtils.checkModelResourceWritable(resource, status);
RefactorResourcesUtils.checkModelResourceWritable(resource, status, IStatus.ERROR, readOnlyStatusMsg);
if (!status.isOK()) return;

RefactorResourcesUtils.checkSavedResource(resource, status);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.viewers.ILabelProvider;
Expand Down Expand Up @@ -81,8 +82,8 @@ public MoveResourcesRefactoring getRefactoring() {
return (MoveResourcesRefactoring) super.getRefactoring();
}

private RefactoringStatus createErrorStatus(String key, Object... args) {
return RefactoringStatus.createFatalErrorStatus(RefactorResourcesUtils.getString(key, args));
private RefactoringStatus createErrorStatus(String msg) {
return RefactoringStatus.createFatalErrorStatus(msg);
}

private boolean verifyResourcesProject(Collection<IResource> resources) {
Expand All @@ -104,7 +105,8 @@ private boolean verifyResourcesProject(Collection<IResource> resources) {

private void verifyDestination(Object selection) {
if (! (selection instanceof IContainer)) {
setPageComplete(createErrorStatus("MoveRefactoring.destinationNotFolder")); //$NON-NLS-1$
String msg = RefactorResourcesUtils.getString("MoveRefactoring.destinationNotFolder"); //$NON-NLS-1$
setPageComplete(createErrorStatus(msg));
return;
}

Expand All @@ -116,7 +118,8 @@ private void verifyDestination(Object selection) {
// Not open or a non-model project
try {
if (! project.isOpen() || project.getNature(ModelerCore.NATURE_ID) == null) {
setPageComplete(createErrorStatus("MoveRefactoring.destinationProjectNotOpen")); //$NON-NLS-1$
String msg = RefactorResourcesUtils.getString("MoveRefactoring.destinationProjectNotOpen"); //$NON-NLS-1$
setPageComplete(createErrorStatus(msg));
return;
}
} catch (CoreException ex) {
Expand All @@ -129,26 +132,32 @@ private void verifyDestination(Object selection) {
RefactoringStatus status = new RefactoringStatus();
RefactorResourcesUtils.checkResourceExists(destination, status);
RefactorResourcesUtils.checkResourceSynched(destination, status);
RefactorResourcesUtils.checkResourceWritable(destination, status);

String readOnlyStatusMsg = RefactorResourcesUtils.getString("ResourcesRefactoring.readOnlyResourceError", //$NON-NLS-1$
destination.getName());
RefactorResourcesUtils.checkResourceWritable(destination, status, IStatus.ERROR, readOnlyStatusMsg);
if (! status.isOK()) {
setPageComplete(createErrorStatus(status.getEntryWithHighestSeverity().getMessage()));
return;
}

List<IResource> resources = getRefactoring().getResources();
if (! verifyResourcesProject(resources)) {
setPageComplete(createErrorStatus("MoveRefactoring.resourcesNotInSameProject")); //$NON-NLS-1$
String msg = RefactorResourcesUtils.getString("MoveRefactoring.resourcesNotInSameProject"); //$NON-NLS-1$
setPageComplete(createErrorStatus(msg));
return;
}

for (IResource resource : resources) {
if (! resource.getProject().equals(destination.getProject())) {
setPageComplete(createErrorStatus("MoveRefactoring.destinationNotSameProject")); //$NON-NLS-1$
String msg = RefactorResourcesUtils.getString("MoveRefactoring.destinationNotSameProject"); //$NON-NLS-1$
setPageComplete(createErrorStatus(msg));
return;
}

if (resource.getParent().equals(destination)) {
setPageComplete(createErrorStatus("MoveRefactoring.destinationSame")); //$NON-NLS-1$
String msg = RefactorResourcesUtils.getString("MoveRefactoring.destinationSame"); //$NON-NLS-1$
setPageComplete(createErrorStatus(msg));
return;
}

Expand All @@ -158,7 +167,8 @@ private void verifyDestination(Object selection) {
// destination cannot be beneath target
final String resourcePath = folderResource.getFullPath().toString() + '/';
if (destinationPath.startsWith(resourcePath)) {
setPageComplete(createErrorStatus("MoveRefactoring.destinationSubFolder")); //$NON-NLS-1$
String msg = RefactorResourcesUtils.getString("MoveRefactoring.destinationSubFolder"); //$NON-NLS-1$
setPageComplete(createErrorStatus(msg));
return;
}
}
Expand All @@ -170,7 +180,8 @@ private void verifyDestination(Object selection) {
final String proposedPath = destinationPath + '/' + resource.getName();
final IWorkspaceRoot workspaceRoot = resource.getWorkspace().getRoot();
if (workspaceRoot.findMember(proposedPath) != null) {
setPageComplete(createErrorStatus("MoveRefactoring.nameClash", resource.getName())); //$NON-NLS-1$
String msg = RefactorResourcesUtils.getString("MoveRefactoring.nameClash", resource.getName()); //$NON-NLS-1$
setPageComplete(createErrorStatus(msg));
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,19 +144,29 @@ public void setNewResourceName(String newName) {

@Override
protected void checkResource(IResource resource, IProgressMonitor progressMonitor, RefactoringStatus status) {
String readOnlyStatusMsg;

if (getResources().contains(resource)) {
readOnlyStatusMsg = RefactorResourcesUtils.getString("ResourcesRefactoring.readOnlyResourceError", //$NON-NLS-1$
resource.getName());
} else {
readOnlyStatusMsg = RefactorResourcesUtils.getString("ResourcesRefactoring.readOnlyRelatedResourceError", //$NON-NLS-1$
resource.getName());
}

RefactorResourcesUtils.checkResourceExists(resource, status);
if (!status.isOK()) return;

RefactorResourcesUtils.checkResourceSynched(resource, status);
if (!status.isOK()) return;

RefactorResourcesUtils.checkResourceWritable(resource, status);
RefactorResourcesUtils.checkResourceWritable(resource, status, IStatus.ERROR, readOnlyStatusMsg);
if (!status.isOK()) return;

RefactorResourcesUtils.checkExtensionManager(resource, RefactorType.MOVE, progressMonitor, status);
if (!status.isOK()) return;

RefactorResourcesUtils.checkModelResourceWritable(resource, status);
RefactorResourcesUtils.checkModelResourceWritable(resource, status, IStatus.ERROR, readOnlyStatusMsg);
if (!status.isOK()) return;

RefactorResourcesUtils.checkSavedResource(resource, status);
Expand Down

0 comments on commit ef27042

Please sign in to comment.