1 /******************************************************************************* 2 * Copyright (c) 2000, 2005 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 15 package org.eclipse.ui.examples.readmetool; 16 17 import org.eclipse.jface.action.Action; 18 import org.eclipse.jface.action.IMenuManager; 19 import org.eclipse.jface.action.IStatusLineManager; 20 import org.eclipse.jface.action.IToolBarManager; 21 import org.eclipse.jface.action.MenuManager; 22 import org.eclipse.jface.action.Separator; 23 import org.eclipse.jface.dialogs.MessageDialog; 24 import org.eclipse.swt.widgets.Shell; 25 import org.eclipse.ui.IActionBars; 26 import org.eclipse.ui.IEditorPart; 27 import org.eclipse.ui.IWorkbenchPage; 28 import org.eclipse.ui.IWorkbenchPart; 29 import org.eclipse.ui.PlatformUI; 30 import org.eclipse.ui.actions.LabelRetargetAction; 31 import org.eclipse.ui.actions.RetargetAction; 32 import org.eclipse.ui.texteditor.BasicTextEditorActionContributor; 33 34 /** 35 * This class demonstrates action contribution for the readme editor. A number 36 * of menu, toolbar, and status line contributions are defined in the workbench 37 * window. These actions are shared among all readme editors, and are only 38 * visible when a readme editor is active. Otherwise, they are invisible. 39 */ 40 public class ReadmeEditorActionBarContributor extends BasicTextEditorActionContributor { 41 private EditorAction action1; 42 43 private RetargetAction action2; 44 45 private LabelRetargetAction action3; 46 47 private EditorAction handler2; 48 49 private EditorAction handler3; 50 51 private EditorAction handler4; 52 53 private EditorAction handler5; 54 55 private DirtyStateContribution dirtyStateContribution; 56 57 class EditorAction extends Action { 58 private Shell shell; 59 60 private IEditorPart activeEditor; 61 EditorAction(String label)62 public EditorAction(String label) { 63 super(label); 64 } 65 setShell(Shell shell)66 public void setShell(Shell shell) { 67 this.shell = shell; 68 } 69 70 @Override run()71 public void run() { 72 String editorName = MessageUtil.getString("Empty_Editor_Name"); //$NON-NLS-1$ 73 if (activeEditor != null) 74 editorName = activeEditor.getTitle(); 75 MessageDialog.openInformation(shell, MessageUtil.getString("Readme_Editor"), //$NON-NLS-1$ 76 MessageUtil.format("ReadmeEditorActionExecuted", new Object[] { getText(), editorName })); //$NON-NLS-1$ 77 } 78 setActiveEditor(IEditorPart part)79 public void setActiveEditor(IEditorPart part) { 80 activeEditor = part; 81 } 82 } 83 84 /** 85 * Creates a new ReadmeEditorActionBarContributor. 86 */ ReadmeEditorActionBarContributor()87 public ReadmeEditorActionBarContributor() { 88 action1 = new EditorAction(MessageUtil.getString("Editor_Action1")); //$NON-NLS-1$ 89 action1.setToolTipText(MessageUtil.getString("Readme_Editor_Action1")); //$NON-NLS-1$ 90 action1.setDisabledImageDescriptor(ReadmeImages.EDITOR_ACTION1_IMAGE_DISABLE); 91 action1.setImageDescriptor(ReadmeImages.EDITOR_ACTION1_IMAGE_ENABLE); 92 action1.setHoverImageDescriptor(ReadmeImages.EDITOR_ACTION1_IMAGE); 93 PlatformUI.getWorkbench().getHelpSystem().setHelp(action1, IReadmeConstants.EDITOR_ACTION1_CONTEXT); 94 95 action2 = new RetargetAction(IReadmeConstants.RETARGET2, MessageUtil.getString("Editor_Action2")); //$NON-NLS-1$ 96 action2.setToolTipText(MessageUtil.getString("Readme_Editor_Action2")); //$NON-NLS-1$ 97 action2.setDisabledImageDescriptor(ReadmeImages.EDITOR_ACTION2_IMAGE_DISABLE); 98 action2.setImageDescriptor(ReadmeImages.EDITOR_ACTION2_IMAGE_ENABLE); 99 action2.setHoverImageDescriptor(ReadmeImages.EDITOR_ACTION2_IMAGE); 100 101 action3 = new LabelRetargetAction(IReadmeConstants.LABELRETARGET3, MessageUtil.getString("Editor_Action3")); //$NON-NLS-1$ 102 action3.setDisabledImageDescriptor(ReadmeImages.EDITOR_ACTION3_IMAGE_DISABLE); 103 action3.setImageDescriptor(ReadmeImages.EDITOR_ACTION3_IMAGE_ENABLE); 104 action3.setHoverImageDescriptor(ReadmeImages.EDITOR_ACTION3_IMAGE); 105 106 handler2 = new EditorAction(MessageUtil.getString("Editor_Action2")); //$NON-NLS-1$ 107 PlatformUI.getWorkbench().getHelpSystem().setHelp(action2, IReadmeConstants.EDITOR_ACTION2_CONTEXT); 108 109 handler3 = new EditorAction(MessageUtil.getString("Editor_Action3")); //$NON-NLS-1$ 110 handler3.setToolTipText(MessageUtil.getString("Readme_Editor_Action3")); //$NON-NLS-1$ 111 PlatformUI.getWorkbench().getHelpSystem().setHelp(action3, IReadmeConstants.EDITOR_ACTION3_CONTEXT); 112 113 handler4 = new EditorAction(MessageUtil.getString("Editor_Action4")); //$NON-NLS-1$ 114 handler5 = new EditorAction(MessageUtil.getString("Editor_Action5")); //$NON-NLS-1$ 115 handler5.setToolTipText(MessageUtil.getString("Readme_Editor_Action5")); //$NON-NLS-1$ 116 117 dirtyStateContribution = new DirtyStateContribution(); 118 } 119 120 @Override contributeToMenu(IMenuManager menuManager)121 public void contributeToMenu(IMenuManager menuManager) { 122 // Run super. 123 super.contributeToMenu(menuManager); 124 125 // Editor-specitic menu 126 MenuManager readmeMenu = new MenuManager(MessageUtil.getString("Readme_Menu")); //$NON-NLS-1$ 127 // It is important to append the menu to the 128 // group "additions". This group is created 129 // between "Project" and "Tools" menus 130 // for this purpose. 131 menuManager.insertAfter("additions", readmeMenu); //$NON-NLS-1$ 132 readmeMenu.add(action1); 133 readmeMenu.add(action2); 134 readmeMenu.add(action3); 135 } 136 137 @Override contributeToStatusLine(IStatusLineManager statusLineManager)138 public void contributeToStatusLine(IStatusLineManager statusLineManager) { 139 // Run super. 140 super.contributeToStatusLine(statusLineManager); 141 // Test status line. 142 statusLineManager.setMessage(MessageUtil.getString("Editor_is_active")); //$NON-NLS-1$ 143 statusLineManager.add(dirtyStateContribution); 144 } 145 146 @Override contributeToToolBar(IToolBarManager toolBarManager)147 public void contributeToToolBar(IToolBarManager toolBarManager) { 148 // Run super. 149 super.contributeToToolBar(toolBarManager); 150 151 // Add toolbar stuff. 152 toolBarManager.add(new Separator("ReadmeEditor")); //$NON-NLS-1$ 153 toolBarManager.add(action1); 154 toolBarManager.add(action2); 155 toolBarManager.add(action3); 156 } 157 158 @Override dispose()159 public void dispose() { 160 // Remove retarget actions as page listeners 161 getPage().removePartListener(action2); 162 getPage().removePartListener(action3); 163 } 164 165 @Override init(IActionBars bars, IWorkbenchPage page)166 public void init(IActionBars bars, IWorkbenchPage page) { 167 super.init(bars, page); 168 bars.setGlobalActionHandler(IReadmeConstants.RETARGET2, handler2); 169 bars.setGlobalActionHandler(IReadmeConstants.LABELRETARGET3, handler3); 170 bars.setGlobalActionHandler(IReadmeConstants.ACTION_SET_RETARGET4, handler4); 171 bars.setGlobalActionHandler(IReadmeConstants.ACTION_SET_LABELRETARGET5, handler5); 172 173 // Hook retarget actions as page listeners 174 page.addPartListener(action2); 175 page.addPartListener(action3); 176 IWorkbenchPart activePart = page.getActivePart(); 177 if (activePart != null) { 178 action2.partActivated(activePart); 179 action3.partActivated(activePart); 180 } 181 } 182 183 @Override setActiveEditor(IEditorPart editor)184 public void setActiveEditor(IEditorPart editor) { 185 // Run super. 186 super.setActiveEditor(editor); 187 188 // Target shared actions to new editor 189 action1.setActiveEditor(editor); 190 handler2.setActiveEditor(editor); 191 handler3.setActiveEditor(editor); 192 handler4.setActiveEditor(editor); 193 handler5.setActiveEditor(editor); 194 dirtyStateContribution.editorChanged(editor); 195 } 196 } 197