1 11 package org.eclipse.ui.internal.texteditor.quickdiff; 12 13 import org.eclipse.jface.viewers.IPostSelectionProvider; 14 import org.eclipse.jface.viewers.ISelection; 15 import org.eclipse.jface.viewers.ISelectionChangedListener; 16 import org.eclipse.jface.viewers.ISelectionProvider; 17 import org.eclipse.jface.viewers.SelectionChangedEvent; 18 19 import org.eclipse.jface.text.IRewriteTarget; 20 import org.eclipse.jface.text.ITextSelection; 21 import org.eclipse.jface.text.source.IAnnotationModel; 22 import org.eclipse.jface.text.source.IAnnotationModelExtension; 23 import org.eclipse.jface.text.source.IChangeRulerColumn; 24 import org.eclipse.jface.text.source.ILineDiffer; 25 import org.eclipse.jface.text.source.IVerticalRulerInfo; 26 27 import org.eclipse.ui.IEditorInput; 28 import org.eclipse.ui.texteditor.IDocumentProvider; 29 import org.eclipse.ui.texteditor.IEditorStatusLine; 30 import org.eclipse.ui.texteditor.ITextEditor; 31 import org.eclipse.ui.texteditor.TextEditorAction; 32 33 40 public abstract class QuickDiffRestoreAction extends TextEditorAction implements ISelectionChangedListener { 41 42 private int fLastLine= -1; 43 private final boolean fIsRulerAction; 44 45 52 QuickDiffRestoreAction(String prefix, ITextEditor editor, boolean isRulerAction) { 53 super(QuickDiffMessages.getBundleForConstructedKeys(), prefix, editor); 54 fIsRulerAction= isRulerAction; 55 56 ISelectionProvider selectionProvider= editor.getSelectionProvider(); 57 if (selectionProvider instanceof IPostSelectionProvider) 58 ((IPostSelectionProvider)selectionProvider).addPostSelectionChangedListener(this); 59 } 60 61 67 protected abstract void runCompoundChange(); 68 69 72 public void run() { 73 ITextEditor editor= getTextEditor(); 74 if (editor == null || !validateEditorInputState()) 75 return; 76 IRewriteTarget target= (IRewriteTarget)editor.getAdapter(IRewriteTarget.class); 77 if (target != null) 78 target.beginCompoundChange(); 79 runCompoundChange(); 80 if (target != null) 81 target.endCompoundChange(); 82 83 } 84 85 88 public void update() { 89 93 super.update(); 94 95 setEnabled(computeEnablement()); 96 } 97 98 102 public void selectionChanged(SelectionChangedEvent event) { 103 update(); 104 } 105 106 112 protected boolean computeEnablement() { 113 if (!super.isEnabled()) 114 return false; 115 116 if (!canModifyEditor()) 117 return false; 118 119 fLastLine= computeLine(fIsRulerAction); 120 return true; 121 } 122 123 128 protected ITextSelection getSelection() { 129 if (getTextEditor() == null) 130 return null; 131 ISelectionProvider sp= getTextEditor().getSelectionProvider(); 132 if (sp == null) 133 return null; 134 ISelection s= sp.getSelection(); 135 if (s instanceof ITextSelection) 136 return (ITextSelection)s; 137 return null; 138 } 139 140 146 protected int getLastLine() { 147 return fLastLine; 148 } 149 150 157 private int computeLine(boolean useRulerInfo) { 158 int lastLine; 159 if (useRulerInfo) { 160 IVerticalRulerInfo ruler= getRuler(); 161 if (ruler == null) 162 lastLine= -1; 163 else 164 lastLine= ruler.getLineOfLastMouseButtonActivity(); 165 } else { 166 ITextSelection selection= getSelection(); 167 if (selection == null) 168 lastLine= -1; 169 else 170 lastLine= selection.getEndLine(); 171 } 172 return lastLine; 173 } 174 175 181 private IAnnotationModelExtension getModel() { 182 if (getTextEditor() == null) 183 return null; 184 IDocumentProvider provider= getTextEditor().getDocumentProvider(); 185 IEditorInput editorInput= getTextEditor().getEditorInput(); 186 IAnnotationModel m= provider.getAnnotationModel(editorInput); 187 if (m instanceof IAnnotationModelExtension) 188 return (IAnnotationModelExtension)m; 189 return null; 190 } 191 192 198 protected ILineDiffer getDiffer() { 199 IAnnotationModelExtension extension= getModel(); 200 if (extension != null) 201 return (ILineDiffer)extension.getAnnotationModel(IChangeRulerColumn.QUICK_DIFF_MODEL_ID); 202 return null; 203 } 204 205 210 protected IVerticalRulerInfo getRuler() { 211 if (getTextEditor() != null) 212 return (IVerticalRulerInfo)getTextEditor().getAdapter(IVerticalRulerInfo.class); 213 return null; 214 } 215 216 221 protected void setStatus(String string) { 222 if (getTextEditor() != null) { 223 IEditorStatusLine statusLine= (IEditorStatusLine) getTextEditor().getAdapter(IEditorStatusLine.class); 224 if (statusLine != null) { 225 statusLine.setMessage(true, string, null); 226 } 227 } 228 } 229 } 230 | Popular Tags |