1 11 package org.eclipse.jdt.ui.actions; 12 13 14 import org.eclipse.jface.dialogs.MessageDialog; 15 import org.eclipse.jface.viewers.IStructuredSelection; 16 17 import org.eclipse.jface.text.ITextSelection; 18 19 import org.eclipse.ui.IWorkbenchSite; 20 import org.eclipse.ui.PlatformUI; 21 22 import org.eclipse.jdt.core.IField; 23 import org.eclipse.jdt.core.IJavaElement; 24 import org.eclipse.jdt.core.JavaModelException; 25 26 import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester; 27 import org.eclipse.jdt.internal.corext.refactoring.RefactoringExecutionStarter; 28 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 29 30 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 31 import org.eclipse.jdt.internal.ui.JavaPlugin; 32 import org.eclipse.jdt.internal.ui.actions.ActionMessages; 33 import org.eclipse.jdt.internal.ui.actions.ActionUtil; 34 import org.eclipse.jdt.internal.ui.actions.SelectionConverter; 35 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; 36 import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection; 37 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages; 38 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 39 40 52 public class SelfEncapsulateFieldAction extends SelectionDispatchAction { 53 54 private JavaEditor fEditor; 55 56 63 public SelfEncapsulateFieldAction(IWorkbenchSite site) { 64 super(site); 65 setText(ActionMessages.SelfEncapsulateFieldAction_label); 66 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.SELF_ENCAPSULATE_ACTION); 67 } 68 69 74 public SelfEncapsulateFieldAction(JavaEditor editor) { 75 this(editor.getEditorSite()); 76 fEditor= editor; 77 setEnabled(SelectionConverter.canOperateOn(fEditor)); 78 } 79 80 82 85 public void selectionChanged(ITextSelection selection) { 86 setEnabled(true); 87 } 88 89 92 public void selectionChanged(JavaTextSelection selection) { 93 try { 94 setEnabled(RefactoringAvailabilityTester.isSelfEncapsulateAvailable(selection)); 95 } catch (JavaModelException e) { 96 if (JavaModelUtil.isExceptionToBeLogged(e)) 98 JavaPlugin.log(e); 99 setEnabled(false); } 101 } 102 103 106 public void run(ITextSelection selection) { 107 try { 108 IJavaElement[] elements= SelectionConverter.codeResolve(fEditor); 109 if (elements.length != 1 || !(elements[0] instanceof IField)) { 110 MessageDialog.openInformation(getShell(), ActionMessages.SelfEncapsulateFieldAction_dialog_title, ActionMessages.SelfEncapsulateFieldAction_dialog_unavailable); 111 return; 112 } 113 IField field= (IField)elements[0]; 114 115 if (!RefactoringAvailabilityTester.isSelfEncapsulateAvailable(field)) { 116 MessageDialog.openInformation(getShell(), ActionMessages.SelfEncapsulateFieldAction_dialog_title, ActionMessages.SelfEncapsulateFieldAction_dialog_unavailable); 117 return; 118 } 119 run(field); 120 } catch (JavaModelException exception) { 121 JavaPlugin.log(exception); 122 return; 123 } 124 } 125 126 128 131 public void selectionChanged(IStructuredSelection selection) { 132 try { 133 setEnabled(RefactoringAvailabilityTester.isSelfEncapsulateAvailable(selection)); 134 } catch (JavaModelException e) { 135 if (JavaModelUtil.isExceptionToBeLogged(e)) 137 JavaPlugin.log(e); 138 setEnabled(false); } 140 } 141 142 146 public void run(IStructuredSelection selection) { 147 try { 148 if (RefactoringAvailabilityTester.isSelfEncapsulateAvailable(selection)) 149 run((IField) selection.getFirstElement()); 150 } catch (JavaModelException e) { 151 ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception); 152 } 153 } 154 155 157 161 public void run(IField field) { 162 if (! ActionUtil.isEditable(fEditor, getShell(), field)) 163 return; 164 RefactoringExecutionStarter.startSelfEncapsulateRefactoring(field, getShell()); 165 } 166 } 167 | Popular Tags |