1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-23 06:32:10 +02:00

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 <baha.elkassaby@gmail.com>
Signed-off-by: Baha El Kassaby <baha.elkassaby@gmail.com>
This commit is contained in:
Jonah Graham 2018-08-20 01:38:39 +02:00
parent b7e9c8e9ff
commit 19cbbb52d1
9 changed files with 332 additions and 7 deletions

View file

@ -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.deprecation=warning
org.eclipse.jdt.core.compiler.problem.deprecationInDeprecatedCode=disabled org.eclipse.jdt.core.compiler.problem.deprecationInDeprecatedCode=disabled
org.eclipse.jdt.core.compiler.problem.deprecationWhenOverridingDeprecatedMethod=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.emptyStatement=warning
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.fallthroughCase=warning org.eclipse.jdt.core.compiler.problem.fallthroughCase=warning

View file

@ -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 * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -79,7 +79,7 @@ public class CNavigatorRefactorActionGroup extends ActionGroup {
moveAction = new MoveResourceAction(shellProvider); moveAction = new MoveResourceAction(shellProvider);
moveAction.setActionDefinitionId(IWorkbenchCommandConstants.FILE_MOVE); moveAction.setActionDefinitionId(IWorkbenchCommandConstants.FILE_MOVE);
renameAction = new RenameResourceAction(shellProvider, tree); renameAction = new CNavigatorRenameResourceAction(shellProvider, tree);
renameAction.setActionDefinitionId(IWorkbenchCommandConstants.FILE_RENAME); renameAction.setActionDefinitionId(IWorkbenchCommandConstants.FILE_RENAME);
} }

View file

@ -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<? extends IResource> 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;
}
}

View file

@ -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 * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -95,6 +95,19 @@ public class CRefactory {
RenameSupport.openDialog(shell, refactoring); 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() { public TextSearchWrapper getTextSearch() {
if (fTextSearch == null) { if (fTextSearch == null) {
return new TextSearchWrapper(); return new TextSearchWrapper();

View file

@ -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("<a>" + RenameMessages.CResourceRenameRefactoringInputPage_open_preferences + "</a>"); //$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());
}
}

View file

@ -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));
}
}

View file

@ -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 * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * 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_virtualMethod;
public static String CRenameTopProcessor_wizard_backup_title; public static String CRenameTopProcessor_wizard_backup_title;
public static String CRenameTopProcessor_wizard_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 HeaderFileMoveParticipant_name;
public static String HeaderFileRenameParticipant_name; public static String HeaderFileRenameParticipant_name;
public static String HeaderReferenceAdjuster_update_include_guards; 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 RenameLinkedMode_error_saving_editor;
public static String RenameSupport_not_available; public static String RenameSupport_not_available;
public static String RenameSupport_dialog_title; public static String RenameSupport_dialog_title;
public static String RenameSupport_rename_resource;
public static String TextSearch_monitor_categorizeMatches; public static String TextSearch_monitor_categorizeMatches;
static { static {

View file

@ -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 # All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0 # are made available under the terms of the Eclipse Public License v1.0
# which accompanies this distribution, and is available at # which accompanies this distribution, and is available at
@ -93,6 +93,10 @@ CRenameTopProcessor_type=type
CRenameTopProcessor_virtualMethod=virtual method CRenameTopProcessor_virtualMethod=virtual method
CRenameTopProcessor_wizard_backup_title=Rename CRenameTopProcessor_wizard_backup_title=Rename
CRenameTopProcessor_wizard_title=Rename ''{0}'' 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 HeaderFileMoveParticipant_name=Header File Move
HeaderFileRenameParticipant_name=Header File Rename HeaderFileRenameParticipant_name=Header File Rename
HeaderReferenceAdjuster_update_include_guards=Update include guards 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. RenameLinkedMode_error_saving_editor=An error occurred while saving the editor.
RenameSupport_not_available=The refactoring operation is not available RenameSupport_not_available=The refactoring operation is not available
RenameSupport_dialog_title=Rename Refactoring RenameSupport_dialog_title=Rename Refactoring
RenameSupport_rename_resource=Rename resource
TextSearch_monitor_categorizeMatches=categorizing matches TextSearch_monitor_categorizeMatches=categorizing matches

View file

@ -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 * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * 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 java.lang.reflect.InvocationTargetException;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.Assert; import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus; 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.ui.CUIPlugin;
import org.eclipse.cdt.internal.ui.refactoring.RefactoringExecutionHelper; import org.eclipse.cdt.internal.ui.refactoring.RefactoringExecutionHelper;
import org.eclipse.cdt.internal.ui.refactoring.RefactoringSaveHelper;
import org.eclipse.cdt.internal.ui.refactoring.RefactoringStarter; import org.eclipse.cdt.internal.ui.refactoring.RefactoringStarter;
/** /**
@ -216,6 +218,49 @@ public class RenameSupport {
return DialogResult.CANCELED; return DialogResult.CANCELED;
} }
/**
* Opens the refactoring dialog for a resource rename refactoring.
*
* <p>
* This method has to be called from within the UI thread.
* </p>
*
* @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.
*
* <p>
* This method has to be called from within the UI thread.
* </p>
*
* @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 * Executes the rename refactoring without showing a dialog to gather
* additional user input (for example the new name of the <tt>ICElement</tt>). * additional user input (for example the new name of the <tt>ICElement</tt>).