Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 34146efb8bb52131e31a976babf1db00b5ad7178 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
 *(c) Copyright QNX Software Systems Ltd. 2002.
 * All Rights Reserved.
 * 
 */

package org.eclipse.cdt.debug.mi.internal.ui;

import java.util.Observable;

import org.eclipse.cdt.debug.mi.core.IGDBServerMILaunchConfigurationConstants;
import org.eclipse.cdt.debug.mi.internal.ui.dialogfields.DialogField;
import org.eclipse.cdt.debug.mi.internal.ui.dialogfields.IDialogFieldListener;
import org.eclipse.cdt.debug.mi.internal.ui.dialogfields.LayoutUtil;
import org.eclipse.cdt.debug.mi.internal.ui.dialogfields.StringDialogField;
import org.eclipse.cdt.utils.ui.controls.ControlFactory;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;

/**
 * Enter type comment.
 * 
 * @since Nov 20, 2003
 */
public class TCPSettingsBlock extends Observable
{
	private final static String DEFAULT_HOST_NAME = "localhost";
	private final static String DEFAULT_PORT_NUMBER = "10000";

	private Shell fShell;

	private StringDialogField fHostNameField;
	private StringDialogField fPortNumberField;
	
	private Control fControl;

	private String fErrorMessage = null;
	
	public TCPSettingsBlock()
	{
		super();
		fHostNameField = createHostNameField();
		fPortNumberField = createPortNumberField();
	}

	public void createBlock( Composite parent )
	{
		fShell = parent.getShell();
		Composite comp = ControlFactory.createCompositeEx( parent, 2, GridData.FILL_BOTH );
		((GridLayout)comp.getLayout()).makeColumnsEqualWidth = false;
		((GridLayout)comp.getLayout()).marginHeight = 0; 
		((GridLayout)comp.getLayout()).marginWidth = 0; 
		comp.setFont( JFaceResources.getDialogFont() );

		PixelConverter converter = new PixelConverter( comp );
		
		fHostNameField.doFillIntoGrid( comp, 2 );
		LayoutUtil.setWidthHint( fHostNameField.getTextControl( null ), converter.convertWidthInCharsToPixels( 20 ) );
		fPortNumberField.doFillIntoGrid( comp, 2 );
		((GridData)fPortNumberField.getTextControl( null ).getLayoutData()).horizontalAlignment = GridData.BEGINNING;
		LayoutUtil.setWidthHint( fPortNumberField.getTextControl( null ), converter.convertWidthInCharsToPixels( 10 ) );
		
		setControl( comp );
	}

	protected Shell getShell()
	{
		return fShell;
	}

	public void dispose()
	{
		deleteObservers();
	}

	public void initializeFrom( ILaunchConfiguration configuration )
	{
		initializeHostName( configuration );
		initializePortNumber( configuration );
	}

	public void setDefaults( ILaunchConfigurationWorkingCopy configuration )
	{
		configuration.setAttribute( IGDBServerMILaunchConfigurationConstants.ATTR_HOST, DEFAULT_HOST_NAME );
		configuration.setAttribute( IGDBServerMILaunchConfigurationConstants.ATTR_PORT, DEFAULT_PORT_NUMBER );
	}

	public void performApply( ILaunchConfigurationWorkingCopy configuration )
	{
		if ( fHostNameField != null )
			configuration.setAttribute( IGDBServerMILaunchConfigurationConstants.ATTR_HOST, fHostNameField.getText().trim() );
		if ( fPortNumberField != null )
			configuration.setAttribute( IGDBServerMILaunchConfigurationConstants.ATTR_PORT, fPortNumberField.getText().trim() );
	}

	private StringDialogField createHostNameField()
	{
		StringDialogField field = new StringDialogField();
		field.setLabelText( "Host name or IP address: " );
		field.setDialogFieldListener( 
						new IDialogFieldListener()
							{
								public void dialogFieldChanged( DialogField field )
								{
									hostNameFieldChanged();
								}
							} );
		return field; 
	}

	private StringDialogField createPortNumberField()
	{ 
		StringDialogField field = new StringDialogField();
		field.setLabelText( "Port number: " );
		field.setDialogFieldListener( 
						new IDialogFieldListener()
							{
								public void dialogFieldChanged( DialogField field )
								{
									portNumberFieldChanged();
								}
							} );
		return field; 
	}

	protected void hostNameFieldChanged()
	{
		updateErrorMessage();
		setChanged();
		notifyObservers();
	}

	protected void portNumberFieldChanged()
	{
		updateErrorMessage();
		setChanged();
		notifyObservers();
	}

	private void initializeHostName( ILaunchConfiguration configuration )
	{
		if ( fHostNameField != null )
		{
			try
			{
				fHostNameField.setText( configuration.getAttribute( IGDBServerMILaunchConfigurationConstants.ATTR_HOST, DEFAULT_HOST_NAME ) );
			}
			catch( CoreException e )
			{
			}
		}
	}

	private void initializePortNumber( ILaunchConfiguration configuration )
	{
		if ( fPortNumberField != null )
		{
			try
			{
				fPortNumberField.setText( configuration.getAttribute( IGDBServerMILaunchConfigurationConstants.ATTR_PORT, DEFAULT_PORT_NUMBER ) );
			}
			catch( CoreException e )
			{
			}
		}
	}

	public Control getControl()
	{
		return fControl;
	}

	protected void setControl( Control control )
	{
		fControl = control;
	}

	public boolean isValid( ILaunchConfiguration configuration )
	{
		updateErrorMessage();
		return ( getErrorMessage() == null );
	}

	private void updateErrorMessage()
	{
		setErrorMessage( null );
		if ( fHostNameField != null && fPortNumberField != null )
		{
			if ( fHostNameField.getText().trim().length() == 0 )
				setErrorMessage( "Host name or IP address must be specified." );
			else if ( !hostNameIsValid( fHostNameField.getText().trim() ) )
				setErrorMessage( "Invalid host name or IP address." );
			else if ( fPortNumberField.getText().trim().length() == 0 )
				setErrorMessage( "Port number must be specified." );
			else if ( !portNumberIsValid( fPortNumberField.getText().trim() ) )
				setErrorMessage( "Invalid port number." );
		}
	}

	public String getErrorMessage()
	{
		return fErrorMessage;
	}

	private void setErrorMessage( String string )
	{
		fErrorMessage = string;
	}

	private boolean hostNameIsValid( String hostName )
	{
		return true;
	}

	private boolean portNumberIsValid( String portNumber )
	{
		try
		{
			int port = Short.parseShort( portNumber );
			if ( port < 0 )
				return false;
		}
		catch( NumberFormatException e )
		{
			return false;
		}
		return true;
	}
}

Back to the top