1 package net.sourceforge.phpdt.externaltools.internal.dialog;
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
10 **********************************************************************/
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;
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;
31 * Visual grouping of controls that allows the user to
32 * select a variable and configure it with extra
35 public class ExternalToolVariableForm {
36 private static final int VISIBLE_ITEM_COUNT = 9;
38 private String variableListLabelText;
39 private ExternalToolVariable[] variables;
40 private IVariableComponent[] components;
41 private IGroupDialogPage page;
43 private Label variableListLabel;
44 private List variableList;
45 private Composite variableComposite;
46 private StackLayout variableLayout;
47 private int activeComponentIndex = -1;
50 * Creates the visual grouping
52 * @param variableListLabelText the label text to use for identifying the list of variables
53 * @param variables the collection of variables to display to the user
55 public ExternalToolVariableForm(String variableListLabelText, ExternalToolVariable[] variables) {
57 this.variableListLabelText = variableListLabelText;
58 this.variables = variables;
59 this.components = new IVariableComponent[variables.length];
62 public Composite createContents(Composite parent, IGroupDialogPage page) {
63 Font font = parent.getFont();
67 Composite mainComposite = new Composite(parent, SWT.NONE);
68 GridLayout layout = new GridLayout();
69 layout.marginWidth = 0;
70 layout.marginHeight = 0;
71 layout.numColumns = 1;
72 GridData data = new GridData(GridData.FILL_BOTH);
73 mainComposite.setLayout(layout);
74 mainComposite.setLayoutData(data);
76 variableListLabel = new Label(mainComposite, SWT.NONE);
77 variableListLabel.setText(variableListLabelText);
78 data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
79 data.horizontalSpan = 1;
80 variableListLabel.setLayoutData(data);
81 variableListLabel.setFont(font);
83 variableList = new List(mainComposite, SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
84 data = new GridData(GridData.FILL_HORIZONTAL);
85 data.heightHint = variableList.getItemHeight() * VISIBLE_ITEM_COUNT;
86 variableList.setLayoutData(data);
87 variableList.setFont(font);
89 variableComposite = new Composite(mainComposite, SWT.NONE);
90 variableLayout = new StackLayout();
91 variableLayout.marginWidth = 0;
92 variableLayout.marginHeight = 0;
93 data = new GridData(GridData.FILL_BOTH);
94 variableComposite.setLayout(variableLayout);
95 variableComposite.setLayoutData(data);
96 variableComposite.setFont(font);
98 createVariableComponents(data);
100 populateVariableList();
102 variableList.addSelectionListener(new SelectionAdapter() {
103 public void widgetSelected(SelectionEvent e) {
104 updateVariableComposite(null, false);
109 return mainComposite;
113 * Creates the visual component for each variable
114 * and determine the initial size so the form
115 * can be layout properly.
117 private void createVariableComponents(GridData data) {
118 for (int i = 0; i < variables.length; i++) {
119 ExternalToolVariable var = variables[i];
120 components[i] = var.getComponent();
121 components[i].createContents(variableComposite, var.getTag(), page);
122 Control control = components[i].getControl();
123 if (control != null) {
124 Point newSize = control.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
125 data.widthHint = Math.max(newSize.x, data.widthHint);
126 data.heightHint = Math.max(newSize.y, data.heightHint);
132 * Returns the formatted variable or <code>null</code> if
135 public String getSelectedVariable() {
136 if (activeComponentIndex != -1) {
137 String varValue = components[activeComponentIndex].getVariableValue();
138 return ToolUtil.buildVariableTag(variables[activeComponentIndex].getTag(), varValue);
145 * Returns whether the current variable selection is
146 * valid, including the selected variable value.
148 public boolean isValid() {
149 if (activeComponentIndex != -1)
150 return components[activeComponentIndex].isValid();
155 private void populateVariableList() {
156 String[] items = new String[variables.length];
157 StringBuffer buffer = new StringBuffer(80);
158 for (int i = 0; i < variables.length; i++) {
159 ToolUtil.buildVariableTag(variables[i].getTag(), null, buffer);
160 buffer.append(" - "); //$NON-NLS-1$
161 buffer.append(variables[i].getDescription());
162 items[i] = buffer.toString();
165 variableList.setItems(items);
168 public void selectVariable(String varName, String varValue) {
169 if (varName != null && varName.length() > 0) {
170 for (int i = 0; i < variables.length; i++) {
171 if (varName.equals(variables[i].getTag())) {
172 variableList.select(i);
173 updateVariableComposite(varValue, true);
179 variableList.deselectAll();
180 updateVariableComposite(varValue, false);
183 private void setComponentVisible(int index) {
185 variableLayout.topControl = null;
187 variableLayout.topControl = components[index].getControl();
188 variableComposite.layout();
192 * Enables or disables the variable form controls.
194 public void setEnabled(boolean enabled) {
195 variableListLabel.setEnabled(enabled);
196 variableList.setEnabled(enabled);
197 if (enabled && variableList.getSelection().length == 0) {
198 if (variableList.getItemCount() > 0) {
199 variableList.select(0);
200 activeComponentIndex= 0;
203 variableComposite.setVisible(enabled);
206 private void updateVariableComposite(String value, boolean setValue) {
207 activeComponentIndex = variableList.getSelectionIndex();
208 setComponentVisible(activeComponentIndex);
209 if (activeComponentIndex != -1 && setValue)
210 components[activeComponentIndex].setVariableValue(value);
214 * Validates the current variable selection is and
215 * its value are acceptable.
217 public void validate() {
218 if (activeComponentIndex != -1)
219 components[activeComponentIndex].validate();