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