Skip to content

Commit

Permalink
Gets rid of the blind usage of WorkbenchHelper.getSheet();
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexisDrogoul committed Sep 17, 2021
1 parent b970549 commit 1ef4cc4
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 152 deletions.
@@ -1,23 +1,21 @@
/*******************************************************************************
* Copyright (c) 2000, 2015 IBM Corporation and others. All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is
* available at http://www.eclipse.org/legal/epl-v10.html
/*******************************************************************************************************
*
* Contributors: IBM Corporation - initial API and implementation Andrey Loskutov <loskutov@gmx.de> - generified
* interface, bug 462760
*******************************************************************************/
* RefreshAction.java, in ummisco.gama.ui.navigator, is part of the source code of the
* GAMA modeling and simulation platform (v.1.8.2).
*
* (c) 2007-2021 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU)
*
* Visit https://github.com/gama-platform/gama for license information and contacts.
*
********************************************************************************************************/
package ummisco.gama.ui.navigator.actions;

import static msi.gama.common.interfaces.IGui.NAVIGATOR_VIEW_ID;
import static org.eclipse.core.resources.IResource.DEPTH_INFINITE;
import static org.eclipse.core.resources.IResource.PROJECT;
import static org.eclipse.core.resources.IResource.ROOT;
import static org.eclipse.core.runtime.Status.OK_STATUS;
import static org.eclipse.jface.dialogs.IDialogConstants.NO_LABEL;
import static org.eclipse.jface.dialogs.IDialogConstants.YES_LABEL;
import static org.eclipse.jface.dialogs.MessageDialog.QUESTION;
import static org.eclipse.jface.viewers.StructuredSelection.EMPTY;
import static org.eclipse.swt.SWT.SHEET;
import static org.eclipse.ui.PlatformUI.PLUGIN_ID;
import static org.eclipse.ui.internal.ide.IIDEHelpContextIds.REFRESH_ACTION;
import static ummisco.gama.ui.navigator.contents.ResourceManager.getInstance;
Expand All @@ -37,18 +35,17 @@
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.window.IShellProvider;
import org.eclipse.osgi.util.NLS;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.WorkspaceAction;
import org.eclipse.ui.actions.WorkspaceModifyOperation;
import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
import org.eclipse.ui.internal.ide.dialogs.IDEResourceInfoUtils;

