Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4244f9c6e34c9b070e7b30200386250297ce31e2 (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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*******************************************************************************
 * Copyright (c) 2008, 2012 QNX Software Systems and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 * QNX Software Systems - Initial API and implementation
 * QNX Software Systems - Catchpoints support https://bugs.eclipse.org/bugs/show_bug.cgi?id=226689
 *******************************************************************************/
package org.eclipse.cdt.debug.internal.ui.dialogs;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import org.eclipse.cdt.debug.core.CDIDebugModel;
import org.eclipse.cdt.debug.core.model.ICBreakpointType;
import org.eclipse.cdt.debug.core.model.ICEventBreakpoint;
import org.eclipse.cdt.debug.internal.ui.breakpoints.CBreakpointPreferenceStore;
import org.eclipse.cdt.debug.ui.CDebugUIPlugin;
import org.eclipse.cdt.debug.ui.breakpoints.CBreakpointUIContributionFactory;
import org.eclipse.cdt.debug.ui.breakpoints.ICBreakpointsUIContribution;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.preference.FieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;

/**
 * The "Add Catchpoint" dialog of the "Add catchpoint" action.
 */
public class AddEventBreakpointDialog extends Dialog implements ModifyListener, SelectionListener {

	private Combo fEventTypeInput;
	private String fEventType;
	private String fEventArgument;
	private Composite fEventArgumentControl;
	private HashMap<String, String> fIdLabelMap = new LinkedHashMap<>();
	private FieldEditorPreferencePage page;
	private CBreakpointUIContributionFactory factory;
	private String debugModelId;

	static class ContributedFields extends FieldEditorPreferencePage {
		private String modelId;
		private String eventType;

		ContributedFields(String modelId, String eventType) {
			this.modelId = modelId;
			this.eventType = eventType;
			noDefaultAndApplyButton();
			setPreferenceStore(new CBreakpointPreferenceStore() {
				@Override
				public String getDefaultString(String name) {
					return ""; //$NON-NLS-1$
				}
			});
		}

		@Override
		protected void createFieldEditors() {
			Composite parent = getFieldEditorParent();
			try {
				Map<String, Object> map = new HashMap<>();
				map.put(ICEventBreakpoint.EVENT_TYPE_ID, eventType);
				ICBreakpointsUIContribution cons[] = CBreakpointUIContributionFactory.getInstance()
						.getBreakpointUIContributions(modelId, ICEventBreakpoint.C_EVENT_BREAKPOINT_MARKER, map);
				for (ICBreakpointsUIContribution con : cons) {

					if (con.getId().equals(ICEventBreakpoint.EVENT_TYPE_ID))
						continue;
					if (con.getId().equals(ICBreakpointType.TYPE))
						continue;
					FieldEditor fieldEditor = con.getFieldEditor(con.getId(), con.getLabel(), parent);
					getPreferenceStore().setValue(con.getId(), ""); //$NON-NLS-1$
					if (fieldEditor != null)
						addField(fieldEditor);
				}
			} catch (Exception ce) {
				CDebugUIPlugin.log(ce);
			}
		}

	}

	/**
	 * Constructor
	 *
	 * @param parentShell
	 */
	public AddEventBreakpointDialog(Shell parentShell) {
		super(parentShell);
		setShellStyle(getShellStyle() | SWT.RESIZE);
		factory = CBreakpointUIContributionFactory.getInstance();
		debugModelId = getDebugModelId();
		// Load events
		loadEventTypes();
	}

	protected String getDebugModelId() {
		return CDIDebugModel.getPluginIdentifier();
	}

	private void loadEventTypes() {
		ICBreakpointsUIContribution[] cons = factory.getBreakpointUIContributions(debugModelId,
				ICEventBreakpoint.C_EVENT_BREAKPOINT_MARKER, null);
		for (int i = 0; i < cons.length; i++) {
			ICBreakpointsUIContribution con = cons[i];
			if (con.getId().equals(ICEventBreakpoint.EVENT_TYPE_ID)) {
				String[] possibleValues = con.getPossibleValues();
				for (String value : possibleValues) {
					fIdLabelMap.put(value, con.getLabelForValue(value));
				}
			}
		}

	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		// The button bar will work better if we make the parent composite
		// a single column grid layout. For the widgets we add, we want a
		// a two-column grid, so we just create a sub composite for that.
		GridLayout gridLayout = new GridLayout();
		parent.setLayout(gridLayout);

		Composite composite = new Composite(parent, SWT.None);
		GridData layoutData = new GridData(GridData.FILL_BOTH);

		layoutData.heightHint = 100;
		composite.setLayoutData(layoutData);
		composite.setLayout(new GridLayout(1, false));

		// Create the controls
		createEventTypeControl(composite);
		createEventArgumentControl(composite);

		fEventTypeInput.setFocus();
		if (fEventTypeInput.getItemCount() > 0)
			fEventTypeInput.select(0);
		updateUI();
		return parent;
	}

	protected void createEventTypeControl(Composite parent) {
		fEventTypeInput = new Combo(parent, SWT.BORDER | SWT.READ_ONLY);
		GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
		fEventTypeInput.setLayoutData(gridData);
		for (String id : fIdLabelMap.keySet()) {
			addEventType(id, fIdLabelMap.get(id));
		}
		fEventTypeInput.addSelectionListener(this);
		fEventTypeInput.select(0);

	}

	protected void addEventType(String id, String label) {
		int index = fEventTypeInput.getItemCount();
		fEventTypeInput.add(label, index);
		fEventTypeInput.setData(getIdDataKey(index), id);
	}

	private String getIdDataKey(int index) {
		return "index." + index; //$NON-NLS-1$
	}

	protected void createEventArgumentControl(Composite parent) {
		fEventArgumentControl = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout();
		//layout.marginHeight = 0;
		//layout.marginWidth = 0;
		fEventArgumentControl.setLayout(layout);
		GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
		fEventArgumentControl.setLayoutData(gridData);
		updateEventType();
		if (fEventType != null) {
			createArgumentSpecificControl();
		}
	}

	private void createArgumentSpecificControl() {
		page = new ContributedFields(getDebugModelId(), fEventType);
		page.createControl(fEventArgumentControl);
		GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
		page.getControl().setLayoutData(gridData);

	}

	private void updateArgumentsControl() {
		updateEventType();
		if (fEventTypeInput != null) {
			if (page != null) {
				Control control = page.getControl();
				control.dispose();
				page.dispose();
				page = null;
			}
			createArgumentSpecificControl();
			fEventArgumentControl.layout(true);
			// resize dialog to fit new fields
			getShell().pack();
		}
	}

	public boolean isActive() {
		return fIdLabelMap.size() > 0;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
	 */
	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText(DialogMessages.getString("AddEventBreakpointDialog.2")); //$NON-NLS-1$
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.eclipse.jface.dialogs.Dialog#okPressed()
	 */
	@Override
	protected void okPressed() {
		if (fEventTypeInput != null) {
			updateEventType();
		}
		if (page != null) {
			page.performOk();
			IPreferenceStore preferenceStore = page.getPreferenceStore();
			if (preferenceStore != null) {
				fEventArgument = preferenceStore.getString(ICEventBreakpoint.EVENT_ARG);
			} else
				fEventArgument = null;
		}

		super.okPressed();
	}

	private void updateEventType() {
		int index = fEventTypeInput.indexOf(fEventTypeInput.getText());
		if (index == -1) {
			fEventType = null;
			return;
		}
		Object data = fEventTypeInput.getData(getIdDataKey(index));
		fEventType = (String) data;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
	 */
	@Override
	public void modifyText(ModifyEvent e) {
		if (e.getSource() == fEventTypeInput) {
			updateArgumentsControl();
		}
		updateUI();
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.eclipse.jface.dialogs.TrayDialog#createButtonBar(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	protected Control createButtonBar(Composite parent) {
		return super.createButtonBar(parent);
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
	 */
	@Override
	public void widgetDefaultSelected(SelectionEvent e) {
		// ignore
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
	 */
	@Override
	public void widgetSelected(SelectionEvent e) {
		if (e.getSource() == fEventTypeInput) {
			updateArgumentsControl();
		}
		updateUI();

	}

	private void updateUI() {
		Button b = getButton(IDialogConstants.OK_ID);
		if (b == null) {
			return;
		}
		b.setEnabled(okayEnabled());

	}

	private boolean okayEnabled() {
		// validate fields here
		String text = fEventTypeInput.getText();
		int index = fEventTypeInput.indexOf(text);
		if (index == -1) {
			return false;
		}
		return true;
	}

	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		// override so we can change the initial okay enabled state
		createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true).setEnabled(okayEnabled());
		createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
	}

	public String getEventTypeId() {
		return fEventType;
	}

	public String getEventArgument() {
		return fEventArgument;
	}

}

Back to the top