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.internal.console;
011:
012: import java.net.URL;
013:
014: import org.eclipse.core.expressions.EvaluationContext;
015: import org.eclipse.core.expressions.EvaluationResult;
016: import org.eclipse.core.expressions.Expression;
017: import org.eclipse.core.expressions.ExpressionConverter;
018: import org.eclipse.core.expressions.ExpressionTagNames;
019: import org.eclipse.core.runtime.CoreException;
020: import org.eclipse.core.runtime.FileLocator;
021: import org.eclipse.core.runtime.IConfigurationElement;
022: import org.eclipse.core.runtime.Path;
023: import org.eclipse.core.runtime.Platform;
024: import org.eclipse.jface.resource.ImageDescriptor;
025: import org.eclipse.ui.IPluginContribution;
026: import org.eclipse.ui.console.ConsolePlugin;
027: import org.eclipse.ui.console.IConsoleFactory;
028: import org.osgi.framework.Bundle;
029:
030: /**
031: * @since 3.1
032: */
033: public class ConsoleFactoryExtension implements IPluginContribution {
034:
035: private IConfigurationElement fConfig;
036: private Expression fEnablementExpression;
037: private String fLabel;
038: private ImageDescriptor fImageDescriptor;
039: private IConsoleFactory fFactory;
040:
041: ConsoleFactoryExtension(IConfigurationElement config) {
042: fConfig = config;
043: }
044:
045: /* (non-Javadoc)
046: * @see org.eclipse.ui.IPluginContribution#getLocalId()
047: */
048: public String getLocalId() {
049: return fConfig.getAttribute("id"); //$NON-NLS-1$
050: }
051:
052: /* (non-Javadoc)
053: * @see org.eclipse.ui.IPluginContribution#getPluginId()
054: */
055: public String getPluginId() {
056: return fConfig.getContributor().getName();
057: }
058:
059: public boolean isEnabled() {
060: try {
061: Expression enablementExpression = getEnablementExpression();
062: if (enablementExpression == null) {
063: return true;
064: }
065: EvaluationContext context = new EvaluationContext(null,
066: this );
067: EvaluationResult evaluationResult = enablementExpression
068: .evaluate(context);
069: return evaluationResult != EvaluationResult.FALSE;
070: } catch (CoreException e) {
071: ConsolePlugin.log(e);
072: return false;
073: }
074: }
075:
076: public Expression getEnablementExpression() throws CoreException {
077: if (fEnablementExpression == null) {
078: IConfigurationElement[] elements = fConfig
079: .getChildren(ExpressionTagNames.ENABLEMENT);
080: IConfigurationElement enablement = elements.length > 0 ? elements[0]
081: : null;
082:
083: if (enablement != null) {
084: fEnablementExpression = ExpressionConverter
085: .getDefault().perform(enablement);
086: }
087: }
088: return fEnablementExpression;
089: }
090:
091: public String getLabel() {
092: if (fLabel == null) {
093: fLabel = fConfig.getAttribute("label"); //$NON-NLS-1$
094: }
095: return fLabel;
096: }
097:
098: /**
099: * @return
100: */
101: public ImageDescriptor getImageDescriptor() {
102: if (fImageDescriptor == null) {
103: String path = fConfig.getAttribute("icon"); //$NON-NLS-1$
104: if (path != null) {
105: Bundle bundle = Platform.getBundle(getPluginId());
106: URL url = FileLocator
107: .find(bundle, new Path(path), null);
108: if (url != null) {
109: fImageDescriptor = ImageDescriptor
110: .createFromURL(url);
111: }
112: }
113: }
114: return fImageDescriptor;
115: }
116:
117: /**
118: * @return
119: * @throws CoreException
120: */
121: public IConsoleFactory createFactory() throws CoreException {
122: if (fFactory == null) {
123: fFactory = (IConsoleFactory) fConfig
124: .createExecutableExtension("class"); //$NON-NLS-1$
125: }
126: return fFactory;
127: }
128: }
|