1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.multipageeditor;
15 
16 import org.eclipse.jface.action.IAction;
17 import org.eclipse.ui.IActionBars;
18 import org.eclipse.ui.IEditorPart;
19 import org.eclipse.ui.actions.ActionFactory;
20 import org.eclipse.ui.ide.IDEActionFactory;
21 import org.eclipse.ui.part.MultiPageEditorActionBarContributor;
22 import org.eclipse.ui.texteditor.ITextEditor;
23 
24 /**
25  * Manages the installation/deinstallation of global actions for multi-page editors.
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 MultiPageContributor extends MultiPageEditorActionBarContributor {
30 	private IEditorPart activeEditorPart;
31 
32 	/**
33 	 * Creates a multi-page contributor.
34 	 */
MultiPageContributor()35 	public MultiPageContributor() {
36 		super();
37 	}
38 
39 	/**
40 	 * Returns the action registed with the given text editor.
41 	 * @return IAction or null if editor is null.
42 	 */
getAction(ITextEditor editor, String actionID)43 	protected IAction getAction(ITextEditor editor, String actionID) {
44 		return (editor == null ? null : editor.getAction(actionID));
45 	}
46 
47 	@Override
setActivePage(IEditorPart part)48 	public void setActivePage(IEditorPart part) {
49 		if (activeEditorPart == part)
50 			return;
51 
52 		activeEditorPart = part;
53 
54 		IActionBars actionBars = getActionBars();
55 		if (actionBars != null) {
56 
57 			ITextEditor editor = (part instanceof ITextEditor) ? (ITextEditor) part
58 					: null;
59 
60 			actionBars.setGlobalActionHandler(ActionFactory.DELETE.getId(),
61 					getAction(editor, ActionFactory.DELETE.getId()));
62 			actionBars.setGlobalActionHandler(ActionFactory.UNDO.getId(),
63 					getAction(editor, ActionFactory.UNDO.getId()));
64 			actionBars.setGlobalActionHandler(ActionFactory.REDO.getId(),
65 					getAction(editor, ActionFactory.REDO.getId()));
66 			actionBars.setGlobalActionHandler(ActionFactory.CUT.getId(),
67 					getAction(editor, ActionFactory.CUT.getId()));
68 			actionBars.setGlobalActionHandler(ActionFactory.COPY.getId(),
69 					getAction(editor, ActionFactory.COPY.getId()));
70 			actionBars.setGlobalActionHandler(ActionFactory.PASTE.getId(),
71 					getAction(editor, ActionFactory.PASTE.getId()));
72 			actionBars.setGlobalActionHandler(ActionFactory.SELECT_ALL.getId(),
73 					getAction(editor, ActionFactory.SELECT_ALL.getId()));
74 			actionBars.setGlobalActionHandler(ActionFactory.FIND.getId(),
75 					getAction(editor, ActionFactory.FIND.getId()));
76 			actionBars.setGlobalActionHandler(
77 					IDEActionFactory.BOOKMARK.getId(), getAction(editor,
78 							IDEActionFactory.BOOKMARK.getId()));
79 			actionBars.setGlobalActionHandler(
80 					IDEActionFactory.ADD_TASK.getId(), getAction(editor,
81 							IDEActionFactory.ADD_TASK.getId()));
82 			actionBars.updateActionBars();
83 		}
84 	}
85 }
86