1 11 package org.eclipse.jdt.ui.actions; 12 13 import java.util.Arrays ; 14 import java.util.HashSet ; 15 import java.util.Iterator ; 16 import java.util.Set ; 17 18 import org.eclipse.jface.dialogs.MessageDialog; 19 import org.eclipse.jface.viewers.IStructuredSelection; 20 21 import org.eclipse.jface.text.ITextSelection; 22 23 import org.eclipse.ui.IWorkbenchSite; 24 import org.eclipse.ui.PlatformUI; 25 26 import org.eclipse.jdt.core.IJavaElement; 27 import org.eclipse.jdt.core.IMember; 28 import org.eclipse.jdt.core.IType; 29 import org.eclipse.jdt.core.JavaModelException; 30 31 import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester; 32 import org.eclipse.jdt.internal.corext.refactoring.RefactoringExecutionStarter; 33 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 34 35 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 36 import org.eclipse.jdt.internal.ui.JavaPlugin; 37 import org.eclipse.jdt.internal.ui.actions.ActionUtil; 38 import org.eclipse.jdt.internal.ui.actions.SelectionConverter; 39 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; 40 import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection; 41 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages; 42 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 43 44 56 public class PushDownAction extends SelectionDispatchAction { 57 58 private static IMember[] getSelectedMembers(IStructuredSelection selection) { 59 if (selection.isEmpty()) 60 return null; 61 if (selection.size() == 1) { 62 try { 63 final IType type= RefactoringAvailabilityTester.getSingleSelectedType(selection); 64 if (type != null) 65 return new IType[] { type}; 66 } catch (JavaModelException exception) { 67 JavaPlugin.log(exception); 68 } 69 } 70 for (Iterator iter= selection.iterator(); iter.hasNext();) { 71 if (!(iter.next() instanceof IMember)) 72 return null; 73 } 74 Set memberSet= new HashSet (); 75 memberSet.addAll(Arrays.asList(selection.toArray())); 76 return (IMember[]) memberSet.toArray(new IMember[memberSet.size()]); 77 } 78 79 private JavaEditor fEditor; 80 81 87 public PushDownAction(JavaEditor editor) { 88 this(editor.getEditorSite()); 89 fEditor= editor; 90 setEnabled(SelectionConverter.canOperateOn(fEditor)); 91 } 92 93 101 public PushDownAction(IWorkbenchSite site) { 102 super(site); 103 setText(RefactoringMessages.PushDownAction_Push_Down); 104 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.PUSH_DOWN_ACTION); 105 } 106 107 private IMember getSelectedMemberFromEditor() throws JavaModelException { 108 IJavaElement element= SelectionConverter.resolveEnclosingElement(fEditor, (ITextSelection) fEditor.getSelectionProvider().getSelection()); 109 if (element == null || !(element instanceof IMember)) 110 return null; 111 return (IMember) element; 112 } 113 114 117 public void run(IStructuredSelection selection) { 118 try { 119 IMember[] members= getSelectedMembers(selection); 120 if (RefactoringAvailabilityTester.isPushDownAvailable(members) && ActionUtil.isEditable(getShell(), members[0])) 121 RefactoringExecutionStarter.startPushDownRefactoring(members, getShell()); 122 } catch (JavaModelException e) { 123 ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception); 124 } 125 } 126 127 130 public void run(ITextSelection selection) { 131 try { 132 if (!ActionUtil.isEditable(fEditor)) 133 return; 134 IMember member= getSelectedMemberFromEditor(); 135 IMember[] array= new IMember[] { member}; 136 if (member != null && RefactoringAvailabilityTester.isPushDownAvailable(array)) { 137 RefactoringExecutionStarter.startPushDownRefactoring(array, getShell()); 138 } else { 139 MessageDialog.openInformation(getShell(), RefactoringMessages.OpenRefactoringWizardAction_unavailable, RefactoringMessages.PushDownAction_To_activate); 140 } 141 } catch (JavaModelException e) { 142 ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception); 143 } 144 } 145 146 149 public void selectionChanged(IStructuredSelection selection) { 150 try { 151 setEnabled(RefactoringAvailabilityTester.isPushDownAvailable(selection)); 152 } catch (JavaModelException e) { 153 if (JavaModelUtil.isExceptionToBeLogged(e)) 155 JavaPlugin.log(e); 156 setEnabled(false); } 158 } 159 160 163 public void selectionChanged(ITextSelection selection) { 164 setEnabled(true); 165 } 166 167 173 public void selectionChanged(JavaTextSelection selection) { 174 try { 175 setEnabled(RefactoringAvailabilityTester.isPushDownAvailable(selection)); 176 } catch (JavaModelException e) { 177 setEnabled(false); 178 } 179 } 180 } 181 | Popular Tags |