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