KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > PDEFormTextEditorContributor


1 /*******************************************************************************
2  * Copyright (c) 2006, 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.pde.internal.ui.editor;
12
13 import java.util.Iterator JavaDoc;
14 import java.util.Map JavaDoc;
15 import java.util.Set JavaDoc;
16
17 import org.eclipse.jdt.ui.IContextMenuConstants;
18 import org.eclipse.jdt.ui.actions.IJavaEditorActionDefinitionIds;
19 import org.eclipse.jface.action.IAction;
20 import org.eclipse.jface.action.IMenuManager;
21 import org.eclipse.jface.action.IStatusLineManager;
22 import org.eclipse.jface.action.IToolBarManager;
23 import org.eclipse.jface.action.Separator;
24 import org.eclipse.pde.internal.ui.editor.actions.FormatAction;
25 import org.eclipse.pde.internal.ui.editor.actions.HyperlinkAction;
26 import org.eclipse.pde.internal.ui.editor.actions.PDEActionConstants;
27 import org.eclipse.ui.IActionBars;
28 import org.eclipse.ui.IEditorActionBarContributor;
29 import org.eclipse.ui.IEditorPart;
30 import org.eclipse.ui.IWorkbenchActionConstants;
31 import org.eclipse.ui.SubActionBars;
32 import org.eclipse.ui.actions.ActionFactory;
33 import org.eclipse.ui.editors.text.TextEditorActionContributor;
34 import org.eclipse.ui.forms.editor.IFormPage;
35 import org.eclipse.ui.texteditor.ITextEditor;
36 import org.eclipse.ui.texteditor.ITextEditorActionConstants;
37 import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
38 import org.eclipse.ui.texteditor.RetargetTextEditorAction;
39
40 public class PDEFormTextEditorContributor extends PDEFormEditorContributor {
41
42     private RetargetTextEditorAction fCorrectionAssist;
43     private HyperlinkAction fHyperlinkAction;
44     private FormatAction fFormatAction;
45     private RetargetTextEditorAction fContentAssist;
46     
47     private TextEditorActionContributor fSourceContributor;
48     private SubActionBars fSourceActionBars;
49     
50     class PDETextEditorActionContributor extends TextEditorActionContributor {
51         public void contributeToMenu(IMenuManager mm) {
52             super.contributeToMenu(mm);
53             IMenuManager editMenu = mm.findMenuUsingPath(IWorkbenchActionConstants.M_EDIT);
54             if (editMenu != null) {
55                 editMenu.add(new Separator(IContextMenuConstants.GROUP_OPEN));
56                 editMenu.add(new Separator(IContextMenuConstants.GROUP_GENERATE));
57                 editMenu.add(new Separator(IContextMenuConstants.GROUP_ADDITIONS));
58                 if (fCorrectionAssist != null)
59                     editMenu.appendToGroup(IContextMenuConstants.GROUP_GENERATE, fCorrectionAssist);
60                 if (fContentAssist != null)
61                     editMenu.appendToGroup(IContextMenuConstants.GROUP_GENERATE, fContentAssist);
62             }
63         }
64         
65         public void contributeToToolBar(IToolBarManager toolBarManager) {
66             super.contributeToToolBar(toolBarManager);
67             if (fHyperlinkAction != null)
68                 toolBarManager.add(fHyperlinkAction);
69         }
70
71         public void setActiveEditor(IEditorPart part) {
72             super.setActiveEditor(part);
73             IActionBars actionBars = getActionBars();
74             IStatusLineManager manager = actionBars.getStatusLineManager();
75             manager.setMessage(null);
76             manager.setErrorMessage(null);
77
78             ITextEditor textEditor = (part instanceof ITextEditor) ? (ITextEditor) part : null;
79             if (fCorrectionAssist != null)
80                 fCorrectionAssist.setAction(getAction(textEditor, ITextEditorActionConstants.QUICK_ASSIST)); //$NON-NLS-1$
81
if (fHyperlinkAction != null)
82                 fHyperlinkAction.setTextEditor(textEditor);
83             if (fFormatAction != null)
84                 fFormatAction.setTextEditor(textEditor);
85             if (fContentAssist != null)
86                 fContentAssist.setAction(getAction(textEditor, "ContentAssist")); //$NON-NLS-1$
87
}
88     }
89
90     public PDEFormTextEditorContributor(String JavaDoc menuName) {
91         super(menuName);
92         fSourceContributor = createSourceContributor();
93         if (supportsCorrectionAssist()) {
94             fCorrectionAssist = new RetargetTextEditorAction(PDESourcePage.getBundleForConstructedKeys(), "CorrectionAssistProposal."); //$NON-NLS-1$
95
fCorrectionAssist.setActionDefinitionId(ITextEditorActionDefinitionIds.QUICK_ASSIST);
96         }
97         if (supportsHyperlinking()) {
98             fHyperlinkAction = new HyperlinkAction();
99             fHyperlinkAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.OPEN_EDITOR);
100         }
101         if (supportsFormatAction()) {
102             fFormatAction = new FormatAction();
103             fFormatAction.setActionDefinitionId(PDEActionConstants.DEFN_FORMAT);
104         }
105         if (supportsContentAssist()) {
106             fContentAssist = new RetargetTextEditorAction(PDESourcePage.getBundleForConstructedKeys(), "ContentAssistProposal."); //$NON-NLS-1$
107
fContentAssist.setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
108         }
109     }
110     
111     public boolean supportsCorrectionAssist() {
112         return false;
113     }
114     
115     public boolean supportsContentAssist() {
116         return false;
117     }
118     
119     public boolean supportsFormatAction() {
120         return false;
121     }
122     
123     public boolean supportsHyperlinking() {
124         return false;
125     }
126
127     public IEditorActionBarContributor getSourceContributor() {
128         return fSourceContributor;
129     }
130     
131     public void init(IActionBars bars) {
132         super.init(bars);
133         fSourceActionBars = new SubActionBars(bars);
134         fSourceContributor.init(fSourceActionBars);
135     }
136
137     public void dispose() {
138         fSourceActionBars.dispose();
139         fSourceContributor.dispose();
140         super.dispose();
141     }
142     
143     protected void setSourceActionBarsActive(boolean active) {
144         IActionBars rootBars = getActionBars();
145         rootBars.clearGlobalActionHandlers();
146         rootBars.updateActionBars();
147         if (active) {
148             fSourceActionBars.activate();
149             Map JavaDoc handlers = fSourceActionBars.getGlobalActionHandlers();
150             if (handlers != null) {
151                 Set JavaDoc keys = handlers.keySet();
152                 for (Iterator JavaDoc iter = keys.iterator(); iter.hasNext();) {
153                     String JavaDoc id = (String JavaDoc) iter.next();
154                     rootBars.setGlobalActionHandler(id, (IAction) handlers.get(id));
155                 }
156             }
157         } else {
158             fSourceActionBars.deactivate();
159             registerGlobalActionHandlers();
160         }
161         rootBars.setGlobalActionHandler(PDEActionConstants.OPEN, active ? fHyperlinkAction : null);
162         rootBars.setGlobalActionHandler(PDEActionConstants.FORMAT, active ? fFormatAction : null);
163         // Register the revert action
164
rootBars.setGlobalActionHandler(ActionFactory.REVERT.getId(), getRevertAction());
165         
166         rootBars.updateActionBars();
167     }
168
169     private void registerGlobalActionHandlers() {
170         registerGlobalAction(ActionFactory.DELETE.getId());
171         registerGlobalAction(ActionFactory.UNDO.getId());
172         registerGlobalAction(ActionFactory.REDO.getId());
173         registerGlobalAction(ActionFactory.CUT.getId());
174         registerGlobalAction(ActionFactory.COPY.getId());
175         registerGlobalAction(ActionFactory.PASTE.getId());
176         registerGlobalAction(ActionFactory.SELECT_ALL.getId());
177         registerGlobalAction(ActionFactory.FIND.getId());
178     }
179
180     private void registerGlobalAction(String JavaDoc id) {
181         IAction action = getGlobalAction(id);
182         getActionBars().setGlobalActionHandler(id, action);
183     }
184
185     public void setActivePage(IEditorPart newEditor) {
186         if (fEditor == null)
187             return;
188         
189         IFormPage oldPage = fPage;
190         fPage = fEditor.getActivePageInstance();
191         if (fPage == null)
192             return;
193         // Update the quick outline action to the navigate menu
194
updateQuickOutlineMenuEntry();
195         
196         updateActions();
197         if (oldPage != null && !oldPage.isEditor() && !fPage.isEditor()) {
198             getActionBars().updateActionBars();
199             return;
200         }
201         
202         boolean isSourcePage = fPage instanceof PDESourcePage;
203         if (isSourcePage && fPage.equals(oldPage))
204             return;
205         fSourceContributor.setActiveEditor(fPage);
206         setSourceActionBarsActive(isSourcePage);
207     }
208     
209     /**
210      *
211      */

