1 11 package org.eclipse.ui.internal.editors.quickdiff; 12 13 import java.util.ResourceBundle ; 14 15 import org.eclipse.jface.viewers.IPostSelectionProvider; 16 import org.eclipse.jface.viewers.ISelection; 17 import org.eclipse.jface.viewers.ISelectionChangedListener; 18 import org.eclipse.jface.viewers.ISelectionProvider; 19 import org.eclipse.jface.viewers.SelectionChangedEvent; 20 21 import org.eclipse.jface.text.IRewriteTarget; 22 import org.eclipse.jface.text.ITextSelection; 23 import org.eclipse.jface.text.source.IAnnotationModel; 24 import org.eclipse.jface.text.source.IAnnotationModelExtension; 25 import org.eclipse.jface.text.source.IChangeRulerColumn; 26 import org.eclipse.jface.text.source.ILineDiffer; 27 import org.eclipse.jface.text.source.IVerticalRulerInfo; 28 29 import org.eclipse.ui.IEditorInput; 30 import org.eclipse.ui.texteditor.IDocumentProvider; 31 import org.eclipse.ui.texteditor.IEditorStatusLine; 32 import org.eclipse.ui.texteditor.ITextEditor; 33 import org.eclipse.ui.texteditor.TextEditorAction; 34 35 42 public abstract class QuickDiffRestoreAction extends TextEditorAction implements ISelectionChangedListener { 43 44 private int fLastLine= -1; 45 private final boolean fIsRulerAction; 46 47 55 QuickDiffRestoreAction(ResourceBundle bundle, String prefix, ITextEditor editor, boolean isRulerAction) { 56 super(bundle, prefix, editor); 57 fIsRulerAction= isRulerAction; 58 59 ISelectionProvider selectionProvider= editor.getSelectionProvider(); 60 if (selectionProvider instanceof IPostSelectionProvider) 61 ((IPostSelectionProvider)selectionProvider).addPostSelectionChangedListener(this); 62 } 63 64 70 protected abstract void runCompoundChange(); 71 72 75 public void run() { 76 ITextEditor editor= getTextEditor(); 77 if (editor == null || !validateEditorInputState()) 78 return; 79 IRewriteTarget target= (IRewriteTarget)editor.getAdapter(IRewriteTarget.class); 80 if (target != null) 81 target.beginCompoundChange(); 82 runCompoundChange(); 83 if (target != null) 84 target.endCompoundChange(); 85 86 } 87 88 91 public void update() { 92 96 super.update(); 97 98 setEnabled(computeEnablement()); 99 } 100 101 105 public void selectionChanged(SelectionChangedEvent event) { 106 update(); 107 } 108 109 115 protected boolean computeEnablement() { 116 if (!super.isEnabled()) 117 return false; 118 119 if (!canModifyEditor()) 120 return false; 121 122 fLastLine= computeLine(fIsRulerAction); 123 return true; 124 } 125 126 131 protected ITextSelection getSelection() { 132 if (getTextEditor() == null) 133 return null; 134 ISelectionProvider sp= getTextEditor().getSelectionProvider(); 135 if (sp == null) 136 return null; 137 ISelection s= sp.getSelection(); 138 if (s instanceof ITextSelection) 139 return (ITextSelection)s; 140 return null; 141 } 142 143 149 protected int getLastLine() { 150 return fLastLine; 151 } 152 153 160 private int computeLine(boolean useRulerInfo) { 161 int lastLine; 162 if (useRulerInfo) { 163 IVerticalRulerInfo ruler= getRuler(); 164 if (ruler == null) 165 lastLine= -1; 166 else 167 lastLine= ruler.getLineOfLastMouseButtonActivity(); 168 } else { 169 ITextSelection selection= getSelection(); 170 if (selection == null) 171 lastLine= -1; 172 else 173 lastLine= selection.getEndLine(); 174 } 175 return lastLine; 176 } 177 178 184 private IAnnotationModelExtension getModel() { 185 if (getTextEditor() == null) 186 return null; 187 IDocumentProvider provider= getTextEditor().getDocumentProvider(); 188 IEditorInput editorInput= getTextEditor().getEditorInput(); 189 IAnnotationModel m= provider.getAnnotationModel(editorInput); 190 if (m instanceof IAnnotationModelExtension) 191 return (IAnnotationModelExtension)m; 192 return null; 193 } 194 195 201 protected ILineDiffer getDiffer() { 202 IAnnotationModelExtension extension= getModel(); 203 if (extension != null) 204 return (ILineDiffer)extension.getAnnotationModel(IChangeRulerColumn.QUICK_DIFF_MODEL_ID); 205 return null; 206 } 207 208 213 protected IVerticalRulerInfo getRuler() { 214 if (getTextEditor() != null) 215 return (IVerticalRulerInfo)getTextEditor().getAdapter(IVerticalRulerInfo.class); 216 return null; 217 } 218 219 224 protected void setStatus(String string) { 225 if (getTextEditor() != null) { 226 IEditorStatusLine statusLine= (IEditorStatusLine) getTextEditor().getAdapter(IEditorStatusLine.class); 227 if (statusLine != null) { 228 statusLine.setMessage(true, string, null); 229 } 230 } 231 } 232 } 233 | Popular Tags |