1   /**
2    * QJ-Pro
3    * Copyright (c) 2004, http://qjpro.sourceforge.net
4    *
5    * This program is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License as published by the
7    * Free Software Foundation; either version 2 of the License, or
8    * (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful, but WITHOUT
11   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13   * details.
14   *
15   * You should have received a copy of the GNU General Public License along with
16   * this program; if not, write to the Free Software Foundation, Inc.,
17   * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18   */
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    /**
48     * DOCUMENT ME!
49     *
50     * @param iAction DOCUMENT ME!
51     * @param iWorkbenchPart DOCUMENT ME!
52     */
53    public void setActivePart(IAction iAction, IWorkbenchPart iWorkbenchPart) {
54      part = iWorkbenchPart;
55      page = part.getSite().getPage();
56      editor = null;
57    }
58  
59    /**
60     * @see IEditorActionDelegate#setActiveEditor(IAction, IEditorPart)
61     */
62    public void setActiveEditor(IAction action, IEditorPart targetEditor) {
63      editor = targetEditor;
64      part = null;
65      page = null;
66    }
67  
68    /**
69     * @see org.eclipse.ui.IActionDelegate#run(IAction)
70     */
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