1 11 package org.eclipse.jdt.internal.ui.actions; 12 13 import java.io.CharConversionException ; 14 import java.util.Arrays ; 15 import java.util.HashSet ; 16 import java.util.Iterator ; 17 import java.util.Set ; 18 19 import org.eclipse.jface.dialogs.MessageDialog; 20 import org.eclipse.jface.viewers.IStructuredSelection; 21 22 import org.eclipse.jface.text.ITextSelection; 23 24 import org.eclipse.ui.IWorkbenchSite; 25 import org.eclipse.ui.PlatformUI; 26 27 import org.eclipse.jdt.core.IJavaElement; 28 import org.eclipse.jdt.core.IMember; 29 import org.eclipse.jdt.core.IType; 30 import org.eclipse.jdt.core.JavaModelException; 31 32 import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester; 33 import org.eclipse.jdt.internal.corext.refactoring.RefactoringExecutionStarter; 34 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 35 36 import org.eclipse.jdt.ui.actions.SelectionDispatchAction; 37 38 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 39 import org.eclipse.jdt.internal.ui.JavaPlugin; 40 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; 41 import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection; 42 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages; 43 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 44 45 58 public class ExtractSuperClassAction extends SelectionDispatchAction { 59 60 66 public static final String EXTRACT_SUPERTYPE= "org.eclipse.jdt.ui.edit.text.java.extract.superclass"; 68 74 public static final String EXTRACT_SUPERTYPES= "org.eclipse.jdt.ui.actions.ExtractSuperclass"; 76 private static IMember[] getSelectedMembers(final IStructuredSelection selection) { 77 if (selection.isEmpty()) 78 return null; 79 if (selection.size() == 1) { 80 try { 81 final IType type= RefactoringAvailabilityTester.getSingleSelectedType(selection); 82 if (type != null) 83 return new IType[] {type}; 84 } catch (JavaModelException exception) { 85 JavaPlugin.log(exception); 86 } 87 } 88 for (final Iterator iterator= selection.iterator(); iterator.hasNext();) { 89 if (!(iterator.next() instanceof IMember)) 90 return null; 91 } 92 final Set set= new HashSet (); 93 set.addAll(Arrays.asList(selection.toArray())); 94 return (IMember[]) set.toArray(new IMember[set.size()]); 95 } 96 97 98 private JavaEditor fEditor; 99 100 107 public ExtractSuperClassAction(final JavaEditor editor) { 108 this(editor.getEditorSite()); 109 fEditor= editor; 110 setEnabled(SelectionConverter.canOperateOn(fEditor)); 111 } 112 113 121 public ExtractSuperClassAction(final IWorkbenchSite site) { 122 super(site); 123 setText(RefactoringMessages.ExtractSuperTypeAction_label); 124 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.EXTRACT_SUPERTYPE_ACTION); 125 } 126 127 private IMember getSelectedMemberFromEditor() throws JavaModelException { 128 final IJavaElement element= SelectionConverter.resolveEnclosingElement(fEditor, (ITextSelection) fEditor.getSelectionProvider().getSelection()); 129 if (element == null || !(element instanceof IMember)) 130 return null; 131 return (IMember) element; 132 } 133 134 137 public void run(final IStructuredSelection selection) { 138 try { 139 final IMember[] members= getSelectedMembers(selection); 140 if (RefactoringAvailabilityTester.isExtractSupertypeAvailable(members) && ActionUtil.isEditable(getShell(), members[0])) 141 RefactoringExecutionStarter.startExtractSupertypeRefactoring(members, getShell()); 142 } catch (final JavaModelException exception) { 143 ExceptionHandler.handle(exception, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception); 144 } 145 } 146 147 150 public void run(final ITextSelection selection) { 151 try { 152 if (! ActionUtil.isEditable(fEditor)) 153 return; 154 final IMember member= getSelectedMemberFromEditor(); 155 final IMember[] array= new IMember[] { member}; 156 if (member != null && RefactoringAvailabilityTester.isExtractSupertypeAvailable(array)) { 157 RefactoringExecutionStarter.startExtractSupertypeRefactoring(array, getShell()); 158 } else { 159 MessageDialog.openInformation(getShell(), RefactoringMessages.OpenRefactoringWizardAction_unavailable, RefactoringMessages.ExtractSuperTypeAction_unavailable); 160 } 161 } catch (JavaModelException exception) { 162 ExceptionHandler.handle(exception, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception); 163 } 164 } 165 166 169 public void selectionChanged(final IStructuredSelection selection) { 170 try { 171 setEnabled(RefactoringAvailabilityTester.isExtractSupertypeAvailable(selection)); 172 } catch (JavaModelException exception) { 173 if (!(exception.getException() instanceof CharConversionException ) && JavaModelUtil.isExceptionToBeLogged(exception)) 175 JavaPlugin.log(exception); 176 setEnabled(false); } 178 } 179 180 183 public void selectionChanged(final ITextSelection selection) { 184 setEnabled(true); 185 } 186 187 190 public void selectionChanged(final JavaTextSelection selection) { 191 try { 192 setEnabled(RefactoringAvailabilityTester.isExtractSupertypeAvailable(selection)); 193 } catch (JavaModelException event) { 194 setEnabled(false); 195 } 196 } 197 } 198 | Popular Tags |