150e4fc028cae9ceebd3a3e26a44ad8409e4cf86
[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 net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
14 import net.sourceforge.phpeclipse.wiki.internal.IConfigurationWorkingCopy;
15 import net.sourceforge.phpeclipse.wiki.preferences.Messages;
16
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;
34 /**
35  * 
36  */
37 public class ConfigurationDialog extends Dialog {
38         protected IConfigurationWorkingCopy fConfiguration;
39         protected boolean isEdit;
40         
41         private Button okButton;
42         private Text fUserName;
43         private Text fUrl;
44         private Text fPassword;
45         
46         interface StringModifyListener {
47                 public void valueChanged(String s);
48         }
49         
50         interface BooleanModifyListener {
51                 public void valueChanged(boolean b);
52         }
53         
54         interface TypeModifyListener {
55                 public void valueChanged(String fType);
56         }
57
58         /**
59          * @param parentShell
60          */
61         public ConfigurationDialog(Shell parentShell, IConfigurationWorkingCopy configuration) {
62                 super(parentShell);
63                 this.fConfiguration = configuration;
64                 isEdit = true;
65         }
66         
67         public ConfigurationDialog(Shell parentShell) {
68                 super(parentShell);
69                 fConfiguration = WikiEditorPlugin.createConfiguration();
70                 isEdit = false;
71         }
72         
73         protected void configureShell(Shell shell) {
74                 super.configureShell(shell);
75                 if (isEdit)
76                         shell.setText(WikiEditorPlugin.getResource("%editConfig"));
77                 else
78                         shell.setText(WikiEditorPlugin.getResource("%newConfig"));
79         }
80         
81         protected Label createLabel(Composite comp, String txt) {
82                 Label label = new Label(comp, SWT.NONE);
83                 label.setText(txt);
84                 label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING | GridData.VERTICAL_ALIGN_BEGINNING));
85                 return label;
86         }
87         
88         protected Text createPassword(Composite comp, String txt, final StringModifyListener listener) {
89             final Text text = new Text(comp, SWT.BORDER | SWT.PASSWORD);
90                 if (txt != null)
91                         text.setText(txt);
92                 GridData data = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
93                 data.widthHint = 150;
94                 text.setLayoutData(data);
95                 if (listener != null)
96                         text.addModifyListener(new ModifyListener() {
97                                 public void modifyText(ModifyEvent e) { 
98                                         listener.valueChanged(text.getText());
99                                 }
100                         });
101                 return text;
102         }
103         
104         protected Text createText(Composite comp, String txt, final StringModifyListener listener) {
105                 final Text text = new Text(comp, SWT.BORDER);
106                 if (txt != null)
107                         text.setText(txt);
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());
115                                 }
116                         });
117                 return text;
118         }
119         
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];
124                 int index = -1;
125                 for (int i = 0; i < size; i++) {
126                         items[i] = types[i];
127                         if (types[i].equals(sel))
128                                 index = i;
129                 }
130                 combo.setItems(items);
131                 if (index >= 0)
132                         combo.select(index);
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()]);
140                                 }
141                                 public void widgetDefaultSelected(SelectionEvent e) {
142                                         widgetSelected(e);
143                                 }
144                         });
145                 return combo;
146         }
147
148         /* (non-Javadoc)
149          * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
150          */
151         protected Control createDialogArea(Composite parent) {
152                 Composite composite = (Composite) super.createDialogArea(parent);
153                 ((GridLayout)composite.getLayout()).numColumns = 2;
154                 
155 //              WorkbenchHelp.setHelp(composite, ContextIds.PREF_DIALOG);
156                                 
157                 createLabel(composite, WikiEditorPlugin.getResource("%name"));          
158                 fUserName = createText(composite, fConfiguration.getName() + "", new StringModifyListener() {
159                         public void valueChanged(String name) {
160                         fConfiguration.setName(name);
161                                 validateFields();
162                         }
163                 });
164                 
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;
170                 
171                 group.setLayoutData(data);
172                 group.setText(WikiEditorPlugin.getResource("%configGroup"));
173                 
174                  
175                 createLabel(group, WikiEditorPlugin.getResource("%user"));              
176                 fUserName = createText(group, fConfiguration.getUser() + "", new StringModifyListener() {
177                         public void valueChanged(String s) {
178                         fConfiguration.setUser(s);
179                                 validateFields();
180                         }
181                 });
182                 
183                 createLabel(group, WikiEditorPlugin.getResource("%password"));          
184                 fPassword = createPassword(group, fConfiguration.getPassword() + "", new StringModifyListener() {
185                         public void valueChanged(String s) {
186                                 fConfiguration.setPassword(s);
187                                 validateFields();
188                         }
189                 });
190                 
191                 createLabel(group, WikiEditorPlugin.getResource("%url"));               
192                 fUrl = createText(group, fConfiguration.getURL(), new StringModifyListener() {
193                         public void valueChanged(String s) {
194                                 fConfiguration.setURL(s);
195                                 validateFields();
196                         }
197                 });
198                 
199                 
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);
204                         }
205                 });
206                 
207                 return composite;
208         }
209
210         /* (non-Javadoc)
211          * @see org.eclipse.jface.dialogs.Dialog#okPressed()
212          */
213         protected void okPressed() {
214                 fConfiguration.save();
215                 super.okPressed();
216         }
217
218         protected Control createButtonBar(Composite parent) {
219                 Control buttonControl = super.createButtonBar(parent);
220                 validateFields();
221                 return buttonControl;
222         }
223
224         private void setOKButtonEnabled(boolean curIsEnabled) {
225                 if (okButton == null)
226                         okButton = getButton(IDialogConstants.OK_ID);
227                 
228                 if (okButton != null)
229                         okButton.setEnabled(curIsEnabled);
230         }
231
232         protected void validateFields() {
233                 boolean result = true;
234
235                 String currHostname = fUrl.getText();
236 //              if (!isValidHostname(currHostname))
237 //                      result = false;
238                 
239                 String currHostnamePort = fPassword.getText();
240 //              try {
241 //                      Integer.parseInt(currHostnamePort);
242 //              } catch (Exception any) {
243 //                      result = false;
244 //              }
245                 
246                 String currMonitorPort = fUserName.getText();
247 //              try {
248 //                      Integer.parseInt(currMonitorPort);
249 //              } catch (Exception any) {
250 //                      result = false;
251 //              }
252                 
253 //              if (result && isLocalhost(currHostname)) {
254 //                      if (currHostnamePort.equals(currMonitorPort))
255 //                              result = false;
256 //              }
257                 setOKButtonEnabled(result);
258         }
259         
260 //      protected static boolean isValidHostname(String host) {
261 //              if (host == null || host.trim().length() < 1)
262 //                      return false;
263 //              if (host.indexOf("/") >= 0)
264 //                      return false;
265 //              if (host.indexOf("\\") >= 0)
266 //                      return false;
267 //              if (host.indexOf(" ") >= 0)
268 //                      return false;
269 //              return true;
270 //      }
271         
272 //      protected static boolean isLocalhost(String host) {
273 //              if (host == null)
274 //                      return false;
275 //              try {
276 //                      if ("localhost".equals(host) || "127.0.0.1".equals(host))
277 //                              return true;
278 //                      InetAddress localHostaddr = InetAddress.getLocalHost();
279 //                      if (localHostaddr.getHostName().equals(host))
280 //                              return true;
281 //              } catch (Exception e) {
282 //                      Trace.trace(Trace.WARNING, "Error checking for localhost", e);
283 //              }
284 //              return false;
285 //      }
286 }