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
9 * IBM - Initial API and implementation
10 **********************************************************************/
11 package net.sourceforge.phpdt.httpquery.preferences;
13 import java.util.ArrayList;
15 import net.sourceforge.phpdt.httpquery.config.IConfigurationWorkingCopy;
16 import net.sourceforge.phpdt.phphelp.PHPHelpPlugin;
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;
39 public class ConfigurationDialog extends Dialog {
40 protected IConfigurationWorkingCopy fConfiguration;
42 protected boolean isEdit;
44 private Button okButton;
50 // private Text fPassword;
52 interface StringModifyListener {
53 public void valueChanged(String s);
56 interface BooleanModifyListener {
57 public void valueChanged(boolean b);
60 interface TypeModifyListener {
61 public void valueChanged(String fType);
67 public ConfigurationDialog(Shell parentShell, IConfigurationWorkingCopy configuration) {
69 this.fConfiguration = configuration;
73 public ConfigurationDialog(Shell parentShell) {
75 fConfiguration = PHPHelpPlugin.createConfiguration();
79 protected void configureShell(Shell shell) {
80 super.configureShell(shell);
82 shell.setText(PHPHelpPlugin.getResource("%editConfig"));
84 shell.setText(PHPHelpPlugin.getResource("%newConfig"));
87 protected Label createLabel(Composite comp, String txt) {
88 Label label = new Label(comp, SWT.NONE);
90 label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING | GridData.VERTICAL_ALIGN_BEGINNING));
94 protected Text createPassword(Composite comp, String txt, final StringModifyListener listener) {
95 final Text text = new Text(comp, SWT.BORDER | SWT.PASSWORD);
98 GridData data = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
100 text.setLayoutData(data);
101 if (listener != null)
102 text.addModifyListener(new ModifyListener() {
103 public void modifyText(ModifyEvent e) {
104 listener.valueChanged(text.getText());
110 protected Text createText(Composite comp, String txt, final StringModifyListener listener) {
111 final Text text = new Text(comp, SWT.BORDER);
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());
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];
131 for (int i = 0; i < size; i++) {
132 items[i] = (String) types.get(i);
133 if (items[i].equals(sel))
136 combo.setItems(items);
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()));
148 public void widgetDefaultSelected(SelectionEvent e) {
155 protected Control createDialogArea(Composite parent) {
156 Composite composite = (Composite) super.createDialogArea(parent);
157 ((GridLayout) composite.getLayout()).numColumns = 2;
159 // WorkbenchHelp.setHelp(composite, ContextIds.PREF_DIALOG);
161 createLabel(composite, PHPHelpPlugin.getResource("%name"));
162 fName = createText(composite, fConfiguration.getName() + "", new StringModifyListener() {
163 public void valueChanged(String name) {
164 fConfiguration.setName(name);
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;
175 group.setLayoutData(data);
176 group.setText(PHPHelpPlugin.getResource("%configGroup"));
178 // createLabel(group, PHPHelpPlugin.getResource("%user"));
179 // fUserName = createText(group, fConfiguration.getUser() + "", new StringModifyListener() {
180 // public void valueChanged(String s) {
181 // fConfiguration.setUser(s);
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"));
202 // data = new GridData(GridData.FILL_HORIZONTAL);
203 // data.widthHint = 300;
204 // warningText.setLayoutData(data);
206 // createLabel(group, PHPHelpPlugin.getResource("%password"));
207 // fPassword = createPassword(group, fConfiguration.getPassword() + "", new StringModifyListener() {
208 // public void valueChanged(String s) {
209 // fConfiguration.setPassword(s);
214 createLabel(group, PHPHelpPlugin.getResource("%url"));
215 fUrl = createText(group, fConfiguration.getURL(), new StringModifyListener() {
216 public void valueChanged(String s) {
217 fConfiguration.setURL(s);
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);
232 protected void okPressed() {
233 fConfiguration.save();
237 protected Control createButtonBar(Composite parent) {
238 Control buttonControl = super.createButtonBar(parent);
240 return buttonControl;
243 private void setOKButtonEnabled(boolean curIsEnabled) {
244 if (okButton == null)
245 okButton = getButton(IDialogConstants.OK_ID);
247 if (okButton != null)
248 okButton.setEnabled(curIsEnabled);
251 protected void validateFields() {
252 boolean result = true;
254 setOKButtonEnabled(result);