Refactory: externaltools plugin.
[phpeclipse.git] / net.sourceforge.phpeclipse.externaltools / src / net / sourceforge / phpdt / externaltools / internal / registry / ExternalToolVariable.java
1 package net.sourceforge.phpdt.externaltools.internal.registry;
2
3 /**********************************************************************
4  Copyright (c) 2002 IBM Corp. and others. All rights reserved.
5  This file is made available under the terms of the Common Public License v1.0
6  which accompanies this distribution, and is available at
7  http://www.eclipse.org/legal/cpl-v10.html
8  �
9  Contributors:
10  **********************************************************************/
11
12 import net.sourceforge.phpdt.externaltools.group.IGroupDialogPage;
13 import net.sourceforge.phpdt.externaltools.internal.model.ExternalToolsModelMessages;
14 import net.sourceforge.phpdt.externaltools.variable.IVariableComponent;
15 import net.sourceforge.phpeclipse.externaltools.ExternalToolsPlugin;
16
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IConfigurationElement;
19 import org.eclipse.jface.resource.JFaceColors;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.layout.GridData;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.Control;
24 import org.eclipse.swt.widgets.Label;
25
26 /**
27  * Abtract representation of the different variables.
28  */
29 public abstract class ExternalToolVariable {
30         private static final IVariableComponent defaultComponent = new DefaultVariableComponent(
31                         false);
32
33         private String tag;
34
35         private String description;
36
37         private IConfigurationElement element;
38
39         /**
40          * Creates an variable definition
41          * 
42          * @param tag
43          *            the variable tag
44          * @param description
45          *            a short description of what the variable will expand to
46          * @param element
47          *            the configuration element
48          */
49         /* package */ExternalToolVariable(String tag, String description,
50                         IConfigurationElement element) {
51                 super();
52                 this.tag = tag;
53                 this.description = description;
54                 this.element = element;
55         }
56
57         /**
58          * Creates an instance of the class specified by the given element attribute
59          * name. Can return <code>null</code> if none or if problems creating the
60          * instance.
61          */
62         protected final Object createObject(String attributeName) {
63                 try {
64                         return element.createExecutableExtension(attributeName);
65                 } catch (CoreException e) {
66                         ExternalToolsPlugin.getDefault().getLog().log(e.getStatus());
67                         return null;
68                 }
69         }
70
71         /**
72          * Returns the component class to allow visual editing of the variable's
73          * value.
74          */
75         public final IVariableComponent getComponent() {
76                 String className = element
77                                 .getAttribute(ExternalToolVariableRegistry.TAG_COMPONENT_CLASS);
78                 if (className == null || className.trim().length() == 0)
79                         return defaultComponent;
80
81                 Object component = createObject(ExternalToolVariableRegistry.TAG_COMPONENT_CLASS);
82                 if (component == null)
83                         return new DefaultVariableComponent(true);
84                 else
85                         return (IVariableComponent) component;
86         }
87
88         /**
89          * Returns the variable's description
90          */
91         public final String getDescription() {
92                 return description;
93         }
94
95         /**
96          * Returns the variable's tag
97          */
98         public final String getTag() {
99                 return tag;
100         }
101
102         /**
103          * Default variable component implementation which does not allow variable
104          * value editing visually.
105          */
106         private static final class DefaultVariableComponent implements
107                         IVariableComponent {
108                 private boolean showError = false;
109
110                 private Label message = null;
111
112                 public DefaultVariableComponent(boolean showError) {
113                         super();
114                         this.showError = showError;
115                 }
116
117                 /*
118                  * (non-Javadoc) Method declared on IVariableComponent.
119                  */
120                 public Control getControl() {
121                         return message;
122                 }
123
124                 /*
125                  * (non-Javadoc) Method declared on IVariableComponent.
126                  */
127                 public void createContents(Composite parent, String varTag,
128                                 IGroupDialogPage page) {
129                         if (showError) {
130                                 message = new Label(parent, SWT.NONE);
131                                 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
132                                 message.setLayoutData(data);
133                                 message.setFont(parent.getFont());
134                                 message
135                                                 .setText(ExternalToolsModelMessages
136                                                                 .getString("ExternalToolVariable.componentErrorMessage")); //$NON-NLS-1$
137                                 message.setForeground(JFaceColors.getErrorText(message
138                                                 .getDisplay()));
139                         }
140                 }
141
142                 /*
143                  * (non-Javadoc) Method declared on IVariableComponent.
144                  */
145                 public String getVariableValue() {
146                         return null;
147                 }
148
149                 /*
150                  * (non-Javadoc) Method declared on IVariableComponent.
151                  */
152                 public boolean isValid() {
153                         return true;
154                 }
155
156                 /*
157                  * (non-Javadoc) Method declared on IVariableComponent.
158                  */
159                 public void setVariableValue(String varValue) {
160                 }
161
162                 /*
163                  * (non-Javadoc) Method declared on IVariableComponent.
164                  */
165                 public void validate() {
166                 }
167         }
168 }