inital plugin from webtools project
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.monitor.ui / src / net / sourceforge / phpdt / monitor / ui / internal / MonitorDialog.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.phpdt.monitor.ui.internal;
12
13 import java.net.InetAddress;
14
15 import net.sourceforge.phpdt.monitor.core.IMonitorWorkingCopy;
16 import net.sourceforge.phpdt.monitor.core.IProtocolAdapter;
17 import net.sourceforge.phpdt.monitor.core.MonitorCore;
18
19 import org.eclipse.jface.dialogs.Dialog;
20 import org.eclipse.jface.dialogs.IDialogConstants;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.events.*;
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 import org.eclipse.ui.help.WorkbenchHelp;
34 /**
35  * 
36  */
37 public class MonitorDialog extends Dialog {
38         protected IMonitorWorkingCopy monitor;
39         protected boolean isEdit;
40         
41         private Button okButton;
42         private Text monitorPort;
43         private Text remoteHostname;
44         private Text remotePort;
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(IProtocolAdapter type);
56         }
57
58         /**
59          * @param parentShell
60          */
61         public MonitorDialog(Shell parentShell, IMonitorWorkingCopy monitor) {
62                 super(parentShell);
63                 this.monitor = monitor;
64                 isEdit = true;
65         }
66         
67         public MonitorDialog(Shell parentShell) {
68                 super(parentShell);
69                 monitor = MonitorCore.createMonitor();
70                 isEdit = false;
71         }
72         
73         protected void configureShell(Shell shell) {
74                 super.configureShell(shell);
75                 if (isEdit)
76                         shell.setText(MonitorUIPlugin.getResource("%editMonitor"));
77                 else
78                         shell.setText(MonitorUIPlugin.getResource("%newMonitor"));
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 createText(Composite comp, String txt, final StringModifyListener listener) {
89                 final Text text = new Text(comp, SWT.BORDER);
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 Combo createTypeCombo(Composite comp, final IProtocolAdapter[] types, IProtocolAdapter sel, final TypeModifyListener listener) {
105                 final Combo combo = new Combo(comp, SWT.DROP_DOWN | SWT.READ_ONLY);
106                 int size = types.length;
107                 String[] items = new String[size];
108                 int index = -1;
109                 for (int i = 0; i < size; i++) {
110                         items[i] = types[i].getName();
111                         if (types[i].equals(sel))
112                                 index = i;
113                 }
114                 combo.setItems(items);
115                 if (index >= 0)
116                         combo.select(index);
117                 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_BEGINNING);
118                 data.widthHint = 150;
119                 combo.setLayoutData(data);
120                 if (listener != null)
121                         combo.addSelectionListener(new SelectionListener() {
122                                 public void widgetSelected(SelectionEvent e) {  
123                                         listener.valueChanged(types[combo.getSelectionIndex()]);
124                                 }
125                                 public void widgetDefaultSelected(SelectionEvent e) {
126                                         widgetSelected(e);
127                                 }
128                         });
129                 return combo;
130         }
131
132         /* (non-Javadoc)
133          * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
134          */
135         protected Control createDialogArea(Composite parent) {
136                 Composite composite = (Composite) super.createDialogArea(parent);
137                 ((GridLayout)composite.getLayout()).numColumns = 2;
138                 
139                 WorkbenchHelp.setHelp(composite, ContextIds.PREF_DIALOG);
140                 
141                 createLabel(composite, MonitorUIPlugin.getResource("%localPort"));              
142                 monitorPort = createText(composite, monitor.getLocalPort() + "", new StringModifyListener() {
143                         public void valueChanged(String s) {
144                                 try {
145                                         monitor.setLocalPort(Integer.parseInt(s));
146                                 } catch (Exception e) { }
147                                 validateFields();
148                         }
149                 });
150                 
151                 Group group = new Group(composite, SWT.NONE);
152                 GridLayout layout = new GridLayout(2, false);
153                 group.setLayout(layout);
154                 GridData data = new GridData(GridData.FILL_HORIZONTAL);
155                 data.horizontalSpan = 2;
156                 group.setLayoutData(data);
157                 group.setText(MonitorUIPlugin.getResource("%remoteGroup"));
158                 
159                 createLabel(group, MonitorUIPlugin.getResource("%remoteHost"));         
160                 remoteHostname = createText(group, monitor.getRemoteHost(), new StringModifyListener() {
161                         public void valueChanged(String s) {
162                                 monitor.setRemoteHost(s);
163                                 validateFields();
164                         }
165                 });
166                 
167                 createLabel(group, MonitorUIPlugin.getResource("%remotePort"));         
168                 remotePort = createText(group, monitor.getRemotePort() + "", new StringModifyListener() {
169                         public void valueChanged(String s) {
170                                 try {
171                                         monitor.setRemotePort(Integer.parseInt(s));
172                                 } catch (Exception e) { }
173                                 validateFields();
174                         }
175                 });
176                 
177                 createLabel(group, MonitorUIPlugin.getResource("%parseType"));          
178                 createTypeCombo(group, MonitorCore.getProtocolAdapters(), monitor.getProtocolAdapter(), new TypeModifyListener() {
179                         public void valueChanged(IProtocolAdapter type) {
180                                 monitor.setProtocolAdapter(type);
181                         }
182                 });
183                 
184                 return composite;
185         }
186
187         /* (non-Javadoc)
188          * @see org.eclipse.jface.dialogs.Dialog#okPressed()
189          */
190         protected void okPressed() {
191                 monitor.save();
192                 super.okPressed();
193         }
194
195         protected Control createButtonBar(Composite parent) {
196                 Control buttonControl = super.createButtonBar(parent);
197                 validateFields();
198                 return buttonControl;
199         }
200
201         private void setOKButtonEnabled(boolean curIsEnabled) {
202                 if (okButton == null)
203                         okButton = getButton(IDialogConstants.OK_ID);
204                 
205                 if (okButton != null)
206                         okButton.setEnabled(curIsEnabled);
207         }
208
209         protected void validateFields() {
210                 if (monitorPort == null)
211                         return;
212                 
213                 boolean result = true;
214
215                 String currHostname = remoteHostname.getText();
216                 if (!isValidHostname(currHostname))
217                         result = false;
218                 
219                 String currHostnamePort = remotePort.getText();
220                 try {
221                         Integer.parseInt(currHostnamePort);
222                 } catch (Exception any) {
223                         result = false;
224                 }
225                 
226                 String currMonitorPort = monitorPort.getText();
227                 try {
228                         Integer.parseInt(currMonitorPort);
229                 } catch (Exception any) {
230                         result = false;
231                 }
232                 
233                 if (result && isLocalhost(currHostname)) {
234                         if (currHostnamePort.equals(currMonitorPort))
235                                 result = false;
236                 }
237                 setOKButtonEnabled(result);
238         }
239         
240         protected static boolean isValidHostname(String host) {
241                 if (host == null || host.trim().length() < 1)
242                         return false;
243                 if (host.indexOf("/") >= 0)
244                         return false;
245                 if (host.indexOf("\\") >= 0)
246                         return false;
247                 if (host.indexOf(" ") >= 0)
248                         return false;
249                 return true;
250         }
251         
252         protected static boolean isLocalhost(String host) {
253                 if (host == null)
254                         return false;
255                 try {
256                         if ("localhost".equals(host) || "127.0.0.1".equals(host))
257                                 return true;
258                         InetAddress localHostaddr = InetAddress.getLocalHost();
259                         if (localHostaddr.getHostName().equals(host))
260                                 return true;
261                 } catch (Exception e) {
262                         Trace.trace(Trace.WARNING, "Error checking for localhost", e);
263                 }
264                 return false;
265         }
266 }