Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e0fbf34091fc1b46362e866c1b702253796a97c1 (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
/*******************************************************************************
 * Copyright (c) 2010, 2015 Ericsson 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:
 *     Ericsson - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.dsf.gdb.internal.ui.tracepointactions;

import java.util.ArrayList;

import org.eclipse.cdt.dsf.gdb.internal.tracepointactions.ITracepointAction;
import org.eclipse.cdt.dsf.gdb.internal.tracepointactions.TracepointActionManager;
import org.eclipse.cdt.dsf.gdb.internal.tracepointactions.WhileSteppingAction;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
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.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

/**
 * @since 2.1
 */
public class TracepointGlobalActionsList extends Composite {

	private Button attachButton;
	private Button deleteButton;
	private Button editButton;
	private Button newButton;
	private Table table;
	private TracepointActionsList clientList;
	private boolean isSubAction;

	// When dealing with a "while-stepping" action, we deal with a "child" global
	// list, and must keep track of the parent global list, to properly update it.
	// This field will be null when the this class represents the parent class itself.
	private TracepointGlobalActionsList parentGlobalList;

	public TracepointGlobalActionsList(Composite parent, int style, boolean useAttachButton,
			TracepointGlobalActionsList parentList, boolean isSub) {
		super(parent, style);
		isSubAction = isSub;
		parentGlobalList = parentList;

		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 5;
		setLayout(gridLayout);

		table = new Table(this, SWT.BORDER | SWT.FULL_SELECTION | SWT.MULTI);
		table.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				updateButtons();
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				HandleEditButton();
			}
		});

		final GridData gridData = new GridData(GridData.FILL_BOTH);
		gridData.horizontalSpan = 5;
		table.setLayoutData(gridData);
		table.setLinesVisible(true);
		table.setHeaderVisible(true);

		final TableColumn nameTableColumn = new TableColumn(table, SWT.NONE);
		nameTableColumn.setWidth(120);
		nameTableColumn.setText(MessagesForTracepointActions.TracepointActions_Name);

		final TableColumn typeTableColumn = new TableColumn(table, SWT.NONE);
		typeTableColumn.setWidth(120);
		typeTableColumn.setText(MessagesForTracepointActions.TracepointActions_Type);

		final TableColumn summaryTableColumn = new TableColumn(table, SWT.NONE);
		summaryTableColumn.setWidth(120);
		summaryTableColumn.setText(MessagesForTracepointActions.TracepointActions_Summary);

		ArrayList<ITracepointAction> actions = TracepointActionManager.getInstance().getActions();

		for (ITracepointAction element : actions) {
			if (isSubAction && element instanceof WhileSteppingAction)
				continue;
			final TableItem tableItem = new TableItem(table, SWT.NONE);
			tableItem.setText(0, element.getName());
			tableItem.setText(1, element.getTypeName());
			tableItem.setText(2, element.getSummary());
			tableItem.setData(element);
		}

		if (useAttachButton) {
			attachButton = new Button(this, SWT.NONE);
			attachButton.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL));
			attachButton.setText(MessagesForTracepointActions.TracepointActions_Attach);
		}

		newButton = new Button(this, SWT.NONE);
		newButton.setLayoutData(new GridData());
		newButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				try {
					HandleNewButton();
				} catch (CoreException e1) {
				}
			}
		});
		newButton.setText(MessagesForTracepointActions.TracepointActions_New);
		newButton.setEnabled(true);

		editButton = new Button(this, SWT.NONE);
		editButton.setText(MessagesForTracepointActions.TracepointActions_Edit);
		editButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				HandleEditButton();
			}
		});
		if (!useAttachButton)
			editButton.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL + GridData.HORIZONTAL_ALIGN_END));

		deleteButton = new Button(this, SWT.NONE);
		deleteButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
		deleteButton.setText(MessagesForTracepointActions.TracepointActions_Delete);

		updateButtons();
	}

	public Button getAttachButton() {
		return attachButton;
	}

	public Button getDeleteButton() {
		return deleteButton;
	}

	public ITracepointAction[] getSelectedActions() {
		TableItem[] selectedItems = table.getSelection();
		ITracepointAction[] actionList = new ITracepointAction[selectedItems.length];
		int actionCount = 0;
		for (int i = 0; i < selectedItems.length; i++) {
			actionList[actionCount++] = (ITracepointAction) selectedItems[i].getData();
		}
		return actionList;
	}

	protected void HandleDeleteButton() {
		TableItem[] selectedItems = table.getSelection();
		for (int i = 0; i < selectedItems.length; i++) {
			ITracepointAction action = (ITracepointAction) selectedItems[i].getData();
			if (clientList != null) {
				clientList.removeAction(action);
			}
			if (parentGlobalList != null) {
				assert isSubAction;
				// Update the parent list also
				parentGlobalList.removeAction(action);
			}
			TracepointActionManager.getInstance().deleteAction(action);
		}
		// Remove all selected actions at once
		table.remove(table.getSelectionIndices());
		if (table.getItemCount() > 0) {
			table.select(table.getItemCount() - 1);
		}
		updateButtons();
	}

	void removeAction(ITracepointAction action) {
		TableItem[] currentItems = table.getItems();
		for (int i = 0; i < currentItems.length; i++) {
			if (((ITracepointAction) currentItems[i].getData()).equals(action)) {
				table.remove(i);
				if (clientList != null) {
					clientList.removeAction(action);
				}
				break;
			}
		}
		updateButtons();
	}

	protected void HandleEditButton() {

		TableItem[] selectedItems = table.getSelection();
		ITracepointAction action = (ITracepointAction) selectedItems[0].getData();

		TracepointActionDialog dialog = new TracepointActionDialog(this.getShell(), action, this, isSubAction);
		int result = dialog.open();
		if (result == Window.OK) {
			action.setName(dialog.getActionName());
			selectedItems[0].setText(0, action.getName());
			selectedItems[0].setText(1, action.getTypeName());
			selectedItems[0].setText(2, action.getSummary());
			if (clientList != null) {
				clientList.updateAction(action);
			}
			if (parentGlobalList != null) {
				assert isSubAction;
				// Update the parent list also
				parentGlobalList.updateAction(action);
			}
		}
	}

	protected void HandleNewButton() throws CoreException {

		TracepointActionDialog dialog = new TracepointActionDialog(this.getShell(), null, this, isSubAction);
		int result = dialog.open();
		if (result == Window.OK) {
			ITracepointAction action = (ITracepointAction) dialog.getTracepointAction();
			action.setName(dialog.getActionName());
			TracepointActionManager.getInstance().addAction(action);
			addAction(action);

			if (parentGlobalList != null) {
				assert isSubAction;
				// Update the parent list also
				parentGlobalList.addAction(action);
			}
		}
	}

	void addAction(ITracepointAction action) {
		final TableItem tableItem = new TableItem(table, SWT.NONE);
		tableItem.setText(0, action.getName());
		tableItem.setText(1, action.getTypeName());
		tableItem.setText(2, action.getSummary());
		tableItem.setData(action);
	}

	public void updateButtons() {
		TableItem[] selectedItems = table.getSelection();
		if (attachButton != null)
			attachButton.setEnabled(selectedItems.length > 0);
		deleteButton.setEnabled(selectedItems.length > 0);
		editButton.setEnabled(selectedItems.length == 1);
	}

	/**
	 * Register client list to be notified of changes to actions.
	 * @param actionsList
	 */
	void setClientList(TracepointActionsList actionsList) {
		clientList = actionsList;
	}

	/**
	 * Update the appearance of given action.
	 * @param action
	 */
	void updateAction(ITracepointAction action) {
		TableItem[] currentItems = table.getItems();
		for (int i = 0; i < currentItems.length; i++) {
			if (((ITracepointAction) currentItems[i].getData()).equals(action)) {
				TableItem tableItem = currentItems[i];
				tableItem.setText(0, action.getName());
				tableItem.setText(1, action.getTypeName());
				tableItem.setText(2, action.getSummary());
				if (clientList != null) {
					clientList.updateAction(action);
				}
				break;
			}
		}
		updateButtons();
	}
}

Back to the top