KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > ui > actions > MoveAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.ui.actions;
12
13 import java.util.Arrays JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Set JavaDoc;
17
18 import org.eclipse.jface.dialogs.MessageDialog;
19 import org.eclipse.jface.viewers.ISelection;
20 import org.eclipse.jface.viewers.IStructuredSelection;
21 import org.eclipse.jface.viewers.SelectionChangedEvent;
22 import org.eclipse.jface.viewers.StructuredSelection;
23
24 import org.eclipse.jface.text.ITextSelection;
25
26 import org.eclipse.ui.IWorkbenchSite;
27 import org.eclipse.ui.PlatformUI;
28
29 import org.eclipse.jdt.core.IJavaElement;
30 import org.eclipse.jdt.core.IMember;
31 import org.eclipse.jdt.core.IMethod;
32 import org.eclipse.jdt.core.JavaModelException;
33
34 import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester;
35
36 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
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.refactoring.RefactoringMessages;
41 import org.eclipse.jdt.internal.ui.refactoring.actions.MoveInstanceMethodAction;
42 import org.eclipse.jdt.internal.ui.refactoring.actions.MoveStaticMembersAction;
43 import org.eclipse.jdt.internal.ui.refactoring.reorg.ReorgMoveAction;
44 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
45
46 /**
47  * This action moves Java elements to a new location. The action prompts
48  * the user for the new location.
49  * <p>
50  * The action is applicable to a homogeneous selection containing either
51  * projects, package fragment roots, package fragments, compilation units,
52  * or static methods.
53  *
54  * <p>
55  * This class may be instantiated; it is not intended to be subclassed.
56  * </p>
57  *
58  * @since 2.0
59  */

60 public class MoveAction extends SelectionDispatchAction{
61 //TODO: remove duplicate availability checks. Look at
62
//- f...Action.selectionChanged
63
//- f...Action.isEnabled
64
//- ...Refactoring.isAvailable
65
//- try...
66
//... and remove duplicated code for text/structured selections.
67
//We have to clean this up, once we have a long term solution to
68
//bug 35748 (no JavaElements for local types).
69

70     private JavaEditor fEditor;
71     private MoveInstanceMethodAction fMoveInstanceMethodAction;
72     private MoveStaticMembersAction fMoveStaticMembersAction;
73     private ReorgMoveAction fReorgMoveAction;
74     
75     /**
76      * Creates a new <code>MoveAction</code>. The action requires
77      * that the selection provided by the site's selection provider is of type <code>
78      * org.eclipse.jface.viewers.IStructuredSelection</code>.
79      *
80      * @param site the site providing context information for this action
81      */

82     public MoveAction(IWorkbenchSite site) {
83         super(site);
84         setText(RefactoringMessages.MoveAction_text);
85         fMoveStaticMembersAction= new MoveStaticMembersAction(site);
86         fMoveInstanceMethodAction= new MoveInstanceMethodAction(site);
87         fReorgMoveAction= new ReorgMoveAction(site);
88         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.MOVE_ACTION);
89     }
90     
91     /**
92      * Note: This constructor is for internal use only. Clients should not call this constructor.
93      * @param editor the java editor
94      */

95     public MoveAction(JavaEditor editor) {
96         super(editor.getEditorSite());
97         fEditor= editor;
98         setText(RefactoringMessages.MoveAction_text);
99         fMoveStaticMembersAction= new MoveStaticMembersAction(editor);
100         fMoveInstanceMethodAction= new MoveInstanceMethodAction(editor);
101         fReorgMoveAction= new ReorgMoveAction(editor.getEditorSite());
102         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.MOVE_ACTION);
103     }
104
105     /*
106      * @see ISelectionChangedListener#selectionChanged(SelectionChangedEvent)
107      */

108     public void selectionChanged(SelectionChangedEvent event) {
109         fMoveStaticMembersAction.selectionChanged(event);
110         fMoveInstanceMethodAction.selectionChanged(event);
111         fReorgMoveAction.selectionChanged(event);
112         setEnabled(computeEnableState());
113     }
114
115     /*
116      * @see org.eclipse.jdt.ui.actions.SelectionDispatchAction#run(org.eclipse.jface.viewers.IStructuredSelection)
117      */

118     public void run(IStructuredSelection selection) {
119         try {
120             if (fMoveInstanceMethodAction.isEnabled() && tryMoveInstanceMethod(selection))
121                 return;
122     
123             if (fMoveStaticMembersAction.isEnabled() && tryMoveStaticMembers(selection))
124                 return;
125     
126             if (fReorgMoveAction.isEnabled())
127                 fReorgMoveAction.run();
128         
129         } catch (JavaModelException e) {
130             ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception);
131         }
132
133     }
134
135     /*
136      * @see org.eclipse.jdt.ui.actions.SelectionDispatchAction#run(org.eclipse.jface.text.ITextSelection)
137      */

