Added more Wikipedia configurations
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / ui / internal / ConfigurationDialog.java
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
7  �*
8  * Contributors:
9  *    IBM - Initial API and implementation
10  **********************************************************************/
11 package net.sourceforge.phpeclipse.wiki.ui.internal;
12
13 import java.util.ArrayList;
14
15 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
16 import net.sourceforge.phpeclipse.wiki.internal.IConfigurationWorkingCopy;
17
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;
35
36 /**
37  *  
38  */
39 public class ConfigurationDialog extends Dialog {
40   protected IConfigurationWorkingCopy fConfiguration;
41
42   protected boolean isEdit;
43
44   private Button okButton;
45
46   private Text fUserName;
47
48   private Text fUrl;
49
50   private Text fPassword;
51
52   interface StringModifyListener {
53     public void valueChanged(String s);
54   }
55
56   interface BooleanModifyListener {
57     public void valueChanged(boolean b);
58   }
59
60   interface TypeModifyListener {
61     public void valueChanged(String fType);
62   }
63
64   /**
65    * @param parentShell
66    */
67   public ConfigurationDialog(Shell parentShell, IConfigurationWorkingCopy configuration) {
68     super(parentShell);
69     this.fConfiguration = configuration;
70     isEdit = true;
71   }
72
73   public ConfigurationDialog(Shell parentShell) {
74     super(parentShell);
75     fConfiguration = WikiEditorPlugin.createConfiguration();
76     isEdit = false;
77   }
78
79   protected void configureShell(Shell shell) {
80     super.configureShell(shell);
81     if (isEdit)
82       shell.setText(WikiEditorPlugin.getResource("%editConfig"));
83     else
84       shell.setText(WikiEditorPlugin.getResource("%newConfig"));
85   }
86
87   protected Label createLabel(Composite comp, String txt) {
88     Label label = new Label(comp, SWT.NONE);
89     label.setText(txt);
90     label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING | GridData.VERTICAL_ALIGN_BEGINNING));
91     return label;
92   }
93
94   protected Text createPassword(Composite comp, String txt, final StringModifyListener listener) {
95     final Text text = new Text(comp, SWT.BORDER | SWT.PASSWORD);
96     if (txt != null)
97       text.setText(txt);
98     GridData data = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
99     data.widthHint = 150;
100     text.setLayoutData(data);
101     if (listener != null)
102       text.addModifyListener(new ModifyListener() {
103         public void modifyText(ModifyEvent e) {
104           listener.valueChanged(text.getText());
105         }
106       });
107     return text;
108   }
109
110   protected Text createText(Composite comp, String txt, final StringModifyListener listener) {
111     final Text text = new Text(comp, SWT.BORDER);
112     if (txt != null)
113       text.setText(txt);
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());
121         }
122       });
123     return text;
124   }
125
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];
130     int index = -1;
131     for (int i = 0; i < size; i++) {
132       items[i] = (String)types.get(i);
133       if (items[i].equals(sel))
134         index = i;
135     }
136     combo.setItems(items);
137     if (index >= 0)
138       combo.select(index);
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()));
146         }
147
148         public void widgetDefaultSelected(SelectionEvent e) {
149           widgetSelected(e);
150         }
151       });
152     return combo;
153   }
154
155   /*
156    * (non-Javadoc)
157    * 
158    * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
159    */
160   protected Control createDialogArea(Composite parent) {
161     Composite composite = (Composite) super.createDialogArea(parent);
162     ((GridLayout) composite.getLayout()).numColumns = 2;
163
164     //          WorkbenchHelp.setHelp(composite, ContextIds.PREF_DIALOG);
165
166     createLabel(composite, WikiEditorPlugin.getResource("%name"));
167     fUserName = createText(composite, fConfiguration.getName() + "", new StringModifyListener() {
168       public void valueChanged(String name) {
169         fConfiguration.setName(name);
170         validateFields();
171       }
172     });
173
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;
179
180     group.setLayoutData(data);
181     group.setText(WikiEditorPlugin.getResource("%configGroup"));
182
183     createLabel(group, WikiEditorPlugin.getResource("%user"));
184     fUserName = createText(group, fConfiguration.getUser() + "", new StringModifyListener() {
185       public void valueChanged(String s) {
186         fConfiguration.setUser(s);
187         validateFields();
188       }
189     });
190
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);
208
209     createLabel(group, WikiEditorPlugin.getResource("%password"));
210     fPassword = createPassword(group, fConfiguration.getPassword() + "", new StringModifyListener() {
211       public void valueChanged(String s) {
212         fConfiguration.setPassword(s);
213         validateFields();
214       }
215     });
216
217     createLabel(group, WikiEditorPlugin.getResource("%url"));
218     fUrl = createText(group, fConfiguration.getURL(), new StringModifyListener() {
219       public void valueChanged(String s) {
220         fConfiguration.setURL(s);
221         validateFields();
222       }
223     });
224
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);
229       }
230     });
231
232     return composite;
233   }
234
235   /*
236    * (non-Javadoc)
237    * 
238    * @see org.eclipse.jface.dialogs.Dialog#okPressed()
239    */
240   protected void okPressed() {
241     fConfiguration.save();
242     super.okPressed();
243   }
244
245   protected Control createButtonBar(Composite parent) {
246     Control buttonControl = super.createButtonBar(parent);
247     validateFields();
248     return buttonControl;
249   }
250
251   private void setOKButtonEnabled(boolean curIsEnabled) {
252     if (okButton == null)
253       okButton = getButton(IDialogConstants.OK_ID);
254
255     if (okButton != null)
256       okButton.setEnabled(curIsEnabled);
257   }
258
259   protected void validateFields() {
260     boolean result = true;
261
262     String currHostname = fUrl.getText();
263     //          if (!isValidHostname(currHostname))
264     //                  result = false;
265
266     String currHostnamePort = fPassword.getText();
267     //          try {
268     //                  Integer.parseInt(currHostnamePort);
269     //          } catch (Exception any) {
270     //                  result = false;
271     //          }
272
273     String currMonitorPort = fUserName.getText();
274     //          try {
275     //                  Integer.parseInt(currMonitorPort);
276     //          } catch (Exception any) {
277     //                  result = false;
278     //          }
279
280     //          if (result && isLocalhost(currHostname)) {
281     //                  if (currHostnamePort.equals(currMonitorPort))
282     //                          result = false;
283     //          }
284     setOKButtonEnabled(result);
285   }
286
287   //    protected static boolean isValidHostname(String host) {
288   //            if (host == null || host.trim().length() < 1)
289   //                    return false;
290   //            if (host.indexOf("/") >= 0)
291   //                    return false;
292   //            if (host.indexOf("\\") >= 0)
293   //                    return false;
294   //            if (host.indexOf(" ") >= 0)
295   //                    return false;
296   //            return true;
297   //    }
298
299   //    protected static boolean isLocalhost(String host) {
300   //            if (host == null)
301   //                    return false;
302   //            try {
303   //                    if ("localhost".equals(host) || "127.0.0.1".equals(host))
304   //                            return true;
305   //                    InetAddress localHostaddr = InetAddress.getLocalHost();
306   //                    if (localHostaddr.getHostName().equals(host))
307   //                            return true;
308   //            } catch (Exception e) {
309   //                    Trace.trace(Trace.WARNING, "Error checking for localhost", e);
310   //            }
311   //            return false;
312   //    }
313 }