1 11 package org.eclipse.jdt.ui.actions; 12 13 import org.eclipse.jface.dialogs.MessageDialog; 14 import org.eclipse.jface.viewers.ISelection; 15 import org.eclipse.jface.viewers.IStructuredSelection; 16 import org.eclipse.jface.viewers.SelectionChangedEvent; 17 18 import org.eclipse.jface.text.ITextSelection; 19 20 import org.eclipse.ui.IWorkbenchSite; 21 import org.eclipse.ui.PlatformUI; 22 23 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 24 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; 25 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages; 26 import org.eclipse.jdt.internal.ui.refactoring.actions.RenameJavaElementAction; 27 import org.eclipse.jdt.internal.ui.refactoring.actions.RenameResourceAction; 28 29 41 public class RenameAction extends SelectionDispatchAction { 42 43 private RenameJavaElementAction fRenameJavaElement; 44 private RenameResourceAction fRenameResource; 45 46 53 public RenameAction(IWorkbenchSite site) { 54 super(site); 55 setText(RefactoringMessages.RenameAction_text); 56 fRenameJavaElement= new RenameJavaElementAction(site); 57 fRenameJavaElement.setText(getText()); 58 fRenameResource= new RenameResourceAction(site); 59 fRenameResource.setText(getText()); 60 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.RENAME_ACTION); 61 } 62 63 67 public RenameAction(JavaEditor editor) { 68 this(editor.getEditorSite()); 69 fRenameJavaElement= new RenameJavaElementAction(editor); 70 } 71 72 75 public void selectionChanged(SelectionChangedEvent event) { 76 fRenameJavaElement.selectionChanged(event); 77 if (fRenameResource != null) 78 fRenameResource.selectionChanged(event); 79 setEnabled(computeEnabledState()); 80 } 81 82 85 public void update(ISelection selection) { 86 fRenameJavaElement.update(selection); 87 88 if (fRenameResource != null) 89 fRenameResource.update(selection); 90 91 setEnabled(computeEnabledState()); 92 } 93 94 private boolean computeEnabledState(){ 95 if (fRenameResource != null) { 96 return fRenameJavaElement.isEnabled() || fRenameResource.isEnabled(); 97 } else { 98 return fRenameJavaElement.isEnabled(); 99 } 100 } 101 102 public void run(IStructuredSelection selection) { 103 if (fRenameJavaElement.isEnabled()) 104 fRenameJavaElement.run(selection); 105 if (fRenameResource != null && fRenameResource.isEnabled()) 106 fRenameResource.run(selection); 107 } 108 109 public void run(ITextSelection selection) { 110 if (fRenameJavaElement.canRunInEditor()) 111 fRenameJavaElement.run(selection); 112 else 113 MessageDialog.openInformation(getShell(), RefactoringMessages.RenameAction_rename, RefactoringMessages.RenameAction_unavailable); 114 } 115 } 116 | Popular Tags |