|
1 | 1 | package de.vogella.plugin.markers.handler; |
2 | 2 |
|
3 | | -import org.eclipse.core.commands.AbstractHandler; |
4 | | -import org.eclipse.core.commands.ExecutionEvent; |
5 | | -import org.eclipse.core.commands.ExecutionException; |
| 3 | +import javax.inject.Named; |
| 4 | + |
6 | 5 | import org.eclipse.core.resources.IMarker; |
7 | 6 | import org.eclipse.core.resources.IResource; |
8 | | -import org.eclipse.jdt.core.IJavaProject; |
| 7 | +import org.eclipse.e4.core.di.annotations.Execute; |
| 8 | +import org.eclipse.e4.core.services.adapter.Adapter; |
| 9 | +import org.eclipse.e4.ui.services.IServiceConstants; |
9 | 10 | import org.eclipse.jface.viewers.IStructuredSelection; |
10 | | -import org.eclipse.ui.handlers.HandlerUtil; |
11 | 11 |
|
12 | | -public class AddMarkerHandler extends AbstractHandler { |
13 | 12 |
|
14 | | - @Override |
15 | | - public Object execute(ExecutionEvent event) throws ExecutionException { |
16 | | - IStructuredSelection selection = (IStructuredSelection) HandlerUtil |
17 | | - .getActiveSite(event).getSelectionProvider().getSelection(); |
18 | | - if (selection == null) { |
19 | | - return null; |
| 13 | +public class AddMarkerHandler { |
| 14 | + |
| 15 | + @Execute |
| 16 | + public void execute(@Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection, Adapter adapter) { |
| 17 | + |
| 18 | + if (selection == null || selection.isEmpty()) { |
| 19 | + return; |
20 | 20 | } |
| 21 | + |
21 | 22 | Object firstElement = selection.getFirstElement(); |
22 | | - if (firstElement instanceof IJavaProject) { |
23 | | - IJavaProject type = (IJavaProject) firstElement; |
24 | | - writeMarkers(type); |
| 23 | + IResource resource = adapter.adapt(firstElement, IResource.class); |
25 | 24 |
|
| 25 | + if (resource != null) { |
| 26 | + writeMarkers(resource); |
26 | 27 | } |
27 | | - return null; |
| 28 | + |
28 | 29 | } |
29 | 30 |
|
30 | | - private void writeMarkers(IJavaProject type) { |
| 31 | + private void writeMarkers(IResource resource) { |
31 | 32 | try { |
32 | | - IResource resource = type.getUnderlyingResource(); |
33 | 33 | IMarker marker = resource.createMarker(IMarker.TASK); |
34 | | - marker.setAttribute(IMarker.MESSAGE, "This a a task"); |
| 34 | + marker.setAttribute(IMarker.MESSAGE, "This is a task"); |
35 | 35 | marker.setAttribute(IMarker.PRIORITY, IMarker.PRIORITY_HIGH); |
36 | 36 | } catch (Exception e) { |
37 | 37 | e.printStackTrace(); |
38 | 38 | } |
39 | 39 | } |
| 40 | + |
40 | 41 | } |
0 commit comments