import ummisco.gama.ui.dialogs.Messages;
import ummisco.gama.ui.interfaces.IRefreshHandler;
import ummisco.gama.ui.metadata.FileMetaDataProvider;
import ummisco.gama.ui.navigator.GamaNavigator;
Expand All @@ -68,14 +65,18 @@
*/
public class RefreshAction extends WorkspaceAction {

/** The navigator. */
GamaNavigator navigator;

/**
* Gets the navigator.
*
* @return the navigator
*/
private GamaNavigator getNavigator() {
if (navigator == null) {
final IWorkbenchPage page = WorkbenchHelper.getPage();
if (page != null) {
navigator = (GamaNavigator) page.findView(NAVIGATOR_VIEW_ID);
}
if (page != null) { navigator = (GamaNavigator) page.findView(NAVIGATOR_VIEW_ID); }
}
return navigator;
}
Expand All @@ -85,6 +86,7 @@ private GamaNavigator getNavigator() {
*/
public static final String ID = PLUGIN_ID + ".RefreshAction";//$NON-NLS-1$

/** The resources. */
public List<? extends IResource> resources;

/**
Expand Down Expand Up @@ -113,58 +115,32 @@ private void initAction() {
* project or not.
*/
void checkLocationDeleted(final IProject project) throws CoreException {
if (!project.exists()) { return; }
if (!project.exists()) return;
final IFileInfo location = IDEResourceInfoUtils.getFileInfo(project.getLocationURI());
if (!location.exists()) {
final String message = NLS.bind(IDEWorkbenchMessages.RefreshAction_locationDeletedMessage,
project.getName(), location.toString());

final MessageDialog dialog =
new MessageDialog(WorkbenchHelper.getShell(), IDEWorkbenchMessages.RefreshAction_dialogTitle, null,
message, QUESTION, new String[] { YES_LABEL, NO_LABEL }, 0) {
@Override
protected int getShellStyle() {
return super.getShellStyle() | SHEET;
}
};
WorkbenchHelper.run(() -> dialog.open());

// Do the deletion back in the operation thread
if (dialog.getReturnCode() == 0) { // yes was chosen
project.delete(true, true, null);
}
if (!location.exists() && Messages.confirm("Project location has been deleted",
"The location for project " + project.getName() + " (" + location.toString()
+ ") has been deleted. Do you want to remove " + project.getName() + " from the workspace ?")) {
project.delete(true, true, null);
}
}

@Override
protected String getOperationMessage() {
return IDEWorkbenchMessages.RefreshAction_progressMessage;
}
protected String getOperationMessage() { return IDEWorkbenchMessages.RefreshAction_progressMessage; }

@Override
protected String getProblemsMessage() {
return IDEWorkbenchMessages.RefreshAction_problemMessage;
}
protected String getProblemsMessage() { return IDEWorkbenchMessages.RefreshAction_problemMessage; }

@Override
protected String getProblemsTitle() {
return IDEWorkbenchMessages.RefreshAction_problemTitle;
}
protected String getProblemsTitle() { return IDEWorkbenchMessages.RefreshAction_problemTitle; }

/**
* Returns a list containing the workspace root if the selection would otherwise be empty.
*/
@Override
protected List<? extends IResource> getSelectedResources() {
final List<IResource> resources1 = new ArrayList<>();
for (final IResource r : super.getSelectedResources()) {
if (r.isAccessible()) {
resources1.add(r);
}
}
if (resources1.isEmpty()) {
resources1.add(ResourcesPlugin.getWorkspace().getRoot());
}
for (final IResource r : super.getSelectedResources()) { if (r.isAccessible()) { resources1.add(r); } }
if (resources1.isEmpty()) { resources1.add(ResourcesPlugin.getWorkspace().getRoot()); }
return resources1;
}

Expand Down Expand Up @@ -201,7 +177,7 @@ public void execute(final IProgressMonitor monitor) {
final IResource resource = resourcesEnum.next();
refreshResource(resource, null);
} catch (final CoreException e) {}
if (monitor.isCanceled()) { throw new OperationCanceledException(); }
if (monitor.isCanceled()) throw new OperationCanceledException();
}
} finally {
monitor.done();
Expand Down Expand Up @@ -229,14 +205,12 @@ protected void refreshResource(final IResource resource, final IProgressMonitor
checkLocationDeleted((IProject) resource);
} else if (resource.getType() == ROOT) {
final IProject[] projects = ((IWorkspaceRoot) resource).getProjects();
for (final IProject project : projects) {
checkLocationDeleted(project);
}
for (final IProject project : projects) { checkLocationDeleted(project); }
}
resource.refreshLocal(DEPTH_INFINITE, monitor);
resource.getParent().refreshLocal(DEPTH_INFINITE, monitor);

runInUI("Refreshing " + resource.getName(), 0, (m) -> {
runInUI("Refreshing " + resource.getName(), 0, m -> {

FileMetaDataProvider.getInstance().storeMetaData(resource, null, true);
FileMetaDataProvider.getInstance().getMetaData(resource, false, true);
Expand All @@ -253,11 +227,9 @@ public void run() {
@Override
public IStatus runInWorkspace(final IProgressMonitor monitor) throws CoreException {
final IRefreshHandler refresh = WorkbenchHelper.getService(IRefreshHandler.class);
if (refresh != null) {
refresh.completeRefresh(resources);
}
if (refresh != null) { refresh.completeRefresh(resources); }
return OK_STATUS;
};
}
};
job.setUser(true);
job.schedule();
Expand Down
@@ -1,10 +1,13 @@
/*******************************************************************************
* Copyright (c) 2000, 2008 IBM Corporation and others. All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is
* available at http://www.eclipse.org/legal/epl-v10.html
/*******************************************************************************************************
*
* Contributors: IBM Corporation - initial API and implementation
*******************************************************************************/
* RenameResourceAction.java, in ummisco.gama.ui.navigator, is part of the source code of the GAMA modeling and
* simulation platform (v.1.8.2).
*
* (c) 2007-2021 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU)
*
* Visit https://github.com/gama-platform/gama for license information and contacts.
*
********************************************************************************************************/
package ummisco.gama.ui.navigator.actions;

import static org.eclipse.ui.internal.ide.IDEWorkbenchMessages.RenameResourceAction_operationTitle;
Expand All @@ -26,19 +29,18 @@
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.IInputValidator;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.window.IShellProvider;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.WorkspaceAction;
import org.eclipse.ui.ide.undo.MoveResourcesOperation;
import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
import org.eclipse.ui.internal.ide.IIDEHelpContextIds;

import ummisco.gama.ui.dialogs.Messages;
import ummisco.gama.ui.metadata.FileMetaDataProvider;
import ummisco.gama.ui.navigator.contents.LinkedFile;
import ummisco.gama.ui.utils.WorkbenchHelper;
Expand Down Expand Up @@ -67,18 +69,25 @@ public class RenameResourceAction extends WorkspaceAction {

private String newNameWithoutExtension;

/** The model provider ids. */
private String[] modelProviderIds;

/** The Constant CHECK_RENAME_TITLE. */
private static final String CHECK_RENAME_TITLE = IDEWorkbenchMessages.RenameResourceAction_checkTitle;

/** The Constant CHECK_RENAME_MESSAGE. */
private static final String CHECK_RENAME_MESSAGE = IDEWorkbenchMessages.RenameResourceAction_readOnlyCheck;

/** The resource exists title. */
private static String RESOURCE_EXISTS_TITLE = IDEWorkbenchMessages.RenameResourceAction_resourceExists;

/** The resource exists message. */
private static String RESOURCE_EXISTS_MESSAGE = IDEWorkbenchMessages.RenameResourceAction_overwriteQuestion;

/** The project exists message. */
private static String PROJECT_EXISTS_MESSAGE = IDEWorkbenchMessages.RenameResourceAction_overwriteProjectQuestion;

/** The project exists title. */
private static String PROJECT_EXISTS_TITLE = IDEWorkbenchMessages.RenameResourceAction_projectExists;

/**
Expand All @@ -94,6 +103,9 @@ public RenameResourceAction(final IShellProvider provider) {
initAction();
}

/**
* Inits the action.
*/
private void initAction() {
setToolTipText(IDEWorkbenchMessages.RenameResourceAction_toolTip);
setId(ID);
Expand All @@ -109,26 +121,16 @@ private void initAction() {
* @param destination
* - the resource to be overwritten
*/
private boolean checkOverwrite(final Shell shell, final IResource destination) {

final boolean[] result = new boolean[1];

// Run it inside of a runnable to make sure we get to parent off of the
// shell as we are not in the UI thread.

final Runnable query = () -> {
final String pathName = destination.getFullPath().makeRelative().toString();
String message = RESOURCE_EXISTS_MESSAGE;
String title = RESOURCE_EXISTS_TITLE;
if (destination.getType() == IResource.PROJECT) {
message = PROJECT_EXISTS_MESSAGE;
title = PROJECT_EXISTS_TITLE;
}
result[0] = MessageDialog.openQuestion(shell, title, MessageFormat.format(message, pathName));
};
private boolean checkOverwrite(final IResource destination) {
final String pathName = destination.getFullPath().makeRelative().toString();
String message = RESOURCE_EXISTS_MESSAGE;
String title = RESOURCE_EXISTS_TITLE;
if (destination.getType() == IResource.PROJECT) {
message = PROJECT_EXISTS_MESSAGE;
title = PROJECT_EXISTS_TITLE;
}
return Messages.question(title, MessageFormat.format(message, pathName));

shell.getDisplay().syncExec(query);
return result[0];
}

/**
Expand All @@ -143,8 +145,8 @@ private boolean checkReadOnlyAndNull(final IResource currentResource) {

// Do a quick read only check
final ResourceAttributes attributes = currentResource.getResourceAttributes();
if (attributes != null && attributes.isReadOnly()) return MessageDialog.openQuestion(WorkbenchHelper.getShell(),
CHECK_RENAME_TITLE, MessageFormat.format(CHECK_RENAME_MESSAGE, currentResource.getName()));
if (attributes != null && attributes.isReadOnly()) return Messages.question(CHECK_RENAME_TITLE,
MessageFormat.format(CHECK_RENAME_MESSAGE, currentResource.getName()));

return true;
}
Expand All @@ -153,25 +155,19 @@ private boolean checkReadOnlyAndNull(final IResource currentResource) {
* (non-Javadoc) Method declared on WorkspaceAction.
*/
@Override
protected String getOperationMessage() {
return IDEWorkbenchMessages.RenameResourceAction_progress;
}
protected String getOperationMessage() { return IDEWorkbenchMessages.RenameResourceAction_progress; }

/*
* (non-Javadoc) Method declared on WorkspaceAction.
*/
@Override
protected String getProblemsMessage() {
return IDEWorkbenchMessages.RenameResourceAction_problemMessage;
}
protected String getProblemsMessage() { return IDEWorkbenchMessages.RenameResourceAction_problemMessage; }

/*
* (non-Javadoc) Method declared on WorkspaceAction.
*/
@Override
protected String getProblemsTitle() {
return IDEWorkbenchMessages.RenameResourceAction_problemTitle;
}
protected String getProblemsTitle() { return IDEWorkbenchMessages.RenameResourceAction_problemTitle; }

/**
* Return the new name to be given to the target resource.
Expand Down Expand Up @@ -265,9 +261,7 @@ protected boolean updateSelection(final IStructuredSelection selection) {
* @return the model provider ids that are known to the client that instantiated this operation.
* @since 3.2
*/
public String[] getModelProviderIds() {
return modelProviderIds;
}
public String[] getModelProviderIds() { return modelProviderIds; }

/**
* Sets the model provider ids that are known to the client that instantiated this operation. Any potential side
Expand All @@ -277,9 +271,7 @@ public String[] getModelProviderIds() {
* the model providers known to the client who is using this operation.
* @since 3.2
*/
public void setModelProviderIds(final String[] modelProviderIds) {
this.modelProviderIds = modelProviderIds;
}
public void setModelProviderIds(final String[] modelProviderIds) { this.modelProviderIds = modelProviderIds; }

/*
* (non-Javadoc)
Expand All @@ -301,7 +293,7 @@ protected IRunnableWithProgress createOperation(final IStatus[] errorStatus) {
? newNameWithoutExtension : newNameWithoutExtension + "." + fileExtension);
final IResource newResource = root.findMember(newPath);
boolean go = true;
if (newResource != null) { go = checkOverwrite(WorkbenchHelper.getShell(), newResource); }
if (newResource != null) { go = checkOverwrite(newResource); }
if (go) {
final MoveResourcesOperation op =
new MoveResourcesOperation(r, newPath, RenameResourceAction_operationTitle);
Expand Down

0 comments on commit 1ef4cc4

Please sign in to comment.