04ef13ecefcc75d735f2c422506297f9119f65ae
[phpeclipse.git] /
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
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;
33 /**
34  * 
35  */
36 public class ConfigurationDialog extends Dialog {
37         protected IConfigurationWorkingCopy fConfiguration;
38         protected boolean isEdit;
39         
40         private Button okButton;
41         private Text fUserName;
42         private Text fUrl;
43         private Text fPassword;
44         
45         interface StringModifyListener {
46                 public void valueChanged(String s);
47         }
48         
49         interface BooleanModifyListener {
50                 public void valueChanged(boolean b);
51         }
52         
53         interface TypeModifyListener {
54                 public void valueChanged(String fType);
55         }
56
57         /**
58          * @param parentShell
59          */
60         public ConfigurationDialog(Shell parentShell, IConfigurationWorkingCopy configuration) {
61                 super(parentShell);
62                 this.fConfiguration = configuration;
63                 isEdit = true;
64         }
65         
66         public ConfigurationDialog(Shell parentShell) {
67                 super(parentShell);
68                 fConfiguration = WikiEditorPlugin.createConfiguration();
69                 isEdit = false;
70         }
71         
72         protected void configureShell(Shell shell) {
73                 super.configureShell(shell);
74                 if (isEdit)
75                         shell.setText(WikiEditorPlugin.getResource("%editConfig"));
76                 else
77                         shell.setText(WikiEditorPlugin.getResource("%newConfig"));
78         }
79         
80         protected Label createLabel(Composite comp, String txt) {
81                 Label label = new Label(comp, SWT.NONE);
82                 label.setText(txt);
83                 label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING | GridData.VERTICAL_ALIGN_BEGINNING));
84                 return label;
85         }
86         
87         protected Text createText(Composite comp, String txt, final StringModifyListener listener) {
88                 final Text text = new Text(comp, SWT.BORDER);
89                 if (txt != null)
90                         text.setText(txt);
91                 GridData data = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
92                 data.widthHint = 150;
93                 text.setLayoutData(data);
94                 if (listener != null)
95                         text.addModifyListener(new ModifyListener() {
96                                 public void modifyText(ModifyEvent e) { 
97                                         listener.valueChanged(text.getText());
98                                 }
99                         });
100                 return text;
101         }
102         
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];
107                 int index = -1;
108                 for (int i = 0; i < size; i++) {
109                         items[i] = types[i];
110                         if (types[i].equals(sel))
111                                 index = i;
112                 }
113                 combo.setItems(items);
114                 if (index >= 0)
115                         combo.select(index);
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()]);
123                                 }
124                                 public void widgetDefaultSelected(SelectionEvent e) {
125                                         widgetSelected(e);
126                                 }
127                         });
128                 return combo;
129         }
130
131         /* (non-Javadoc)
132          * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
133          */
134         protected Control createDialogArea(Composite parent) {
135                 Composite composite = (Composite) super.createDialogArea(parent);
136                 ((GridLayout)composite.getLayout()).numColumns = 2;
137                 
138 //              WorkbenchHelp.setHelp(composite, ContextIds.PREF_DIALOG);
139                                 
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"));
147                 
148                 createLabel(group, WikiEditorPlugin.getResource("%url"));               
149                 fUrl = createText(group, fConfiguration.getURL(), new StringModifyListener() {
150                         public void valueChanged(String s) {
151                                 fConfiguration.setURL(s);
152                                 validateFields();
153                         }
154                 });
155                  
156                 createLabel(composite, WikiEditorPlugin.getResource("%user"));          
157                 fUserName = createText(composite, fConfiguration.getUser() + "", new StringModifyListener() {
158                         public void valueChanged(String s) {
159                         fConfiguration.setUser(s);
160                                 validateFields();
161                         }
162                 });
163                 
164                 createLabel(group, WikiEditorPlugin.getResource("%password"));          
165                 fPassword = createText(group, fConfiguration.getPassword() + "", new StringModifyListener() {
166                         public void valueChanged(String s) {
167                                 fConfiguration.setPassword(s);
168                                 validateFields();
169                         }
170                 });
171                 
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);
176                         }
177                 });
178                 
179                 return composite;
180         }
181
182         /* (non-Javadoc)
183          * @see org.eclipse.jface.dialogs.Dialog#okPressed()
184          */
185         protected void okPressed() {
186                 fConfiguration.save();
187                 super.okPressed();
188         }
189
190         protected Control createButtonBar(Composite parent) {
191                 Control buttonControl = super.createButtonBar(parent);
192                 validateFields();
193                 return buttonControl;
194         }
195
196         private void setOKButtonEnabled(boolean curIsEnabled) {
197                 if (okButton == null)
198                         okButton = getButton(IDialogConstants.OK_ID);
199                 
200                 if (okButton != null)
201                         okButton.setEnabled(curIsEnabled);
202         }
203
204         protected void validateFields() {
205                 boolean result = true;
206
207                 String currHostname = fUrl.getText();
208 //              if (!isValidHostname(currHostname))
209 //                      result = false;
210                 
211                 String currHostnamePort = fPassword.getText();
212 //              try {
213 //                      Integer.parseInt(currHostnamePort);
214 //              } catch (Exception any) {
215 //                      result = false;
216 //              }
217                 
218                 String currMonitorPort = fUserName.getText();
219 //              try {
220 //                      Integer.parseInt(currMonitorPort);
221 //              } catch (Exception any) {
222 //                      result = false;
223 //              }
224                 
225 //              if (result && isLocalhost(currHostname)) {
226 //                      if (currHostnamePort.equals(currMonitorPort))
227 //                              result = false;
228 //              }
229                 setOKButtonEnabled(result);
230         }
231         
232 //      protected static boolean isValidHostname(String host) {
233 //              if (host == null || host.trim().length() < 1)
234 //                      return false;
235 //              if (host.indexOf("/") >= 0)
236 //                      return false;
237 //              if (host.indexOf("\\") >= 0)
238 //                      return false;
239 //              if (host.indexOf(" ") >= 0)
240 //                      return false;
241 //              return true;
242 //      }
243         
244 //      protected static boolean isLocalhost(String host) {
245 //              if (host == null)
246 //                      return false;
247 //              try {
248 //                      if ("localhost".equals(host) || "127.0.0.1".equals(host))
249 //                              return true;
250 //                      InetAddress localHostaddr = InetAddress.getLocalHost();
251 //                      if (localHostaddr.getHostName().equals(host))
252 //                              return true;
253 //              } catch (Exception e) {
254 //                      Trace.trace(Trace.WARNING, "Error checking for localhost", e);
255 //              }
256 //              return false;
257 //      }
258 }