KickJava   Java API By Example, From Geeks To Geeks.

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


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.ui.actions;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.commands.operations.IUndoContext;
18
19 import org.eclipse.core.runtime.PerformanceStats;
20
21 import org.eclipse.core.resources.ResourcesPlugin;
22
23 import org.eclipse.swt.events.MenuAdapter;
24 import org.eclipse.swt.events.MenuEvent;
25 import org.eclipse.swt.widgets.Menu;
26
27 import org.eclipse.jface.action.Action;
28 import org.eclipse.jface.action.IAction;
29 import org.eclipse.jface.action.IMenuListener;
30 import org.eclipse.jface.action.IMenuManager;
31 import org.eclipse.jface.action.MenuManager;
32 import org.eclipse.jface.action.Separator;
33 import org.eclipse.jface.commands.ActionHandler;
34 import org.eclipse.jface.viewers.ISelection;
35 import org.eclipse.jface.viewers.ISelectionChangedListener;
36 import org.eclipse.jface.viewers.ISelectionProvider;
37
38 import org.eclipse.jface.text.IDocument;
39 import org.eclipse.jface.text.ITextSelection;
40
41 import org.eclipse.ui.IActionBars;
42 import org.eclipse.ui.IEditorInput;
43 import org.eclipse.ui.IViewPart;
44 import org.eclipse.ui.IWorkbenchSite;
45 import org.eclipse.ui.actions.ActionFactory;
46 import org.eclipse.ui.actions.ActionGroup;
47 import org.eclipse.ui.handlers.IHandlerActivation;
48 import org.eclipse.ui.handlers.IHandlerService;
49 import org.eclipse.ui.operations.UndoRedoActionGroup;
50 import org.eclipse.ui.part.Page;
51
52 import org.eclipse.jdt.core.IJavaElement;
53
54 import org.eclipse.jdt.ui.IContextMenuConstants;
55
56 import org.eclipse.jdt.internal.ui.JavaPlugin;
57 import org.eclipse.jdt.internal.ui.actions.ActionMessages;
58 import org.eclipse.jdt.internal.ui.actions.ActionUtil;
59 import org.eclipse.jdt.internal.ui.actions.ExtractSuperClassAction;
60 import org.eclipse.jdt.internal.ui.actions.IntroduceParameterObjectAction;
61 import org.eclipse.jdt.internal.ui.actions.JDTQuickMenuAction;
62 import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
63 import org.eclipse.jdt.internal.ui.javaeditor.IClassFileEditorInput;
64 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
65 import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection;
66 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
67
68 /**
69  * Action group that adds refactor actions (for example 'Rename', 'Move')
70  * to a context menu and the global menu bar.
71  *
72  * <p>
73  * This class may be instantiated; it is not intended to be subclassed.
74  * </p>
75  *
76  * @since 2.0
77  */

