KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > refactoring > reorg > CutAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.refactoring.reorg;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.Iterator JavaDoc;
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                 // cannot cut top-level types. this deletes the cu and then you cannot paste because the cu is gone.
63
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                 // no ui here - this happens on selection changes
71
// http://bugs.eclipse.org/bugs/show_bug.cgi?id=19253
72
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 JavaDoc iter = selection.iterator(); iter.hasNext();) {
82             Object JavaDoc 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 JavaDoc iter = selection.iterator(); iter.hasNext();) {
91             Object JavaDoc 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 JavaDoc e) {
108             //OK
109
} catch (InvocationTargetException JavaDoc e) {
110             ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception);
111         }
112     }
113 }
114
Popular Tags