1
19 package com.qasystems.qstudio.java.integration.eclipse.actions;
20
21 import com.qasystems.debug.DebugWriter;
22 import com.qasystems.qstudio.java.annotator.AnnotatedSourceViewer;
23 import com.qasystems.qstudio.java.gui.ProjectMember;
24 import com.qasystems.qstudio.java.integration.eclipse.EclipseEnvironment;
25 import com.qasystems.qstudio.java.integration.eclipse.EclipseProjectMember;
26
27 import java.io.File;
28
29 import org.eclipse.jdt.core.IJavaElement;
30 import org.eclipse.jface.action.IAction;
31 import org.eclipse.jface.viewers.ISelectionProvider;
32 import org.eclipse.jface.viewers.StructuredSelection;
33 import org.eclipse.ui.IEditorActionDelegate;
34 import org.eclipse.ui.IEditorPart;
35 import org.eclipse.ui.IObjectActionDelegate;
36 import org.eclipse.ui.IWorkbenchPage;
37 import org.eclipse.ui.IWorkbenchPart;
38 import org.eclipse.ui.IWorkbenchPartSite;
39 import org.eclipse.ui.actions.ActionDelegate;
40
41 public class AnnotatedViewAction extends ActionDelegate
42 implements IObjectActionDelegate, IEditorActionDelegate {
43 private IEditorPart editor;
44 private IWorkbenchPart part;
45 private IWorkbenchPage page;
46
47
53 public void setActivePart(IAction iAction, IWorkbenchPart iWorkbenchPart) {
54 part = iWorkbenchPart;
55 page = part.getSite().getPage();
56 editor = null;
57 }
58
59
62 public void setActiveEditor(IAction action, IEditorPart targetEditor) {
63 editor = targetEditor;
64 part = null;
65 page = null;
66 }
67
68
71 public void run(final IAction action) {
72 IJavaElement element = null;
73
74 if (part != null) {
75 final IWorkbenchPartSite site = part.getSite();
76 final ISelectionProvider provider = site.getSelectionProvider();
77 final StructuredSelection selection =
78 (StructuredSelection) provider.getSelection();
79 final Object object = selection.getFirstElement();
80
81 if (object instanceof IJavaElement) {
82 element = (IJavaElement) object;
83 }
84 } else if (editor != null) {
85 element = EclipseEnvironment.getJavaElement(editor);
86 } else {
87 element = null;
88 }
89
90 openAnnotatedView(element);
91 }
92
93 private void openAnnotatedView(IJavaElement element) {
94 if (element != null) {
95 try {
96 final File outputPath =
97 EclipseEnvironment.getAbsoluteOutputLocation(
98 element.getJavaProject()
99 );
100 final ProjectMember member = new EclipseProjectMember(element);
101
102 new AnnotatedSourceViewer(
103 member.getFile(),
104 new File(
105 member.createOutputFileName(
106 (outputPath != null) ? outputPath.getAbsolutePath() : null, "chk"
107 )
108 )
109 );
110 } catch (Exception e) {
111 new DebugWriter().writeException(e, this);
112 }
113 }
114 }
115 }
116