KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > console > ConsoleFactoryExtension


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.console;
12
13 import java.net.URL JavaDoc;
14
15 import org.eclipse.core.expressions.EvaluationContext;
16 import org.eclipse.core.expressions.EvaluationResult;
17 import org.eclipse.core.expressions.Expression;
18 import org.eclipse.core.expressions.ExpressionConverter;
19 import org.eclipse.core.expressions.ExpressionTagNames;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.FileLocator;
22 import org.eclipse.core.runtime.IConfigurationElement;
23 import org.eclipse.core.runtime.Path;
24 import org.eclipse.core.runtime.Platform;
25 import org.eclipse.jface.resource.ImageDescriptor;
26 import org.eclipse.ui.IPluginContribution;
27 import org.eclipse.ui.console.ConsolePlugin;
28 import org.eclipse.ui.console.IConsoleFactory;
29 import org.osgi.framework.Bundle;
30
31 /**
32  * @since 3.1
33  */

34 public class ConsoleFactoryExtension implements IPluginContribution {
35
36     private IConfigurationElement fConfig;
37     private Expression fEnablementExpression;
38     private String JavaDoc fLabel;
39     private ImageDescriptor fImageDescriptor;
40     private IConsoleFactory fFactory;
41
42     ConsoleFactoryExtension(IConfigurationElement config) {
43         fConfig = config;
44     }
45
46     /* (non-Javadoc)
47      * @see org.eclipse.ui.IPluginContribution#getLocalId()
48      */

49     public String JavaDoc getLocalId() {
50         return fConfig.getAttribute("id"); //$NON-NLS-1$
51
}
52
53     /* (non-Javadoc)
54      * @see org.eclipse.ui.IPluginContribution#getPluginId()
55      */

56     public String JavaDoc getPluginId() {
57         return fConfig.getContributor().getName();
58     }
59
60     public boolean isEnabled() {
61         try {
62             Expression enablementExpression = getEnablementExpression();
63             if (enablementExpression == null) {
64                 return true;
65             }
66             EvaluationContext context = new EvaluationContext(null, this);
67             EvaluationResult evaluationResult = enablementExpression.evaluate(context);
68             return evaluationResult != EvaluationResult.FALSE;
69         } catch (CoreException e) {
70             ConsolePlugin.log(e);
71             return false;
72         }
73     }
74     
75     public Expression getEnablementExpression() throws CoreException {
76         if (fEnablementExpression == null) {
77             IConfigurationElement[] elements = fConfig.getChildren(ExpressionTagNames.ENABLEMENT);
78             IConfigurationElement enablement = elements.length > 0 ? elements[0] : null;
79
80             if (enablement != null) {
81                 fEnablementExpression = ExpressionConverter.getDefault().perform(enablement);
82             }
83         }
84         return fEnablementExpression;
85     }
86
87     public String JavaDoc getLabel() {
88         if (fLabel == null) {
89             fLabel = fConfig.getAttribute("label"); //$NON-NLS-1$
90
}
91         return fLabel;
92     }
93
94     /**
95      * @return
96      */

97     public ImageDescriptor getImageDescriptor() {
98         if (fImageDescriptor == null) {
99             String JavaDoc path = fConfig.getAttribute("icon"); //$NON-NLS-1$
100
if (path != null) {
101                 Bundle bundle = Platform.getBundle(getPluginId());
102                 URL JavaDoc url = FileLocator.find(bundle, new Path(path), null);
103                 if (url != null) {
104                     fImageDescriptor = ImageDescriptor.createFromURL(url);
105                 }
106             }
107         }
108         return fImageDescriptor;
109     }
110
111     /**
112      * @return
113      * @throws CoreException
114      */

115     public IConsoleFactory createFactory() throws CoreException {
116         if (fFactory == null) {
117             fFactory = (IConsoleFactory) fConfig.createExecutableExtension("class"); //$NON-NLS-1$
118
}
119         return fFactory;
120     }
121 }
122
Popular Tags