KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > javaeditor > BasicCompilationUnitEditorActionContributor


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.internal.ui.javaeditor;
12
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collection JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18
19 import org.eclipse.jface.action.IAction;
20 import org.eclipse.jface.action.IMenuListener;
21 import org.eclipse.jface.action.IMenuManager;
22 import org.eclipse.jface.action.MenuManager;
23 import org.eclipse.jface.action.Separator;
24
25 import org.eclipse.ui.IActionBars;
26 import org.eclipse.ui.IEditorPart;
27 import org.eclipse.ui.IWorkbenchActionConstants;
28 import org.eclipse.ui.IWorkbenchPage;
29 import org.eclipse.ui.actions.RetargetAction;
30 import org.eclipse.ui.texteditor.ITextEditor;
31 import org.eclipse.ui.texteditor.ITextEditorActionConstants;
32 import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
33 import org.eclipse.ui.texteditor.RetargetTextEditorAction;
34
35 import org.eclipse.ui.ide.IDEActionFactory;
36
37 import org.eclipse.jdt.ui.actions.JdtActionConstants;
38
39 import org.eclipse.jdt.internal.ui.JavaPluginImages;
40 import org.eclipse.jdt.internal.ui.text.java.CompletionProposalCategory;
41 import org.eclipse.jdt.internal.ui.text.java.CompletionProposalComputerRegistry;
42
43
44 public class BasicCompilationUnitEditorActionContributor extends BasicJavaEditorActionContributor {
45
46     /**
47      * A menu listener that can remove itself from the menu it listens to.
48      * @since 3.2
49      */

50     private final class MenuListener implements IMenuListener {
51         private final IMenuManager fMenu;
52
53         public MenuListener(IMenuManager menu) {
54             fMenu= menu;
55         }
56
57         public void menuAboutToShow(IMenuManager manager) {
58             for (int i= 0; i < fSpecificAssistActions.length; i++) {
59                 fSpecificAssistActions[i].update();
60             }
61         }
62         
63         public void dispose() {
64             fMenu.removeMenuListener(this);
65         }
66     }
67
68     protected RetargetAction fRetargetContentAssist;
69     protected RetargetTextEditorAction fContentAssist;
70     protected RetargetTextEditorAction fContextInformation;
71     protected RetargetTextEditorAction fQuickAssistAction;
72     protected RetargetTextEditorAction fChangeEncodingAction;
73     
74     /* @since 3.2 */
75     protected SpecificContentAssistAction[] fSpecificAssistActions;
76     /* @since 3.2 */
77     private MenuListener fContentAssistMenuListener;
78
79
80     public BasicCompilationUnitEditorActionContributor() {
81
82         fRetargetContentAssist= new RetargetAction(JdtActionConstants.CONTENT_ASSIST, JavaEditorMessages.ContentAssistProposal_label);
83         fRetargetContentAssist.setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
84         fRetargetContentAssist.setImageDescriptor(JavaPluginImages.DESC_ELCL_CODE_ASSIST);
85         fRetargetContentAssist.setDisabledImageDescriptor(JavaPluginImages.DESC_DLCL_CODE_ASSIST);
86         markAsPartListener(fRetargetContentAssist);
87
88         fContentAssist= new RetargetTextEditorAction(JavaEditorMessages.getBundleForConstructedKeys(), "ContentAssistProposal."); //$NON-NLS-1$
89
fContentAssist.setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
90         fContentAssist.setImageDescriptor(JavaPluginImages.DESC_ELCL_CODE_ASSIST);
91         fContentAssist.setDisabledImageDescriptor(JavaPluginImages.DESC_DLCL_CODE_ASSIST);
92
93         fContextInformation= new RetargetTextEditorAction(JavaEditorMessages.getBundleForConstructedKeys(), "ContentAssistContextInformation."); //$NON-NLS-1$
94
fContextInformation.setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_CONTEXT_INFORMATION);
95
96         fQuickAssistAction= new RetargetTextEditorAction(JavaEditorMessages.getBundleForConstructedKeys(), "CorrectionAssistProposal."); //$NON-NLS-1$
97
fQuickAssistAction.setActionDefinitionId(ITextEditorActionDefinitionIds.QUICK_ASSIST);
98
99         fChangeEncodingAction= new RetargetTextEditorAction(JavaEditorMessages.getBundleForConstructedKeys(), "Editor.ChangeEncodingAction."); //$NON-NLS-1$
100
}
101
102     /*
103      * @see EditorActionBarContributor#contributeToMenu(IMenuManager)
104      */

