1 11 12 13 package org.eclipse.ui.texteditor; 14 15 16 import org.eclipse.swt.events.MouseEvent; 17 import org.eclipse.swt.events.MouseListener; 18 import org.eclipse.swt.widgets.Control; 19 import org.eclipse.swt.widgets.Event; 20 21 import org.eclipse.jface.action.IAction; 22 import org.eclipse.jface.action.IMenuListener; 23 import org.eclipse.jface.action.IMenuManager; 24 import org.eclipse.jface.viewers.ISelection; 25 26 import org.eclipse.jface.text.source.IVerticalRulerInfo; 27 28 import org.eclipse.ui.IActionDelegate2; 29 import org.eclipse.ui.IEditorActionDelegate; 30 import org.eclipse.ui.IEditorPart; 31 import org.eclipse.ui.actions.ActionDelegate; 32 33 44 public abstract class AbstractRulerActionDelegate extends ActionDelegate implements IEditorActionDelegate, IActionDelegate2, MouseListener, IMenuListener { 45 46 47 private ITextEditor fEditor; 48 49 private IAction fCallerAction; 50 51 private IAction fAction; 52 53 60 protected abstract IAction createAction(ITextEditor editor, IVerticalRulerInfo rulerInfo); 61 62 63 66 public void setActiveEditor(IAction callerAction, IEditorPart targetEditor) { 67 if (fEditor != null) { 68 IVerticalRulerInfo rulerInfo= (IVerticalRulerInfo) fEditor.getAdapter(IVerticalRulerInfo.class); 69 if (rulerInfo != null) { 70 Control control= rulerInfo.getControl(); 71 if (control != null && !control.isDisposed()) 72 control.removeMouseListener(this); 73 } 74 75 if (fEditor instanceof ITextEditorExtension) 76 ((ITextEditorExtension) fEditor).removeRulerContextMenuListener(this); 77 } 78 79 fEditor= (ITextEditor)(targetEditor == null ? null : targetEditor.getAdapter(ITextEditor.class)); 80 fCallerAction= callerAction; 81 fAction= null; 82 83 if (fEditor != null) { 84 if (fEditor instanceof ITextEditorExtension) 85 ((ITextEditorExtension) fEditor).addRulerContextMenuListener(this); 86 87 IVerticalRulerInfo rulerInfo= (IVerticalRulerInfo) fEditor.getAdapter(IVerticalRulerInfo.class); 88 if (rulerInfo != null) { 89 fAction= createAction(fEditor, rulerInfo); 90 update(); 91 92 Control control= rulerInfo.getControl(); 93 if (control != null && !control.isDisposed()) 94 control.addMouseListener(this); 95 } 96 } 97 } 98 99 102 public void run(IAction callerAction) { 103 if (fAction != null) 104 fAction.run(); 105 } 106 107 111 public void runWithEvent(IAction action, Event event) { 112 if (fAction != null) 113 fAction.runWithEvent(event); 114 } 115 116 119 public void selectionChanged(IAction action, ISelection selection) { 120 125 } 127 128 131 private void update() { 132 if (fAction instanceof IUpdate) { 133 ((IUpdate) fAction).update(); 134 if (fCallerAction != null) { 135 fCallerAction.setText(fAction.getText()); 136 fCallerAction.setEnabled(fAction.isEnabled()); 137 } 138 } 139 } 140 141 144 public void menuAboutToShow(IMenuManager manager) { 145 update(); 146 } 147 148 151 public void mouseDoubleClick(MouseEvent e) { 152 } 153 154 157 public void mouseDown(MouseEvent e) { 158 update(); 159 } 160 161 164 public void mouseUp(MouseEvent e) { 165 } 166 } 167 | Popular Tags |