From 19cbbb52d1620b91918fd8855abcdf5d5243fc01 Mon Sep 17 00:00:00 2001 From: Jonah Graham Date: Mon, 20 Aug 2018 01:38:39 +0200 Subject: [PATCH] Bug 537176: add "update references" checkbox to resource rename Add a wizard page for the rename of CNature resource files. The "discouragedReference" is downgraded to "warning" because of the use of some LTK internal classes. As a reference, most CDT plug-ins this setting is ignore or warning. See Bug 538133 for changing LTK to make it easier to do this. Change-Id: I7b63b3fc97bddf364805c0b92c22ecaafbfbb6d1 Also-by: Baha El Kassaby Signed-off-by: Baha El Kassaby --- .../.settings/org.eclipse.jdt.core.prefs | 2 +- .../CNavigatorRefactorActionGroup.java | 4 +- .../CNavigatorRenameResourceAction.java | 67 ++++++++ .../ui/refactoring/rename/CRefactory.java | 15 +- .../CResourceRenameRefactoringInputPage.java | 161 ++++++++++++++++++ .../CResourceRenameRefactoringWizard.java | 29 ++++ .../ui/refactoring/rename/RenameMessages.java | 7 +- .../rename/RenameMessages.properties | 7 +- .../ui/refactoring/rename/RenameSupport.java | 47 ++++- 9 files changed, 332 insertions(+), 7 deletions(-) create mode 100644 core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/navigator/CNavigatorRenameResourceAction.java create mode 100644 core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CResourceRenameRefactoringInputPage.java create mode 100644 core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CResourceRenameRefactoringWizard.java diff --git a/core/org.eclipse.cdt.ui/.settings/org.eclipse.jdt.core.prefs b/core/org.eclipse.cdt.ui/.settings/org.eclipse.jdt.core.prefs index ca337be63c5..f8172158496 100644 --- a/core/org.eclipse.cdt.ui/.settings/org.eclipse.jdt.core.prefs +++ b/core/org.eclipse.cdt.ui/.settings/org.eclipse.jdt.core.prefs @@ -13,7 +13,7 @@ org.eclipse.jdt.core.compiler.problem.autoboxing=ignore org.eclipse.jdt.core.compiler.problem.deprecation=warning org.eclipse.jdt.core.compiler.problem.deprecationInDeprecatedCode=disabled org.eclipse.jdt.core.compiler.problem.deprecationWhenOverridingDeprecatedMethod=disabled -org.eclipse.jdt.core.compiler.problem.discouragedReference=error +org.eclipse.jdt.core.compiler.problem.discouragedReference=warning org.eclipse.jdt.core.compiler.problem.emptyStatement=warning org.eclipse.jdt.core.compiler.problem.enumIdentifier=error org.eclipse.jdt.core.compiler.problem.fallthroughCase=warning diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/navigator/CNavigatorRefactorActionGroup.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/navigator/CNavigatorRefactorActionGroup.java index b7f5183b527..6b3506b1fb6 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/navigator/CNavigatorRefactorActionGroup.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/navigator/CNavigatorRefactorActionGroup.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2010 IBM Corporation and others. + * Copyright (c) 2000, 2018 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 @@ -79,7 +79,7 @@ public class CNavigatorRefactorActionGroup extends ActionGroup { moveAction = new MoveResourceAction(shellProvider); moveAction.setActionDefinitionId(IWorkbenchCommandConstants.FILE_MOVE); - renameAction = new RenameResourceAction(shellProvider, tree); + renameAction = new CNavigatorRenameResourceAction(shellProvider, tree); renameAction.setActionDefinitionId(IWorkbenchCommandConstants.FILE_RENAME); } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/navigator/CNavigatorRenameResourceAction.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/navigator/CNavigatorRenameResourceAction.java new file mode 100644 index 00000000000..a21463ee893 --- /dev/null +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/navigator/CNavigatorRenameResourceAction.java @@ -0,0 +1,67 @@ +/******************************************************************************* + * Copyright (c) 2018 Kichwa Coders 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: + * Jonah Graham (Kichwa Coders) - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.internal.ui.navigator; + +import java.util.List; + +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.jface.window.IShellProvider; +import org.eclipse.swt.widgets.Tree; +import org.eclipse.ui.actions.RenameResourceAction; + +import org.eclipse.cdt.core.CCProjectNature; +import org.eclipse.cdt.core.CProjectNature; + +import org.eclipse.cdt.internal.ui.refactoring.rename.CRefactory; + +public class CNavigatorRenameResourceAction extends RenameResourceAction { + + private IShellProvider shell; + + public CNavigatorRenameResourceAction(IShellProvider shell, Tree tree) { + super(shell, tree); + this.shell = shell; + } + + @Override + public void run() { + List resources = getSelectedResources(); + + if (resources.size() == 1) { + IResource selectedResource = resources.get(0); + if (selectedResource.exists()) { + IProject project = selectedResource.getProject(); + if (hasCNature(project)) { + CRefactory.getInstance().renameResource(shell.getShell(), selectedResource); + return; + } + } + } + super.run(); + } + + private boolean hasCNature(IProject project) { + boolean hasNature = false; + try { + hasNature = project.hasNature(CProjectNature.C_NATURE_ID) + || project.hasNature(CCProjectNature.CC_NATURE_ID); + } catch (CoreException e1) { + /* + * don't perform rename with CDT specific dialog if we can't test + * nature, it means that project either does not exist or is closed. + */ + } + return hasNature; + } + +} diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CRefactory.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CRefactory.java index 7f46dd655a4..fc613a790bd 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CRefactory.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CRefactory.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2013 Wind River Systems, Inc. and others. + * Copyright (c) 2004, 2018 Wind River Systems, Inc. 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 @@ -95,6 +95,19 @@ public class CRefactory { RenameSupport.openDialog(shell, refactoring); } + /** + * Rename a resource selected in the navigator + * + * @param shell + * @param selected + */ + public void renameResource(Shell shell, IResource selected) { + if (!IDE.saveAllEditors(new IResource[] { ResourcesPlugin.getWorkspace().getRoot() }, false)) { + return; + } + RenameSupport.openRenameResourceDialog(shell, selected); + } + public TextSearchWrapper getTextSearch() { if (fTextSearch == null) { return new TextSearchWrapper(); diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CResourceRenameRefactoringInputPage.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CResourceRenameRefactoringInputPage.java new file mode 100644 index 00000000000..6a4960e39ff --- /dev/null +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CResourceRenameRefactoringInputPage.java @@ -0,0 +1,161 @@ +/******************************************************************************* + * Copyright (c) 2018 Kichwa Coders 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: + * Jonah Graham (Kichwa Coders) - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.internal.ui.refactoring.rename; + +import org.eclipse.core.resources.IResource; +import org.eclipse.jface.dialogs.Dialog; +import org.eclipse.jface.dialogs.IDialogSettings; +import org.eclipse.jface.preference.PreferenceDialog; +import org.eclipse.jface.wizard.IWizardPage; +import org.eclipse.ltk.core.refactoring.RefactoringStatus; +import org.eclipse.ltk.internal.core.refactoring.resource.RenameResourceProcessor; +import org.eclipse.ltk.ui.refactoring.UserInputWizardPage; +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.ModifyEvent; +import org.eclipse.swt.events.ModifyListener; +import org.eclipse.swt.events.SelectionAdapter; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Link; +import org.eclipse.swt.widgets.Text; +import org.eclipse.ui.dialogs.PreferencesUtil; + +import org.eclipse.cdt.ui.CUIPlugin; + +import org.eclipse.cdt.internal.ui.preferences.OrganizeIncludesPreferencePage; + +public class CResourceRenameRefactoringInputPage extends UserInputWizardPage { + + /** + * Dialog settings key for default setting of update references checkbox. + */ + public static final String KEY_UPDATE_REFERENCES = "updateReferences"; //$NON-NLS-1$ + + private static final String DIALOG_SETTINGS_KEY = "CResourceRenameRefactoringInputPage"; //$NON-NLS-1$ + private IDialogSettings fDialogSettings; + + private Text fNameField; + private RenameResourceProcessor fRefactoringProcessor; + private Button updateReferences; + + public CResourceRenameRefactoringInputPage(RenameResourceProcessor processor) { + super("CResourceRenameRefactoringInputPage"); //$NON-NLS-1$ + fRefactoringProcessor = processor; + IDialogSettings ds = CUIPlugin.getDefault().getDialogSettings(); + fDialogSettings = ds.getSection(DIALOG_SETTINGS_KEY); + if (fDialogSettings == null) { + fDialogSettings = ds.addNewSection(DIALOG_SETTINGS_KEY); + } + } + + @Override + public void createControl(Composite parent) { + Composite composite = new Composite(parent, SWT.NONE); + composite.setLayout(new GridLayout(2, false)); + composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); + composite.setFont(parent.getFont()); + + Label label = new Label(composite, SWT.NONE); + label.setText(RenameMessages.CResourceRenameRefactoringInputPage_new_name); + label.setLayoutData(new GridData()); + + fNameField = new Text(composite, SWT.BORDER); + String resourceName = fRefactoringProcessor.getNewResourceName(); + fNameField.setText(resourceName); + fNameField.setLayoutData(new GridData(GridData.FILL, GridData.BEGINNING, true, false)); + fNameField.addModifyListener(new ModifyListener() { + @Override + public void modifyText(ModifyEvent e) { + validatePage(); + } + }); + + int lastIndexOfDot = resourceName.lastIndexOf('.'); + if ((fRefactoringProcessor.getResource().getType() == IResource.FILE) && (lastIndexOfDot > 0)) { + fNameField.setSelection(0, lastIndexOfDot); + } else { + fNameField.selectAll(); + } + + updateReferences = new Button(composite, SWT.CHECK); + updateReferences.setText(RenameMessages.CResourceRenameRefactoringInputPage_update_references); + String value = fDialogSettings.get(KEY_UPDATE_REFERENCES); + boolean updateRefs = value != null ? Boolean.parseBoolean(value) : true; + + updateReferences.setSelection(updateRefs); + GridData gridData = new GridData(GridData.FILL_HORIZONTAL); + gridData.horizontalSpan = 2; + updateReferences.setLayoutData(gridData); + + // link to open preference page + Link link = new Link(composite, SWT.NONE); + link.setText("" + RenameMessages.CResourceRenameRefactoringInputPage_open_preferences + ""); //$NON-NLS-1$ //$NON-NLS-2$ + link.setToolTipText(RenameMessages.CResourceRenameRefactoringInputPage_open_preferences_tooltip); + gridData = new GridData(GridData.FILL_HORIZONTAL); + gridData.horizontalSpan = 2; + link.setLayoutData(gridData); + link.addSelectionListener(new SelectionAdapter() { + @Override + public void widgetSelected(SelectionEvent e) { + PreferenceDialog dialog = PreferencesUtil.createPreferenceDialogOn(getShell(), + OrganizeIncludesPreferencePage.PREF_ID, null, null); + dialog.open(); + } + }); + + Dialog.applyDialogFont(composite); + + setPageComplete(false); + setControl(composite); + } + + @Override + public void setVisible(boolean visible) { + if (visible) { + fNameField.setFocus(); + } + super.setVisible(visible); + } + + private void validatePage() { + String text = fNameField.getText(); + RefactoringStatus status = fRefactoringProcessor.validateNewElementName(text); + setPageComplete(status); + } + + @Override + protected boolean performFinish() { + saveRefactoringSettings(); + saveDialogSettings(); + return super.performFinish(); + } + + @Override + public IWizardPage getNextPage() { + saveRefactoringSettings(); + saveDialogSettings(); + return super.getNextPage(); + } + + private void saveDialogSettings() { + fDialogSettings.put(KEY_UPDATE_REFERENCES, updateReferences.getSelection()); + } + + private void saveRefactoringSettings() { + fRefactoringProcessor.setNewResourceName(fNameField.getText()); + fRefactoringProcessor.setUpdateReferences(updateReferences.getSelection()); + } + +} diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CResourceRenameRefactoringWizard.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CResourceRenameRefactoringWizard.java new file mode 100644 index 00000000000..b08883ca70b --- /dev/null +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CResourceRenameRefactoringWizard.java @@ -0,0 +1,29 @@ +/******************************************************************************* + * Copyright (c) 2018 Kichwa Coders 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: + * Jonah Graham (Kichwa Coders) - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.internal.ui.refactoring.rename; + +import org.eclipse.core.resources.IResource; +import org.eclipse.ltk.internal.core.refactoring.resource.RenameResourceProcessor; +import org.eclipse.ltk.ui.refactoring.resource.RenameResourceWizard; + +public class CResourceRenameRefactoringWizard extends RenameResourceWizard { + + public CResourceRenameRefactoringWizard(IResource resource) { + super(resource); + } + + @Override + protected void addUserInputPages() { + RenameResourceProcessor processor = getRefactoring().getAdapter(RenameResourceProcessor.class); + addPage(new CResourceRenameRefactoringInputPage(processor)); + } + +} diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/RenameMessages.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/RenameMessages.java index c6c8c3b0d31..f71c3e30479 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/RenameMessages.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/RenameMessages.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2014 Wind River Systems, Inc. + * Copyright (c) 2004, 2018 Wind River Systems, Inc. * 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 @@ -97,6 +97,10 @@ class RenameMessages extends NLS { public static String CRenameTopProcessor_virtualMethod; public static String CRenameTopProcessor_wizard_backup_title; public static String CRenameTopProcessor_wizard_title; + public static String CResourceRenameRefactoringInputPage_new_name; + public static String CResourceRenameRefactoringInputPage_open_preferences; + public static String CResourceRenameRefactoringInputPage_open_preferences_tooltip; + public static String CResourceRenameRefactoringInputPage_update_references; public static String HeaderFileMoveParticipant_name; public static String HeaderFileRenameParticipant_name; public static String HeaderReferenceAdjuster_update_include_guards; @@ -121,6 +125,7 @@ class RenameMessages extends NLS { public static String RenameLinkedMode_error_saving_editor; public static String RenameSupport_not_available; public static String RenameSupport_dialog_title; + public static String RenameSupport_rename_resource; public static String TextSearch_monitor_categorizeMatches; static { diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/RenameMessages.properties b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/RenameMessages.properties index a7c6ccbcadd..93870cbe2d3 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/RenameMessages.properties +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/RenameMessages.properties @@ -1,5 +1,5 @@ ############################################################################### -# Copyright (c) 2005, 2014 IBM Corporation and others. +# Copyright (c) 2005, 2018 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 @@ -93,6 +93,10 @@ CRenameTopProcessor_type=type CRenameTopProcessor_virtualMethod=virtual method CRenameTopProcessor_wizard_backup_title=Rename CRenameTopProcessor_wizard_title=Rename ''{0}'' +CResourceRenameRefactoringInputPage_new_name=New na&me: +CResourceRenameRefactoringInputPage_open_preferences=Open preferences... +CResourceRenameRefactoringInputPage_open_preferences_tooltip=Renaming files, folders, or projects can change the contents of source files when updating references. Open preferences to customize the settings. +CResourceRenameRefactoringInputPage_update_references=Update &references HeaderFileMoveParticipant_name=Header File Move HeaderFileRenameParticipant_name=Header File Rename HeaderReferenceAdjuster_update_include_guards=Update include guards @@ -117,4 +121,5 @@ RenameInformationPopup_delayJobName=delayed RenameInformationPopup RenameLinkedMode_error_saving_editor=An error occurred while saving the editor. RenameSupport_not_available=The refactoring operation is not available RenameSupport_dialog_title=Rename Refactoring +RenameSupport_rename_resource=Rename resource TextSearch_monitor_categorizeMatches=categorizing matches diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/RenameSupport.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/RenameSupport.java index 73da8c8309f..9994acb7de2 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/RenameSupport.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/RenameSupport.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2014 IBM Corporation and others. + * Copyright (c) 2000, 2018 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 @@ -13,6 +13,7 @@ package org.eclipse.cdt.internal.ui.refactoring.rename; import java.lang.reflect.InvocationTargetException; +import org.eclipse.core.resources.IResource; import org.eclipse.core.runtime.Assert; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IStatus; @@ -31,6 +32,7 @@ import org.eclipse.cdt.core.model.ICProject; import org.eclipse.cdt.ui.CUIPlugin; import org.eclipse.cdt.internal.ui.refactoring.RefactoringExecutionHelper; +import org.eclipse.cdt.internal.ui.refactoring.RefactoringSaveHelper; import org.eclipse.cdt.internal.ui.refactoring.RefactoringStarter; /** @@ -216,6 +218,49 @@ public class RenameSupport { return DialogResult.CANCELED; } + /** + * Opens the refactoring dialog for a resource rename refactoring. + * + *

+ * This method has to be called from within the UI thread. + *

+ * + * @param shell a shell used as a parent for the refactoring dialog. + * @param resource selected resource to rename + * + * @see #openRenameResourceDialog(Shell, DialogMode, IResource) + */ + public static void openRenameResourceDialog(Shell shell, IResource resource) { + openRenameResourceDialog(shell, DialogMode.ALL_PAGES, resource); + } + + /** + * Opens the resource renaming refactoring dialog. + * + *

+ * This method has to be called from within the UI thread. + *

+ * + * @param shell A shell used as a parent for the refactoring, preview, or error dialog + * @param mode One of DialogMode values. ALL_PAGES opens wizard with all pages shown; + * PREVIEW_ONLY opens the preview page only; CONDITIONAL_PREVIEW opens the wizard with + * preview page only and only if a warning was generated during the final conditions check. + * @param resource selected resource to rename + * @return One of DialogResult values. OK is returned if the dialog was shown and + * the refactoring change was applied; CANCELED is returned if the refactoring was + * cancelled. SKIPPED is returned if the dialog was skipped in CONDITIONAL_PREVIEW mode and + * the refactoring change has not been applied yet. + */ + static DialogResult openRenameResourceDialog(Shell shell, final DialogMode mode, IResource resource) { + CResourceRenameRefactoringWizard wizard = new CResourceRenameRefactoringWizard(resource); + RefactoringStarter starter = new RefactoringStarter(); + if (starter.activate(wizard, shell, RenameMessages.RenameSupport_rename_resource, RefactoringSaveHelper.SAVE_ALL)) { + return DialogResult.OK; + } + + return DialogResult.CANCELED; + } + /** * Executes the rename refactoring without showing a dialog to gather * additional user input (for example the new name of the ICElement).