5a9de706f47de6ab37351be5fdcf49e129014b91
[phpeclipse.git] /
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
32  * select a variable and configure it with extra
33  * information.
34  */
35 public class ExternalToolVariableForm {
36         private static final int VISIBLE_ITEM_COUNT = 9;
37         
38         private String variableListLabelText;
39         private ExternalToolVariable[] variables;
40         private IVariableComponent[] components;
41         private IGroupDialogPage page;
42         
43         private Label variableListLabel;
44         private List variableList;
45         private Composite variableComposite;
46         private StackLayout variableLayout;
47         private int activeComponentIndex = -1;
48         
49         /**
50          * Creates the visual grouping
51          * 
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
54          */
55         public ExternalToolVariableForm(String variableListLabelText, ExternalToolVariable[] variables) {
56                 super();
57                 this.variableListLabelText = variableListLabelText;
58                 this.variables = variables;
59                 this.components = new IVariableComponent[variables.length];
60         }
61
62         public Composite createContents(Composite parent, IGroupDialogPage page) {
63                 Font font = parent.getFont();
64                 
65                 this.page = page;
66                 
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);
75
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);
82                 
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);
88
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);
97                 
98                 createVariableComponents(data);
99                 
100                 populateVariableList();
101                 
102                 variableList.addSelectionListener(new SelectionAdapter() {
103                         public void widgetSelected(SelectionEvent e) {
104                                 updateVariableComposite(null, false);
105                         }
106                 });
107                 
108                 setEnabled(true);
109                 return mainComposite;
110         }
111         
112         /**
113          * Creates the visual component for each variable
114          * and determine the initial size so the form
115          * can be layout properly.
116          */
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);
127                         }
128                 }
129         }
130         
131         /**
132          * Returns the formatted variable or <code>null</code> if
133          * none selected.
134          */
135         public String getSelectedVariable() {
136                 if (activeComponentIndex != -1) {
137                         String varValue = components[activeComponentIndex].getVariableValue();
138                         return ToolUtil.buildVariableTag(variables[activeComponentIndex].getTag(), varValue);
139                 }
140
141                 return null;
142         }
143
144         /**
145          * Returns whether the current variable selection is
146          * valid, including the selected variable value.
147          */
148         public boolean isValid() {
149                 if (activeComponentIndex != -1)
150                         return components[activeComponentIndex].isValid();
151                 
152                 return true;
153         }
154
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();
163                         buffer.setLength(0);
164                 }
165                 variableList.setItems(items);
166         }
167
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);
174                                         return;
175                                 }
176                         }
177                 }
178                 
179                 variableList.deselectAll();
180                 updateVariableComposite(varValue, false);
181         }
182         
183         private void setComponentVisible(int index) {
184                 if (index == -1)
185                         variableLayout.topControl = null;
186                 else
187                         variableLayout.topControl = components[index].getControl();
188                 variableComposite.layout();
189         }
190         
191         /**
192          * Enables or disables the variable form controls.
193          */
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;
201                         }
202                 }
203                 variableComposite.setVisible(enabled);
204         }
205         
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);
211         }
212
213         /**
214          * Validates the current variable selection is and
215          * its value are acceptable.
216          */
217         public void validate() {
218                 if (activeComponentIndex != -1)
219                         components[activeComponentIndex].validate();
220         }
221 }