001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.examples.readmetool;
011:
012: import org.eclipse.jface.action.Action;
013: import org.eclipse.jface.action.IMenuManager;
014: import org.eclipse.jface.action.IStatusLineManager;
015: import org.eclipse.jface.action.IToolBarManager;
016: import org.eclipse.jface.action.MenuManager;
017: import org.eclipse.jface.action.Separator;
018: import org.eclipse.jface.dialogs.MessageDialog;
019: import org.eclipse.swt.widgets.Shell;
020: import org.eclipse.ui.IActionBars;
021: import org.eclipse.ui.IEditorPart;
022: import org.eclipse.ui.IWorkbenchPage;
023: import org.eclipse.ui.IWorkbenchPart;
024: import org.eclipse.ui.PlatformUI;
025: import org.eclipse.ui.actions.LabelRetargetAction;
026: import org.eclipse.ui.actions.RetargetAction;
027: import org.eclipse.ui.texteditor.BasicTextEditorActionContributor;
028:
029: /**
030: * This class demonstrates action contribution for the readme editor.
031: * A number of menu, toolbar, and status line contributions are defined
032: * in the workbench window. These actions are shared among all
033: * readme editors, and are only visible when a readme editor is
034: * active. Otherwise, they are invisible.
035: */
036: public class ReadmeEditorActionBarContributor extends
037: BasicTextEditorActionContributor {
038: private EditorAction action1;
039:
040: private RetargetAction action2;
041:
042: private LabelRetargetAction action3;
043:
044: private EditorAction handler2;
045:
046: private EditorAction handler3;
047:
048: private EditorAction handler4;
049:
050: private EditorAction handler5;
051:
052: private DirtyStateContribution dirtyStateContribution;
053:
054: class EditorAction extends Action {
055: private Shell shell;
056:
057: private IEditorPart activeEditor;
058:
059: public EditorAction(String label) {
060: super (label);
061: }
062:
063: public void setShell(Shell shell) {
064: this .shell = shell;
065: }
066:
067: public void run() {
068: String editorName = MessageUtil
069: .getString("Empty_Editor_Name"); //$NON-NLS-1$
070: if (activeEditor != null)
071: editorName = activeEditor.getTitle();
072: MessageDialog
073: .openInformation(shell,
074: MessageUtil.getString("Readme_Editor"), //$NON-NLS-1$
075: MessageUtil
076: .format(
077: "ReadmeEditorActionExecuted", new Object[] { getText(), editorName })); //$NON-NLS-1$
078: }
079:
080: public void setActiveEditor(IEditorPart part) {
081: activeEditor = part;
082: }
083: }
084:
085: /**
086: * Creates a new ReadmeEditorActionBarContributor.
087: */
088: public ReadmeEditorActionBarContributor() {
089: action1 = new EditorAction(MessageUtil
090: .getString("Editor_Action1")); //$NON-NLS-1$
091: action1.setToolTipText(MessageUtil
092: .getString("Readme_Editor_Action1")); //$NON-NLS-1$
093: action1
094: .setDisabledImageDescriptor(ReadmeImages.EDITOR_ACTION1_IMAGE_DISABLE);
095: action1
096: .setImageDescriptor(ReadmeImages.EDITOR_ACTION1_IMAGE_ENABLE);
097: action1
098: .setHoverImageDescriptor(ReadmeImages.EDITOR_ACTION1_IMAGE);
099: PlatformUI.getWorkbench().getHelpSystem().setHelp(action1,
100: IReadmeConstants.EDITOR_ACTION1_CONTEXT);
101:
102: action2 = new RetargetAction(IReadmeConstants.RETARGET2,
103: MessageUtil.getString("Editor_Action2")); //$NON-NLS-1$
104: action2.setToolTipText(MessageUtil
105: .getString("Readme_Editor_Action2")); //$NON-NLS-1$
106: action2
107: .setDisabledImageDescriptor(ReadmeImages.EDITOR_ACTION2_IMAGE_DISABLE);
108: action2
109: .setImageDescriptor(ReadmeImages.EDITOR_ACTION2_IMAGE_ENABLE);
110: action2
111: .setHoverImageDescriptor(ReadmeImages.EDITOR_ACTION2_IMAGE);
112:
113: action3 = new LabelRetargetAction(
114: IReadmeConstants.LABELRETARGET3, MessageUtil
115: .getString("Editor_Action3")); //$NON-NLS-1$
116: action3
117: .setDisabledImageDescriptor(ReadmeImages.EDITOR_ACTION3_IMAGE_DISABLE);
118: action3
119: .setImageDescriptor(ReadmeImages.EDITOR_ACTION3_IMAGE_ENABLE);
120: action3
121: .setHoverImageDescriptor(ReadmeImages.EDITOR_ACTION3_IMAGE);
122:
123: handler2 = new EditorAction(MessageUtil
124: .getString("Editor_Action2")); //$NON-NLS-1$
125: PlatformUI.getWorkbench().getHelpSystem().setHelp(action2,
126: IReadmeConstants.EDITOR_ACTION2_CONTEXT);
127:
128: handler3 = new EditorAction(MessageUtil
129: .getString("Editor_Action3")); //$NON-NLS-1$
130: handler3.setToolTipText(MessageUtil
131: .getString("Readme_Editor_Action3")); //$NON-NLS-1$
132: PlatformUI.getWorkbench().getHelpSystem().setHelp(action3,
133: IReadmeConstants.EDITOR_ACTION3_CONTEXT);
134:
135: handler4 = new EditorAction(MessageUtil
136: .getString("Editor_Action4")); //$NON-NLS-1$
137: handler5 = new EditorAction(MessageUtil
138: .getString("Editor_Action5")); //$NON-NLS-1$
139: handler5.setToolTipText(MessageUtil
140: .getString("Readme_Editor_Action5")); //$NON-NLS-1$
141:
142: dirtyStateContribution = new DirtyStateContribution();
143: }
144:
145: /** (non-Javadoc)
146: * Method declared on EditorActionBarContributor
147: */
148: public void contributeToMenu(IMenuManager menuManager) {
149: // Run super.
150: super .contributeToMenu(menuManager);
151:
152: // Editor-specitic menu
153: MenuManager readmeMenu = new MenuManager(MessageUtil
154: .getString("Readme_Menu")); //$NON-NLS-1$
155: // It is important to append the menu to the
156: // group "additions". This group is created
157: // between "Project" and "Tools" menus
158: // for this purpose.
159: menuManager.insertAfter("additions", readmeMenu); //$NON-NLS-1$
160: readmeMenu.add(action1);
161: readmeMenu.add(action2);
162: readmeMenu.add(action3);
163: }
164:
165: /** (non-Javadoc)
166: * Method declared on EditorActionBarContributor
167: */
168: public void contributeToStatusLine(
169: IStatusLineManager statusLineManager) {
170: // Run super.
171: super .contributeToStatusLine(statusLineManager);
172: // Test status line.
173: statusLineManager.setMessage(MessageUtil
174: .getString("Editor_is_active")); //$NON-NLS-1$
175: statusLineManager.add(dirtyStateContribution);
176: }
177:
178: /** (non-Javadoc)
179: * Method declared on EditorActionBarContributor
180: */
181: public void contributeToToolBar(IToolBarManager toolBarManager) {
182: // Run super.
183: super .contributeToToolBar(toolBarManager);
184:
185: // Add toolbar stuff.
186: toolBarManager.add(new Separator("ReadmeEditor")); //$NON-NLS-1$
187: toolBarManager.add(action1);
188: toolBarManager.add(action2);
189: toolBarManager.add(action3);
190: }
191:
192: /** (non-Javadoc)
193: * Method declared on IEditorActionBarContributor
194: */
195: public void dispose() {
196: // Remove retarget actions as page listeners
197: getPage().removePartListener(action2);
198: getPage().removePartListener(action3);
199: }
200:
201: /** (non-Javadoc)
202: * Method declared on IEditorActionBarContributor
203: */
204: public void init(IActionBars bars, IWorkbenchPage page) {
205: super .init(bars, page);
206: bars.setGlobalActionHandler(IReadmeConstants.RETARGET2,
207: handler2);
208: bars.setGlobalActionHandler(IReadmeConstants.LABELRETARGET3,
209: handler3);
210: bars.setGlobalActionHandler(
211: IReadmeConstants.ACTION_SET_RETARGET4, handler4);
212: bars.setGlobalActionHandler(
213: IReadmeConstants.ACTION_SET_LABELRETARGET5, handler5);
214:
215: // Hook retarget actions as page listeners
216: page.addPartListener(action2);
217: page.addPartListener(action3);
218: IWorkbenchPart activePart = page.getActivePart();
219: if (activePart != null) {
220: action2.partActivated(activePart);
221: action3.partActivated(activePart);
222: }
223: }
224:
225: /** (non-Javadoc)
226: * Method declared on IEditorActionBarContributor
227: */
228: public void setActiveEditor(IEditorPart editor) {
229: // Run super.
230: super .setActiveEditor(editor);
231:
232: // Target shared actions to new editor
233: action1.setActiveEditor(editor);
234: handler2.setActiveEditor(editor);
235: handler3.setActiveEditor(editor);
236: handler4.setActiveEditor(editor);
237: handler5.setActiveEditor(editor);
238: dirtyStateContribution.editorChanged(editor);
239: }
240: }
|