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
9 * IBM - Initial API and implementation
10 **********************************************************************/
11 package net.sourceforge.phpdt.monitor.ui.internal;
13 import java.net.InetAddress;
15 import net.sourceforge.phpdt.monitor.core.IMonitorWorkingCopy;
16 import net.sourceforge.phpdt.monitor.core.IProtocolAdapter;
17 import net.sourceforge.phpdt.monitor.core.MonitorCore;
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;
40 public class MonitorDialog extends Dialog {
41 protected IMonitorWorkingCopy monitor;
42 protected boolean isEdit;
44 private Button okButton;
45 private Text monitorPort;
46 private Text remoteHostname;
47 private Text remotePort;
49 interface StringModifyListener {
50 public void valueChanged(String s);
53 interface BooleanModifyListener {
54 public void valueChanged(boolean b);
57 interface TypeModifyListener {
58 public void valueChanged(IProtocolAdapter type);
64 public MonitorDialog(Shell parentShell, IMonitorWorkingCopy monitor) {
66 this.monitor = monitor;
70 public MonitorDialog(Shell parentShell) {
72 monitor = MonitorCore.createMonitor();
76 protected void configureShell(Shell shell) {
77 super.configureShell(shell);
79 shell.setText(MonitorUIPlugin.getResource("%editMonitor"));
81 shell.setText(MonitorUIPlugin.getResource("%newMonitor"));
84 protected Label createLabel(Composite comp, String txt) {
85 Label label = new Label(comp, SWT.NONE);
87 label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING | GridData.VERTICAL_ALIGN_BEGINNING));
91 protected Text createText(Composite comp, String txt, final StringModifyListener listener) {
92 final Text text = new Text(comp, SWT.BORDER);
95 GridData data = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
97 text.setLayoutData(data);
99 text.addModifyListener(new ModifyListener() {
100 public void modifyText(ModifyEvent e) {
101 listener.valueChanged(text.getText());
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];
112 for (int i = 0; i < size; i++) {
113 items[i] = types[i].getName();
114 if (types[i].equals(sel))
117 combo.setItems(items);
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()]);
128 public void widgetDefaultSelected(SelectionEvent e) {
136 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
138 protected Control createDialogArea(Composite parent) {
139 Composite composite = (Composite) super.createDialogArea(parent);
140 ((GridLayout)composite.getLayout()).numColumns = 2;
142 WorkbenchHelp.setHelp(composite, ContextIds.PREF_DIALOG);
144 createLabel(composite, MonitorUIPlugin.getResource("%localPort"));
145 monitorPort = createText(composite, monitor.getLocalPort() + "", new StringModifyListener() {
146 public void valueChanged(String s) {
148 monitor.setLocalPort(Integer.parseInt(s));
149 } catch (Exception e) { }
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"));
162 createLabel(group, MonitorUIPlugin.getResource("%remoteHost"));
163 remoteHostname = createText(group, monitor.getRemoteHost(), new StringModifyListener() {
164 public void valueChanged(String s) {
165 monitor.setRemoteHost(s);
170 createLabel(group, MonitorUIPlugin.getResource("%remotePort"));
171 remotePort = createText(group, monitor.getRemotePort() + "", new StringModifyListener() {
172 public void valueChanged(String s) {
174 monitor.setRemotePort(Integer.parseInt(s));
175 } catch (Exception e) { }
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);
191 * @see org.eclipse.jface.dialogs.Dialog#okPressed()
193 protected void okPressed() {
198 protected Control createButtonBar(Composite parent) {
199 Control buttonControl = super.createButtonBar(parent);
201 return buttonControl;
204 private void setOKButtonEnabled(boolean curIsEnabled) {
205 if (okButton == null)
206 okButton = getButton(IDialogConstants.OK_ID);
208 if (okButton != null)
209 okButton.setEnabled(curIsEnabled);
212 protected void validateFields() {
213 if (monitorPort == null)
216 boolean result = true;
218 String currHostname = remoteHostname.getText();
219 if (!isValidHostname(currHostname))
222 String currHostnamePort = remotePort.getText();
224 Integer.parseInt(currHostnamePort);
225 } catch (Exception any) {
229 String currMonitorPort = monitorPort.getText();
231 Integer.parseInt(currMonitorPort);
232 } catch (Exception any) {
236 if (result && isLocalhost(currHostname)) {
237 if (currHostnamePort.equals(currMonitorPort))
240 setOKButtonEnabled(result);
243 protected static boolean isValidHostname(String host) {
244 if (host == null || host.trim().length() < 1)
246 if (host.indexOf("/") >= 0)
248 if (host.indexOf("\\") >= 0)
250 if (host.indexOf(" ") >= 0)
255 protected static boolean isLocalhost(String host) {
259 if ("localhost".equals(host) || "127.0.0.1".equals(host))
261 InetAddress localHostaddr = InetAddress.getLocalHost();
262 if (localHostaddr.getHostName().equals(host))
264 } catch (Exception e) {
265 Trace.trace(Trace.WARNING, "Error checking for localhost", e);