Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 54835ab51edaf638bf36947a20b0d12fe99fcd35 (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
/*******************************************************************************
 * Copyright (c) 2013, 2014 Mentor Graphics and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Mentor Graphics - Initial API and implementation
 * Red Hat Inc. - modified for use in Standalone Debugger
 *******************************************************************************/

package org.eclipse.cdt.debug.application;

import java.io.File;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class NewExecutableDialog extends TitleAreaDialog {

	public static final int REMOTE = 0x1;

	private int fFlags = 0;
	private NewExecutableInfo fInfo = null;
	
	private Text fHostBinaryText;
	private Text fTargetBinaryText;
	private Text fBuildLogText;
	private Text fArgumentsText;
	
	private final String fHostBinary;
	private final String fBuildLog;
	private final String fArgs;

	public NewExecutableDialog (Shell parentShell) {
		this(parentShell, 0);
	}
	
	public NewExecutableDialog( Shell parentShell, int flags ) {
		this(parentShell, flags, null, null, null);
	}

	public NewExecutableDialog( Shell parentShell, int flags, String hostBinary, String buildLog, String args) {
		super( parentShell );
		setShellStyle( getShellStyle() | SWT.RESIZE );
		fFlags = flags;
		fHostBinary = hostBinary;
		fBuildLog = buildLog;
		fArgs = args;
	}

	@Override
	protected Control createContents( Composite parent ) {
		Control control = super.createContents( parent );
		validate();
		return control;
	}

	@Override
	protected Control createDialogArea( Composite parent ) {
		boolean remote = (fFlags & REMOTE) > 0;

		getShell().setText( Messages.GdbDebugNewExecutableCommand_Debug_New_Executable ); 
		setTitle( Messages.GdbDebugNewExecutableCommand_Select_Binary );
		String message = ( remote ) ? 
				Messages.GdbDebugNewExecutableCommand_Select_binaries_on_host_and_target :
				Messages.GdbDebugNewExecutableCommand_Select_binary_and_specify_arguments;
		setMessage( message );

		Composite control = (Composite)super.createDialogArea( parent );
		Composite comp = new Composite( control, SWT.NONE );
		GridData gd = new GridData( SWT.FILL, SWT.FILL, true, true );
		GridLayout layout = new GridLayout( 3, false );
		comp.setLayout( layout );
		comp.setLayoutData( gd );
		
		new Label( comp, SWT.None ).setText( remote ? Messages.GdbDebugNewExecutableCommand_Binary_on_host : Messages.GdbDebugExecutableCommand_Binary );
		fHostBinaryText = new Text( comp, SWT.BORDER );
		if (fHostBinary != null)
			fHostBinaryText.setText(fHostBinary);
		fHostBinaryText.setLayoutData( new GridData( SWT.FILL, SWT.CENTER, true, false ) );
		fHostBinaryText.addModifyListener( new ModifyListener() {
			
			@Override
			public void modifyText( ModifyEvent e ) {
				validate();
			}
		} );
		Button browseButton = new Button( comp, SWT.PUSH );
		browseButton.setText( Messages.GdbDebugExecutableCommand_Browse );
		browseButton.setFont( JFaceResources.getDialogFont() );
		setButtonLayoutData( browseButton );
		browseButton.addSelectionListener( new SelectionAdapter() {

			@Override
			public void widgetSelected( SelectionEvent e ) {
				FileDialog dialog = new FileDialog( getShell() );
				dialog.setFileName( fHostBinaryText.getText() );
				String result = dialog.open();
				if ( result != null ) {
					fHostBinaryText.setText( result );
				}
			}
		} );
		
		if ( remote ) {
			new Label( comp, SWT.None ).setText( Messages.GdbDebugNewExecutableCommand_Binary_on_target );
			fTargetBinaryText = new Text( comp, SWT.BORDER );
			fTargetBinaryText.setLayoutData( new GridData( SWT.FILL, SWT.CENTER, true, false, 2, 1 ) );
			fTargetBinaryText.addModifyListener( new ModifyListener() {
				
				@Override
				public void modifyText( ModifyEvent e ) {
					validate();
				}
			} );
		}
		

		new Label( comp, SWT.None ).setText( Messages.GdbDebugNewExecutableCommand_Arguments );
		fArgumentsText = new Text( comp, SWT.BORDER );
		fArgumentsText.setLayoutData( new GridData( SWT.FILL, SWT.CENTER, true, false, 2, 1 ) );
		if (fArgs != null)
			fArgumentsText.setText(fArgs);


		new Label( comp, SWT.None ).setText( Messages.GdbDebugExecutableCommand_BuildLog );
		fBuildLogText = new Text( comp, SWT.BORDER );
		if (fBuildLog != null)
			fBuildLogText.setText(fBuildLog);
		fBuildLogText.setLayoutData( new GridData( SWT.FILL, SWT.CENTER, true, false, 2, 1 ) );
		fBuildLogText.addModifyListener( new ModifyListener() {
			
			@Override
			public void modifyText( ModifyEvent e ) {
				validate();
			}
		} );
		return control;
	}

	@Override
	protected void okPressed() {
		String targetPath = ( fTargetBinaryText != null ) ? fTargetBinaryText.getText().trim() : null;
		String args = fArgumentsText.getText().trim();
		String buildLog = fBuildLogText.getText().trim();
		fInfo = new NewExecutableInfo( fHostBinaryText.getText().trim(), targetPath, buildLog, args );
		super.okPressed();
	}

	public NewExecutableInfo getExecutableInfo() {
		return fInfo;
	}

	private void validate() {
		boolean remote = (fFlags & REMOTE) > 0;
		StringBuilder sb = new StringBuilder();
		String hostBinary = fHostBinaryText.getText().trim();
		if ( hostBinary.isEmpty() ) {
			sb.append( ( remote ) ? 
				Messages.GdbDebugNewExecutableCommand_Host_binary_must_be_specified : 
				Messages.GdbDebugNewExecutableCommand_Binary_must_be_specified );
		}
		else {
			File file = new File( hostBinary );
			if ( !file.exists() ) {
				sb.append( ( remote ) ? 
					Messages.GdbDebugNewExecutableCommand_Host_binary_file_does_not_exist :
					Messages.GdbDebugNewExecutableCommand_Binary_file_does_not_exist );
			}
			else if ( file.isDirectory() ) {
				sb.append( ( remote ) ?
					Messages.GdbDebugNewExecutableCommand_Invalid_host_binary :
					Messages.GdbDebugNewExecutableCommand_Invalid_binary );
			}
		}
		String buildLog = fBuildLogText.getText();
		if (sb.length() == 0 && !buildLog.isEmpty()) {
			File file = new File( buildLog );
			if ( !file.exists() ) {
				sb.append( Messages.GdbDebugNewExecutableCommand_BuildLog_file_does_not_exist );
			}
			else if ( file.isDirectory() ) {
				sb.append( Messages.GdbDebugNewExecutableCommand_Invalid_buildLog );
			}
		}
		if (sb.length() == 0 && fTargetBinaryText != null) {
			if ( fTargetBinaryText.getText().trim().length() == 0 ) {
				if ( sb.length() != 0 ) {
					sb.append( "\n " ); //$NON-NLS-1$
				}
				sb.append( Messages.GdbDebugNewExecutableCommand_Binary_on_target_must_be_specified );
			}
		}
		setErrorMessage( ( sb.length() != 0 ) ? sb.toString() : null );
		getButton( IDialogConstants.OK_ID ).setEnabled( getErrorMessage() == null );
	}
}

Back to the top