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;
16 import org.eclipse.jface.dialogs.Dialog;
17 import org.eclipse.jface.dialogs.IDialogConstants;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.events.ModifyEvent;
20 import org.eclipse.swt.events.ModifyListener;
21 import org.eclipse.swt.events.SelectionEvent;
22 import org.eclipse.swt.events.SelectionListener;
23 import org.eclipse.swt.layout.GridData;
24 import org.eclipse.swt.layout.GridLayout;
25 import org.eclipse.swt.widgets.Button;
26 import org.eclipse.swt.widgets.Combo;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.swt.widgets.Control;
29 import org.eclipse.swt.widgets.Group;
30 import org.eclipse.swt.widgets.Label;
31 import org.eclipse.swt.widgets.Shell;
32 import org.eclipse.swt.widgets.Text;
36 public class ConfigurationDialog extends Dialog {
37 protected IConfigurationWorkingCopy fConfiguration;
38 protected boolean isEdit;
40 private Button okButton;
41 private Text fUserName;
43 private Text fPassword;
45 interface StringModifyListener {
46 public void valueChanged(String s);
49 interface BooleanModifyListener {
50 public void valueChanged(boolean b);
53 interface TypeModifyListener {
54 public void valueChanged(String fType);
60 public ConfigurationDialog(Shell parentShell, IConfigurationWorkingCopy configuration) {
62 this.fConfiguration = configuration;
66 public ConfigurationDialog(Shell parentShell) {
68 fConfiguration = WikiEditorPlugin.createConfiguration();
72 protected void configureShell(Shell shell) {
73 super.configureShell(shell);
75 shell.setText(WikiEditorPlugin.getResource("%editConfig"));
77 shell.setText(WikiEditorPlugin.getResource("%newConfig"));
80 protected Label createLabel(Composite comp, String txt) {
81 Label label = new Label(comp, SWT.NONE);
83 label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING | GridData.VERTICAL_ALIGN_BEGINNING));
87 protected Text createText(Composite comp, String txt, final StringModifyListener listener) {
88 final Text text = new Text(comp, SWT.BORDER);
91 GridData data = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
93 text.setLayoutData(data);
95 text.addModifyListener(new ModifyListener() {
96 public void modifyText(ModifyEvent e) {
97 listener.valueChanged(text.getText());
103 protected Combo createTypeCombo(Composite comp, final String[] types, String sel, final TypeModifyListener listener) {
104 final Combo combo = new Combo(comp, SWT.DROP_DOWN | SWT.READ_ONLY);
105 int size = types.length;
106 String[] items = new String[size];
108 for (int i = 0; i < size; i++) {
110 if (types[i].equals(sel))
113 combo.setItems(items);
116 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_BEGINNING);
117 data.widthHint = 150;
118 combo.setLayoutData(data);
119 if (listener != null)
120 combo.addSelectionListener(new SelectionListener() {
121 public void widgetSelected(SelectionEvent e) {
122 listener.valueChanged(types[combo.getSelectionIndex()]);
124 public void widgetDefaultSelected(SelectionEvent e) {
132 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
134 protected Control createDialogArea(Composite parent) {
135 Composite composite = (Composite) super.createDialogArea(parent);
136 ((GridLayout)composite.getLayout()).numColumns = 2;
138 // WorkbenchHelp.setHelp(composite, ContextIds.PREF_DIALOG);
140 Group group = new Group(composite, SWT.NONE);
141 GridLayout layout = new GridLayout(2, false);
142 group.setLayout(layout);
143 GridData data = new GridData(GridData.FILL_HORIZONTAL);
144 data.horizontalSpan = 2;
145 group.setLayoutData(data);
146 group.setText(WikiEditorPlugin.getResource("%remoteGroup"));
148 createLabel(group, WikiEditorPlugin.getResource("%url"));
149 fUrl = createText(group, fConfiguration.getURL(), new StringModifyListener() {
150 public void valueChanged(String s) {
151 fConfiguration.setURL(s);
156 createLabel(composite, WikiEditorPlugin.getResource("%user"));
157 fUserName = createText(composite, fConfiguration.getUser() + "", new StringModifyListener() {
158 public void valueChanged(String s) {
159 fConfiguration.setUser(s);
164 createLabel(group, WikiEditorPlugin.getResource("%password"));
165 fPassword = createText(group, fConfiguration.getPassword() + "", new StringModifyListener() {
166 public void valueChanged(String s) {
167 fConfiguration.setPassword(s);
172 createLabel(group, WikiEditorPlugin.getResource("%parseType"));
173 createTypeCombo(group, WikiEditorPlugin.getTypes(), fConfiguration.getType(), new TypeModifyListener() {
174 public void valueChanged(String fType) {
175 fConfiguration.setType(fType);
183 * @see org.eclipse.jface.dialogs.Dialog#okPressed()
185 protected void okPressed() {
186 fConfiguration.save();
190 protected Control createButtonBar(Composite parent) {
191 Control buttonControl = super.createButtonBar(parent);
193 return buttonControl;
196 private void setOKButtonEnabled(boolean curIsEnabled) {
197 if (okButton == null)
198 okButton = getButton(IDialogConstants.OK_ID);
200 if (okButton != null)
201 okButton.setEnabled(curIsEnabled);
204 protected void validateFields() {
205 boolean result = true;
207 String currHostname = fUrl.getText();
208 // if (!isValidHostname(currHostname))
211 String currHostnamePort = fPassword.getText();
213 // Integer.parseInt(currHostnamePort);
214 // } catch (Exception any) {
218 String currMonitorPort = fUserName.getText();
220 // Integer.parseInt(currMonitorPort);
221 // } catch (Exception any) {
225 // if (result && isLocalhost(currHostname)) {
226 // if (currHostnamePort.equals(currMonitorPort))
229 setOKButtonEnabled(result);
232 // protected static boolean isValidHostname(String host) {
233 // if (host == null || host.trim().length() < 1)
235 // if (host.indexOf("/") >= 0)
237 // if (host.indexOf("\\") >= 0)
239 // if (host.indexOf(" ") >= 0)
244 // protected static boolean isLocalhost(String host) {
248 // if ("localhost".equals(host) || "127.0.0.1".equals(host))
250 // InetAddress localHostaddr = InetAddress.getLocalHost();
251 // if (localHostaddr.getHostName().equals(host))
253 // } catch (Exception e) {
254 // Trace.trace(Trace.WARNING, "Error checking for localhost", e);