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.phpeclipse.wiki.ui.internal;
13 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
14 import net.sourceforge.phpeclipse.wiki.internal.IConfigurationWorkingCopy;
15 import net.sourceforge.phpeclipse.wiki.preferences.Messages;
17 import org.eclipse.jface.dialogs.Dialog;
18 import org.eclipse.jface.dialogs.IDialogConstants;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.events.ModifyEvent;
21 import org.eclipse.swt.events.ModifyListener;
22 import org.eclipse.swt.events.SelectionEvent;
23 import org.eclipse.swt.events.SelectionListener;
24 import org.eclipse.swt.layout.GridData;
25 import org.eclipse.swt.layout.GridLayout;
26 import org.eclipse.swt.widgets.Button;
27 import org.eclipse.swt.widgets.Combo;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.Control;
30 import org.eclipse.swt.widgets.Group;
31 import org.eclipse.swt.widgets.Label;
32 import org.eclipse.swt.widgets.Shell;
33 import org.eclipse.swt.widgets.Text;
37 public class ConfigurationDialog extends Dialog {
38 protected IConfigurationWorkingCopy fConfiguration;
39 protected boolean isEdit;
41 private Button okButton;
42 private Text fUserName;
44 private Text fPassword;
46 interface StringModifyListener {
47 public void valueChanged(String s);
50 interface BooleanModifyListener {
51 public void valueChanged(boolean b);
54 interface TypeModifyListener {
55 public void valueChanged(String fType);
61 public ConfigurationDialog(Shell parentShell, IConfigurationWorkingCopy configuration) {
63 this.fConfiguration = configuration;
67 public ConfigurationDialog(Shell parentShell) {
69 fConfiguration = WikiEditorPlugin.createConfiguration();
73 protected void configureShell(Shell shell) {
74 super.configureShell(shell);
76 shell.setText(WikiEditorPlugin.getResource("%editConfig"));
78 shell.setText(WikiEditorPlugin.getResource("%newConfig"));
81 protected Label createLabel(Composite comp, String txt) {
82 Label label = new Label(comp, SWT.NONE);
84 label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING | GridData.VERTICAL_ALIGN_BEGINNING));
88 protected Text createPassword(Composite comp, String txt, final StringModifyListener listener) {
89 final Text text = new Text(comp, SWT.BORDER | SWT.PASSWORD);
92 GridData data = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
94 text.setLayoutData(data);
96 text.addModifyListener(new ModifyListener() {
97 public void modifyText(ModifyEvent e) {
98 listener.valueChanged(text.getText());
104 protected Text createText(Composite comp, String txt, final StringModifyListener listener) {
105 final Text text = new Text(comp, SWT.BORDER);
108 GridData data = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
109 data.widthHint = 150;
110 text.setLayoutData(data);
111 if (listener != null)
112 text.addModifyListener(new ModifyListener() {
113 public void modifyText(ModifyEvent e) {
114 listener.valueChanged(text.getText());
120 protected Combo createTypeCombo(Composite comp, final String[] types, String sel, final TypeModifyListener listener) {
121 final Combo combo = new Combo(comp, SWT.DROP_DOWN | SWT.READ_ONLY);
122 int size = types.length;
123 String[] items = new String[size];
125 for (int i = 0; i < size; i++) {
127 if (types[i].equals(sel))
130 combo.setItems(items);
133 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_BEGINNING);
134 data.widthHint = 150;
135 combo.setLayoutData(data);
136 if (listener != null)
137 combo.addSelectionListener(new SelectionListener() {
138 public void widgetSelected(SelectionEvent e) {
139 listener.valueChanged(types[combo.getSelectionIndex()]);
141 public void widgetDefaultSelected(SelectionEvent e) {
149 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
151 protected Control createDialogArea(Composite parent) {
152 Composite composite = (Composite) super.createDialogArea(parent);
153 ((GridLayout)composite.getLayout()).numColumns = 2;
155 // WorkbenchHelp.setHelp(composite, ContextIds.PREF_DIALOG);
157 createLabel(composite, WikiEditorPlugin.getResource("%name"));
158 fUserName = createText(composite, fConfiguration.getName() + "", new StringModifyListener() {
159 public void valueChanged(String name) {
160 fConfiguration.setName(name);
165 Group group = new Group(composite, SWT.NONE);
166 GridLayout layout = new GridLayout(2, false);
167 group.setLayout(layout);
168 GridData data = new GridData(GridData.FILL_HORIZONTAL);
169 data.horizontalSpan = 2;
171 group.setLayoutData(data);
172 group.setText(WikiEditorPlugin.getResource("%configGroup"));
175 createLabel(group, WikiEditorPlugin.getResource("%user"));
176 fUserName = createText(group, fConfiguration.getUser() + "", new StringModifyListener() {
177 public void valueChanged(String s) {
178 fConfiguration.setUser(s);
183 createLabel(group, WikiEditorPlugin.getResource("%password"));
184 fPassword = createPassword(group, fConfiguration.getPassword() + "", new StringModifyListener() {
185 public void valueChanged(String s) {
186 fConfiguration.setPassword(s);
191 createLabel(group, WikiEditorPlugin.getResource("%url"));
192 fUrl = createText(group, fConfiguration.getURL(), new StringModifyListener() {
193 public void valueChanged(String s) {
194 fConfiguration.setURL(s);
200 createLabel(group, WikiEditorPlugin.getResource("%parseType"));
201 createTypeCombo(group, WikiEditorPlugin.getTypes(), fConfiguration.getType(), new TypeModifyListener() {
202 public void valueChanged(String fType) {
203 fConfiguration.setType(fType);
211 * @see org.eclipse.jface.dialogs.Dialog#okPressed()
213 protected void okPressed() {
214 fConfiguration.save();
218 protected Control createButtonBar(Composite parent) {
219 Control buttonControl = super.createButtonBar(parent);
221 return buttonControl;
224 private void setOKButtonEnabled(boolean curIsEnabled) {
225 if (okButton == null)
226 okButton = getButton(IDialogConstants.OK_ID);
228 if (okButton != null)
229 okButton.setEnabled(curIsEnabled);
232 protected void validateFields() {
233 boolean result = true;
235 String currHostname = fUrl.getText();
236 // if (!isValidHostname(currHostname))
239 String currHostnamePort = fPassword.getText();
241 // Integer.parseInt(currHostnamePort);
242 // } catch (Exception any) {
246 String currMonitorPort = fUserName.getText();
248 // Integer.parseInt(currMonitorPort);
249 // } catch (Exception any) {
253 // if (result && isLocalhost(currHostname)) {
254 // if (currHostnamePort.equals(currMonitorPort))
257 setOKButtonEnabled(result);
260 // protected static boolean isValidHostname(String host) {
261 // if (host == null || host.trim().length() < 1)
263 // if (host.indexOf("/") >= 0)
265 // if (host.indexOf("\\") >= 0)
267 // if (host.indexOf(" ") >= 0)
272 // protected static boolean isLocalhost(String host) {
276 // if ("localhost".equals(host) || "127.0.0.1".equals(host))
278 // InetAddress localHostaddr = InetAddress.getLocalHost();
279 // if (localHostaddr.getHostName().equals(host))
281 // } catch (Exception e) {
282 // Trace.trace(Trace.WARNING, "Error checking for localhost", e);