KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > editors > quickdiff > CompositeRevertAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.editors.quickdiff;
12
13 import org.eclipse.core.runtime.Assert;
14
15 import org.eclipse.jface.action.Action;
16 import org.eclipse.jface.action.IAction;
17 import org.eclipse.jface.viewers.IPostSelectionProvider;
18 import org.eclipse.jface.viewers.ISelectionChangedListener;
19 import org.eclipse.jface.viewers.ISelectionProvider;
20 import org.eclipse.jface.viewers.SelectionChangedEvent;
21
22 import org.eclipse.ui.texteditor.ITextEditor;
23 import org.eclipse.ui.texteditor.IUpdate;
24
25
26 /**
27  * Combines a list of actions, taking the personality of the first that is
28  * enabled.
29  *
30  * @since 3.1
31  */

32 public final class CompositeRevertAction extends Action implements IUpdate, ISelectionChangedListener {
33
34     /**
35      * The actions.
36      */

37     private final IAction[] fActions;
38
39     /**
40      * Creates an action combining the two given actions.
41      *
42      * @param editor the editor
43      * @param actions the list of actions
44      */

45     public CompositeRevertAction(ITextEditor editor, IAction[] actions) {
46         fActions= new IAction[actions.length];
47         for (int i= 0; i < actions.length; i++)
48             Assert.isNotNull(actions[i]);
49
50         System.arraycopy(actions, 0, fActions, 0, actions.length);
51         
52         ISelectionProvider selectionProvider= editor.getSelectionProvider();
53         if (selectionProvider instanceof IPostSelectionProvider)
54             ((IPostSelectionProvider)selectionProvider).addPostSelectionChangedListener(this);
55         
56         update();
57     }
58
59     /*
60      * @see org.eclipse.ui.texteditor.IUpdate#update()
61      */

62     public void update() {
63         for (int i= 0; i < fActions.length; i++) {
64             if (fActions[i] instanceof IUpdate)
65                 ((IUpdate) fActions[i]).update();
66         }
67         IAction action= getEnabledAction();
68         setEnabled(getEnabledAction() != null);
69         if (action == null)
70             return;
71         setText(action.getText());
72         setToolTipText(action.getToolTipText());
73     }
74
75     /*
76      * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent)
77      * @since 3.3
78      */

79     public void selectionChanged(SelectionChangedEvent event) {
80         update();
81     }
82
83     /*
84      * @see org.eclipse.jface.action.IAction#run()
85      */

86     public void run() {
87         IAction action= getEnabledAction();
88         if (action != null)
89             action.run();
90     }
91
92     /**
93      * Returns the first enabled action, or <code>null</code> if none is
94      * enabled.
95      *
96      * @return the first enabled action, or <code>null</code> if none is
97      * enabled
98      */

99     private IAction getEnabledAction() {
100         for (int i= 0; i < fActions.length; i++) {
101             if (fActions[i].isEnabled())
102                 return fActions[i];
103         }
104         return null;
105     }
106 }
107
Popular Tags