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 org.osgi.framework.Bundle;
013:
014: import org.eclipse.core.runtime.Assert;
015: import org.eclipse.core.runtime.CoreException;
016: import org.eclipse.core.runtime.IConfigurationElement;
017: import org.eclipse.core.runtime.Platform;
018:
019: /**
020: * Describes an extension to the <code>quickdiff.referenceprovider</code> extension point.
021: *
022: * @see org.eclipse.ui.internal.texteditor.quickdiff.ReferenceSelectionAction
023: * @see QuickDiff
024: * @since 3.0
025: */
026: public class ReferenceProviderDescriptor {
027:
028: /** Name of the <code>label</code> attribute. */
029: private static final String LABEL_ATTRIBUTE = "label"; //$NON-NLS-1$
030: /** Name of the <code>class</code> attribute. */
031: private static final String CLASS_ATTRIBUTE = "class"; //$NON-NLS-1$
032: /** Name of the <code>id</code> attribute. */
033: private static final String ID_ATTRIBUTE = "id"; //$NON-NLS-1$
034: /** Name of the <code>default</code> attribute. */
035: private static final String DEFAULT_ATTRIBUTE = "default"; //$NON-NLS-1$
036:
037: /** The configuration element describing this extension. */
038: private IConfigurationElement fConfiguration;
039: /** The value of the <code>label</code> attribute, if read. */
040: private String fLabel;
041: /** The value of the <code>id</code> attribute, if read. */
042: private String fId;
043: /** The value of the <code>default</code> attribute, if read. */
044: private Boolean fDefault;
045: /** The bundle where this extension was defined. */
046: private Bundle fBundle;
047:
048: /**
049: * Creates a new descriptor for <code>element</code>.
050: * <p>
051: * This method is for internal use only.
052: * </p>
053: *
054: * @param element the extension point element to be described.
055: */
056: public ReferenceProviderDescriptor(IConfigurationElement element) {
057: Assert.isLegal(element != null);
058: fConfiguration = element;
059: }
060:
061: /**
062: * Reads (if needed) and returns the label of this extension.
063: *
064: * @return the label for this extension.
065: */
066: public String getLabel() {
067: if (fLabel == null) {
068: fLabel = fConfiguration.getAttribute(LABEL_ATTRIBUTE);
069: Assert.isNotNull(fLabel);
070: }
071: return fLabel;
072: }
073:
074: /**
075: * Reads (if needed) and returns the id of this extension.
076: *
077: * @return the id for this extension.
078: */
079: public String getId() {
080: if (fId == null) {
081: fId = fConfiguration.getAttribute(ID_ATTRIBUTE);
082: Assert.isNotNull(fId);
083: }
084: return fId;
085: }
086:
087: /**
088: * Creates a reference provider as described in the extension's xml. Sets the id on the provider.
089: * @return a new instance of the reference provider described by this descriptor.
090: */
091: public IQuickDiffReferenceProvider createProvider() {
092: try {
093: IQuickDiffReferenceProvider impl = (IQuickDiffReferenceProvider) fConfiguration
094: .createExecutableExtension(CLASS_ATTRIBUTE);
095: impl.setId(getId());
096: return impl;
097: } catch (CoreException e) {
098: return null;
099: }
100: }
101:
102: /**
103: * States whether the plug-in declaring this extension has been loaded already.
104: *
105: * @return <code>true</code> if the extension point's plug-in has been loaded, <code>false</code> otherwise.
106: */
107: public boolean isPluginLoaded() {
108: if (fBundle == null)
109: fBundle = Platform.getBundle(fConfiguration
110: .getContributor().getName());
111: return (fBundle != null && fBundle.getState() == Bundle.ACTIVE);
112: }
113:
114: /**
115: * Reads (if needed) and returns the default attribute value of this extension.
116: *
117: * @return the default attribute value for this extension.
118: * @deprecated as of 3.2, the default flag should not be used any longer
119: */
120: public boolean getDefault() {
121: if (fDefault == null) {
122: String def = fConfiguration.getAttribute(DEFAULT_ATTRIBUTE);
123: if ("true".equalsIgnoreCase(def)) //$NON-NLS-1$
124: fDefault = Boolean.TRUE;
125: else
126: fDefault = Boolean.FALSE;
127: }
128: return fDefault.booleanValue();
129: }
130:
131: }
|