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.Project;
23  import com.qasystems.qstudio.java.integration.eclipse.EclipseAnalyzer;
24  import com.qasystems.qstudio.java.integration.eclipse.EclipseEnvironment;
25  
26  import java.util.Vector;
27  
28  import org.eclipse.core.resources.IFile;
29  import org.eclipse.core.resources.IResource;
30  import org.eclipse.core.runtime.CoreException;
31  import org.eclipse.jdt.core.ICompilationUnit;
32  import org.eclipse.jdt.core.IJavaProject;
33  import org.eclipse.jdt.core.IPackageFragment;
34  import org.eclipse.jdt.core.JavaCore;
35  import org.eclipse.jface.action.IAction;
36  import org.eclipse.jface.viewers.ISelection;
37  import org.eclipse.jface.viewers.ISelectionProvider;
38  import org.eclipse.jface.viewers.StructuredSelection;
39  import org.eclipse.ui.IObjectActionDelegate;
40  import org.eclipse.ui.IWorkbenchPage;
41  import org.eclipse.ui.IWorkbenchPart;
42  import org.eclipse.ui.IWorkbenchPartSite;
43  import org.eclipse.ui.actions.ActionDelegate;
44  
45  /**
46   * Action to be added to the context menu of the Navigator or
47   * Packages view. Enables the "Inspect" action in the context menu
48   */
49  public class InspectAction extends ActionDelegate
50    implements IObjectActionDelegate {
51    /** The page containing the part target. */
52    private IWorkbenchPage page;
53  
54    /** The part target. */
55    private IWorkbenchPart part;
56  
57    /**
58     * DOCUMENT ME!
59     *
60     * @param iAction DOCUMENT ME!
61     * @param iWorkbenchPart DOCUMENT ME!
62     */
63    public void setActivePart(IAction iAction, IWorkbenchPart iWorkbenchPart) {
64      part = iWorkbenchPart;
65      page = part.getSite().getPage();
66    }
67  
68    /**
69     * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
70     */
71    public void selectionChanged(IAction action, ISelection selection) {
72    }
73  
74    /**
75     * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
76     */
77    public void run(final IAction action) {
78      final IWorkbenchPartSite site = part.getSite();
79      final ISelectionProvider provider = site.getSelectionProvider();
80      final StructuredSelection selection =
81        (StructuredSelection) provider.getSelection();
82      final Vector files = new Vector();
83  
84      retrieveJavaMembers(selection, files);
85  
86      if (files.size() > 0) {
87        final IFile file = (IFile) files.get(0);
88        final Project project =
89          EclipseEnvironment.toQSJavaProject(
90            JavaCore.create(file).getJavaProject()
91          );
92  
93        //    if ( OS.isWindows() ) {
94        //      ExternalAnalyzer analyzer = new ExternalAnalyzer(project, files);
95        //      analyzer.setObservationView(observationView);
96        //      analyzer.runAnalyzerThread();
97        //    } else {
98        new EclipseAnalyzer(project, files);
99  
100       //}
101     }
102   }
103 
104   private void retrieveJavaMembers(
105     final StructuredSelection selection, Vector files
106   ) {
107     final Object[] items = selection.toArray();
108 
109     try {
110       for (int i = 0; i < items.length; i++) {
111         if (items[i] instanceof IResource) {
112           EclipseEnvironment.getJavaFilesFromResource(
113             (IResource) items[i], files
114           );
115         } else if (items[i] instanceof ICompilationUnit) {
116           EclipseEnvironment.getJavaFilesFromCompilationUnit(
117             (ICompilationUnit) items[i], files
118           );
119         } else if (items[i] instanceof IPackageFragment) {
120           EclipseEnvironment.getJavaFilesFromPackage(
121             (IPackageFragment) items[i], files
122           );
123         } else if (items[i] instanceof IJavaProject) {
124           EclipseEnvironment.getJavaFilesFromProject(
125             (IJavaProject) items[i], files
126           );
127         }
128       }
129     } catch (CoreException e) {
130       new DebugWriter().writeException(e, this);
131     }
132   }
133 }
134