Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3312fa4ae650caba486e2a39836ff84def3f0cbf (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
package org.eclipse.ui.externaltools.internal.view;

/**********************************************************************
Copyright (c) 2002 IBM Corp. and others. All rights reserved.
This file is made available under the terms of the Common Public License v1.0
which accompanies this distribution, and is available at
http://www.eclipse.org/legal/cpl-v10.html
 
Contributors:
**********************************************************************/

import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.IOpenListener;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.OpenEvent;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.actions.ActionContext;
import org.eclipse.ui.externaltools.internal.model.ExternalToolsPlugin;
import org.eclipse.ui.externaltools.internal.model.IHelpContextIds;
import org.eclipse.ui.externaltools.internal.registry.ExternalToolType;
import org.eclipse.ui.externaltools.model.ExternalTool;
import org.eclipse.ui.help.WorkbenchHelp;
import org.eclipse.ui.part.ViewPart;

/**
 * The view to display all the external tools available to
 * the user. From this view, a user can create a new external
 * tool, delete an existing external tool, run an external
 * tool, or edit the properties of an existing external tool.
 */
public class ExternalToolView extends ViewPart {
	private TreeViewer viewer;
	private ExternalToolActionGroup actionGroup;
	
	/**
	 * Creates a new instance of the external tools view
	 */
	public ExternalToolView() {
		super();
	}

	/* (non-Javadoc)
	 * Method declared on IWorkbenchPart.
	 */
	public void createPartControl(Composite parent) {
		viewer = createViewer(parent);
		initContextMenu();
		makeActions();
		viewer.setInput(getInitialInput());

		// Fill the action bars and update the global action handlers'
		// enabled state to match the current selection.
		actionGroup.fillActionBars(getViewSite().getActionBars());
		updateActionBars((IStructuredSelection) viewer.getSelection());

		getSite().setSelectionProvider(viewer);
		WorkbenchHelp.setHelp(viewer.getControl(), getHelpContextId());
	}

	/**
	 * Creates the viewer control for the view.
	 * 
	 * @param parent the parent composite
	 * @return the tree viewer for the view
	 */
	protected TreeViewer createViewer(Composite parent) {
		TreeViewer treeViewer = new TreeViewer(parent, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL);
		treeViewer.setUseHashlookup(true);
		initContentProvider(treeViewer);
		initLabelProvider(treeViewer);
		initFilters(treeViewer);
		initListeners(treeViewer);

		return treeViewer;
	}

	/* (non-Javadoc)
	 * Method declared on IWorkbenchPart.
	 */
	public void dispose() {
		if (actionGroup != null)
			actionGroup.dispose();
		
		super.dispose();
	}
	
	/**
	 * Called when the context menu is about to open.
	 * Delegates to the action group using the 
	 * viewer's selection as the action context.
	 */
	protected void fillContextMenu(IMenuManager menu) {
		IStructuredSelection selection =
			(IStructuredSelection) viewer.getSelection();
		actionGroup.setContext(new ActionContext(selection));
		actionGroup.fillContextMenu(menu);
	}

	/**
	 * Returns the action group.
	 * 
	 * @return the action group
	 */
	protected final ExternalToolActionGroup getActionGroup() {
		return actionGroup;
	}

	/**
	 * Returns the help context id to use for this view.
	 */
	protected String getHelpContextId() {
		return IHelpContextIds.EXTERNAL_TOOLS_VIEW;
	}

	/** 
	 * Returns the initial input for the viewer.
	 */
	protected Object getInitialInput() {
		return ExternalToolsPlugin.getDefault().getTypeRegistry();
	}

	/**
	 * Returns the message to show in the status line.
	 *
	 * @param selection the current selection
	 * @return the status line message
	 */
	protected String getStatusLineMessage(IStructuredSelection selection) {
		if (selection.size() == 1) {
			Object element = selection.getFirstElement();
			if (element instanceof ExternalToolType)
				return ((ExternalToolType)element).getDescription();
			if (element instanceof ExternalTool)
				return ((ExternalTool)element).getDescription();
		}
		return ""; //$NON-NLS-1$
	}

	/**
	 * Returns the tree viewer which shows the external tools.
	 */
	public final TreeViewer getTreeViewer() {
		return viewer;
	}

	/**
	 * Handles a double-click event from the viewer.
	 * Expands or collapses a type when double-clicked.
	 * 
	 * @param event the double-click event
	 */
	protected void handleDoubleClick(DoubleClickEvent event) {
		IStructuredSelection selection = (IStructuredSelection) event.getSelection();
		Object element = selection.getFirstElement();
		if (viewer.isExpandable(element))
			viewer.setExpandedState(element, !viewer.getExpandedState(element));
	}

	/**
	 * Handles a key press event from the viewer.
	 * Delegates to the action group.
	 * 
	 * @param event the key event
	 */
	protected void handleKeyPressed(KeyEvent event) {
		actionGroup.handleKeyPressed(event);
	}

	/**
	 * Handles a key release in the viewer. Does 
	 * nothing by default.
	 * 
	 * @param event the key event
	 */
	protected void handleKeyReleased(KeyEvent event) {
	}

	/**
	 * Handles an open event from the viewer.
	 * Causes the selected external tool to be run.
	 * 
	 * @param event the open event
	 */
	protected void handleOpen(OpenEvent event) {
		IStructuredSelection selection = (IStructuredSelection) event.getSelection();
		actionGroup.runDefaultAction(selection);
	}

	/**
	 * Handles a selection changed event from the viewer.
	 * Updates the status line and the action bars.
	 * 
	 * @param event the selection event
	 */
	protected void handleSelectionChanged(SelectionChangedEvent event) {
		IStructuredSelection sel = (IStructuredSelection) event.getSelection();
		updateStatusLine(sel);
		updateActionBars(sel);
	}

	/**
	 * Initializes and registers the context menu.
	 */
	protected void initContextMenu() {
		MenuManager menuMgr = new MenuManager(); //$NON-NLS-1$
		menuMgr.setRemoveAllWhenShown(true);
		menuMgr.addMenuListener(new IMenuListener() {
			public void menuAboutToShow(IMenuManager manager) {
				fillContextMenu(manager);
			}
		});
		Menu menu = menuMgr.createContextMenu(viewer.getTree());
		viewer.getTree().setMenu(menu);
		getSite().registerContextMenu(menuMgr, viewer);
	}

	/**
	 * Sets the content provider for the viewer.
	 * 
	 * @param treeViewer the tree viewer
	 */
	protected void initContentProvider(TreeViewer treeViewer) {
		Shell shell = getSite().getShell();
		treeViewer.setContentProvider(new ExternalToolContentProvider(shell));
	}

	/**
	 * Sets the label provider for the viewer.
	 * 
	 * @param treeViewer the tree viewer
	 */
	protected void initLabelProvider(TreeViewer treeViewer) {
		treeViewer.setLabelProvider(new ExternalToolLabelProvider());
	}

	/**
	 * Adds the filters to the viewer.
	 * 
	 * @param treeViewer the tree viewer
	 */
	protected void initFilters(TreeViewer treeViewer) {
	}

	/**
	 * Adds the listeners to the viewer.
	 * 
	 * @param treeViewer the tree viewer
	 */
	protected void initListeners(TreeViewer treeViewer) {
		treeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
			public void selectionChanged(SelectionChangedEvent event) {
				handleSelectionChanged(event);
			}
		});
		
		treeViewer.addDoubleClickListener(new IDoubleClickListener() {
			public void doubleClick(DoubleClickEvent event) {
				handleDoubleClick(event);
			}
		});
		
		treeViewer.addOpenListener(new IOpenListener() {
			public void open(OpenEvent event) {
				handleOpen(event);
			}
		});
		
		treeViewer.getControl().addKeyListener(new KeyListener() {
			public void keyPressed(KeyEvent event) {
				handleKeyPressed(event);
			}
			public void keyReleased(KeyEvent event) {
				handleKeyReleased(event);
			}
		});
	}

	/**
	 * Creates the action group, which encapsulates all actions
	 * for the view.
	 */
	protected void makeActions() {
		setActionGroup(new ExternalToolActionGroup(this));
	}

	/**
	 * Sets the action group.
	 * 
	 * @param actionGroup the action group
	 */
	protected void setActionGroup(ExternalToolActionGroup actionGroup) {
		this.actionGroup = actionGroup;
	}

	/* (non-Javadoc)
	 * Method declared on IWorkbenchPart.
	 */
	public void setFocus() {
		viewer.getTree().setFocus();
	}

	/**
	 * Updates the action bar actions.
	 * 
	 * @param selection the current selection
	 */
	protected void updateActionBars(IStructuredSelection selection) {
		actionGroup.setContext(new ActionContext(selection));
		actionGroup.updateActionBars();
	}

	/**
	 * Updates the message shown in the status line.
	 *
	 * @param selection the current selection
	 */
	protected void updateStatusLine(IStructuredSelection selection) {
		String msg = getStatusLineMessage(selection);
		getViewSite().getActionBars().getStatusLineManager().setMessage(msg);
	}
}

Back to the top