212     private void updateQuickOutlineMenuEntry() {
213         // Get the main action bar
214
IActionBars actionBars = getActionBars();
215         IMenuManager menuManager = actionBars.getMenuManager();
216         // Get the navigate menu
217
IMenuManager navigateMenu =
218             menuManager.findMenuUsingPath(IWorkbenchActionConstants.M_NAVIGATE);
219         // Ensure there is a navigate menu
220
if (navigateMenu == null) {
221             return;
222         }
223         // Remove the previous version of the quick outline menu entry - if
224
// one exists
225
// Prevent duplicate menu entries
226
// Prevent wrong quick outline menu from being brought up for the wrong
227
// page
228
navigateMenu.remove(PDEActionConstants.COMMAND_ID_QUICK_OUTLINE);
229         // Ensure the active page is a source page
230
// Only add the quick outline menu to the source pages
231
if ((fPage instanceof PDEProjectionSourcePage) == false) {
232             return;
233         }
234         PDEProjectionSourcePage page = (PDEProjectionSourcePage)fPage;
235         // Only add the action if the source page supports it
236
if (page.isQuickOutlineEnabled() == false) {
237             return;
238         }
239         // Get the appropriate quick outline action associated with the active
240
// source page
241
IAction quickOutlineAction = page.getAction(
242                 PDEActionConstants.COMMAND_ID_QUICK_OUTLINE);
243         // Ensure it is defined
244
if (quickOutlineAction == null) {
245             return;
246         }
247         // Add the quick outline action after the "Show In" menu contributed
248
// by JDT
249
// This could break if JDT changes the "Show In" menu ID
250
try {
251             navigateMenu.insertAfter("showIn", quickOutlineAction); //$NON-NLS-1$
252
} catch (IllegalArgumentException JavaDoc e) {
253             // Ignore
254
}
255     }
256
257     protected TextEditorActionContributor createSourceContributor() {
258         return new PDETextEditorActionContributor();
259     }
260     
261     protected HyperlinkAction getHyperlinkAction() {
262         return fHyperlinkAction;
263     }
264     protected FormatAction getFormatAction() {
265         return fFormatAction;
266     }
267
268 }
269
Popular Tags