1 /******************************************************************************* 2 * Copyright (c) 2000, 2008 IBM Corporation and others. 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * IBM Corporation - initial API and implementation 13 *******************************************************************************/ 14 package org.eclipse.ui.examples.javaeditor; 15 16 17 import org.eclipse.jface.action.IMenuManager; 18 import org.eclipse.jface.action.IToolBarManager; 19 import org.eclipse.jface.action.Separator; 20 21 import org.eclipse.ui.IActionBars; 22 import org.eclipse.ui.IEditorPart; 23 import org.eclipse.ui.IWorkbenchActionConstants; 24 25 import org.eclipse.ui.texteditor.ITextEditor; 26 import org.eclipse.ui.texteditor.ITextEditorActionConstants; 27 import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds; 28 import org.eclipse.ui.texteditor.RetargetTextEditorAction; 29 import org.eclipse.ui.texteditor.TextEditorAction; 30 31 import org.eclipse.ui.editors.text.TextEditorActionContributor; 32 33 /** 34 * Contributes interesting Java actions to the desktop's Edit menu and the toolbar. 35 */ 36 public class JavaActionContributor extends TextEditorActionContributor { 37 38 protected RetargetTextEditorAction fContentAssistProposal; 39 protected RetargetTextEditorAction fContentAssistTip; 40 protected TextEditorAction fTogglePresentation; 41 42 /** 43 * Default constructor. 44 */ JavaActionContributor()45 public JavaActionContributor() { 46 super(); 47 fContentAssistProposal= new RetargetTextEditorAction(JavaEditorMessages.getResourceBundle(), "ContentAssistProposal."); //$NON-NLS-1$ 48 fContentAssistProposal.setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS); 49 fContentAssistTip= new RetargetTextEditorAction(JavaEditorMessages.getResourceBundle(), "ContentAssistTip."); //$NON-NLS-1$ 50 fContentAssistTip.setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_CONTEXT_INFORMATION); 51 fTogglePresentation= new PresentationAction(); 52 } 53 54 @Override init(IActionBars bars)55 public void init(IActionBars bars) { 56 super.init(bars); 57 58 IMenuManager menuManager= bars.getMenuManager(); 59 IMenuManager editMenu= menuManager.findMenuUsingPath(IWorkbenchActionConstants.M_EDIT); 60 if (editMenu != null) { 61 editMenu.add(new Separator()); 62 editMenu.add(fContentAssistProposal); 63 editMenu.add(fContentAssistTip); 64 } 65 66 IToolBarManager toolBarManager= bars.getToolBarManager(); 67 if (toolBarManager != null) { 68 toolBarManager.add(new Separator()); 69 toolBarManager.add(fTogglePresentation); 70 } 71 } 72 doSetActiveEditor(IEditorPart part)73 private void doSetActiveEditor(IEditorPart part) { 74 super.setActiveEditor(part); 75 76 ITextEditor editor= null; 77 if (part instanceof ITextEditor) 78 editor= (ITextEditor) part; 79 80 fContentAssistProposal.setAction(getAction(editor, ITextEditorActionConstants.CONTENT_ASSIST)); 81 fContentAssistTip.setAction(getAction(editor, ITextEditorActionConstants.CONTENT_ASSIST_CONTEXT_INFORMATION)); 82 83 fTogglePresentation.setEditor(editor); 84 fTogglePresentation.update(); 85 } 86 87 @Override setActiveEditor(IEditorPart part)88 public void setActiveEditor(IEditorPart part) { 89 super.setActiveEditor(part); 90 doSetActiveEditor(part); 91 } 92 93 @Override dispose()94 public void dispose() { 95 doSetActiveEditor(null); 96 super.dispose(); 97 } 98 } 99