138     public void run(ITextSelection selection) {
139         try {
140             if (!ActionUtil.isEditable(fEditor))
141                 return;
142             if (fMoveStaticMembersAction.isEnabled() && tryMoveStaticMembers(selection))
143                 return;
144         
145             if (fMoveInstanceMethodAction.isEnabled() && tryMoveInstanceMethod(selection))
146                 return;
147     
148             if (tryReorgMove(selection))
149                 return;
150             
151             MessageDialog.openInformation(getShell(), RefactoringMessages.MoveAction_Move, RefactoringMessages.MoveAction_select);
152         } catch (JavaModelException e) {
153             ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception);
154         }
155     }
156
157     private boolean tryMoveStaticMembers(ITextSelection selection) throws JavaModelException {
158         IJavaElement element= SelectionConverter.getElementAtOffset(fEditor);
159         if (element == null || !(element instanceof IMember))
160             return false;
161         IMember[] array= new IMember[] { (IMember) element};
162         if (!RefactoringAvailabilityTester.isMoveStaticMembersAvailable(array))
163             return false;
164         fMoveStaticMembersAction.run(selection);
165         return true;
166     }
167
168     private static IMember[] getSelectedMembers(IStructuredSelection selection){
169         if (selection.isEmpty())
170             return null;
171         
172         for (Iterator JavaDoc iter= selection.iterator(); iter.hasNext(); ) {
173             if (! (iter.next() instanceof IMember))
174                 return null;
175         }
176         return convertToMemberArray(selection.toArray());
177     }
178
179     private static IMember[] convertToMemberArray(Object JavaDoc[] obj) {
180         if (obj == null)
181             return null;
182         Set JavaDoc memberSet= new HashSet JavaDoc();
183         memberSet.addAll(Arrays.asList(obj));
184         return (IMember[]) memberSet.toArray(new IMember[memberSet.size()]);
185     }
186
187     private boolean tryMoveStaticMembers(IStructuredSelection selection) throws JavaModelException {
188         IMember[] array= getSelectedMembers(selection);
189         if (!RefactoringAvailabilityTester.isMoveStaticMembersAvailable(array))
190             return false;
191         fMoveStaticMembersAction.run(selection);
192         return true;
193     }
194
195     private boolean tryMoveInstanceMethod(ITextSelection selection) throws JavaModelException {
196         IJavaElement element= SelectionConverter.getElementAtOffset(fEditor);
197         if (element == null || !(element instanceof IMethod))
198             return false;
199
200         IMethod method= (IMethod) element;
201         if (!RefactoringAvailabilityTester.isMoveMethodAvailable(method))
202             return false;
203         fMoveInstanceMethodAction.run(selection);
204         return true;
205     }
206
207     private boolean tryMoveInstanceMethod(IStructuredSelection selection) throws JavaModelException {
208         IMethod method= getSingleSelectedMethod(selection);
209         if (method == null)
210             return false;
211         if (!RefactoringAvailabilityTester.isMoveMethodAvailable(method))
212             return false;
213         fMoveInstanceMethodAction.run(selection);
214         return true;
215     }
216
217     private static IMethod getSingleSelectedMethod(IStructuredSelection selection) {
218         if (selection.isEmpty() || selection.size() != 1)
219             return null;
220         
221         Object JavaDoc first= selection.getFirstElement();
222         if (! (first instanceof IMethod))
223             return null;
224         return (IMethod) first;
225     }
226     
227
228     private boolean tryReorgMove(ITextSelection selection) throws JavaModelException{
229         IJavaElement element= SelectionConverter.getElementAtOffset(fEditor);
230         if (element == null)
231             return false;
232         StructuredSelection mockStructuredSelection= new StructuredSelection(element);
233         fReorgMoveAction.selectionChanged(mockStructuredSelection);
234         if (!fReorgMoveAction.isEnabled())
235             return false;
236             
237         fReorgMoveAction.run(mockStructuredSelection);
238         return true;
239     }
240
241
242     /*
243      * @see SelectionDispatchAction#update(ISelection)
244      */

245     public void update(ISelection selection) {
246         fMoveStaticMembersAction.update(selection);
247         fMoveInstanceMethodAction.update(selection);
248         fReorgMoveAction.update(selection);
249         setEnabled(computeEnableState());
250     }
251     
252     private boolean computeEnableState(){
253         return fMoveStaticMembersAction.isEnabled()
254                 || fMoveInstanceMethodAction.isEnabled()
255                 || fReorgMoveAction.isEnabled();
256     }
257 }
258
Popular Tags