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 org.eclipse.swt.SWT;
13 import org.eclipse.swt.custom.StackLayout;
14 import org.eclipse.swt.events.SelectionAdapter;
15 import org.eclipse.swt.events.SelectionEvent;
16 import org.eclipse.swt.graphics.Font;
17 import org.eclipse.swt.graphics.Point;
18 import org.eclipse.swt.layout.GridData;
19 import org.eclipse.swt.layout.GridLayout;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Control;
22 import org.eclipse.swt.widgets.Label;
23 import org.eclipse.swt.widgets.List;
24 import net.sourceforge.phpdt.externaltools.group.IGroupDialogPage;
25 import net.sourceforge.phpdt.externaltools.internal.registry.ExternalToolVariable;
26 import net.sourceforge.phpdt.externaltools.model.ToolUtil;
27 import net.sourceforge.phpdt.externaltools.variable.IVariableComponent;
30 * Visual grouping of controls that allows the user to
31 * select a variable and configure it with extra
34 public class ExternalToolVariableForm {
35 private static final int VISIBLE_ITEM_COUNT = 9;
37 private String variableListLabelText;
38 private ExternalToolVariable[] variables;
39 private IVariableComponent[] components;
40 private IGroupDialogPage page;
42 private Label variableListLabel;
43 private List variableList;
44 private Composite variableComposite;
45 private StackLayout variableLayout;
46 private int activeComponentIndex = -1;
49 * Creates the visual grouping
51 * @param variableListLabelText the label text to use for identifying the list of variables
52 * @param variables the collection of variables to display to the user
54 public ExternalToolVariableForm(String variableListLabelText, ExternalToolVariable[] variables) {
56 this.variableListLabelText = variableListLabelText;
57 this.variables = variables;
58 this.components = new IVariableComponent[variables.length];
61 public Composite createContents(Composite parent, IGroupDialogPage page) {
62 Font font = parent.getFont();
66 Composite mainComposite = new Composite(parent, SWT.NONE);
67 GridLayout layout = new GridLayout();
68 layout.marginWidth = 0;
69 layout.marginHeight = 0;
70 layout.numColumns = 1;
71 GridData data = new GridData(GridData.FILL_BOTH);
72 mainComposite.setLayout(layout);
73 mainComposite.setLayoutData(data);
75 variableListLabel = new Label(mainComposite, SWT.NONE);
76 variableListLabel.setText(variableListLabelText);
77 data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
78 data.horizontalSpan = 1;
79 variableListLabel.setLayoutData(data);
80 variableListLabel.setFont(font);
82 variableList = new List(mainComposite, SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
83 data = new GridData(GridData.FILL_HORIZONTAL);
84 data.heightHint = variableList.getItemHeight() * VISIBLE_ITEM_COUNT;
85 variableList.setLayoutData(data);
86 variableList.setFont(font);
88 variableComposite = new Composite(mainComposite, SWT.NONE);
89 variableLayout = new StackLayout();
90 variableLayout.marginWidth = 0;
91 variableLayout.marginHeight = 0;
92 data = new GridData(GridData.FILL_BOTH);
93 variableComposite.setLayout(variableLayout);
94 variableComposite.setLayoutData(data);
95 variableComposite.setFont(font);
97 createVariableComponents(data);
99 populateVariableList();
101 variableList.addSelectionListener(new SelectionAdapter() {
102 public void widgetSelected(SelectionEvent e) {
103 updateVariableComposite(null, false);
108 return mainComposite;
112 * Creates the visual component for each variable
113 * and determine the initial size so the form
114 * can be layout properly.
116 private void createVariableComponents(GridData data) {
117 for (int i = 0; i < variables.length; i++) {
118 ExternalToolVariable var = variables[i];
119 components[i] = var.getComponent();
120 components[i].createContents(variableComposite, var.getTag(), page);
121 Control control = components[i].getControl();
122 if (control != null) {
123 Point newSize = control.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
124 data.widthHint = Math.max(newSize.x, data.widthHint);
125 data.heightHint = Math.max(newSize.y, data.heightHint);
131 * Returns the formatted variable or <code>null</code> if
134 public String getSelectedVariable() {
135 if (activeComponentIndex != -1) {
136 String varValue = components[activeComponentIndex].getVariableValue();
137 return ToolUtil.buildVariableTag(variables[activeComponentIndex].getTag(), varValue);
144 * Returns whether the current variable selection is
145 * valid, including the selected variable value.
147 public boolean isValid() {
148 if (activeComponentIndex != -1)
149 return components[activeComponentIndex].isValid();
154 private void populateVariableList() {
155 String[] items = new String[variables.length];
156 StringBuffer buffer = new StringBuffer(80);
157 for (int i = 0; i < variables.length; i++) {
158 ToolUtil.buildVariableTag(variables[i].getTag(), null, buffer);
159 buffer.append(" - "); //$NON-NLS-1$
160 buffer.append(variables[i].getDescription());
161 items[i] = buffer.toString();
164 variableList.setItems(items);
167 public void selectVariable(String varName, String varValue) {
168 if (varName != null && varName.length() > 0) {
169 for (int i = 0; i < variables.length; i++) {
170 if (varName.equals(variables[i].getTag())) {
171 variableList.select(i);
172 updateVariableComposite(varValue, true);
178 variableList.deselectAll();
179 updateVariableComposite(varValue, false);
182 private void setComponentVisible(int index) {
184 variableLayout.topControl = null;
186 variableLayout.topControl = components[index].getControl();
187 variableComposite.layout();
191 * Enables or disables the variable form controls.
193 public void setEnabled(boolean enabled) {
194 variableListLabel.setEnabled(enabled);
195 variableList.setEnabled(enabled);
196 if (enabled && variableList.getSelection().length == 0) {
197 if (variableList.getItemCount() > 0) {
198 variableList.select(0);
199 activeComponentIndex= 0;
202 variableComposite.setVisible(enabled);
205 private void updateVariableComposite(String value, boolean setValue) {
206 activeComponentIndex = variableList.getSelectionIndex();
207 setComponentVisible(activeComponentIndex);
208 if (activeComponentIndex != -1 && setValue)
209 components[activeComponentIndex].setVariableValue(value);
213 * Validates the current variable selection is and
214 * its value are acceptable.
216 public void validate() {
217 if (activeComponentIndex != -1)
218 components[activeComponentIndex].validate();