New Localhost query action
[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, IConfigurationWorkingCopy configuration) {
68                 super(parentShell);
69                 this.fConfiguration = configuration;
70                 isEdit = true;
71         }
72
73         public ConfigurationDialog(Shell parentShell) {
74                 super(parentShell);
75                 fConfiguration = PHPHelpPlugin.createConfiguration();
76                 isEdit = false;
77         }
78
79         protected void configureShell(Shell shell) {
80                 super.configureShell(shell);
81                 if (isEdit)
82                         shell.setText(PHPHelpPlugin.getResource("%editConfig"));
83                 else
84                         shell.setText(PHPHelpPlugin.getResource("%newConfig"));
85         }
86
87         protected Label createLabel(Composite comp, String txt) {
88                 Label label = new Label(comp, SWT.NONE);
89                 label.setText(txt);
90                 label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING | GridData.VERTICAL_ALIGN_BEGINNING));
91                 return label;
92         }
93
94         protected Text createPassword(Composite comp, String txt, final StringModifyListener listener) {
95                 final Text text = new Text(comp, SWT.BORDER | SWT.PASSWORD);
96                 if (txt != null)
97                         text.setText(txt);
98                 GridData data = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
99                 data.widthHint = 150;
100                 text.setLayoutData(data);
101                 if (listener != null)
102                         text.addModifyListener(new ModifyListener() {
103                                 public void modifyText(ModifyEvent e) {
104                                         listener.valueChanged(text.getText());
105                                 }
106                         });
107                 return text;
108         }
109
110         protected Text createText(Composite comp, String txt, final StringModifyListener listener) {
111                 final Text text = new Text(comp, SWT.BORDER);
112                 if (txt != null)
113                         text.setText(txt);
114                 GridData data = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
115                 data.widthHint = 150;
116                 text.setLayoutData(data);
117                 if (listener != null)
118                         text.addModifyListener(new ModifyListener() {
119                                 public void modifyText(ModifyEvent e) {
120                                         listener.valueChanged(text.getText());
121                                 }
122                         });
123                 return text;
124         }
125
126         protected Combo createTypeCombo(Composite comp, final ArrayList types, String sel, final TypeModifyListener listener) {
127                 final Combo combo = new Combo(comp, SWT.DROP_DOWN | SWT.READ_ONLY);
128                 int size = types.size();
129                 String[] items = new String[size];
130                 int index = -1;
131                 for (int i = 0; i < size; i++) {
132                         items[i] = (String) types.get(i);
133                         if (items[i].equals(sel))
134                                 index = i;
135                 }
136                 combo.setItems(items);
137                 if (index >= 0)
138                         combo.select(index);
139                 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_BEGINNING);
140                 data.widthHint = 150;
141                 combo.setLayoutData(data);
142                 if (listener != null)
143                         combo.addSelectionListener(new SelectionListener() {
144                                 public void widgetSelected(SelectionEvent e) {
145                                         listener.valueChanged((String) types.get(combo.getSelectionIndex()));
146                                 }
147
148                                 public void widgetDefaultSelected(SelectionEvent e) {
149                                         widgetSelected(e);
150                                 }
151                         });
152                 return combo;
153         }
154
155         protected Control createDialogArea(Composite parent) {
156                 Composite composite = (Composite) super.createDialogArea(parent);
157                 ((GridLayout) composite.getLayout()).numColumns = 2;
158
159                 // WorkbenchHelp.setHelp(composite, ContextIds.PREF_DIALOG);
160
161                 createLabel(composite, PHPHelpPlugin.getResource("%name"));
162                 fName = createText(composite, fConfiguration.getName() + "", new StringModifyListener() {
163                         public void valueChanged(String name) {
164                                 fConfiguration.setName(name);
165                                 validateFields();
166                         }
167                 });
168
169                 Group group = new Group(composite, SWT.NONE);
170                 GridLayout layout = new GridLayout(2, false);
171                 group.setLayout(layout);
172                 GridData data = new GridData(GridData.FILL_HORIZONTAL);
173                 data.horizontalSpan = 2;
174
175                 group.setLayoutData(data);
176                 group.setText(PHPHelpPlugin.getResource("%configGroup"));
177
178 //              createLabel(group, PHPHelpPlugin.getResource("%user"));
179 //              fUserName = createText(group, fConfiguration.getUser() + "", new StringModifyListener() {
180 //                      public void valueChanged(String s) {
181 //                              fConfiguration.setUser(s);
182 //                              validateFields();
183 //                      }
184 //              });
185
186 //              Composite warningComposite = new Composite(group, SWT.NONE);
187 //              layout = new GridLayout();
188 //              layout.numColumns = 2;
189 //              layout.marginHeight = 0;
190 //              layout.marginHeight = 0;
191 //              warningComposite.setLayout(layout);
192 //              data = new GridData(GridData.FILL_HORIZONTAL);
193 //              data.horizontalSpan = 3;
194 //              warningComposite.setLayoutData(data);
195                 // Label warningLabel = new Label(warningComposite, SWT.NONE);
196                 // warningLabel.setImage(getImage(DLG_IMG_MESSAGE_WARNING));
197                 // warningLabel.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING
198                 // | GridData.HORIZONTAL_ALIGN_BEGINNING));
199                 // Label warningText = new Label(warningComposite, SWT.WRAP);
200                 // warningText.setText(PHPHelpPlugin.getResource("%scrambledPassword"));
201                 // //$NON-NLS-1$
202                 // data = new GridData(GridData.FILL_HORIZONTAL);
203                 // data.widthHint = 300;
204                 // warningText.setLayoutData(data);
205
206 //              createLabel(group, PHPHelpPlugin.getResource("%password"));
207 //              fPassword = createPassword(group, fConfiguration.getPassword() + "", new StringModifyListener() {
208 //                      public void valueChanged(String s) {
209 //                              fConfiguration.setPassword(s);
210 //                              validateFields();
211 //                      }
212 //              });
213
214                 createLabel(group, PHPHelpPlugin.getResource("%url"));
215                 fUrl = createText(group, fConfiguration.getURL(), new StringModifyListener() {
216                         public void valueChanged(String s) {
217                                 fConfiguration.setURL(s);
218                                 validateFields();
219                         }
220                 });
221
222                 createLabel(group, PHPHelpPlugin.getResource("%parseType"));
223                 createTypeCombo(group, PHPHelpPlugin.getTypes(), fConfiguration.getType(), new TypeModifyListener() {
224                         public void valueChanged(String fType) {
225                                 fConfiguration.setType(fType);
226                         }
227                 });
228
229                 return composite;
230         }
231
232         protected void okPressed() {
233                 fConfiguration.save();
234                 super.okPressed();
235         }
236
237         protected Control createButtonBar(Composite parent) {
238                 Control buttonControl = super.createButtonBar(parent);
239                 validateFields();
240                 return buttonControl;
241         }
242
243         private void setOKButtonEnabled(boolean curIsEnabled) {
244                 if (okButton == null)
245                         okButton = getButton(IDialogConstants.OK_ID);
246
247                 if (okButton != null)
248                         okButton.setEnabled(curIsEnabled);
249         }
250
251         protected void validateFields() {
252                 boolean result = true;
253
254                 setOKButtonEnabled(result);
255         }
256
257 }