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;
46 private Text fUserName;
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) {
158 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
160 protected Control createDialogArea(Composite parent) {
161 Composite composite = (Composite) super.createDialogArea(parent);
162 ((GridLayout) composite.getLayout()).numColumns = 2;
164 // WorkbenchHelp.setHelp(composite, ContextIds.PREF_DIALOG);
166 createLabel(composite, PHPHelpPlugin.getResource("%name"));
167 fUserName = createText(composite, fConfiguration.getName() + "", new StringModifyListener() {
168 public void valueChanged(String name) {
169 fConfiguration.setName(name);
174 Group group = new Group(composite, SWT.NONE);
175 GridLayout layout = new GridLayout(2, false);
176 group.setLayout(layout);
177 GridData data = new GridData(GridData.FILL_HORIZONTAL);
178 data.horizontalSpan = 2;
180 group.setLayoutData(data);
181 group.setText(PHPHelpPlugin.getResource("%configGroup"));
183 createLabel(group, PHPHelpPlugin.getResource("%user"));
184 fUserName = createText(group, fConfiguration.getUser() + "", new StringModifyListener() {
185 public void valueChanged(String s) {
186 fConfiguration.setUser(s);
191 Composite warningComposite = new Composite(group, SWT.NONE);
192 layout = new GridLayout();
193 layout.numColumns = 2;
194 layout.marginHeight = 0;
195 layout.marginHeight = 0;
196 warningComposite.setLayout(layout);
197 data = new GridData(GridData.FILL_HORIZONTAL);
198 data.horizontalSpan = 3;
199 warningComposite.setLayoutData(data);
200 Label warningLabel = new Label(warningComposite, SWT.NONE);
201 warningLabel.setImage(getImage(DLG_IMG_MESSAGE_WARNING));
202 warningLabel.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING | GridData.HORIZONTAL_ALIGN_BEGINNING));
203 Label warningText = new Label(warningComposite, SWT.WRAP);
204 warningText.setText(PHPHelpPlugin.getResource("%scrambledPassword")); //$NON-NLS-1$
205 data = new GridData(GridData.FILL_HORIZONTAL);
206 data.widthHint = 300;
207 warningText.setLayoutData(data);
209 createLabel(group, PHPHelpPlugin.getResource("%password"));
210 fPassword = createPassword(group, fConfiguration.getPassword() + "", new StringModifyListener() {
211 public void valueChanged(String s) {
212 fConfiguration.setPassword(s);
217 createLabel(group, PHPHelpPlugin.getResource("%url"));
218 fUrl = createText(group, fConfiguration.getURL(), new StringModifyListener() {
219 public void valueChanged(String s) {
220 fConfiguration.setURL(s);
225 createLabel(group, PHPHelpPlugin.getResource("%parseType"));
226 createTypeCombo(group, PHPHelpPlugin.getTypes(), fConfiguration.getType(), new TypeModifyListener() {
227 public void valueChanged(String fType) {
228 fConfiguration.setType(fType);
235 protected void okPressed() {
236 fConfiguration.save();
240 protected Control createButtonBar(Composite parent) {
241 Control buttonControl = super.createButtonBar(parent);
243 return buttonControl;
246 private void setOKButtonEnabled(boolean curIsEnabled) {
247 if (okButton == null)
248 okButton = getButton(IDialogConstants.OK_ID);
250 if (okButton != null)
251 okButton.setEnabled(curIsEnabled);
254 protected void validateFields() {
255 boolean result = true;
257 setOKButtonEnabled(result);