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,
68 IConfigurationWorkingCopy configuration) {
70 this.fConfiguration = configuration;
74 public ConfigurationDialog(Shell parentShell) {
76 fConfiguration = PHPHelpPlugin.createConfiguration();
80 protected void configureShell(Shell shell) {
81 super.configureShell(shell);
83 shell.setText(PHPHelpPlugin.getResource("%editConfig"));
85 shell.setText(PHPHelpPlugin.getResource("%newConfig"));
88 protected Label createLabel(Composite comp, String txt) {
89 Label label = new Label(comp, SWT.NONE);
91 label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING
92 | GridData.VERTICAL_ALIGN_BEGINNING));
96 protected Text createPassword(Composite comp, String txt,
97 final StringModifyListener listener) {
98 final Text text = new Text(comp, SWT.BORDER | SWT.PASSWORD);
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());
114 protected Text createText(Composite comp, String txt,
115 final StringModifyListener listener) {
116 final Text text = new Text(comp, SWT.BORDER);
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());
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];
138 for (int i = 0; i < size; i++) {
139 items[i] = (String) types.get(i);
140 if (items[i].equals(sel))
143 combo.setItems(items);
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()));
157 public void widgetDefaultSelected(SelectionEvent e) {
164 protected Control createDialogArea(Composite parent) {
165 Composite composite = (Composite) super.createDialogArea(parent);
166 ((GridLayout) composite.getLayout()).numColumns = 2;
168 // WorkbenchHelp.setHelp(composite, ContextIds.PREF_DIALOG);
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);
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;
185 group.setLayoutData(data);
186 group.setText(PHPHelpPlugin.getResource("%configGroup"));
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);
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"));
214 // data = new GridData(GridData.FILL_HORIZONTAL);
215 // data.widthHint = 300;
216 // warningText.setLayoutData(data);
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);
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);
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);
247 protected void okPressed() {
248 fConfiguration.save();
252 protected Control createButtonBar(Composite parent) {
253 Control buttonControl = super.createButtonBar(parent);
255 return buttonControl;
258 private void setOKButtonEnabled(boolean curIsEnabled) {
259 if (okButton == null)
260 okButton = getButton(IDialogConstants.OK_ID);
262 if (okButton != null)
263 okButton.setEnabled(curIsEnabled);
266 protected void validateFields() {
267 boolean result = true;
269 setOKButtonEnabled(result);