Skip to main content
summaryrefslogtreecommitdiffstats
blob: 5382fefa415e209048834296474c6a6acecd10d6 (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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/*******************************************************************************
 * Copyright (c) 2008 Nokia 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:
 * Nokia - Initial API and implementation
 *******************************************************************************/

package org.eclipse.cdt.debug.internal.ui.views.executables;

import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.eclipse.cdt.debug.core.executables.Executable;
import org.eclipse.cdt.debug.core.executables.ExecutablesManager;
import org.eclipse.cdt.debug.ui.CDebugUIPlugin;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Preferences;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.XMLMemento;
import org.eclipse.ui.dialogs.ListSelectionDialog;
import org.eclipse.ui.part.ViewPart;

/**
 * ExecutablesView displays a list of executable files either in the workspace
 * or created by projects in the workspace. The list of executables comes from
 * the ExecutablesManager. This view has two subviews: one that shows the list
 * of executables and another that shows the list of source files in the
 * selected executable.
 * 
 */
public class ExecutablesView extends ViewPart {

	/**
	 * Settings for the view including the sorted column for the sub views and
	 * the list of visible columns.
	 */

	public static final String P_ORDER_TYPE_EXE = "orderTypeEXE"; //$NON-NLS-1$
	public static final String P_ORDER_VALUE_EXE = "orderValueEXE"; //$NON-NLS-1$
	public static final String P_ORDER_TYPE_SF = "orderTypeSF"; //$NON-NLS-1$
	public static final String P_ORDER_VALUE_SF = "orderValueSF"; //$NON-NLS-1$
	public static final String P_VISIBLE_COLUMNS = "visibleColumns"; //$NON-NLS-1$

	/**
	 * Constants for the columns.
	 */

	public final static int NAME = 0x0;
	public final static int PROJECT = 0x1;
	public final static int LOCATION = 0x2;
	public final static int ORG_LOCATION = 0x3;
	public final static int SIZE = 0x4;
	public final static int MODIFIED = 0x5;
	public final static int TYPE = 0x6;

	/**
	 * Constants for the column sort order.
	 */

	public static int ASCENDING = 1;
	public static int DESCENDING = -1;

	/**
	 * Display constants and icons.
	 */

	public final static String ICONS_PATH = "icons/"; //$NON-NLS-1$
	public static final String EXECUTABLES_VIEW_CONTEXT = "org.eclipse.cdt.debug.ui.executables_View_context"; //$NON-NLS-1$;
	private static final String PATH_LCL = ICONS_PATH + "elcl16/"; //$NON-NLS-1$
	private static final String PATH_LCL_DISABLED = ICONS_PATH + "dlcl16/"; //$NON-NLS-1$
	public static final ImageDescriptor DESC_REFRESH = create(PATH_LCL, "refresh.gif"); //$NON-NLS-1$
	public static final ImageDescriptor DESC_REFRESH_DISABLED = create(PATH_LCL_DISABLED, "refresh.gif"); //$NON-NLS-1$
	public static final ImageDescriptor DESC_IMPORT = create(PATH_LCL, "import.gif"); //$NON-NLS-1$
	public static final ImageDescriptor DESC_IMPORT_DISABLED = create(PATH_LCL_DISABLED, "import.gif"); //$NON-NLS-1$
	public static final ImageDescriptor DESC_COLUMNS = create(PATH_LCL, "columns.gif"); //$NON-NLS-1$
	public static final ImageDescriptor DESC_COLUMNS_DISABLED = create(PATH_LCL_DISABLED, "columns.gif"); //$NON-NLS-1$
	public static final int COLUMN_WIDTH_PADDING = 24;

	private static ImageDescriptor create(String prefix, String name) {
		return ImageDescriptor.createFromURL(makeIconURL(prefix, name));
	}

	private static URL makeIconURL(String prefix, String name) {
		String path = "$nl$/" + prefix + name; //$NON-NLS-1$
		return FileLocator.find(CDebugUIPlugin.getDefault().getBundle(), new Path(path), null);
	}

	/**
	 * Complete list of column names for both sub views. These are not display
	 * names and should not be localized. Display names are set when the columns
	 * are created in the sub views.
	 */
	private String[] columnNames = { "Executable Name", "Executable Project", "Executable Location", "Executable Size", "Executable Date",
			"Executable Type", "Source File Name", "Source File Location", "Source File Original Location", "Source File Size", "Source File Date",
			"Source File Type" };

	/**
	 * Not all the columns are visible by default. Here are the ones that are.
	 */
	private String defaultVisibleColumns = "Executable Name,Executable Project,Executable Location,Source File Name,Source File Location";
	private TreeColumn[] allColumns = new TreeColumn[columnNames.length];

	/**
	 * Configures the list of columns show in the view.
	 */
	public class ConfigureColumnsAction extends Action {

		public static final String CONFIGURE_COLUMNS_DIALOG = "org.eclipse.cdt.debug.ui.configure_columns_dialog_context"; //$NON-NLS-1$;
		public static final String CONFIGURE_COLUMNS_ACTION = "org.eclipse.cdt.debug.ui.configure_columns_action_context"; //$NON-NLS-1$;

		class ColumnContentProvider implements IStructuredContentProvider {

			/*
			 * (non-Javadoc)
			 * 
			 * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
			 */
			public Object[] getElements(Object inputElement) {
				return columnNames;
			}

			/*
			 * (non-Javadoc)
			 * 
			 * @see org.eclipse.jface.viewers.IContentProvider#dispose()
			 */
			public void dispose() {
			}

			/*
			 * (non-Javadoc)
			 * 
			 * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer,
			 *      java.lang.Object, java.lang.Object)
			 */
			public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
			}

		}

		class ColumnLabelProvider extends LabelProvider {

			public String getText(Object element) {
				return (String) element;
			}

		}

		public ConfigureColumnsAction() {
			setText("Configure Columns");
			setId(CDebugUIPlugin.getUniqueIdentifier() + ".ConfigureColumnsAction"); //$NON-NLS-1$
			PlatformUI.getWorkbench().getHelpSystem().setHelp(this, CONFIGURE_COLUMNS_ACTION);
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see org.eclipse.jface.action.Action#run()
		 */
		public void run() {
			ListSelectionDialog dialog = new ListSelectionDialog(ExecutablesView.this.getExecutablesViewer().getTree().getShell(), this,
					new ColumnContentProvider(), new ColumnLabelProvider(), "Select the columns to show");
			PlatformUI.getWorkbench().getHelpSystem().setHelp(this, CONFIGURE_COLUMNS_DIALOG);
			String[] visibleColumns = getVisibleColumns();
			List<String> initialSelection = new ArrayList<String>(visibleColumns.length);
			for (int i = 0; i < visibleColumns.length; i++) {
				initialSelection.add(visibleColumns[i]);
			}
			dialog.setTitle("Configure COlumns");
			dialog.setInitialElementSelections(initialSelection);
			if (dialog.open() == Window.OK) {
				Object[] result = dialog.getResult();
				String[] ids = new String[result.length];
				System.arraycopy(result, 0, ids, 0, result.length);
				setVisibleColumns(ids);
			}

		}

	}

	/**
	 * Sub viewers and trees
	 */
	private SourceFilesViewer sourceFilesViewer;
	private ExecutablesViewer executablesViewer;

	/**
	 * Associated Actions
	 */
	Action refreshAction;
	Action importAction;
	private Action configureColumnsAction;

	private IMemento memento;

	/**
	 * Create contents of the Executables View
	 * 
	 * @param parent
	 */
	@Override
	public void createPartControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NONE);
		container.setLayout(new FillLayout());

		final SashForm sashForm = new SashForm(container, SWT.NONE);

		// Create the two sub viewers.
		executablesViewer = new ExecutablesViewer(this, sashForm, SWT.FULL_SELECTION + SWT.BORDER);
		ExecutablesManager.getExecutablesManager().addExecutablesChangeListener(executablesViewer);
		sourceFilesViewer = new SourceFilesViewer(this, sashForm, SWT.BORDER);

		sashForm.setWeights(new int[] { 1, 1 });

		// Keep a combined list of all the columns so
		// we can easily operate on them all.
		allColumns[0] = executablesViewer.nameColumn;
		allColumns[1] = executablesViewer.projectColumn;
		allColumns[2] = executablesViewer.locationColumn;
		allColumns[3] = executablesViewer.sizeColumn;
		allColumns[4] = executablesViewer.modifiedColumn;
		allColumns[5] = executablesViewer.typeColumn;
		allColumns[6] = sourceFilesViewer.nameColumn;
		allColumns[7] = sourceFilesViewer.locationColumn;
		allColumns[8] = sourceFilesViewer.originalLocationColumn;
		allColumns[9] = sourceFilesViewer.sizeColumn;
		allColumns[10] = sourceFilesViewer.modifiedColumn;
		allColumns[11] = sourceFilesViewer.typeColumn;

		createActions();

		// When the selection changes in the executables list
		// update the source files viewer
		executablesViewer.addSelectionChangedListener(new ISelectionChangedListener() {

			public void selectionChanged(SelectionChangedEvent event) {
				ISelection newSelection = event.getSelection();
				if (newSelection instanceof IStructuredSelection) {
					Object firstElement = ((IStructuredSelection) newSelection).getFirstElement();
					sourceFilesViewer.setInput(firstElement);
					if (firstElement instanceof Executable) {
						sourceFilesViewer.packColumns();
					}
				}
			}
		});

		// Initialize the list of visible columns
		if (memento.getString(P_VISIBLE_COLUMNS).length() > 0) {
			String[] visibleColumns = memento.getString(P_VISIBLE_COLUMNS).split(",");
			setVisibleColumns(visibleColumns);
		} else {
			setVisibleColumns(defaultVisibleColumns.split(","));
		}
		sourceFilesViewer.packColumns();

		PlatformUI.getWorkbench().getHelpSystem().setHelp(container, EXECUTABLES_VIEW_CONTEXT);
	}

	private void setVisibleColumns(String[] ids) {
		List<String> visibleNames = Arrays.asList(ids);
		for (int i = 0; i < columnNames.length; i++) {
			makeColumnVisible(visibleNames.contains(columnNames[i]), allColumns[i]);
		}

		StringBuffer visibleColumns = new StringBuffer();
		for (int i = 0; i < ids.length; i++) {
			if (i > 0)
				visibleColumns.append(",");
			visibleColumns.append(ids[i]);
		}
		memento.putString(P_VISIBLE_COLUMNS, visibleColumns.toString());
	}

	private void makeColumnVisible(boolean visible, TreeColumn column) {
		boolean isVisible = column.getWidth() > 0;
		if (isVisible != visible) {
			if (visible) {
				column.setResizable(true);
				column.pack();
				column.setWidth(column.getWidth() + COLUMN_WIDTH_PADDING);
			} else {
				column.setWidth(0);
				column.setResizable(false);
			}
		}
	}

	private String[] getVisibleColumns() {
		ArrayList<String> visibleNames = new ArrayList<String>();

		for (int i = 0; i < columnNames.length; i++) {
			if (allColumns[i].getWidth() > 0)
				visibleNames.add(columnNames[i]);
		}

		return visibleNames.toArray(new String[visibleNames.size()]);
	}

	/**
	 * Create the actions to refresh, import, and configure the columns
	 */
	private void createActions() {
		IActionBars bars = getViewSite().getActionBars();
		IToolBarManager toolBarManager = bars.getToolBarManager();

		refreshAction = createRefreshAction();
		toolBarManager.add(refreshAction);

		importAction = createImportAction();
		toolBarManager.add(importAction);

		configureColumnsAction = createConfigureColumnsAction();
		toolBarManager.add(configureColumnsAction);

	}

	private Action createConfigureColumnsAction() {
		ConfigureColumnsAction action = new ConfigureColumnsAction();
		action.setToolTipText("Columns");
		action.setImageDescriptor(ExecutablesView.DESC_COLUMNS);
		action.setDisabledImageDescriptor(ExecutablesView.DESC_COLUMNS_DISABLED);
		action.setEnabled(true);
		return action;
	}

	protected void importExecutables(final String[] fileNames) {
		if (fileNames.length > 0) {

			Job importJob = new Job("Import Executables") {

				@Override
				public IStatus run(IProgressMonitor monitor) {
					ExecutablesManager.getExecutablesManager().importExecutables(fileNames, monitor);
					return Status.OK_STATUS;
				}
			};
			importJob.schedule();
		}
	}

	private Action createImportAction() {
		Action action = new Action("Import") {
			public void run() {
				FileDialog dialog = new FileDialog(getViewSite().getShell(), SWT.NONE);
				dialog.setText("Select an executable file");
				String res = dialog.open();
				if (res != null) {
					if (Platform.getOS().equals(Platform.OS_MACOSX) && res.endsWith(".app")) {
						// On Mac OS X the file dialog will let you select the
						// package but not the executable inside.
						Path macPath = new Path(res);
						res = res + "/Contents/MacOS/" + macPath.lastSegment();
						res = res.substring(0, res.length() - 4);
					}
					importExecutables(new String[] { res });
				}
			}
		};
		action.setToolTipText("Import an executable file");
		action.setImageDescriptor(ExecutablesView.DESC_IMPORT);
		action.setDisabledImageDescriptor(ExecutablesView.DESC_IMPORT_DISABLED);
		action.setEnabled(true);
		return action;
	}

	private Action createRefreshAction() {
		Action action = new Action("Refresh") {
			public void run() {
				ExecutablesManager.getExecutablesManager().scheduleRefresh(null, 0);
			}
		};
		action.setToolTipText("Refresh the list of executables");
		action.setImageDescriptor(ExecutablesView.DESC_REFRESH);
		action.setDisabledImageDescriptor(ExecutablesView.DESC_REFRESH_DISABLED);
		action.setEnabled(true);
		return action;
	}

	@Override
	public void setFocus() {
		// Set the focus
	}

	@Override
	public void init(IViewSite site, IMemento memento) throws PartInitException {
		if (memento == null)
			this.memento = XMLMemento.createWriteRoot("EXECUTABLESVIEW"); //$NON-NLS-1$
		else
			this.memento = memento;
		super.init(site, memento);
		readSettings();
	}

	private Preferences getViewPreferences() {
		return CDebugUIPlugin.getDefault().getPluginPreferences();
	}

	private void initializeMemento() {
		memento.putInteger(P_ORDER_VALUE_EXE, DESCENDING);
		memento.putInteger(P_ORDER_TYPE_EXE, NAME);
		memento.putInteger(P_ORDER_VALUE_SF, DESCENDING);
		memento.putInteger(P_ORDER_TYPE_SF, NAME);
		memento.putString(P_VISIBLE_COLUMNS, defaultVisibleColumns);
	}

	private void readSettings() {
		Preferences p = getViewPreferences();
		if (p == null) {
			initializeMemento();
			return;
		}
		try {
			int order = p.getInt(P_ORDER_VALUE_EXE);
			memento.putInteger(P_ORDER_VALUE_EXE, order == 0 ? DESCENDING : order);
			memento.putInteger(P_ORDER_TYPE_EXE, p.getInt(P_ORDER_TYPE_EXE));
			order = p.getInt(P_ORDER_VALUE_SF);
			memento.putInteger(P_ORDER_VALUE_SF, order == 0 ? DESCENDING : order);
			memento.putInteger(P_ORDER_TYPE_SF, p.getInt(P_ORDER_TYPE_SF));
			memento.putString(P_VISIBLE_COLUMNS, p.getString(P_VISIBLE_COLUMNS));
		} catch (NumberFormatException e) {
			memento.putInteger(P_ORDER_TYPE_EXE, NAME);
			memento.putInteger(P_ORDER_VALUE_EXE, DESCENDING);
			memento.putInteger(P_ORDER_TYPE_SF, NAME);
			memento.putInteger(P_ORDER_VALUE_SF, DESCENDING);
		}
	}

	private void writeSettings() {
		Preferences preferences = getViewPreferences();
		int order = memento.getInteger(P_ORDER_VALUE_EXE).intValue();
		preferences.setValue(P_ORDER_VALUE_EXE, order == 0 ? DESCENDING : order);
		preferences.setValue(P_ORDER_TYPE_EXE, memento.getInteger(P_ORDER_TYPE_EXE).intValue());
		order = memento.getInteger(P_ORDER_VALUE_SF).intValue();
		preferences.setValue(P_ORDER_VALUE_SF, order == 0 ? DESCENDING : order);
		preferences.setValue(P_ORDER_TYPE_SF, memento.getInteger(P_ORDER_TYPE_SF).intValue());
		preferences.setValue(P_VISIBLE_COLUMNS, memento.getString(P_VISIBLE_COLUMNS));
	}

	@Override
	public void saveState(IMemento memento) {
		if (this.memento == null || memento == null)
			return;
		memento.putMemento(this.memento);
		writeSettings();
	}

	public SourceFilesViewer getSourceFilesViewer() {
		return sourceFilesViewer;
	}

	public ExecutablesViewer getExecutablesViewer() {
		return executablesViewer;
	}

	@Override
	public void dispose() {
		ExecutablesManager.getExecutablesManager().removeExecutablesChangeListener(executablesViewer);
		super.dispose();
	}

	public IMemento getMemento() {
		return memento;
	}

}

Back to the top