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.*;
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;
37 public class MonitorDialog extends Dialog {
38 protected IMonitorWorkingCopy monitor;
39 protected boolean isEdit;
41 private Button okButton;
42 private Text monitorPort;
43 private Text remoteHostname;
44 private Text remotePort;
46 interface StringModifyListener {
47 public void valueChanged(String s);
50 interface BooleanModifyListener {
51 public void valueChanged(boolean b);
54 interface TypeModifyListener {
55 public void valueChanged(IProtocolAdapter type);
61 public MonitorDialog(Shell parentShell, IMonitorWorkingCopy monitor) {
63 this.monitor = monitor;
67 public MonitorDialog(Shell parentShell) {
69 monitor = MonitorCore.createMonitor();
73 protected void configureShell(Shell shell) {
74 super.configureShell(shell);
76 shell.setText(MonitorUIPlugin.getResource("%editMonitor"));
78 shell.setText(MonitorUIPlugin.getResource("%newMonitor"));
81 protected Label createLabel(Composite comp, String txt) {
82 Label label = new Label(comp, SWT.NONE);
84 label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING | GridData.VERTICAL_ALIGN_BEGINNING));
88 protected Text createText(Composite comp, String txt, final StringModifyListener listener) {
89 final Text text = new Text(comp, SWT.BORDER);
92 GridData data = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
94 text.setLayoutData(data);
96 text.addModifyListener(new ModifyListener() {
97 public void modifyText(ModifyEvent e) {
98 listener.valueChanged(text.getText());
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];
109 for (int i = 0; i < size; i++) {
110 items[i] = types[i].getName();
111 if (types[i].equals(sel))
114 combo.setItems(items);
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()]);
125 public void widgetDefaultSelected(SelectionEvent e) {
133 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
135 protected Control createDialogArea(Composite parent) {
136 Composite composite = (Composite) super.createDialogArea(parent);
137 ((GridLayout)composite.getLayout()).numColumns = 2;
139 WorkbenchHelp.setHelp(composite, ContextIds.PREF_DIALOG);
141 createLabel(composite, MonitorUIPlugin.getResource("%localPort"));
142 monitorPort = createText(composite, monitor.getLocalPort() + "", new StringModifyListener() {
143 public void valueChanged(String s) {
145 monitor.setLocalPort(Integer.parseInt(s));
146 } catch (Exception e) { }
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"));
159 createLabel(group, MonitorUIPlugin.getResource("%remoteHost"));
160 remoteHostname = createText(group, monitor.getRemoteHost(), new StringModifyListener() {
161 public void valueChanged(String s) {
162 monitor.setRemoteHost(s);
167 createLabel(group, MonitorUIPlugin.getResource("%remotePort"));
168 remotePort = createText(group, monitor.getRemotePort() + "", new StringModifyListener() {
169 public void valueChanged(String s) {
171 monitor.setRemotePort(Integer.parseInt(s));
172 } catch (Exception e) { }
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);
188 * @see org.eclipse.jface.dialogs.Dialog#okPressed()
190 protected void okPressed() {
195 protected Control createButtonBar(Composite parent) {
196 Control buttonControl = super.createButtonBar(parent);
198 return buttonControl;
201 private void setOKButtonEnabled(boolean curIsEnabled) {
202 if (okButton == null)
203 okButton = getButton(IDialogConstants.OK_ID);
205 if (okButton != null)
206 okButton.setEnabled(curIsEnabled);
209 protected void validateFields() {
210 if (monitorPort == null)
213 boolean result = true;
215 String currHostname = remoteHostname.getText();
216 if (!isValidHostname(currHostname))
219 String currHostnamePort = remotePort.getText();
221 Integer.parseInt(currHostnamePort);
222 } catch (Exception any) {
226 String currMonitorPort = monitorPort.getText();
228 Integer.parseInt(currMonitorPort);
229 } catch (Exception any) {
233 if (result && isLocalhost(currHostname)) {
234 if (currHostnamePort.equals(currMonitorPort))
237 setOKButtonEnabled(result);
240 protected static boolean isValidHostname(String host) {
241 if (host == null || host.trim().length() < 1)
243 if (host.indexOf("/") >= 0)
245 if (host.indexOf("\\") >= 0)
247 if (host.indexOf(" ") >= 0)
252 protected static boolean isLocalhost(String host) {
256 if ("localhost".equals(host) || "127.0.0.1".equals(host))
258 InetAddress localHostaddr = InetAddress.getLocalHost();
259 if (localHostaddr.getHostName().equals(host))
261 } catch (Exception e) {
262 Trace.trace(Trace.WARNING, "Error checking for localhost", e);