1 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 32 public final class CompositeRevertAction extends Action implements IUpdate, ISelectionChangedListener { 33 34 37 private final IAction[] fActions; 38 39 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 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 79 public void selectionChanged(SelectionChangedEvent event) { 80 update(); 81 } 82 83 86 public void run() { 87 IAction action= getEnabledAction(); 88 if (action != null) 89 action.run(); 90 } 91 92 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 |