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.gui.ComplianceView;
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 org.eclipse.jdt.core.IJavaElement;
28  import org.eclipse.jface.action.IAction;
29  import org.eclipse.jface.viewers.ISelectionProvider;
30  import org.eclipse.jface.viewers.StructuredSelection;
31  import org.eclipse.ui.IEditorActionDelegate;
32  import org.eclipse.ui.IEditorPart;
33  import org.eclipse.ui.IObjectActionDelegate;
34  import org.eclipse.ui.IWorkbenchPage;
35  import org.eclipse.ui.IWorkbenchPart;
36  import org.eclipse.ui.IWorkbenchPartSite;
37  import org.eclipse.ui.actions.ActionDelegate;
38  
39  public class ComplianceViewAction extends ActionDelegate
40    implements IObjectActionDelegate, IEditorActionDelegate {
41    private IEditorPart editor;
42    private IWorkbenchPart part;
43    private IWorkbenchPage page;
44  
45    /**
46     * DOCUMENT ME!
47     *
48     * @param iAction DOCUMENT ME!
49     * @param iWorkbenchPart DOCUMENT ME!
50     */
51    public void setActivePart(IAction iAction, IWorkbenchPart iWorkbenchPart) {
52      part = iWorkbenchPart;
53      page = part.getSite().getPage();
54      editor = null;
55    }
56  
57    /**
58     * @see IEditorActionDelegate#setActiveEditor(IAction, IEditorPart)
59     */
60    public void setActiveEditor(IAction action, IEditorPart targetEditor) {
61      editor = targetEditor;
62      part = null;
63      page = null;
64    }
65  
66    /**
67     * @see org.eclipse.ui.IActionDelegate#run(IAction)
68     */
69    public void run(final IAction action) {
70      try {
71        IJavaElement element = null;
72  
73        if (part != null) {
74          final IWorkbenchPartSite site = part.getSite();
75          final ISelectionProvider provider = site.getSelectionProvider();
76          final StructuredSelection selection =
77            (StructuredSelection) provider.getSelection();
78          final Object object = selection.getFirstElement();
79  
80          if (object instanceof IJavaElement) {
81            element = (IJavaElement) object;
82          }
83        } else if (editor != null) {
84          element = EclipseEnvironment.getJavaElement(editor);
85        } else {
86          element = null;
87        }
88  
89        openComplianceView(element);
90      } catch (Exception e) {
91        e.printStackTrace();
92      }
93    }
94  
95    private void openComplianceView(IJavaElement element) {
96      if (element != null) {
97        try {
98          final ProjectMember member = new EclipseProjectMember(element);
99  
100         new ComplianceView(member).openComplianceView();
101       } catch (Exception e) {
102         new DebugWriter().writeException(e, this);
103       }
104     }
105   }
106 }
107