78 public class RefactorActionGroup extends ActionGroup {
79     
80     private static final String JavaDoc PERF_REFACTOR_ACTION_GROUP= "org.eclipse.jdt.ui/perf/explorer/RefactorActionGroup"; //$NON-NLS-1$
81

82     /**
83      * Pop-up menu: id of the refactor sub menu (value <code>org.eclipse.jdt.ui.refactoring.menu</code>).
84      *
85      * @since 2.1
86      */

87     public static final String JavaDoc MENU_ID= "org.eclipse.jdt.ui.refactoring.menu"; //$NON-NLS-1$
88

89     /**
90      * Pop-up menu: id of the reorg group of the refactor sub menu (value
91      * <code>reorgGroup</code>).
92      *
93      * @since 2.1
94      */

95     public static final String JavaDoc GROUP_REORG= "reorgGroup"; //$NON-NLS-1$
96

97     /**
98      * Pop-up menu: id of the type group of the refactor sub menu (value
99      * <code>typeGroup</code>).
100      *
101      * @since 2.1
102      */

103     public static final String JavaDoc GROUP_TYPE= "typeGroup"; //$NON-NLS-1$
104

105     /**
106      * Pop-up menu: id of the coding group of the refactor sub menu (value
107      * <code>codingGroup</code>).
108      *
109      * @since 2.1
110      */

111     public static final String JavaDoc GROUP_CODING= "codingGroup"; //$NON-NLS-1$
112

113     /**
114      * Pop-up menu: id of the coding group 2 of the refactor sub menu (value
115      * <code>codingGroup2</code>).
116      *
117      * @since 3.2
118      */

119     public static final String JavaDoc GROUP_CODING2= "codingGroup2"; //$NON-NLS-1$
120

121     /**
122      * Pop-up menu: id of the reorg group 2 of the refactor sub menu (value
123      * <code>reorgGroup2</code>).
124      *
125      * @since 3.2
126      */

127     private static final String JavaDoc GROUP_REORG2= "reorgGroup2"; //$NON-NLS-1$ //TODO(3.3): make public
128

129     /**
130      * Pop-up menu: id of the type group 2 of the refactor sub menu (value
131      * <code>typeGroup2</code>).
132      *
133      * @since 3.2
134      */

135     private static final String JavaDoc GROUP_TYPE2= "typeGroup2"; //$NON-NLS-1$ //TODO(3.3): make public
136

137     private IWorkbenchSite fSite;
138     private JavaEditor fEditor;
139     private String JavaDoc fGroupName= IContextMenuConstants.GROUP_REORGANIZE;
140     private boolean fBinary= false;
141
142     private SelectionDispatchAction fMoveAction;
143     private SelectionDispatchAction fRenameAction;
144     private SelectionDispatchAction fModifyParametersAction;
145     private SelectionDispatchAction fConvertAnonymousToNestedAction;
146     private SelectionDispatchAction fConvertNestedToTopAction;
147     
148     private SelectionDispatchAction fPullUpAction;
149     private SelectionDispatchAction fPushDownAction;
150     private SelectionDispatchAction fExtractInterfaceAction;
151     private SelectionDispatchAction fExtractSupertypeAction;
152     private SelectionDispatchAction fChangeTypeAction;
153     private SelectionDispatchAction fUseSupertypeAction;
154     private SelectionDispatchAction fInferTypeArgumentsAction;
155     
156     private SelectionDispatchAction fInlineAction;
157 // private SelectionDispatchAction fReplaceInvocationsAction;
158
private SelectionDispatchAction fIntroduceIndirectionAction;
159     private SelectionDispatchAction fExtractMethodAction;
160     private SelectionDispatchAction fExtractTempAction;
161     private SelectionDispatchAction fExtractConstantAction;
162     private SelectionDispatchAction fIntroduceParameterAction;
163     private SelectionDispatchAction fIntroduceParameterObjectAction;
164     private SelectionDispatchAction fIntroduceFactoryAction;
165     private SelectionDispatchAction fConvertLocalToFieldAction;
166     private SelectionDispatchAction fSelfEncapsulateField;
167     
168     private UndoRedoActionGroup fUndoRedoActionGroup;
169     
170     private final List JavaDoc fActions= new ArrayList JavaDoc();
171     
172     private static final String JavaDoc QUICK_MENU_ID= "org.eclipse.jdt.ui.edit.text.java.refactor.quickMenu"; //$NON-NLS-1$
173

174     private class RefactorQuickAccessAction extends JDTQuickMenuAction {
175         public RefactorQuickAccessAction(JavaEditor editor) {
176             super(editor, QUICK_MENU_ID);
177         }
178         protected void fillMenu(IMenuManager menu) {
179             fillQuickMenu(menu);
180         }
181     }
182     
183     private JDTQuickMenuAction fQuickAccessAction;
184     private IHandlerActivation fQuickAccessHandlerActivation;
185     private IHandlerService fHandlerService;
186
187     private static class NoActionAvailable extends Action {
188         public NoActionAvailable() {
189             setEnabled(true);
190             setText(RefactoringMessages.RefactorActionGroup_no_refactoring_available);
191         }
192     }
193     private Action fNoActionAvailable= new NoActionAvailable();
194         
195     /**
196      * Creates a new <code>RefactorActionGroup</code>. The group requires
197      * that the selection provided by the part's selection provider is of type <code>
198      * org.eclipse.jface.viewers.IStructuredSelection</code>.
199      *
200      * @param part the view part that owns this action group
201      */

202     public RefactorActionGroup(IViewPart part) {
203         this(part.getSite());
204         
205         IUndoContext workspaceContext= (IUndoContext)ResourcesPlugin.getWorkspace().getAdapter(IUndoContext.class);
206         fUndoRedoActionGroup= new UndoRedoActionGroup(part.getViewSite(), workspaceContext, true);
207     }
208     
209     /**
210      * Creates a new <code>RefactorActionGroup</code>. The action requires
211      * that the selection provided by the page's selection provider is of type <code>
212      * org.eclipse.jface.viewers.IStructuredSelection</code>.
213      *
214      * @param page the page that owns this action group
215      */

216     public RefactorActionGroup(Page page) {
217         this(page.getSite());
218     }
219     
220     /**
221      * Note: This constructor is for internal use only. Clients should not call this constructor.
222      * @param editor the java editor
223      * @param groupName the group name to add the actions to
224      * @param binary <code>true</code> if the action group is used in a binary environment, <code>false</code> otherwise
225      */

226     public RefactorActionGroup(JavaEditor editor, String JavaDoc groupName, boolean binary) {
227
228         final PerformanceStats stats= PerformanceStats.getStats(PERF_REFACTOR_ACTION_GROUP, this);
229         stats.startRun();
230
231         fSite= editor.getEditorSite();
232         fEditor= editor;
233         fGroupName= groupName;
234         fBinary= binary;
235
236         ISelectionProvider provider= editor.getSelectionProvider();
237         ISelection selection= provider.getSelection();
238         
239         if (!fBinary) {
240             fRenameAction= new RenameAction(editor);
241             initAction(fRenameAction, selection, IJavaEditorActionDefinitionIds.RENAME_ELEMENT);
242             editor.setAction("RenameElement", fRenameAction); //$NON-NLS-1$
243

244             fMoveAction= new MoveAction(editor);
245             initAction(fMoveAction, selection, IJavaEditorActionDefinitionIds.MOVE_ELEMENT);
246             editor.setAction("MoveElement", fMoveAction); //$NON-NLS-1$
247

248             fModifyParametersAction= new ModifyParametersAction(editor);
249             initAction(fModifyParametersAction, selection, IJavaEditorActionDefinitionIds.MODIFY_METHOD_PARAMETERS);
250             editor.setAction("ModifyParameters", fModifyParametersAction); //$NON-NLS-1$
251

252             fConvertAnonymousToNestedAction= new ConvertAnonymousToNestedAction(editor);
253             initUpdatingAction(fConvertAnonymousToNestedAction, provider, selection, IJavaEditorActionDefinitionIds.CONVERT_ANONYMOUS_TO_NESTED);
254             editor.setAction("ConvertAnonymousToNested", fConvertAnonymousToNestedAction); //$NON-NLS-1$
255

256             fConvertNestedToTopAction= new ConvertNestedToTopAction(editor);
257             initAction(fConvertNestedToTopAction, selection, IJavaEditorActionDefinitionIds.MOVE_INNER_TO_TOP);
258             editor.setAction("MoveInnerToTop", fConvertNestedToTopAction); //$NON-NLS-1$
259

260             fPullUpAction= new PullUpAction(editor);
261             initAction(fPullUpAction, selection, IJavaEditorActionDefinitionIds.PULL_UP);
262             editor.setAction("PullUp", fPullUpAction); //$NON-NLS-1$
263

264             fPushDownAction= new PushDownAction(editor);
265             initAction(fPushDownAction, selection, IJavaEditorActionDefinitionIds.PUSH_DOWN);
266             editor.setAction("PushDown", fPushDownAction); //$NON-NLS-1$
267

268             fExtractSupertypeAction= new ExtractSuperClassAction(editor);
269             initAction(fExtractSupertypeAction, selection, ExtractSuperClassAction.EXTRACT_SUPERTYPE);
270             editor.setAction("ExtractSupertype", fExtractSupertypeAction); //$NON-NLS-1$
271

272             fExtractInterfaceAction= new ExtractInterfaceAction(editor);
273             initAction(fExtractInterfaceAction, selection, IJavaEditorActionDefinitionIds.EXTRACT_INTERFACE);
274             editor.setAction("ExtractInterface", fExtractInterfaceAction); //$NON-NLS-1$
275

276             fChangeTypeAction= new ChangeTypeAction(editor);
277             initUpdatingAction(fChangeTypeAction, provider, selection, IJavaEditorActionDefinitionIds.CHANGE_TYPE);
278             editor.setAction("ChangeType", fChangeTypeAction); //$NON-NLS-1$
279

280             fInferTypeArgumentsAction= new InferTypeArgumentsAction(editor);
281             initAction(fInferTypeArgumentsAction, selection, IJavaEditorActionDefinitionIds.INFER_TYPE_ARGUMENTS_ACTION);
282             editor.setAction("InferTypeArguments", fInferTypeArgumentsAction); //$NON-NLS-1$
283

284             fExtractMethodAction= new ExtractMethodAction(editor);
285             initUpdatingAction(fExtractMethodAction, provider, selection, IJavaEditorActionDefinitionIds.EXTRACT_METHOD);
286             editor.setAction("ExtractMethod", fExtractMethodAction); //$NON-NLS-1$
287

288             fExtractTempAction= new ExtractTempAction(editor);
289             initUpdatingAction(fExtractTempAction, provider, selection, IJavaEditorActionDefinitionIds.EXTRACT_LOCAL_VARIABLE);
290             editor.setAction("ExtractLocalVariable", fExtractTempAction); //$NON-NLS-1$
291

292             fExtractConstantAction= new ExtractConstantAction(editor);
293             initUpdatingAction(fExtractConstantAction, provider, selection, IJavaEditorActionDefinitionIds.EXTRACT_CONSTANT);
294             editor.setAction("ExtractConstant", fExtractConstantAction); //$NON-NLS-1$
295

296             fIntroduceParameterAction= new IntroduceParameterAction(editor);
297             initUpdatingAction(fIntroduceParameterAction, provider, selection, IJavaEditorActionDefinitionIds.INTRODUCE_PARAMETER);
298             editor.setAction("IntroduceParameter", fIntroduceParameterAction); //$NON-NLS-1$
299

300             fIntroduceFactoryAction= new IntroduceFactoryAction(editor);
301             initUpdatingAction(fIntroduceFactoryAction, provider, selection, IJavaEditorActionDefinitionIds.INTRODUCE_FACTORY);
302             editor.setAction("IntroduceFactory", fIntroduceFactoryAction); //$NON-NLS-1$
303

304             fConvertLocalToFieldAction= new ConvertLocalToFieldAction(editor);
305             initUpdatingAction(fConvertLocalToFieldAction, provider, selection, IJavaEditorActionDefinitionIds.PROMOTE_LOCAL_VARIABLE);
306             editor.setAction("PromoteTemp", fConvertLocalToFieldAction); //$NON-NLS-1$
307

308             fSelfEncapsulateField= new SelfEncapsulateFieldAction(editor);
309             initAction(fSelfEncapsulateField, selection, IJavaEditorActionDefinitionIds.SELF_ENCAPSULATE_FIELD);
310             editor.setAction("SelfEncapsulateField", fSelfEncapsulateField); //$NON-NLS-1$
311

312             fIntroduceParameterObjectAction= new IntroduceParameterObjectAction(editor);
313             initAction(fIntroduceParameterObjectAction, selection, IntroduceParameterObjectAction.ACTION_DEFINITION_ID);//TODO Place in IJavaEditorActionDefinitionIds
314
editor.setAction("IntroduceParameterObjectAction", fIntroduceParameterObjectAction); //$NON-NLS-1$
315
}
316         fIntroduceIndirectionAction= new IntroduceIndirectionAction(editor);
317         initUpdatingAction(fIntroduceIndirectionAction, provider, selection, IJavaEditorActionDefinitionIds.INTRODUCE_INDIRECTION);
318         editor.setAction("IntroduceIndirection", fIntroduceIndirectionAction); //$NON-NLS-1$
319

320         fUseSupertypeAction= new UseSupertypeAction(editor);
321         initAction(fUseSupertypeAction, selection, IJavaEditorActionDefinitionIds.USE_SUPERTYPE);
322         editor.setAction("UseSupertype", fUseSupertypeAction); //$NON-NLS-1$
323

324         fInlineAction= new InlineAction(editor);
325         initAction(fInlineAction, selection, IJavaEditorActionDefinitionIds.INLINE);
326         editor.setAction("Inline", fInlineAction); //$NON-NLS-1$
327

328         installQuickAccessAction();
329         
330         stats.endRun();
331     }
332
333     private RefactorActionGroup(IWorkbenchSite site) {
334
335         final PerformanceStats stats= PerformanceStats.getStats(PERF_REFACTOR_ACTION_GROUP, this);
336         stats.startRun();
337
338         fSite= site;
339         ISelectionProvider provider= fSite.getSelectionProvider();
340         ISelection selection= provider.getSelection();
341         
342         if (!fBinary) {
343             
344             fMoveAction= new MoveAction(site);
345             initUpdatingAction(fMoveAction, provider, selection, IJavaEditorActionDefinitionIds.MOVE_ELEMENT);
346     
347             fRenameAction= new RenameAction(site);
348             initUpdatingAction(fRenameAction, provider, selection, IJavaEditorActionDefinitionIds.RENAME_ELEMENT);
349     
350             fModifyParametersAction= new ModifyParametersAction(fSite);
351             initUpdatingAction(fModifyParametersAction, provider, selection, IJavaEditorActionDefinitionIds.MODIFY_METHOD_PARAMETERS);
352     
353             fPullUpAction= new PullUpAction(fSite);
354             initUpdatingAction(fPullUpAction, provider, selection, IJavaEditorActionDefinitionIds.PULL_UP);
355     
356             fPushDownAction= new PushDownAction(fSite);
357             initUpdatingAction(fPushDownAction, provider, selection, IJavaEditorActionDefinitionIds.PUSH_DOWN);
358         
359             fSelfEncapsulateField= new SelfEncapsulateFieldAction(fSite);
360             initUpdatingAction(fSelfEncapsulateField, provider, selection, IJavaEditorActionDefinitionIds.SELF_ENCAPSULATE_FIELD);
361     
362             fIntroduceParameterObjectAction= new IntroduceParameterObjectAction(fSite);
363             initUpdatingAction(fIntroduceParameterObjectAction, provider, selection, IntroduceParameterObjectAction.ACTION_DEFINITION_ID);
364             
365             fExtractSupertypeAction= new ExtractSuperClassAction(fSite);
366             initUpdatingAction(fExtractSupertypeAction, provider, selection, ExtractSuperClassAction.EXTRACT_SUPERTYPE);
367     
368             fExtractInterfaceAction= new ExtractInterfaceAction(fSite);
369             initUpdatingAction(fExtractInterfaceAction, provider, selection, IJavaEditorActionDefinitionIds.EXTRACT_INTERFACE);
370     
371             fChangeTypeAction= new ChangeTypeAction(fSite);
372             initUpdatingAction(fChangeTypeAction, provider, selection, IJavaEditorActionDefinitionIds.CHANGE_TYPE);
373     
374             fConvertNestedToTopAction= new ConvertNestedToTopAction(fSite);
375             initUpdatingAction(fConvertNestedToTopAction, provider, selection, IJavaEditorActionDefinitionIds.MOVE_INNER_TO_TOP);
376     
377             fInferTypeArgumentsAction= new InferTypeArgumentsAction(fSite);
378             initUpdatingAction(fInferTypeArgumentsAction, provider, selection, IJavaEditorActionDefinitionIds.INFER_TYPE_ARGUMENTS_ACTION);
379     
380             fIntroduceFactoryAction= new IntroduceFactoryAction(fSite);
381             initUpdatingAction(fIntroduceFactoryAction, provider, selection, IJavaEditorActionDefinitionIds.INTRODUCE_FACTORY);
382     
383             fConvertAnonymousToNestedAction= new ConvertAnonymousToNestedAction(fSite);
384             initUpdatingAction(fConvertAnonymousToNestedAction, provider, selection, IJavaEditorActionDefinitionIds.CONVERT_ANONYMOUS_TO_NESTED);
385         }
386         fInlineAction= new InlineAction(fSite);
387         initUpdatingAction(fInlineAction, provider, selection, IJavaEditorActionDefinitionIds.INLINE);
388
389         fUseSupertypeAction= new UseSupertypeAction(fSite);
390         initUpdatingAction(fUseSupertypeAction, provider, selection, IJavaEditorActionDefinitionIds.USE_SUPERTYPE);
391         
392         fIntroduceIndirectionAction= new IntroduceIndirectionAction(fSite);
393         initUpdatingAction(fIntroduceIndirectionAction, provider, selection, IJavaEditorActionDefinitionIds.INTRODUCE_INDIRECTION);
394         
395         installQuickAccessAction();
396         
397         stats.endRun();
398     }
399     
400     private void installQuickAccessAction() {
401         fHandlerService= (IHandlerService)fSite.getService(IHandlerService.class);
402         if (fHandlerService != null) {
403             fQuickAccessAction= new RefactorQuickAccessAction(fEditor);
404             fQuickAccessHandlerActivation= fHandlerService.activateHandler(fQuickAccessAction.getActionDefinitionId(), new ActionHandler(fQuickAccessAction));
405         }
406     }
407     
408     private void initAction(SelectionDispatchAction action, ISelection selection, String JavaDoc actionDefinitionId){
409         initUpdatingAction(action, null, selection, actionDefinitionId);
410     }
411     
412     /**
413      * Sets actionDefinitionId, updates enablement, adds to fActions,
414      * and adds selection changed listener if provider is not <code>null</code>.
415      *
416      * @param action
417      * @param provider can be <code>null</code>
418      * @param selection
419      * @param actionDefinitionId
420      */

421     private void initUpdatingAction(SelectionDispatchAction action, ISelectionProvider provider, ISelection selection, String JavaDoc actionDefinitionId){
422         action.setActionDefinitionId(actionDefinitionId);
423         action.update(selection);
424         if (provider != null)
425             provider.addSelectionChangedListener(action);
426         fActions.add(action);
427     }
428     
429     /* (non-Javadoc)
430      * Method declared in ActionGroup
431      */

432     public void fillActionBars(IActionBars actionBars) {
433         super.fillActionBars(actionBars);
434         if (!fBinary) {
435             actionBars.setGlobalActionHandler(JdtActionConstants.SELF_ENCAPSULATE_FIELD, fSelfEncapsulateField);
436             actionBars.setGlobalActionHandler(JdtActionConstants.MOVE, fMoveAction);
437             actionBars.setGlobalActionHandler(JdtActionConstants.RENAME, fRenameAction);
438             actionBars.setGlobalActionHandler(JdtActionConstants.MODIFY_PARAMETERS, fModifyParametersAction);
439             actionBars.setGlobalActionHandler(JdtActionConstants.PULL_UP, fPullUpAction);
440             actionBars.setGlobalActionHandler(JdtActionConstants.PUSH_DOWN, fPushDownAction);
441             actionBars.setGlobalActionHandler(JdtActionConstants.EXTRACT_TEMP, fExtractTempAction);
442             actionBars.setGlobalActionHandler(JdtActionConstants.EXTRACT_CONSTANT, fExtractConstantAction);
443             actionBars.setGlobalActionHandler(JdtActionConstants.INTRODUCE_PARAMETER, fIntroduceParameterAction);
444             actionBars.setGlobalActionHandler(JdtActionConstants.INTRODUCE_FACTORY, fIntroduceFactoryAction);
445             actionBars.setGlobalActionHandler(JdtActionConstants.EXTRACT_METHOD, fExtractMethodAction);
446             // actionBars.setGlobalActionHandler(JdtActionConstants.REPLACE_INVOCATIONS, fReplaceInvocationsAction);
447
actionBars.setGlobalActionHandler(JdtActionConstants.EXTRACT_INTERFACE, fExtractInterfaceAction);
448             actionBars.setGlobalActionHandler(ExtractSuperClassAction.EXTRACT_SUPERTYPES, fExtractSupertypeAction);
449             actionBars.setGlobalActionHandler(JdtActionConstants.CHANGE_TYPE, fChangeTypeAction);
450             actionBars.setGlobalActionHandler(JdtActionConstants.CONVERT_NESTED_TO_TOP, fConvertNestedToTopAction);
451             actionBars.setGlobalActionHandler(JdtActionConstants.INFER_TYPE_ARGUMENTS, fInferTypeArgumentsAction);
452             actionBars.setGlobalActionHandler(JdtActionConstants.CONVERT_LOCAL_TO_FIELD, fConvertLocalToFieldAction);
453             actionBars.setGlobalActionHandler(JdtActionConstants.CONVERT_ANONYMOUS_TO_NESTED, fConvertAnonymousToNestedAction);
454             actionBars.setGlobalActionHandler(IntroduceParameterObjectAction.ACTION_ID, fIntroduceParameterObjectAction); //TODO promote action ID to API
455
}
456         actionBars.setGlobalActionHandler(JdtActionConstants.INLINE, fInlineAction);
457         actionBars.setGlobalActionHandler(JdtActionConstants.USE_SUPERTYPE, fUseSupertypeAction);
458         actionBars.setGlobalActionHandler(JdtActionConstants.INTRODUCE_INDIRECTION, fIntroduceIndirectionAction);
459         if (fUndoRedoActionGroup != null) {
460             fUndoRedoActionGroup.fillActionBars(actionBars);
461         }
462     }
463     
464     /**
465      * Retargets the File actions with the corresponding refactoring actions.
466      *
467      * @param actionBars the action bar to register the move and rename action with
468      */

469     public void retargetFileMenuActions(IActionBars actionBars) {
470         actionBars.setGlobalActionHandler(ActionFactory.RENAME.getId(), fRenameAction);
471         actionBars.setGlobalActionHandler(ActionFactory.MOVE.getId(), fMoveAction);
472     }
473     
474     /* (non-Javadoc)
475      * Method declared in ActionGroup
476      */

477     public void fillContextMenu(IMenuManager menu) {
478         super.fillContextMenu(menu);
479         addRefactorSubmenu(menu);
480     }
481     
482     /*
483      * @see ActionGroup#dispose()
484      */

485     public void dispose() {
486         ISelectionProvider provider= fSite.getSelectionProvider();
487         if (!fBinary) {
488             disposeAction(fSelfEncapsulateField, provider);
489             disposeAction(fMoveAction, provider);
490             disposeAction(fRenameAction, provider);
491             disposeAction(fModifyParametersAction, provider);
492             disposeAction(fPullUpAction, provider);
493             disposeAction(fPushDownAction, provider);
494             disposeAction(fExtractTempAction, provider);
495             disposeAction(fExtractConstantAction, provider);
496             disposeAction(fIntroduceParameterAction, provider);
497             disposeAction(fIntroduceParameterObjectAction, provider);
498             disposeAction(fIntroduceFactoryAction, provider);
499             disposeAction(fExtractMethodAction, provider);
500             // disposeAction(fReplaceInvocationsAction, provider);
501
disposeAction(fExtractInterfaceAction, provider);
502             disposeAction(fExtractSupertypeAction, provider);
503             disposeAction(fChangeTypeAction, provider);
504             disposeAction(fConvertNestedToTopAction, provider);
505             disposeAction(fInferTypeArgumentsAction, provider);
506             disposeAction(fConvertLocalToFieldAction, provider);
507             disposeAction(fConvertAnonymousToNestedAction, provider);
508         }
509         disposeAction(fIntroduceIndirectionAction, provider);
510         disposeAction(fInlineAction, provider);
511         disposeAction(fUseSupertypeAction, provider);
512         if (fQuickAccessHandlerActivation != null && fHandlerService != null) {
513             fHandlerService.deactivateHandler(fQuickAccessHandlerActivation);
514         }
515         if (fUndoRedoActionGroup != null) {
516             fUndoRedoActionGroup.dispose();
517         }
518         super.dispose();
519     }
520     
521     private void disposeAction(ISelectionChangedListener action, ISelectionProvider provider) {
522         if (action != null)
523             provider.removeSelectionChangedListener(action);
524     }
525     
526     private void addRefactorSubmenu(IMenuManager menu) {
527         String JavaDoc menuText= ActionMessages.RefactorMenu_label;
528         if (fQuickAccessAction != null) {
529             menuText= fQuickAccessAction.addShortcut(menuText);
530         }
531         IMenuManager refactorSubmenu= new MenuManager(menuText, MENU_ID);
532         if (fEditor != null) {
533             IJavaElement element= SelectionConverter.getInput(fEditor);
534             if (element != null && ActionUtil.isOnBuildPath(element)) {
535                 refactorSubmenu.addMenuListener(new IMenuListener() {
536                     public void menuAboutToShow(IMenuManager manager) {
537                         refactorMenuShown(manager);
538                     }
539                 });
540                 refactorSubmenu.add(fNoActionAvailable);
541                 menu.appendToGroup(fGroupName, refactorSubmenu);
542             }
543         } else {
544             ISelection selection= fSite.getSelectionProvider().getSelection();
545             for (Iterator JavaDoc iter= fActions.iterator(); iter.hasNext(); ) {
546                 ((SelectionDispatchAction)iter.next()).update(selection);
547             }
548             if (fillRefactorMenu(refactorSubmenu) > 0)
549                 menu.appendToGroup(fGroupName, refactorSubmenu);
550         }
551     }
552     
553     private int fillRefactorMenu(IMenuManager refactorSubmenu) {
554         int added= 0;
555         refactorSubmenu.add(new Separator(GROUP_REORG));
556         added+= addAction(refactorSubmenu, fRenameAction);
557         added+= addAction(refactorSubmenu, fMoveAction);
558         refactorSubmenu.add(new Separator(GROUP_CODING));
559         added+= addAction(refactorSubmenu, fModifyParametersAction);
560         added+= addAction(refactorSubmenu, fExtractMethodAction);
561         added+= addAction(refactorSubmenu, fExtractTempAction);
562         added+= addAction(refactorSubmenu, fExtractConstantAction);
563         added+= addAction(refactorSubmenu, fInlineAction);
564         refactorSubmenu.add(new Separator(GROUP_REORG2));
565         added+= addAction(refactorSubmenu, fConvertAnonymousToNestedAction);
566         added+= addAction(refactorSubmenu, fConvertNestedToTopAction);
567         added+= addAction(refactorSubmenu, fConvertLocalToFieldAction);
568         refactorSubmenu.add(new Separator(GROUP_TYPE));
569         added+= addAction(refactorSubmenu, fExtractInterfaceAction);
570         added+= addAction(refactorSubmenu, fExtractSupertypeAction);
571         added+= addAction(refactorSubmenu, fUseSupertypeAction);
572         added+= addAction(refactorSubmenu, fPullUpAction);
573         added+= addAction(refactorSubmenu, fPushDownAction);
574         refactorSubmenu.add(new Separator(GROUP_CODING2));
575         added+= addAction(refactorSubmenu, fIntroduceIndirectionAction);
576         added+= addAction(refactorSubmenu, fIntroduceFactoryAction);
577         added+= addAction(refactorSubmenu, fIntroduceParameterAction);
578         added+= addAction(refactorSubmenu, fIntroduceParameterObjectAction);
579         added+= addAction(refactorSubmenu, fSelfEncapsulateField);
580 // added+= addAction(refactorSubmenu, fReplaceInvocationsAction);
581
refactorSubmenu.add(new Separator(GROUP_TYPE2));
582         added+= addAction(refactorSubmenu, fChangeTypeAction);
583         added+= addAction(refactorSubmenu, fInferTypeArgumentsAction);
584         return added;
585     }
586     
587     private int addAction(IMenuManager menu, IAction action) {
588         if (action != null && action.isEnabled()) {
589             menu.add(action);
590             return 1;
591         }
592         return 0;
593     }
594     
595     private void refactorMenuShown(final IMenuManager refactorSubmenu) {
596         // we know that we have an MenuManager since we created it in
597
// addRefactorSubmenu.
598
Menu menu= ((MenuManager)refactorSubmenu).getMenu();
599         menu.addMenuListener(new MenuAdapter() {
600             public void menuHidden(MenuEvent e) {
601                 refactorMenuHidden(refactorSubmenu);
602             }
603         });
604         ITextSelection textSelection= (ITextSelection)fEditor.getSelectionProvider().getSelection();
605         JavaTextSelection javaSelection= new JavaTextSelection(
606             getEditorInput(), getDocument(), textSelection.getOffset(), textSelection.getLength());
607         
608         for (Iterator JavaDoc iter= fActions.iterator(); iter.hasNext(); ) {
609             SelectionDispatchAction action= (SelectionDispatchAction)iter.next();
610             action.update(javaSelection);
611         }
612         refactorSubmenu.removeAll();
613         if (fillRefactorMenu(refactorSubmenu) == 0)
614             refactorSubmenu.add(fNoActionAvailable);
615     }
616     
617     private void refactorMenuHidden(IMenuManager manager) {
618         ITextSelection textSelection= (ITextSelection)fEditor.getSelectionProvider().getSelection();
619         for (Iterator JavaDoc iter= fActions.iterator(); iter.hasNext(); ) {
620             SelectionDispatchAction action= (SelectionDispatchAction)iter.next();
621             action.update(textSelection);
622         }
623     }
624     
625     private IJavaElement getEditorInput() {
626         final IEditorInput input= fEditor.getEditorInput();
627         if (input instanceof IClassFileEditorInput) {
628             IClassFileEditorInput extended= (IClassFileEditorInput) input;
629             return extended.getClassFile();
630         }
631         return JavaPlugin.getDefault().getWorkingCopyManager().getWorkingCopy(input);
632     }
633     
634     private IDocument getDocument() {
635         return JavaPlugin.getDefault().getCompilationUnitDocumentProvider().
636             getDocument(fEditor.getEditorInput());
637     }
638     
639     private void fillQuickMenu(IMenuManager menu) {
640         if (fEditor != null) {
641             IJavaElement element= SelectionConverter.getInput(fEditor);
642             if (element == null || !ActionUtil.isOnBuildPath(element)) {
643                 menu.add(fNoActionAvailable);
644                 return;
645             }
646             ITextSelection textSelection= (ITextSelection)fEditor.getSelectionProvider().getSelection();
647             JavaTextSelection javaSelection= new JavaTextSelection(
648                 getEditorInput(), getDocument(), textSelection.getOffset(), textSelection.getLength());
649             
650             for (Iterator JavaDoc iter= fActions.iterator(); iter.hasNext(); ) {
651                 ((SelectionDispatchAction)iter.next()).update(javaSelection);
652             }
653             fillRefactorMenu(menu);
654             for (Iterator JavaDoc iter= fActions.iterator(); iter.hasNext(); ) {
655                 ((SelectionDispatchAction)iter.next()).update(textSelection);
656             }
657             
658         } else {
659             ISelection selection= fSite.getSelectionProvider().getSelection();
660             for (Iterator JavaDoc iter= fActions.iterator(); iter.hasNext(); ) {
661                 ((SelectionDispatchAction)iter.next()).update(selection);
662             }
663             fillRefactorMenu(menu);
664         }
665     }
666 }
667
Popular Tags