001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.editors.quickdiff;
011:
012: import org.eclipse.core.runtime.Assert;
013:
014: import org.eclipse.jface.action.Action;
015: import org.eclipse.jface.action.IAction;
016: import org.eclipse.jface.viewers.IPostSelectionProvider;
017: import org.eclipse.jface.viewers.ISelectionChangedListener;
018: import org.eclipse.jface.viewers.ISelectionProvider;
019: import org.eclipse.jface.viewers.SelectionChangedEvent;
020:
021: import org.eclipse.ui.texteditor.ITextEditor;
022: import org.eclipse.ui.texteditor.IUpdate;
023:
024: /**
025: * Combines a list of actions, taking the personality of the first that is
026: * enabled.
027: *
028: * @since 3.1
029: */
030: public final class CompositeRevertAction extends Action implements
031: IUpdate, ISelectionChangedListener {
032:
033: /**
034: * The actions.
035: */
036: private final IAction[] fActions;
037:
038: /**
039: * Creates an action combining the two given actions.
040: *
041: * @param editor the editor
042: * @param actions the list of actions
043: */
044: public CompositeRevertAction(ITextEditor editor, IAction[] actions) {
045: fActions = new IAction[actions.length];
046: for (int i = 0; i < actions.length; i++)
047: Assert.isNotNull(actions[i]);
048:
049: System.arraycopy(actions, 0, fActions, 0, actions.length);
050:
051: ISelectionProvider selectionProvider = editor
052: .getSelectionProvider();
053: if (selectionProvider instanceof IPostSelectionProvider)
054: ((IPostSelectionProvider) selectionProvider)
055: .addPostSelectionChangedListener(this );
056:
057: update();
058: }
059:
060: /*
061: * @see org.eclipse.ui.texteditor.IUpdate#update()
062: */
063: public void update() {
064: for (int i = 0; i < fActions.length; i++) {
065: if (fActions[i] instanceof IUpdate)
066: ((IUpdate) fActions[i]).update();
067: }
068: IAction action = getEnabledAction();
069: setEnabled(getEnabledAction() != null);
070: if (action == null)
071: return;
072: setText(action.getText());
073: setToolTipText(action.getToolTipText());
074: }
075:
076: /*
077: * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent)
078: * @since 3.3
079: */
080: public void selectionChanged(SelectionChangedEvent event) {
081: update();
082: }
083:
084: /*
085: * @see org.eclipse.jface.action.IAction#run()
086: */
087: public void run() {
088: IAction action = getEnabledAction();
089: if (action != null)
090: action.run();
091: }
092:
093: /**
094: * Returns the first enabled action, or <code>null</code> if none is
095: * enabled.
096: *
097: * @return the first enabled action, or <code>null</code> if none is
098: * enabled
099: */
100: private IAction getEnabledAction() {
101: for (int i = 0; i < fActions.length; i++) {
102: if (fActions[i].isEnabled())
103: return fActions[i];
104: }
105: return null;
106: }
107: }
|