A massive organize imports and formatting of the sources using default Eclipse code...
[phpeclipse.git] / net.sourceforge.phpeclipse.externaltools / src / net / sourceforge / phpdt / externaltools / internal / dialog / ExternalToolVariableForm.java
1 package net.sourceforge.phpdt.externaltools.internal.dialog;
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.registry.ExternalToolVariable;
14 import net.sourceforge.phpdt.externaltools.model.ToolUtil;
15 import net.sourceforge.phpdt.externaltools.variable.IVariableComponent;
16
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.custom.StackLayout;
19 import org.eclipse.swt.events.SelectionAdapter;
20 import org.eclipse.swt.events.SelectionEvent;
21 import org.eclipse.swt.graphics.Font;
22 import org.eclipse.swt.graphics.Point;
23 import org.eclipse.swt.layout.GridData;
24 import org.eclipse.swt.layout.GridLayout;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Control;
27 import org.eclipse.swt.widgets.Label;
28 import org.eclipse.swt.widgets.List;
29
30 /**
31  * Visual grouping of controls that allows the user to select a variable and
32  * configure it with extra information.
33  */
34 public class ExternalToolVariableForm {
35         private static final int VISIBLE_ITEM_COUNT = 9;
36
37         private String variableListLabelText;
38
39         private ExternalToolVariable[] variables;
40
41         private IVariableComponent[] components;
42
43         private IGroupDialogPage page;
44
45         private Label variableListLabel;
46
47         private List variableList;
48
49         private Composite variableComposite;
50
51         private StackLayout variableLayout;
52
53         private int activeComponentIndex = -1;
54
55         /**
56          * Creates the visual grouping
57          * 
58          * @param variableListLabelText
59          *            the label text to use for identifying the list of variables
60          * @param variables
61          *            the collection of variables to display to the user
62          */
63         public ExternalToolVariableForm(String variableListLabelText,
64                         ExternalToolVariable[] variables) {
65                 super();
66                 this.variableListLabelText = variableListLabelText;
67                 this.variables = variables;
68                 this.components = new IVariableComponent[variables.length];
69         }
70
71         public Composite createContents(Composite parent, IGroupDialogPage page) {
72                 Font font = parent.getFont();
73
74                 this.page = page;
75
76                 Composite mainComposite = new Composite(parent, SWT.NONE);
77                 GridLayout layout = new GridLayout();
78                 layout.marginWidth = 0;
79                 layout.marginHeight = 0;
80                 layout.numColumns = 1;
81                 GridData data = new GridData(GridData.FILL_BOTH);
82                 mainComposite.setLayout(layout);
83                 mainComposite.setLayoutData(data);
84
85                 variableListLabel = new Label(mainComposite, SWT.NONE);
86                 variableListLabel.setText(variableListLabelText);
87                 data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
88                 data.horizontalSpan = 1;
89                 variableListLabel.setLayoutData(data);
90                 variableListLabel.setFont(font);
91
92                 variableList = new List(mainComposite, SWT.SINGLE | SWT.BORDER
93                                 | SWT.H_SCROLL | SWT.V_SCROLL);
94                 data = new GridData(GridData.FILL_HORIZONTAL);
95                 data.heightHint = variableList.getItemHeight() * VISIBLE_ITEM_COUNT;
96                 variableList.setLayoutData(data);
97                 variableList.setFont(font);
98
99                 variableComposite = new Composite(mainComposite, SWT.NONE);
100                 variableLayout = new StackLayout();
101                 variableLayout.marginWidth = 0;
102                 variableLayout.marginHeight = 0;
103                 data = new GridData(GridData.FILL_BOTH);
104                 variableComposite.setLayout(variableLayout);
105                 variableComposite.setLayoutData(data);
106                 variableComposite.setFont(font);
107
108                 createVariableComponents(data);
109
110                 populateVariableList();
111
112                 variableList.addSelectionListener(new SelectionAdapter() {
113                         public void widgetSelected(SelectionEvent e) {
114                                 updateVariableComposite(null, false);
115                         }
116                 });
117
118                 setEnabled(true);
119                 return mainComposite;
120         }
121
122         /**
123          * Creates the visual component for each variable and determine the initial
124          * size so the form can be layout properly.
125          */
126         private void createVariableComponents(GridData data) {
127                 for (int i = 0; i < variables.length; i++) {
128                         ExternalToolVariable var = variables[i];
129                         components[i] = var.getComponent();
130                         components[i].createContents(variableComposite, var.getTag(), page);
131                         Control control = components[i].getControl();
132                         if (control != null) {
133                                 Point newSize = control.computeSize(SWT.DEFAULT, SWT.DEFAULT,
134                                                 true);
135                                 data.widthHint = Math.max(newSize.x, data.widthHint);
136                                 data.heightHint = Math.max(newSize.y, data.heightHint);
137                         }
138                 }
139         }
140
141         /**
142          * Returns the formatted variable or <code>null</code> if none selected.
143          */
144         public String getSelectedVariable() {
145                 if (activeComponentIndex != -1) {
146                         String varValue = components[activeComponentIndex]
147                                         .getVariableValue();
148                         return ToolUtil.buildVariableTag(variables[activeComponentIndex]
149                                         .getTag(), varValue);
150                 }
151
152                 return null;
153         }
154
155         /**
156          * Returns whether the current variable selection is valid, including the
157          * selected variable value.
158          */
159         public boolean isValid() {
160                 if (activeComponentIndex != -1)
161                         return components[activeComponentIndex].isValid();
162
163                 return true;
164         }
165
166         private void populateVariableList() {
167                 String[] items = new String[variables.length];
168                 StringBuffer buffer = new StringBuffer(80);
169                 for (int i = 0; i < variables.length; i++) {
170                         ToolUtil.buildVariableTag(variables[i].getTag(), null, buffer);
171                         buffer.append(" - "); //$NON-NLS-1$
172                         buffer.append(variables[i].getDescription());
173                         items[i] = buffer.toString();
174                         buffer.setLength(0);
175                 }
176                 variableList.setItems(items);
177         }
178
179         public void selectVariable(String varName, String varValue) {
180                 if (varName != null && varName.length() > 0) {
181                         for (int i = 0; i < variables.length; i++) {
182                                 if (varName.equals(variables[i].getTag())) {
183                                         variableList.select(i);
184                                         updateVariableComposite(varValue, true);
185                                         return;
186                                 }
187                         }
188                 }
189
190                 variableList.deselectAll();
191                 updateVariableComposite(varValue, false);
192         }
193
194         private void setComponentVisible(int index) {
195                 if (index == -1)
196                         variableLayout.topControl = null;
197                 else
198                         variableLayout.topControl = components[index].getControl();
199                 variableComposite.layout();
200         }
201
202         /**
203          * Enables or disables the variable form controls.
204          */
205         public void setEnabled(boolean enabled) {
206                 variableListLabel.setEnabled(enabled);
207                 variableList.setEnabled(enabled);
208                 if (enabled && variableList.getSelection().length == 0) {
209                         if (variableList.getItemCount() > 0) {
210                                 variableList.select(0);
211                                 activeComponentIndex = 0;
212                         }
213                 }
214                 variableComposite.setVisible(enabled);
215         }
216
217         private void updateVariableComposite(String value, boolean setValue) {
218                 activeComponentIndex = variableList.getSelectionIndex();
219                 setComponentVisible(activeComponentIndex);
220                 if (activeComponentIndex != -1 && setValue)
221                         components[activeComponentIndex].setVariableValue(value);
222         }
223
224         /**
225          * Validates the current variable selection is and its value are acceptable.
226          */
227         public void validate() {
228                 if (activeComponentIndex != -1)
229                         components[activeComponentIndex].validate();
230         }
231 }