blob: 2c62bc841d40ea75cb2d81a4f0f7ef4a006c4f5d (
plain) (
blame)
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
93
94
95
96
97
98
99
100
101
102
|
/*******************************************************************************
* Copyright (c) 2000, 2008 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.ui.internal.editors.quickdiff;
import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.IPostSelectionProvider;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.ui.texteditor.ITextEditor;
import org.eclipse.ui.texteditor.IUpdate;
/**
* Combines a list of actions, taking the personality of the first that is
* enabled.
*
* @since 3.1
*/
public final class CompositeRevertAction extends Action implements IUpdate, ISelectionChangedListener {
/**
* The actions.
*/
private final IAction[] fActions;
/**
* Creates an action combining the two given actions.
*
* @param editor the editor
* @param actions the list of actions
*/
public CompositeRevertAction(ITextEditor editor, IAction[] actions) {
fActions= new IAction[actions.length];
for (int i= 0; i < actions.length; i++)
Assert.isNotNull(actions[i]);
System.arraycopy(actions, 0, fActions, 0, actions.length);
ISelectionProvider selectionProvider= editor.getSelectionProvider();
if (selectionProvider instanceof IPostSelectionProvider)
((IPostSelectionProvider)selectionProvider).addPostSelectionChangedListener(this);
update();
}
@Override
public void update() {
for (int i= 0; i < fActions.length; i++) {
if (fActions[i] instanceof IUpdate)
((IUpdate) fActions[i]).update();
}
IAction action= getEnabledAction();
setEnabled(getEnabledAction() != null);
if (action == null)
return;
setText(action.getText());
setToolTipText(action.getToolTipText());
}
@Override
public void selectionChanged(SelectionChangedEvent event) {
update();
}
@Override
public void run() {
IAction action= getEnabledAction();
if (action != null)
action.run();
}
/**
* Returns the first enabled action, or <code>null</code> if none is
* enabled.
*
* @return the first enabled action, or <code>null</code> if none is
* enabled
*/
private IAction getEnabledAction() {
for (int i= 0; i < fActions.length; i++) {
if (fActions[i].isEnabled())
return fActions[i];
}
return null;
}
}
|