Merge branch 'master' of ssh://git.phpeclipse.com/phpeclipse
[phpeclipse.git] / net.sourceforge.phpeclipse.phphelp / src / net / sourceforge / phpdt / httpquery / preferences / ConfigurationDialog.java
1 /**********************************************************************
2  * Copyright (c) 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  �*
8  * Contributors:
9  *    IBM - Initial API and implementation
10  **********************************************************************/
11 package net.sourceforge.phpdt.httpquery.preferences;
12
13 import java.util.ArrayList;
14
15 import net.sourceforge.phpdt.httpquery.config.IConfigurationWorkingCopy;
16 import net.sourceforge.phpdt.phphelp.PHPHelpPlugin;
17
18 import org.eclipse.jface.dialogs.Dialog;
19 import org.eclipse.jface.dialogs.IDialogConstants;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.events.ModifyEvent;
22 import org.eclipse.swt.events.ModifyListener;
23 import org.eclipse.swt.events.SelectionEvent;
24 import org.eclipse.swt.events.SelectionListener;
25 import org.eclipse.swt.layout.GridData;
26 import org.eclipse.swt.layout.GridLayout;
27 import org.eclipse.swt.widgets.Button;
28 import org.eclipse.swt.widgets.Combo;
29 import org.eclipse.swt.widgets.Composite;
30 import org.eclipse.swt.widgets.Control;
31 import org.eclipse.swt.widgets.Group;
32 import org.eclipse.swt.widgets.Label;
33 import org.eclipse.swt.widgets.Shell;
34 import org.eclipse.swt.widgets.Text;
35
36 /**
37  * 
38  */
39 public class ConfigurationDialog extends Dialog {
40         protected IConfigurationWorkingCopy fConfiguration;
41
42         protected boolean isEdit;
43
44         private Button okButton;
45
46         //private Text fName;
47
48         //private Text fUrl;
49
50         // private Text fPassword;
51
52         interface StringModifyListener {
53                 public void valueChanged(String s);
54         }
55
56         interface BooleanModifyListener {
57                 public void valueChanged(boolean b);
58         }
59
60         interface TypeModifyListener {
61                 public void valueChanged(String fType);
62         }
63
64         /**
65          * @param parentShell
66          */
67         public ConfigurationDialog(Shell parentShell,
68                         IConfigurationWorkingCopy configuration) {
69                 super(parentShell);
70                 this.fConfiguration = configuration;
71                 isEdit = true;
72         }
73
74         public ConfigurationDialog(Shell parentShell) {
75                 super(parentShell);
76                 fConfiguration = PHPHelpPlugin.createConfiguration();
77                 isEdit = false;
78         }
79
80         protected void configureShell(Shell shell) {
81                 super.configureShell(shell);
82                 if (isEdit)
83                         shell.setText(PHPHelpPlugin.getResource("%editConfig"));
84                 else
85                         shell.setText(PHPHelpPlugin.getResource("%newConfig"));
86         }
87
88         protected Label createLabel(Composite comp, String txt) {
89                 Label label = new Label(comp, SWT.NONE);
90                 label.setText(txt);
91                 label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING
92                                 | GridData.VERTICAL_ALIGN_BEGINNING));
93                 return label;
94         }
95
96         protected Text createPassword(Composite comp, String txt,
97                         final StringModifyListener listener) {
98                 final Text text = new Text(comp, SWT.BORDER | SWT.PASSWORD);
99                 if (txt != null)
100                         text.setText(txt);
101                 GridData data = new GridData(GridData.FILL_HORIZONTAL
102                                 | GridData.VERTICAL_ALIGN_BEGINNING);
103                 data.widthHint = 150;
104                 text.setLayoutData(data);
105                 if (listener != null)
106                         text.addModifyListener(new ModifyListener() {
107                                 public void modifyText(ModifyEvent e) {
108                                         listener.valueChanged(text.getText());
109                                 }
110                         });
111                 return text;
112         }
113
114         protected Text createText(Composite comp, String txt,
115                         final StringModifyListener listener) {
116                 final Text text = new Text(comp, SWT.BORDER);
117                 if (txt != null)
118                         text.setText(txt);
119                 GridData data = new GridData(GridData.FILL_HORIZONTAL
120                                 | GridData.VERTICAL_ALIGN_BEGINNING);
121                 data.widthHint = 150;
122                 text.setLayoutData(data);
123                 if (listener != null)
124                         text.addModifyListener(new ModifyListener() {
125                                 public void modifyText(ModifyEvent e) {
126                                         listener.valueChanged(text.getText());
127                                 }
128                         });
129                 return text;
130         }
131
132         protected Combo createTypeCombo(Composite comp, final ArrayList types,
133                         String sel, final TypeModifyListener listener) {
134                 final Combo combo = new Combo(comp, SWT.DROP_DOWN | SWT.READ_ONLY);
135                 int size = types.size();
136                 String[] items = new String[size];
137                 int index = -1;
138                 for (int i = 0; i < size; i++) {
139                         items[i] = (String) types.get(i);
140                         if (items[i].equals(sel))
141                                 index = i;
142                 }
143                 combo.setItems(items);
144                 if (index >= 0)
145                         combo.select(index);
146                 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL
147                                 | GridData.VERTICAL_ALIGN_BEGINNING);
148                 data.widthHint = 150;
149                 combo.setLayoutData(data);
150                 if (listener != null)
151                         combo.addSelectionListener(new SelectionListener() {
152                                 public void widgetSelected(SelectionEvent e) {
153                                         listener.valueChanged((String) types.get(combo
154                                                         .getSelectionIndex()));
155                                 }
156
157                                 public void widgetDefaultSelected(SelectionEvent e) {
158                                         widgetSelected(e);
159                                 }
160                         });
161                 return combo;
162         }
163
164         protected Control createDialogArea(Composite parent) {
165                 Composite composite = (Composite) super.createDialogArea(parent);
166                 ((GridLayout) composite.getLayout()).numColumns = 2;
167
168                 // WorkbenchHelp.setHelp(composite, ContextIds.PREF_DIALOG);
169
170                 createLabel(composite, PHPHelpPlugin.getResource("%name"));
171 //              fName = createText(composite, fConfiguration.getName() + "",
172 //                              new StringModifyListener() {
173 //                                      public void valueChanged(String name) {
174 //                                              fConfiguration.setName(name);
175 //                                              validateFields();
176 //                                      }
177 //                              });
178
179                 Group group = new Group(composite, SWT.NONE);
180                 GridLayout layout = new GridLayout(2, false);
181                 group.setLayout(layout);
182                 GridData data = new GridData(GridData.FILL_HORIZONTAL);
183                 data.horizontalSpan = 2;
184
185                 group.setLayoutData(data);
186                 group.setText(PHPHelpPlugin.getResource("%configGroup"));
187
188                 // createLabel(group, PHPHelpPlugin.getResource("%user"));
189                 // fUserName = createText(group, fConfiguration.getUser() + "", new
190                 // StringModifyListener() {
191                 // public void valueChanged(String s) {
192                 // fConfiguration.setUser(s);
193                 // validateFields();
194                 // }
195                 // });
196
197                 // Composite warningComposite = new Composite(group, SWT.NONE);
198                 // layout = new GridLayout();
199                 // layout.numColumns = 2;
200                 // layout.marginHeight = 0;
201                 // layout.marginHeight = 0;
202                 // warningComposite.setLayout(layout);
203                 // data = new GridData(GridData.FILL_HORIZONTAL);
204                 // data.horizontalSpan = 3;
205                 // warningComposite.setLayoutData(data);
206                 // Label warningLabel = new Label(warningComposite, SWT.NONE);
207                 // warningLabel.setImage(getImage(DLG_IMG_MESSAGE_WARNING));
208                 // warningLabel.setLayoutData(new
209                 // GridData(GridData.VERTICAL_ALIGN_BEGINNING
210                 // | GridData.HORIZONTAL_ALIGN_BEGINNING));
211                 // Label warningText = new Label(warningComposite, SWT.WRAP);
212                 // warningText.setText(PHPHelpPlugin.getResource("%scrambledPassword"));
213                 // //$NON-NLS-1$
214                 // data = new GridData(GridData.FILL_HORIZONTAL);
215                 // data.widthHint = 300;
216                 // warningText.setLayoutData(data);
217
218                 // createLabel(group, PHPHelpPlugin.getResource("%password"));
219                 // fPassword = createPassword(group, fConfiguration.getPassword() + "",
220                 // new StringModifyListener() {
221                 // public void valueChanged(String s) {
222                 // fConfiguration.setPassword(s);
223                 // validateFields();
224                 // }
225                 // });
226
227                 createLabel(group, PHPHelpPlugin.getResource("%url"));
228 //              fUrl = createText(group, fConfiguration.getURL(),
229 //                              new StringModifyListener() {
230 //                                      public void valueChanged(String s) {
231 //                                              fConfiguration.setURL(s);
232 //                                              validateFields();
233 //                                      }
234 //                              });
235
236                 createLabel(group, PHPHelpPlugin.getResource("%parseType"));
237                 createTypeCombo(group, PHPHelpPlugin.getTypes(), fConfiguration
238                                 .getType(), new TypeModifyListener() {
239                         public void valueChanged(String fType) {
240                                 fConfiguration.setType(fType);
241                         }
242                 });
243
244                 return composite;
245         }
246
247         protected void okPressed() {
248                 fConfiguration.save();
249                 super.okPressed();
250         }
251
252         protected Control createButtonBar(Composite parent) {
253                 Control buttonControl = super.createButtonBar(parent);
254                 validateFields();
255                 return buttonControl;
256         }
257
258         private void setOKButtonEnabled(boolean curIsEnabled) {
259                 if (okButton == null)
260                         okButton = getButton(IDialogConstants.OK_ID);
261
262                 if (okButton != null)
263                         okButton.setEnabled(curIsEnabled);
264         }
265
266         protected void validateFields() {
267                 boolean result = true;
268
269                 setOKButtonEnabled(result);
270         }
271
272 }