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;
38 public class ConfigurationDialog extends Dialog {
39 protected IConfigurationWorkingCopy fConfiguration;
41 protected boolean isEdit;
43 private Button okButton;
45 private Text fUserName;
49 private Text fPassword;
51 interface StringModifyListener {
52 public void valueChanged(String s);
55 interface BooleanModifyListener {
56 public void valueChanged(boolean b);
59 interface TypeModifyListener {
60 public void valueChanged(String fType);
66 public ConfigurationDialog(Shell parentShell, IConfigurationWorkingCopy configuration) {
68 this.fConfiguration = configuration;
72 public ConfigurationDialog(Shell parentShell) {
74 fConfiguration = WikiEditorPlugin.createConfiguration();
78 protected void configureShell(Shell shell) {
79 super.configureShell(shell);
81 shell.setText(WikiEditorPlugin.getResource("%editConfig"));
83 shell.setText(WikiEditorPlugin.getResource("%newConfig"));
86 protected Label createLabel(Composite comp, String txt) {
87 Label label = new Label(comp, SWT.NONE);
89 label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING | GridData.VERTICAL_ALIGN_BEGINNING));
93 protected Text createPassword(Composite comp, String txt, final StringModifyListener listener) {
94 final Text text = new Text(comp, SWT.BORDER | SWT.PASSWORD);
97 GridData data = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
99 text.setLayoutData(data);
100 if (listener != null)
101 text.addModifyListener(new ModifyListener() {
102 public void modifyText(ModifyEvent e) {
103 listener.valueChanged(text.getText());
109 protected Text createText(Composite comp, String txt, final StringModifyListener listener) {
110 final Text text = new Text(comp, SWT.BORDER);
113 GridData data = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
114 data.widthHint = 150;
115 text.setLayoutData(data);
116 if (listener != null)
117 text.addModifyListener(new ModifyListener() {
118 public void modifyText(ModifyEvent e) {
119 listener.valueChanged(text.getText());
125 protected Combo createTypeCombo(Composite comp, final String[] types, String sel, final TypeModifyListener listener) {
126 final Combo combo = new Combo(comp, SWT.DROP_DOWN | SWT.READ_ONLY);
127 int size = types.length;
128 String[] items = new String[size];
130 for (int i = 0; i < size; i++) {
132 if (types[i].equals(sel))
135 combo.setItems(items);
138 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_BEGINNING);
139 data.widthHint = 150;
140 combo.setLayoutData(data);
141 if (listener != null)
142 combo.addSelectionListener(new SelectionListener() {
143 public void widgetSelected(SelectionEvent e) {
144 listener.valueChanged(types[combo.getSelectionIndex()]);
147 public void widgetDefaultSelected(SelectionEvent e) {
157 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
159 protected Control createDialogArea(Composite parent) {
160 Composite composite = (Composite) super.createDialogArea(parent);
161 ((GridLayout) composite.getLayout()).numColumns = 2;
163 // WorkbenchHelp.setHelp(composite, ContextIds.PREF_DIALOG);
165 createLabel(composite, WikiEditorPlugin.getResource("%name"));
166 fUserName = createText(composite, fConfiguration.getName() + "", new StringModifyListener() {
167 public void valueChanged(String name) {
168 fConfiguration.setName(name);
173 Group group = new Group(composite, SWT.NONE);
174 GridLayout layout = new GridLayout(2, false);
175 group.setLayout(layout);
176 GridData data = new GridData(GridData.FILL_HORIZONTAL);
177 data.horizontalSpan = 2;
179 group.setLayoutData(data);
180 group.setText(WikiEditorPlugin.getResource("%configGroup"));
182 createLabel(group, WikiEditorPlugin.getResource("%user"));
183 fUserName = createText(group, fConfiguration.getUser() + "", new StringModifyListener() {
184 public void valueChanged(String s) {
185 fConfiguration.setUser(s);
190 Composite warningComposite = new Composite(group, SWT.NONE);
191 layout = new GridLayout();
192 layout.numColumns = 2;
193 layout.marginHeight = 0;
194 layout.marginHeight = 0;
195 warningComposite.setLayout(layout);
196 data = new GridData(GridData.FILL_HORIZONTAL);
197 data.horizontalSpan = 3;
198 warningComposite.setLayoutData(data);
199 Label warningLabel = new Label(warningComposite, SWT.NONE);
200 warningLabel.setImage(getImage(DLG_IMG_MESSAGE_WARNING));
201 warningLabel.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING | GridData.HORIZONTAL_ALIGN_BEGINNING));
202 Label warningText = new Label(warningComposite, SWT.WRAP);
203 warningText.setText(WikiEditorPlugin.getResource("%scrambledPassword")); //$NON-NLS-1$
204 data = new GridData(GridData.FILL_HORIZONTAL);
205 data.widthHint = 300;
206 warningText.setLayoutData(data);
208 createLabel(group, WikiEditorPlugin.getResource("%password"));
209 fPassword = createPassword(group, fConfiguration.getPassword() + "", new StringModifyListener() {
210 public void valueChanged(String s) {
211 fConfiguration.setPassword(s);
216 createLabel(group, WikiEditorPlugin.getResource("%url"));
217 fUrl = createText(group, fConfiguration.getURL(), new StringModifyListener() {
218 public void valueChanged(String s) {
219 fConfiguration.setURL(s);
224 createLabel(group, WikiEditorPlugin.getResource("%parseType"));
225 createTypeCombo(group, WikiEditorPlugin.getTypes(), fConfiguration.getType(), new TypeModifyListener() {
226 public void valueChanged(String fType) {
227 fConfiguration.setType(fType);
237 * @see org.eclipse.jface.dialogs.Dialog#okPressed()
239 protected void okPressed() {
240 fConfiguration.save();
244 protected Control createButtonBar(Composite parent) {
245 Control buttonControl = super.createButtonBar(parent);
247 return buttonControl;
250 private void setOKButtonEnabled(boolean curIsEnabled) {
251 if (okButton == null)
252 okButton = getButton(IDialogConstants.OK_ID);
254 if (okButton != null)
255 okButton.setEnabled(curIsEnabled);
258 protected void validateFields() {
259 boolean result = true;
261 String currHostname = fUrl.getText();
262 // if (!isValidHostname(currHostname))
265 String currHostnamePort = fPassword.getText();
267 // Integer.parseInt(currHostnamePort);
268 // } catch (Exception any) {
272 String currMonitorPort = fUserName.getText();
274 // Integer.parseInt(currMonitorPort);
275 // } catch (Exception any) {
279 // if (result && isLocalhost(currHostname)) {
280 // if (currHostnamePort.equals(currMonitorPort))
283 setOKButtonEnabled(result);
286 // protected static boolean isValidHostname(String host) {
287 // if (host == null || host.trim().length() < 1)
289 // if (host.indexOf("/") >= 0)
291 // if (host.indexOf("\\") >= 0)
293 // if (host.indexOf(" ") >= 0)
298 // protected static boolean isLocalhost(String host) {
302 // if ("localhost".equals(host) || "127.0.0.1".equals(host))
304 // InetAddress localHostaddr = InetAddress.getLocalHost();
305 // if (localHostaddr.getHostName().equals(host))
307 // } catch (Exception e) {
308 // Trace.trace(Trace.WARNING, "Error checking for localhost", e);