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 java.util.ArrayList;
15 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
16 import net.sourceforge.phpeclipse.wiki.internal.IConfigurationWorkingCopy;
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 = WikiEditorPlugin.createConfiguration();
79 protected void configureShell(Shell shell) {
80 super.configureShell(shell);
82 shell.setText(WikiEditorPlugin.getResource("%editConfig"));
84 shell.setText(WikiEditorPlugin.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, WikiEditorPlugin.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(WikiEditorPlugin.getResource("%configGroup"));
183 createLabel(group, WikiEditorPlugin.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(WikiEditorPlugin.getResource("%scrambledPassword")); //$NON-NLS-1$
205 data = new GridData(GridData.FILL_HORIZONTAL);
206 data.widthHint = 300;
207 warningText.setLayoutData(data);
209 createLabel(group, WikiEditorPlugin.getResource("%password"));
210 fPassword = createPassword(group, fConfiguration.getPassword() + "", new StringModifyListener() {
211 public void valueChanged(String s) {
212 fConfiguration.setPassword(s);
217 createLabel(group, WikiEditorPlugin.getResource("%url"));
218 fUrl = createText(group, fConfiguration.getURL(), new StringModifyListener() {
219 public void valueChanged(String s) {
220 fConfiguration.setURL(s);
225 createLabel(group, WikiEditorPlugin.getResource("%parseType"));
226 createTypeCombo(group, WikiEditorPlugin.getTypes(), fConfiguration.getType(), new TypeModifyListener() {
227 public void valueChanged(String fType) {
228 fConfiguration.setType(fType);
238 * @see org.eclipse.jface.dialogs.Dialog#okPressed()
240 protected void okPressed() {
241 fConfiguration.save();
245 protected Control createButtonBar(Composite parent) {
246 Control buttonControl = super.createButtonBar(parent);
248 return buttonControl;
251 private void setOKButtonEnabled(boolean curIsEnabled) {
252 if (okButton == null)
253 okButton = getButton(IDialogConstants.OK_ID);
255 if (okButton != null)
256 okButton.setEnabled(curIsEnabled);
259 protected void validateFields() {
260 boolean result = true;
262 String currHostname = fUrl.getText();
263 // if (!isValidHostname(currHostname))
266 String currHostnamePort = fPassword.getText();
268 // Integer.parseInt(currHostnamePort);
269 // } catch (Exception any) {
273 String currMonitorPort = fUserName.getText();
275 // Integer.parseInt(currMonitorPort);
276 // } catch (Exception any) {
280 // if (result && isLocalhost(currHostname)) {
281 // if (currHostnamePort.equals(currMonitorPort))
284 setOKButtonEnabled(result);
287 // protected static boolean isValidHostname(String host) {
288 // if (host == null || host.trim().length() < 1)
290 // if (host.indexOf("/") >= 0)
292 // if (host.indexOf("\\") >= 0)
294 // if (host.indexOf(" ") >= 0)
299 // protected static boolean isLocalhost(String host) {
303 // if ("localhost".equals(host) || "127.0.0.1".equals(host))
305 // InetAddress localHostaddr = InetAddress.getLocalHost();
306 // if (localHostaddr.getHostName().equals(host))
308 // } catch (Exception e) {
309 // Trace.trace(Trace.WARNING, "Error checking for localhost", e);