1 11 package org.eclipse.jdt.internal.ui.refactoring.reorg; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.Iterator ; 15 16 import org.eclipse.core.runtime.CoreException; 17 18 import org.eclipse.swt.dnd.Clipboard; 19 20 import org.eclipse.jface.viewers.IStructuredSelection; 21 22 import org.eclipse.ui.ISharedImages; 23 import org.eclipse.ui.IWorkbenchSite; 24 import org.eclipse.ui.PlatformUI; 25 26 27 import org.eclipse.jdt.core.IJavaElement; 28 import org.eclipse.jdt.core.IType; 29 30 import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester; 31 import org.eclipse.jdt.internal.corext.refactoring.RefactoringExecutionStarter; 32 import org.eclipse.jdt.internal.corext.refactoring.reorg.ReorgUtils; 33 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 34 35 import org.eclipse.jdt.ui.actions.SelectionDispatchAction; 36 37 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 38 import org.eclipse.jdt.internal.ui.JavaPlugin; 39 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages; 40 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 41 42 public class CutAction extends SelectionDispatchAction{ 43 44 private CopyToClipboardAction fCopyToClipboardAction; 45 46 public CutAction(IWorkbenchSite site, Clipboard clipboard) { 47 super(site); 48 setText(ReorgMessages.CutAction_text); 49 fCopyToClipboardAction= new CopyToClipboardAction(site, clipboard); 50 51 ISharedImages workbenchImages= JavaPlugin.getDefault().getWorkbench().getSharedImages(); 52 setDisabledImageDescriptor(workbenchImages.getImageDescriptor(ISharedImages.IMG_TOOL_CUT_DISABLED)); 53 setImageDescriptor(workbenchImages.getImageDescriptor(ISharedImages.IMG_TOOL_CUT)); 54 setHoverImageDescriptor(workbenchImages.getImageDescriptor(ISharedImages.IMG_TOOL_CUT)); 55 56 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.CUT_ACTION); 57 } 58 59 public void selectionChanged(IStructuredSelection selection) { 60 if (!selection.isEmpty()) { 61 try { 62 if (!containsOnlyElementsInsideCompilationUnits(selection) || containsTopLevelTypes(selection)) { 64 setEnabled(false); 65 return; 66 } 67 fCopyToClipboardAction.selectionChanged(selection); 68 setEnabled(fCopyToClipboardAction.isEnabled() && RefactoringAvailabilityTester.isDeleteAvailable(selection)); 69 } catch (CoreException e) { 70 if (JavaModelUtil.isExceptionToBeLogged(e)) 73 JavaPlugin.log(e); 74 setEnabled(false); 75 } 76 } else 77 setEnabled(false); 78 } 79 80 private static boolean containsOnlyElementsInsideCompilationUnits(IStructuredSelection selection) { 81 for (Iterator iter = selection.iterator(); iter.hasNext();) { 82 Object object= iter.next(); 83 if (! (object instanceof IJavaElement && ReorgUtils.isInsideCompilationUnit((IJavaElement)object))) 84 return false; 85 } 86 return true; 87 } 88 89 private static boolean containsTopLevelTypes(IStructuredSelection selection) { 90 for (Iterator iter = selection.iterator(); iter.hasNext();) { 91 Object each= iter.next(); 92 if ((each instanceof IType) && ((IType)each).getDeclaringType() == null) 93 return true; 94 } 95 return false; 96 } 97 98 public void run(IStructuredSelection selection) { 99 try { 100 selectionChanged(selection); 101 if (isEnabled()) { 102 fCopyToClipboardAction.run(selection); 103 RefactoringExecutionStarter.startCutRefactoring(selection.toArray(), getShell()); 104 } 105 } catch (CoreException e) { 106 ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception); 107 } catch (InterruptedException e) { 108 } catch (InvocationTargetException e) { 110 ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception); 111 } 112 } 113 } 114 | Popular Tags |