1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
/*******************************************************************************
* Copyright (c) 2008-2010 Sonatype, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Sonatype, Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.m2e.editor.pom;
import org.eclipse.jface.action.IAction;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.ide.IDEActionFactory;
import org.eclipse.ui.part.MultiPageEditorActionBarContributor;
import org.eclipse.ui.texteditor.ITextEditor;
import org.eclipse.ui.texteditor.ITextEditorActionConstants;
/**
* Manages the installation/deinstallation of global actions for multi-page
* editors. Responsible for the redirection of global actions to the active
* editor. Multi-page contributor replaces the contributors for the individual
* editors in the multi-page editor.
*/
public class MavenPomEditorContributor extends MultiPageEditorActionBarContributor {
private MavenPomEditor editorPart;
public void setActiveEditor(IEditorPart targetEditor) {
if (targetEditor instanceof MavenPomEditor) {
editorPart = (MavenPomEditor) targetEditor;
setActivePage(editorPart.getActiveEditor());
}
}
public void setActivePage(IEditorPart part) {
//set the text editor
IActionBars actionBars = getActionBars();
if(editorPart !=null) {
if (actionBars != null) {
actionBars.clearGlobalActionHandlers();
// undo/redo always enabled
actionBars.setGlobalActionHandler(ActionFactory.UNDO.getId(), //
getAction(ITextEditorActionConstants.UNDO));
actionBars.setGlobalActionHandler(ActionFactory.REDO.getId(), //
getAction(ITextEditorActionConstants.REDO));
// all other action, for text editor only (FormPage doesn't provide for these actions...)
if (part instanceof ITextEditor) {
actionBars.setGlobalActionHandler(ActionFactory.DELETE.getId(), //
getAction(ITextEditorActionConstants.DELETE));
actionBars.setGlobalActionHandler(ActionFactory.CUT.getId(), //
getAction(ITextEditorActionConstants.CUT));
actionBars.setGlobalActionHandler(ActionFactory.COPY.getId(), //
getAction(ITextEditorActionConstants.COPY));
actionBars.setGlobalActionHandler(ActionFactory.PASTE.getId(), //
getAction(ITextEditorActionConstants.PASTE));
actionBars.setGlobalActionHandler(ActionFactory.SELECT_ALL.getId(), //
getAction(ITextEditorActionConstants.SELECT_ALL));
actionBars.setGlobalActionHandler(ActionFactory.FIND.getId(), //
getAction(ITextEditorActionConstants.FIND));
actionBars.setGlobalActionHandler(IDEActionFactory.BOOKMARK.getId(), //
getAction(IDEActionFactory.BOOKMARK.getId()));
}
actionBars.updateActionBars();
}
}
}
/**
* Returns the action registered with the given text editor.
*
* @return IAction or null if editor is null.
*/
protected IAction getAction(String actionId) {
if(editorPart != null) {
try {
return editorPart.getSourcePage().getAction(actionId);
} catch (NullPointerException e) {
//editor has been disposed, ignore
}
}
return null;
}
}
|