Source code: com/port80/draw2d/imageviewer/views/EditorContributor.java
1 package com.port80.draw2d.imageviewer.views;
2
3 /*
4 * (c) Copyright IBM Corp. 2000, 2002.
5 * All Rights Reserved.
6 */
7
8 import org.eclipse.jface.action.Action;
9 import org.eclipse.jface.action.IAction;
10 import org.eclipse.jface.action.IMenuManager;
11 import org.eclipse.jface.action.IToolBarManager;
12 import org.eclipse.jface.action.MenuManager;
13 import org.eclipse.jface.action.Separator;
14 import org.eclipse.jface.dialogs.MessageDialog;
15 import org.eclipse.ui.IActionBars;
16 import org.eclipse.ui.IEditorPart;
17 import org.eclipse.ui.ISharedImages;
18 import org.eclipse.ui.IWorkbenchActionConstants;
19 import org.eclipse.ui.PlatformUI;
20 import org.eclipse.ui.part.MultiPageEditorActionBarContributor;
21 import org.eclipse.ui.texteditor.ITextEditor;
22 import org.eclipse.ui.texteditor.ITextEditorActionConstants;
23
24 /**
25 * Manages the installation/deinstallation of global actions for image viewer.
26 * Responsible for the redirection of global actions to the active editor.
27 * Multi-page contributor replaces the contributors for the individual editors in the multi-page editor.
28 */
29 public class EditorContributor extends MultiPageEditorActionBarContributor {
30 private IEditorPart activeEditorPart;
31 private Action sampleAction;
32 /**
33 * Creates a multi-page contributor.
34 */
35 public EditorContributor() {
36 super();
37 createActions();
38 }
39 /**
40 * Returns the action registed with the given text editor.
41 * @return IAction or null if editor is null.
42 */
43 protected IAction getAction(ITextEditor editor, String actionID) {
44 return (editor == null ? null : editor.getAction(actionID));
45 }
46 /* (non-JavaDoc)
47 * Method declared in AbstractMultiPageEditorActionBarContributor.
48 */
49
50 public void setActivePage(IEditorPart part) {
51 if (activeEditorPart == part)
52 return;
53
54 activeEditorPart = part;
55
56 IActionBars actionBars = getActionBars();
57 if (actionBars != null) {
58
59 ITextEditor editor = (part instanceof ITextEditor) ? (ITextEditor) part : null;
60
61 actionBars.setGlobalActionHandler(
62 IWorkbenchActionConstants.DELETE,
63 getAction(editor, ITextEditorActionConstants.DELETE));
64 actionBars.setGlobalActionHandler(
65 IWorkbenchActionConstants.UNDO,
66 getAction(editor, ITextEditorActionConstants.UNDO));
67 actionBars.setGlobalActionHandler(
68 IWorkbenchActionConstants.REDO,
69 getAction(editor, ITextEditorActionConstants.REDO));
70 actionBars.setGlobalActionHandler(
71 IWorkbenchActionConstants.CUT,
72 getAction(editor, ITextEditorActionConstants.CUT));
73 actionBars.setGlobalActionHandler(
74 IWorkbenchActionConstants.COPY,
75 getAction(editor, ITextEditorActionConstants.COPY));
76 actionBars.setGlobalActionHandler(
77 IWorkbenchActionConstants.PASTE,
78 getAction(editor, ITextEditorActionConstants.PASTE));
79 actionBars.setGlobalActionHandler(
80 IWorkbenchActionConstants.SELECT_ALL,
81 getAction(editor, ITextEditorActionConstants.SELECT_ALL));
82 actionBars.setGlobalActionHandler(
83 IWorkbenchActionConstants.FIND,
84 getAction(editor, ITextEditorActionConstants.FIND));
85 actionBars.setGlobalActionHandler(
86 IWorkbenchActionConstants.BOOKMARK,
87 getAction(editor, ITextEditorActionConstants.BOOKMARK));
88 actionBars.updateActionBars();
89 }
90 }
91 private void createActions() {
92 final String name="Port80 ImageViewer Sample Action";
93 sampleAction = new Action() {
94 public void run() {
95 MessageDialog.openInformation(null, "ImageViewer Plugin", name);
96 }
97 };
98 sampleAction.setText("name");
99 sampleAction.setToolTipText(name+" Tool Tips");
100 sampleAction.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().
101 getImageDescriptor(ISharedImages.IMG_OBJS_TASK_TSK));
102 }
103 public void contributeToMenu(IMenuManager manager) {
104 IMenuManager menu = new MenuManager("Port80 &ImageViewer");
105 manager.prependToGroup(IWorkbenchActionConstants.MB_ADDITIONS, menu);
106 menu.add(sampleAction);
107 }
108 public void contributeToToolBar(IToolBarManager manager) {
109 manager.add(new Separator());
110 manager.add(sampleAction);
111 }
112 }