Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2ab02427338b90ba9b7c39a91a5cf268baebc9cd (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
/*******************************************************************************
 * Copyright (c) 2008, 2010 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.util.List;

import org.eclipse.cdt.debug.core.executables.Executable;
import org.eclipse.cdt.debug.core.executables.ExecutablesManager;
import org.eclipse.cdt.debug.core.executables.IExecutablesChangeListener;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerComparator;
import org.eclipse.jface.viewers.ViewerDropAdapter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.FileTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.dnd.TransferData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.progress.UIJob;

/**
 * Displays the list of executables gathered by the ExecutablesManager
 */
public class ExecutablesViewer extends BaseViewer implements IExecutablesChangeListener {

	private static final String P_COLUMN_ORDER_KEY_EXE = "columnOrderKeyEXE"; //$NON-NLS-1$
	private static final String P_SORTED_COLUMN_INDEX_KEY_EXE = "sortedColumnIndexKeyEXE"; //$NON-NLS-1$
	private static final String P_COLUMN_SORT_DIRECTION_KEY_EXE = "columnSortDirectionKeyEXE"; //$NON-NLS-1$
	private static final String P_VISIBLE_COLUMNS_KEY_EXE = "visibleColumnsKeyEXE"; //$NON-NLS-1$

	
	/**
	 * Handles dropping executable files into the view
	 */
	public class ExecutablesDropAdapter extends ViewerDropAdapter {

		protected ExecutablesDropAdapter(Viewer viewer) {
			super(viewer);
		}

		@Override
		public boolean performDrop(Object data) {
			final String[] fileNames = (String[]) data;
			ExecutablesViewer.this.getExecutablesView().importExecutables(fileNames);
			return true;
		}

		@Override
		public boolean validateDrop(Object target, int operation, TransferData transferType) {
			return FileTransfer.getInstance().isSupportedType(transferType);
		}

	}

	public TreeColumn projectColumn;

	public ExecutablesViewer(ExecutablesView executablesView, Composite parent, int style) {
		super(executablesView, parent, style);

		// Setup D&D support
		int ops = DND.DROP_COPY | DND.DROP_MOVE | DND.DROP_DEFAULT;
		Transfer[] transfers = new Transfer[] { FileTransfer.getInstance() };
		ExecutablesDropAdapter adapter = new ExecutablesDropAdapter(this);
		adapter.setFeedbackEnabled(false);
		addDropSupport(ops | DND.DROP_DEFAULT, transfers, adapter);

		// Setup content provider
		ExecutablesContentProvider exeContentProvider = new ExecutablesContentProvider(this);
		setContentProvider(exeContentProvider);
		setLabelProvider(exeContentProvider);

		getTree().setHeaderVisible(true);
		getTree().setLinesVisible(true);
		executablesView.getSite().setSelectionProvider(this);
		getTree().setData(".uid", "ExecutablesViewerTree"); //$NON-NLS-1$ //$NON-NLS-2$

		createColumns();

		setInput(ExecutablesManager.getExecutablesManager());

		MenuManager popupMenuManager = new MenuManager("#PopupMenu"); //$NON-NLS-1$
		IMenuListener listener = new IMenuListener() {
			public void menuAboutToShow(IMenuManager manager) {
				manager.add(ExecutablesViewer.this.getExecutablesView().refreshAction);
				manager.add(ExecutablesViewer.this.getExecutablesView().importAction);
				manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
			}
		};
		popupMenuManager.addMenuListener(listener);
		popupMenuManager.setRemoveAllWhenShown(true);
		getExecutablesView().getSite().registerContextMenu(popupMenuManager, this.getExecutablesView().getSite().getSelectionProvider());
		Menu menu = popupMenuManager.createContextMenu(getTree());
		getTree().setMenu(menu);
	}

	private void createColumns() {
		nameColumn = new TreeColumn(getTree(), SWT.NONE);
		nameColumn.setWidth(100);
		nameColumn.setText(Messages.ExecutablesViewer_ExecutableName);
		nameColumn.setMoveable(true);
		nameColumn.addSelectionListener(new ColumnSelectionAdapter(ExecutablesView.NAME));

		projectColumn = new TreeColumn(getTree(), SWT.NONE);
		projectColumn.setWidth(100);
		projectColumn.setMoveable(true);
		projectColumn.setText(Messages.ExecutablesViewer_Project);
		projectColumn.addSelectionListener(new ColumnSelectionAdapter(ExecutablesView.PROJECT));

		locationColumn = new TreeColumn(getTree(), SWT.NONE);
		locationColumn.setWidth(100);
		locationColumn.setText(Messages.ExecutablesViewer_Location);
		locationColumn.setMoveable(true);
		locationColumn.addSelectionListener(new ColumnSelectionAdapter(ExecutablesView.LOCATION));

		sizeColumn = new TreeColumn(getTree(), SWT.NONE);
		sizeColumn.setWidth(100);
		sizeColumn.setText(Messages.ExecutablesViewer_Size);
		sizeColumn.setMoveable(true);
		sizeColumn.addSelectionListener(new ColumnSelectionAdapter(ExecutablesView.SIZE));

		modifiedColumn = new TreeColumn(getTree(), SWT.NONE);
		modifiedColumn.setWidth(100);
		modifiedColumn.setText(Messages.ExecutablesViewer_Modified);
		modifiedColumn.setMoveable(true);
		modifiedColumn.addSelectionListener(new ColumnSelectionAdapter(ExecutablesView.MODIFIED));

		typeColumn = new TreeColumn(getTree(), SWT.NONE);
		typeColumn.setWidth(100);
		typeColumn.setText(Messages.ExecutablesViewer_Type);
		typeColumn.setMoveable(true);
		typeColumn.addSelectionListener(new ColumnSelectionAdapter(ExecutablesView.TYPE));

	}

	@Override
	protected ViewerComparator getViewerComparator(int sortType) {
		if (sortType == ExecutablesView.PROJECT) {
			return new ExecutablesViewerComparator(sortType, column_sort_order[ExecutablesView.PROJECT]) {
				@SuppressWarnings("unchecked") //$NON-NLS-1$
				public int compare(Viewer viewer, Object e1, Object e2) {
					Executable entry1 = (Executable) e1;
					Executable entry2 = (Executable) e2;
					return getComparator().compare(entry1.getProject().getName(), entry2.getProject().getName())
							* column_sort_order[ExecutablesView.PROJECT];
				}
			};
		}
		return new ExecutablesViewerComparator(sortType, column_sort_order[sortType]);
	}

	@Override
	protected String getColumnOrderKey() {
		return P_COLUMN_ORDER_KEY_EXE;
	}

	@Override
	protected String getSortedColumnIndexKey() {
		return P_SORTED_COLUMN_INDEX_KEY_EXE;
	}

	@Override
	protected String getSortedColumnDirectionKey() {
		return P_COLUMN_SORT_DIRECTION_KEY_EXE;
	}

	@Override
	protected String getVisibleColumnsKey() {
		return P_VISIBLE_COLUMNS_KEY_EXE;
	}

	@Override
	protected String getDefaultVisibleColumnsValue() {
		// default visible columns
		return "1,1,1,0,0,0"; //$NON-NLS-1$
	}

	public void executablesChanged(final List<Executable> executables) {
		// some executables have been updated.  if one of them is currently
		// selected, we need to update the source file list
		UIJob refreshJob = new UIJob(Messages.ExecutablesViewer_RefreshExecutablesView) {

			@Override
			public IStatus runInUIThread(IProgressMonitor monitor) {
				// if the user has selected an executable, they expect its
				// list of source files to be refreshed automatically
				if (getSelection() != null &&
					getSelection() instanceof IStructuredSelection) {
					IStructuredSelection selection = (IStructuredSelection)getSelection();
					
					Object firstElement = selection.getFirstElement();
					if (firstElement instanceof Executable) {
						Executable executable = (Executable) firstElement;
						if (executables.contains(executable)) {
							executable.setRefreshSourceFiles(true);
							setSelection(selection);
						}
					}
				}
				return Status.OK_STATUS;
			}
		};
		refreshJob.schedule();
	}

	public void executablesListChanged() {
		// Executables list has changed so refresh the view.
		UIJob refreshJob = new UIJob(Messages.ExecutablesViewer_RefreshExecutablesView) {

			@Override
			public IStatus runInUIThread(IProgressMonitor monitor) {
				refresh(null);
				packColumns();
				return Status.OK_STATUS;
			}
		};
		refreshJob.schedule();
	}
}

Back to the top