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 select a variable and
32 * configure it with extra information.
34 public class ExternalToolVariableForm {
35 private static final int VISIBLE_ITEM_COUNT = 9;
37 private String variableListLabelText;
39 private ExternalToolVariable[] variables;
41 private IVariableComponent[] components;
43 private IGroupDialogPage page;
45 private Label variableListLabel;
47 private List variableList;
49 private Composite variableComposite;
51 private StackLayout variableLayout;
53 private int activeComponentIndex = -1;
56 * Creates the visual grouping
58 * @param variableListLabelText
59 * the label text to use for identifying the list of variables
61 * the collection of variables to display to the user
63 public ExternalToolVariableForm(String variableListLabelText,
64 ExternalToolVariable[] variables) {
66 this.variableListLabelText = variableListLabelText;
67 this.variables = variables;
68 this.components = new IVariableComponent[variables.length];
71 public Composite createContents(Composite parent, IGroupDialogPage page) {
72 Font font = parent.getFont();
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);
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);
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);
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);
108 createVariableComponents(data);
110 populateVariableList();
112 variableList.addSelectionListener(new SelectionAdapter() {
113 public void widgetSelected(SelectionEvent e) {
114 updateVariableComposite(null, false);
119 return mainComposite;
123 * Creates the visual component for each variable and determine the initial
124 * size so the form can be layout properly.
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,
135 data.widthHint = Math.max(newSize.x, data.widthHint);
136 data.heightHint = Math.max(newSize.y, data.heightHint);
142 * Returns the formatted variable or <code>null</code> if none selected.
144 public String getSelectedVariable() {
145 if (activeComponentIndex != -1) {
146 String varValue = components[activeComponentIndex]
148 return ToolUtil.buildVariableTag(variables[activeComponentIndex]
149 .getTag(), varValue);
156 * Returns whether the current variable selection is valid, including the
157 * selected variable value.
159 public boolean isValid() {
160 if (activeComponentIndex != -1)
161 return components[activeComponentIndex].isValid();
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();
176 variableList.setItems(items);
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);
190 variableList.deselectAll();
191 updateVariableComposite(varValue, false);
194 private void setComponentVisible(int index) {
196 variableLayout.topControl = null;
198 variableLayout.topControl = components[index].getControl();
199 variableComposite.layout();
203 * Enables or disables the variable form controls.
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;
214 variableComposite.setVisible(enabled);
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);
225 * Validates the current variable selection is and its value are acceptable.
227 public void validate() {
228 if (activeComponentIndex != -1)
229 components[activeComponentIndex].validate();