1 11 12 package org.eclipse.ui.internal.texteditor.quickdiff; 13 14 import org.eclipse.core.runtime.Assert; 15 16 import org.eclipse.jface.action.Action; 17 18 import org.eclipse.jface.text.source.IAnnotationModel; 19 import org.eclipse.jface.text.source.IAnnotationModelExtension; 20 import org.eclipse.jface.text.source.IChangeRulerColumn; 21 22 import org.eclipse.ui.IEditorInput; 23 import org.eclipse.ui.texteditor.quickdiff.IQuickDiffReferenceProvider; 24 import org.eclipse.ui.texteditor.quickdiff.ReferenceProviderDescriptor; 25 import org.eclipse.ui.texteditor.IDocumentProvider; 26 import org.eclipse.ui.texteditor.ITextEditor; 27 import org.eclipse.ui.texteditor.ITextEditorExtension3; 28 import org.eclipse.ui.texteditor.IUpdate; 29 30 31 39 public class ReferenceSelectionAction extends Action implements IUpdate { 40 41 42 private ITextEditor fEditor= null; 43 44 private final ReferenceProviderDescriptor fDescriptor; 45 46 private IQuickDiffReferenceProvider fProvider; 47 48 54 public ReferenceSelectionAction(ReferenceProviderDescriptor descriptor, ITextEditor editor) { 55 super("", AS_RADIO_BUTTON); setChecked(false); 57 setEnabled(true); 58 Assert.isLegal(descriptor != null); 59 fDescriptor= descriptor; 60 fEditor= editor; 61 update(); 62 } 63 64 69 private IQuickDiffReferenceProvider getProvider() { 70 if (fProvider == null) { 71 fProvider= fDescriptor.createProvider(); 72 } 73 return fProvider; 74 } 75 76 79 public void run() { 80 81 DocumentLineDiffer differ= getDiffer(true); if (differ == null) 83 return; 84 85 if (fEditor instanceof ITextEditorExtension3) { 86 ITextEditorExtension3 extension= (ITextEditorExtension3) fEditor; 87 IQuickDiffReferenceProvider provider= getProvider(); 88 if (provider != null) { 89 provider.setActiveEditor(fEditor); 90 if (provider.isEnabled()) { 91 differ.setReferenceProvider(provider); 92 extension.showChangeInformation(true); 93 setEnabled(true); 94 } else 95 setEnabled(false); 96 } 97 } 98 } 99 100 103 public void update() { 104 110 setText(fDescriptor.getLabel()); 111 DocumentLineDiffer differ= getDiffer(false); setChecked(false); 113 if (differ != null) { 114 IQuickDiffReferenceProvider provider= differ.getReferenceProvider(); 115 if (provider != null && provider.getId().equals(fDescriptor.getId())) { 116 setChecked(true); 117 } 118 } 119 120 if (fDescriptor.isPluginLoaded()) { 121 getProvider(); 122 if (fProvider == null) { 123 setEnabled(false); 124 } else { 125 fProvider.setActiveEditor(fEditor); 126 setEnabled(fProvider.isEnabled()); 127 } 128 } else { 129 setEnabled(true); 131 } 132 } 133 134 142 private DocumentLineDiffer getDiffer(boolean createIfNeeded) { 143 if (fEditor == null) 145 return null; 146 147 IDocumentProvider provider= fEditor.getDocumentProvider(); 148 IEditorInput editorInput= fEditor.getEditorInput(); 149 if (provider == null || editorInput == null) 150 return null; 151 152 IAnnotationModel m= provider.getAnnotationModel(editorInput); 153 IAnnotationModelExtension model= null; 154 if (m instanceof IAnnotationModelExtension) { 155 model= (IAnnotationModelExtension)m; 156 } else { 157 return null; 158 } 159 160 DocumentLineDiffer differ= (DocumentLineDiffer)model.getAnnotationModel(IChangeRulerColumn.QUICK_DIFF_MODEL_ID); 162 163 if (differ == null && createIfNeeded) { 165 differ= new DocumentLineDiffer(); 166 model.addAnnotationModel(IChangeRulerColumn.QUICK_DIFF_MODEL_ID, differ); 167 } 168 169 return differ; 170 } 171 } 172 | Popular Tags |