001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.texteditor.quickdiff;
011:
012: import java.util.Collections;
013: import java.util.Iterator;
014: import java.util.List;
015: import java.util.ListIterator;
016:
017: import org.eclipse.core.runtime.Assert;
018:
019: import org.eclipse.jface.text.source.IAnnotationModel;
020:
021: import org.eclipse.ui.internal.texteditor.TextEditorPlugin;
022: import org.eclipse.ui.internal.texteditor.quickdiff.DocumentLineDiffer;
023: import org.eclipse.ui.internal.texteditor.quickdiff.QuickDiffExtensionsRegistry;
024: import org.eclipse.ui.texteditor.ITextEditor;
025:
026: /**
027: * Access class for the quick diff reference provider extension point.
028: * <p>
029: * This class may be instantiated, it is not intended to be subclassed.
030: * </p>
031: * @since 3.0
032: */
033: public class QuickDiff {
034:
035: /**
036: * Creates a new instance.
037: */
038: public QuickDiff() {
039: }
040:
041: /**
042: * Returns the descriptor of the "last saved version" reference provider.
043: * <p>
044: * Clients should not cache this value because it can change when plug-ins get dynamically added or removed.
045: * </p>
046: *
047: * @return the descriptor of "last saved version" reference provider or <code>null</code> if none
048: */
049: public ReferenceProviderDescriptor getDefaultProvider() {
050: QuickDiffExtensionsRegistry registry = TextEditorPlugin
051: .getDefault().getQuickDiffExtensionRegistry();
052: if (registry != null)
053: return registry.getDefaultProvider();
054:
055: return null;
056: }
057:
058: /**
059: * Returns a non-modifiable list of <code>ReferenceProviderDescriptor</code> describing all extension
060: * to the <code>quickDiffReferenceProvider</code> extension point.
061: * <p>
062: * Clients should not cache this list because it can change when plug-ins get dynamically added or removed.
063: * </p>
064: *
065: * @return the non-modifiable list of extensions to the <code>quickDiffReferenceProvider</code> extension point.
066: */
067: public List getReferenceProviderDescriptors() {
068: QuickDiffExtensionsRegistry registry = TextEditorPlugin
069: .getDefault().getQuickDiffExtensionRegistry();
070: if (registry != null)
071: return registry.getReferenceProviderDescriptors();
072:
073: return Collections.EMPTY_LIST;
074: }
075:
076: /**
077: * Returns the quick diff reference provider registered under <code>id</code>, or the default
078: * reference provider. The returned provider gets its editor set to <code>editor</code>. If neither
079: * the requested provider nor the default provider return <code>true</code> from <code>isEnabled</code> after
080: * having the editor set, <code>null</code> is returned.
081: * <p>
082: * Clients should not cache this value because it can change when plug-ins get dynamically added or removed.
083: * </p>
084: *
085: * @param editor the editor to be installed with the returned provider
086: * @param id the id as specified in the <code>plugin.xml</code> that installs the reference provider
087: * @return the reference provider registered under <code>id</code>, or the default reference provider, or <code>null</code>
088: */
089: public IQuickDiffReferenceProvider getReferenceProviderOrDefault(
090: ITextEditor editor, String id) {
091: Assert.isNotNull(editor);
092: Assert.isNotNull(id);
093:
094: List descs = getReferenceProviderDescriptors();
095: // try to fetch preferred provider; load if needed
096: for (Iterator iter = descs.iterator(); iter.hasNext();) {
097: ReferenceProviderDescriptor desc = (ReferenceProviderDescriptor) iter
098: .next();
099: if (desc.getId().equals(id)) {
100: IQuickDiffReferenceProvider provider = desc
101: .createProvider();
102: if (provider != null) {
103: provider.setActiveEditor(editor);
104: if (provider.isEnabled())
105: return provider;
106: provider.dispose();
107: provider = null;
108: }
109: }
110: }
111:
112: for (ListIterator iter = descs.listIterator(descs.size()); iter
113: .hasPrevious();) {
114: ReferenceProviderDescriptor desc = (ReferenceProviderDescriptor) iter
115: .previous();
116: IQuickDiffReferenceProvider provider = desc
117: .createProvider();
118: if (provider != null) {
119: provider.setActiveEditor(editor);
120: if (provider.isEnabled())
121: return provider;
122: provider.dispose();
123: provider = null;
124: }
125: }
126:
127: return null;
128: }
129:
130: /**
131: * Creates a new line differ annotation model with its reference provider set to the reference provider
132: * obtained by calling <code>getReferenceProviderOrDefault(editor, id)</code>.
133: *
134: * @param editor the editor to be installed with the returned provider
135: * @param id the id as specified in the <code>plugin.xml</code> that installs the reference provider
136: * @return a quick diff annotation model
137: */
138: public IAnnotationModel createQuickDiffAnnotationModel(
139: ITextEditor editor, String id) {
140: IQuickDiffReferenceProvider provider = getReferenceProviderOrDefault(
141: editor, id);
142: if (provider != null) {
143: DocumentLineDiffer differ = new DocumentLineDiffer();
144: differ.setReferenceProvider(provider);
145: return differ;
146: }
147: return null;
148: }
149:
150: /**
151: * Returns the identifier of the quick diff provider installed with the given diff annotation
152: * model, or the empty string if it is not a diff annotation model or has no configured diff
153: * provider.
154: *
155: * @param differ a diff annotation model
156: * @return the reference provider id, or the empty string for none
157: * @since 3.2
158: */
159: public Object getConfiguredQuickDiffProvider(IAnnotationModel differ) {
160: if (differ instanceof DocumentLineDiffer) {
161: DocumentLineDiffer lineDiffer = (DocumentLineDiffer) differ;
162: IQuickDiffReferenceProvider provider = lineDiffer
163: .getReferenceProvider();
164: if (provider != null)
165: return provider.getId();
166: }
167: return ""; //$NON-NLS-1$
168: }
169:
170: }
|