Eclipse 3M7
[phpeclipse.git] / net.sourceforge.phpeclipse / 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.PHPeclipsePlugin;
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(false);
31         
32         private String tag;
33         private String description;
34         private IConfigurationElement element;
35
36         /**
37          * Creates an variable definition
38          * 
39          * @param tag the variable tag
40          * @param description a short description of what the variable will expand to
41          * @param element the configuration element
42          */
43         /*package*/ ExternalToolVariable(String tag, String description, IConfigurationElement element) {
44                 super();
45                 this.tag = tag;
46                 this.description = description;
47                 this.element = element;
48         }
49         
50         /**
51          * Creates an instance of the class specified by
52          * the given element attribute name. Can return
53          * <code>null</code> if none or if problems creating
54          * the instance.
55          */
56         protected final Object createObject(String attributeName) {
57                 try {
58                         return element.createExecutableExtension(attributeName);
59                 } catch (CoreException e) {
60       PHPeclipsePlugin.getDefault().getLog().log(e.getStatus());
61                         return null;
62                 }
63         }
64         
65         /**
66          * Returns the component class to allow
67          * visual editing of the variable's value.
68          */
69         public final IVariableComponent getComponent() {
70                 String className = element.getAttribute(ExternalToolVariableRegistry.TAG_COMPONENT_CLASS);
71                 if (className == null || className.trim().length() == 0)
72                         return defaultComponent;
73                         
74                 Object component = createObject(ExternalToolVariableRegistry.TAG_COMPONENT_CLASS);
75                 if (component == null)
76                         return new DefaultVariableComponent(true);
77                 else
78                         return (IVariableComponent)component;
79         }
80         
81         /**
82          * Returns the variable's description
83          */
84         public final String getDescription() {
85                 return description;
86         }
87
88         /**
89          * Returns the variable's tag
90          */
91         public final String getTag() {
92                 return tag;
93         }
94
95
96         /**
97          * Default variable component implementation which does not
98          * allow variable value editing visually.
99          */     
100         private static final class DefaultVariableComponent implements IVariableComponent {
101                 private boolean showError = false;
102                 private Label message = null;
103                 
104                 public DefaultVariableComponent(boolean showError) {
105                         super();
106                         this.showError = showError;
107                 }
108                 
109                 /* (non-Javadoc)
110                  * Method declared on IVariableComponent.
111                  */
112                 public Control getControl() {
113                         return message;
114                 }
115                                 
116                 /* (non-Javadoc)
117                  * Method declared on IVariableComponent.
118                  */
119                 public void createContents(Composite parent, String varTag, IGroupDialogPage page) {
120                         if (showError) {
121                                 message = new Label(parent, SWT.NONE);
122                                 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
123                                 message.setLayoutData(data);
124                                 message.setFont(parent.getFont());
125                                 message.setText(ExternalToolsModelMessages.getString("ExternalToolVariable.componentErrorMessage")); //$NON-NLS-1$
126                                 message.setForeground(JFaceColors.getErrorText(message.getDisplay()));
127                         }
128                 }
129                 
130                 /* (non-Javadoc)
131                  * Method declared on IVariableComponent.
132                  */
133                 public String getVariableValue() {
134                         return null;
135                 }
136                 
137                 /* (non-Javadoc)
138                  * Method declared on IVariableComponent.
139                  */
140                 public boolean isValid() {
141                         return true;
142                 }
143                 
144                 /* (non-Javadoc)
145                  * Method declared on IVariableComponent.
146                  */
147                 public void setVariableValue(String varValue) {
148                 }
149
150                 /* (non-Javadoc)
151                  * Method declared on IVariableComponent.
152                  */
153                 public void validate() {
154                 }
155         }
156 }