105     public void contributeToMenu(IMenuManager menu) {
106
107         super.contributeToMenu(menu);
108         if (fContentAssistMenuListener != null)
109             fContentAssistMenuListener.dispose();
110
111         IMenuManager editMenu= menu.findMenuUsingPath(IWorkbenchActionConstants.M_EDIT);
112         if (editMenu != null) {
113             editMenu.add(fChangeEncodingAction);
114             IMenuManager caMenu= new MenuManager(JavaEditorMessages.BasicEditorActionContributor_specific_content_assist_menu, "specific_content_assist"); //$NON-NLS-1$
115
editMenu.insertAfter(ITextEditorActionConstants.GROUP_ASSIST, caMenu);
116             
117             caMenu.add(fRetargetContentAssist);
118             Collection JavaDoc descriptors= CompletionProposalComputerRegistry.getDefault().getProposalCategories();
119             List JavaDoc specificAssistActions= new ArrayList JavaDoc(descriptors.size());
120             for (Iterator JavaDoc it= descriptors.iterator(); it.hasNext();) {
121                 final CompletionProposalCategory cat= (CompletionProposalCategory) it.next();
122                 if (cat.hasComputers()) {
123                     IAction caAction= new SpecificContentAssistAction(cat);
124                     caMenu.add(caAction);
125                     specificAssistActions.add(caAction);
126                 }
127             }
128             fSpecificAssistActions= (SpecificContentAssistAction[]) specificAssistActions.toArray(new SpecificContentAssistAction[specificAssistActions.size()]);
129             if (fSpecificAssistActions.length > 0) {
130                 fContentAssistMenuListener= new MenuListener(caMenu);
131                 caMenu.addMenuListener(fContentAssistMenuListener);
132             }
133             caMenu.add(new Separator("context_info")); //$NON-NLS-1$
134
caMenu.add(fContextInformation);
135             
136             editMenu.appendToGroup(ITextEditorActionConstants.GROUP_ASSIST, fQuickAssistAction);
137         }
138     }
139
140     /*
141      * @see IEditorActionBarContributor#setActiveEditor(IEditorPart)
142      */

143     public void setActiveEditor(IEditorPart part) {
144         super.setActiveEditor(part);
145
146         ITextEditor textEditor= null;
147         if (part instanceof ITextEditor)
148             textEditor= (ITextEditor) part;
149
150         fContentAssist.setAction(getAction(textEditor, "ContentAssistProposal")); //$NON-NLS-1$
151
fContextInformation.setAction(getAction(textEditor, "ContentAssistContextInformation")); //$NON-NLS-1$
152
fQuickAssistAction.setAction(getAction(textEditor, ITextEditorActionConstants.QUICK_ASSIST));
153         
154         if (fSpecificAssistActions != null) {
155             for (int i= 0; i < fSpecificAssistActions.length; i++) {
156                 SpecificContentAssistAction assistAction= fSpecificAssistActions[i];
157                 assistAction.setActiveEditor(part);
158             }
159         }
160
161         fChangeEncodingAction.setAction(getAction(textEditor, ITextEditorActionConstants.CHANGE_ENCODING));
162
163         IActionBars actionBars= getActionBars();
164         actionBars.setGlobalActionHandler(JdtActionConstants.SHIFT_RIGHT, getAction(textEditor, "ShiftRight")); //$NON-NLS-1$
165
actionBars.setGlobalActionHandler(JdtActionConstants.SHIFT_LEFT, getAction(textEditor, "ShiftLeft")); //$NON-NLS-1$
166

167         actionBars.setGlobalActionHandler(IDEActionFactory.ADD_TASK.getId(), getAction(textEditor, IDEActionFactory.ADD_TASK.getId()));
168         actionBars.setGlobalActionHandler(IDEActionFactory.BOOKMARK.getId(), getAction(textEditor, IDEActionFactory.BOOKMARK.getId()));
169     }
170
171     /*
172      * @see IEditorActionBarContributor#init(IActionBars, IWorkbenchPage)
173      */

174     public void init(IActionBars bars, IWorkbenchPage page) {
175         super.init(bars, page);
176         // register actions that have a dynamic editor.
177
bars.setGlobalActionHandler(JdtActionConstants.CONTENT_ASSIST, fContentAssist);
178     }
179     
180     /*
181      * @see org.eclipse.jdt.internal.ui.javaeditor.BasicJavaEditorActionContributor#dispose()
182      * @since 3.1
183      */

184     public void dispose() {
185         if (fRetargetContentAssist != null) {
186             fRetargetContentAssist.dispose();
187             fRetargetContentAssist= null;
188         }
189         if (fContentAssistMenuListener != null) {
190             fContentAssistMenuListener.dispose();
191             fContentAssistMenuListener= null;
192         }
193         super.dispose();
194     }
195 }
196
Popular Tags