View Javadoc

1   /* ====================================================================
2    *   Copyright 2003-2004 Fabrizio Giustina.
3    *
4    *   Licensed under the Apache License, Version 2.0 (the "License");
5    *   you may not use this file except in compliance with the License.
6    *   You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *   Unless required by applicable law or agreed to in writing, software
11   *   distributed under the License is distributed on an "AS IS" BASIS,
12   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *   See the License for the specific language governing permissions and
14   *   limitations under the License.
15   * ====================================================================
16   */
17  package net.sf.commonclipse.popup.actions;
18  
19  import org.eclipse.jdt.core.ICompilationUnit;
20  import org.eclipse.jdt.core.IJavaElement;
21  import org.eclipse.jdt.core.IType;
22  import org.eclipse.jdt.core.JavaModelException;
23  import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
24  import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
25  import org.eclipse.jdt.ui.IWorkingCopyManager;
26  import org.eclipse.jdt.ui.JavaUI;
27  import org.eclipse.jface.action.IAction;
28  import org.eclipse.jface.viewers.ISelection;
29  import org.eclipse.swt.widgets.Shell;
30  import org.eclipse.ui.IEditorActionDelegate;
31  import org.eclipse.ui.IEditorPart;
32  import org.eclipse.ui.IWorkbenchPage;
33  import org.eclipse.ui.IWorkbenchWindow;
34  import org.eclipse.ui.PlatformUI;
35  import org.eclipse.ui.texteditor.ITextEditor;
36  
37  /***
38   * action called from viewer contribution (right click on editor).
39   * @author fgiust
40   * @version $Revision: 1.3 $ ($Author: fgiust $)
41   */
42  public class JavaTypeViewerAction extends JavaTypeAction implements IEditorActionDelegate
43  {
44  
45      /***
46       * selected editor.
47       */
48      private ITextEditor editor;
49  
50      /***
51       * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
52       */
53      public void run(IAction action)
54      {
55          IType type = null;
56  
57          if (this.editor != null)
58          {
59              if (this.editor instanceof JavaEditor)
60              {
61                  try
62                  {
63                      IJavaElement element = SelectionConverter.getElementAtOffset((JavaEditor) this.editor);
64                      if (element != null)
65                      {
66                          type = (IType) element.getAncestor(IJavaElement.TYPE);
67                      }
68                  }
69                  catch (JavaModelException e)
70                  {
71                      // ignore, simply get the primary type
72                  }
73              }
74  
75              if (type == null)
76              {
77                  IWorkingCopyManager manager = JavaUI.getWorkingCopyManager();
78                  ICompilationUnit unit = manager.getWorkingCopy(this.editor.getEditorInput());
79  
80                  type = unit.findPrimaryType();
81              }
82          }
83  
84          if (type != null)
85          {
86              runAction(action, type, new Shell());
87          }
88      }
89  
90      /***
91       * selected editor has changed.
92       * @see org.eclipse.ui.IActionDelegate#selectionChanged(IAction, ISelection)
93       */
94      public void selectionChanged(IAction action, ISelection selection)
95      {
96  
97          IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
98          if (window != null)
99          {
100             IWorkbenchPage page = window.getActivePage();
101             if (page != null)
102             {
103                 IEditorPart activeEditor = page.getActiveEditor();
104                 if (activeEditor != null && activeEditor instanceof ITextEditor)
105                 {
106                     setActiveEditor(action, activeEditor);
107                 }
108             }
109         }
110 
111     }
112 
113     /***
114      * @see org.eclipse.ui.IEditorActionDelegate#setActiveEditor(IAction, IEditorPart)
115      */
116     public void setActiveEditor(IAction action, IEditorPart targetEditor)
117     {
118         if (targetEditor instanceof ITextEditor)
119         {
120             this.editor = (ITextEditor) targetEditor;
121         }
122 
123     }
124 
125 }