1 11 12 package org.eclipse.ui.texteditor.quickdiff; 13 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.jface.action.IAction; 18 import org.eclipse.jface.action.IMenuListener; 19 import org.eclipse.jface.action.IMenuManager; 20 import org.eclipse.jface.action.MenuManager; 21 import org.eclipse.jface.action.Separator; 22 import org.eclipse.jface.viewers.ISelection; 23 24 import org.eclipse.ui.IEditorActionDelegate; 25 import org.eclipse.ui.IEditorPart; 26 import org.eclipse.ui.IWorkbenchActionConstants; 27 import org.eclipse.ui.texteditor.ITextEditor; 28 import org.eclipse.ui.texteditor.ITextEditorExtension; 29 import org.eclipse.ui.texteditor.ITextEditorExtension3; 30 import org.eclipse.ui.texteditor.IUpdate; 31 32 import org.eclipse.ui.internal.texteditor.quickdiff.QuickDiffMessages; 33 import org.eclipse.ui.internal.texteditor.quickdiff.QuickDiffRestoreAction; 34 import org.eclipse.ui.internal.texteditor.quickdiff.ReferenceSelectionAction; 35 import org.eclipse.ui.internal.texteditor.quickdiff.RestoreAction; 36 import org.eclipse.ui.internal.texteditor.quickdiff.RevertBlockAction; 37 import org.eclipse.ui.internal.texteditor.quickdiff.RevertLineAction; 38 import org.eclipse.ui.internal.texteditor.quickdiff.RevertSelectionAction; 39 40 46 public class QuickDiffToggleAction implements IEditorActionDelegate, IUpdate { 47 48 49 ITextEditor fEditor= null; 50 51 52 IAction fProxy; 53 54 55 QuickDiffRestoreAction[] fRestoreActions= 56 new QuickDiffRestoreAction[] { 57 new RevertSelectionAction(fEditor, true), 58 new RevertBlockAction(fEditor, true), 59 new RevertLineAction(fEditor, true), 60 new RestoreAction(fEditor, true), 61 }; 62 63 64 private IMenuListener fListener= new IMenuListener() { 65 66 private static final String GROUP_ADD= "add"; 68 private static final String GROUP_DEBUB= "debug"; private static final String GROUP_QUICKDIFF= "quickdiff"; private static final String MENU_ID= "quickdiff.menu"; private static final String GROUP_RESTORE= "restore"; 73 public void menuAboutToShow(IMenuManager manager) { 74 update(); 76 77 IMenuManager menu= (IMenuManager)manager.find(MENU_ID); 78 if (menu == null) { 80 87 if (manager.find(GROUP_DEBUB) == null) 88 manager.insertBefore(IWorkbenchActionConstants.MB_ADDITIONS, new Separator(GROUP_DEBUB)); 89 if (manager.find(GROUP_ADD) == null) 90 manager.insertAfter(IWorkbenchActionConstants.MB_ADDITIONS, new Separator(GROUP_ADD)); 91 if (manager.find(GROUP_RESTORE) == null) 92 manager.insertAfter(GROUP_ADD, new Separator(GROUP_RESTORE)); 93 if (manager.find(GROUP_QUICKDIFF) == null) 94 manager.insertAfter(GROUP_RESTORE, new Separator(GROUP_QUICKDIFF)); 95 96 menu= new MenuManager(QuickDiffMessages.quickdiff_menu_label, MENU_ID); 98 List descriptors= new QuickDiff().getReferenceProviderDescriptors(); 99 for (Iterator it= descriptors.iterator(); it.hasNext();) { 100 ReferenceProviderDescriptor desc= (ReferenceProviderDescriptor) it.next(); 101 ReferenceSelectionAction action= new ReferenceSelectionAction(desc, fEditor); 102 if (action.isEnabled()) 103 menu.add(action); 104 } 105 manager.appendToGroup(GROUP_QUICKDIFF, menu); 106 107 if (isConnected()) { 109 for (int i= 0; i < fRestoreActions.length; i++) { 110 fRestoreActions[i].update(); 111 } 112 if (fRestoreActions[0].isEnabled()) 114 manager.appendToGroup(GROUP_RESTORE, fRestoreActions[0]); 115 else if (fRestoreActions[1].isEnabled()) 116 manager.appendToGroup(GROUP_RESTORE, fRestoreActions[1]); 117 if (fRestoreActions[2].isEnabled()) 118 manager.appendToGroup(GROUP_RESTORE, fRestoreActions[2]); 119 if (fRestoreActions[3].isEnabled()) 120 manager.appendToGroup(GROUP_RESTORE, fRestoreActions[3]); 121 } 122 } 123 } 124 }; 125 126 129 public void setActiveEditor(IAction action, IEditorPart targetEditor) { 130 fProxy= action; 131 removePopupMenu(); 132 if (targetEditor instanceof ITextEditor) { 133 fEditor= (ITextEditor)targetEditor; 134 } else 135 fEditor= null; 136 for (int i= 0; i < fRestoreActions.length; i++) { 137 fRestoreActions[i].setEditor(fEditor); 138 } 139 setPopupMenu(); 140 } 141 142 145 private void removePopupMenu() { 146 if (!(fEditor instanceof ITextEditorExtension)) 147 return; 148 ((ITextEditorExtension)fEditor).removeRulerContextMenuListener(fListener); 149 } 150 151 156 private void setPopupMenu() { 157 if (!(fEditor instanceof ITextEditorExtension)) 158 return; 159 ((ITextEditorExtension)fEditor).addRulerContextMenuListener(fListener); 160 } 161 162 168 boolean isConnected() { 169 if (!(fEditor instanceof ITextEditorExtension3)) 170 return false; 171 return ((ITextEditorExtension3)fEditor).isChangeInformationShowing(); 172 } 173 174 177 public void run(IAction action) { 178 fProxy= action; 179 if (fEditor == null) 180 return; 181 182 if (fEditor instanceof ITextEditorExtension3) { 183 ITextEditorExtension3 extension= (ITextEditorExtension3)fEditor; 184 extension.showChangeInformation(!extension.isChangeInformationShowing()); 185 } 186 } 187 188 191 public void selectionChanged(IAction action, ISelection selection) { 192 fProxy= action; 193 } 194 195 198 public void update() { 199 if (fProxy == null) 200 return; 201 if (isConnected()) 202 fProxy.setText(QuickDiffMessages.quickdiff_toggle_disable); 203 else 204 fProxy.setText(QuickDiffMessages.quickdiff_toggle_enable); 205 } 206 207 } 208 | Popular Tags |