Skip to content

Commit c8068d2

Browse files
committed
Updated AddMarkerHandler to an Eclipse 4 Handler
Change-Id: I481fe26d25f916784971197aa8e11b7afee923b2 Signed-off-by: Simon Scholz <simon.scholz@vogella.com>
1 parent e35cd8c commit c8068d2

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed
Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,41 @@
11
package de.vogella.plugin.markers.handler;
22

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+
65
import org.eclipse.core.resources.IMarker;
76
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;
910
import org.eclipse.jface.viewers.IStructuredSelection;
10-
import org.eclipse.ui.handlers.HandlerUtil;
1111

12-
public class AddMarkerHandler extends AbstractHandler {
1312

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;
2020
}
21+
2122
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);
2524

25+
if (resource != null) {
26+
writeMarkers(resource);
2627
}
27-
return null;
28+
2829
}
2930

30-
private void writeMarkers(IJavaProject type) {
31+
private void writeMarkers(IResource resource) {
3132
try {
32-
IResource resource = type.getUnderlyingResource();
3333
IMarker marker = resource.createMarker(IMarker.TASK);
34-
marker.setAttribute(IMarker.MESSAGE, "This a a task");
34+
marker.setAttribute(IMarker.MESSAGE, "This is a task");
3535
marker.setAttribute(IMarker.PRIORITY, IMarker.PRIORITY_HIGH);
3636
} catch (Exception e) {
3737
e.printStackTrace();
3838
}
3939
}
40+
4041
}

0 commit comments

Comments
 (0)