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 java.text.MessageFormat;
20  import java.util.Iterator;
21  import java.util.List;
22  
23  import net.sf.commonclipse.CCMessages;
24  
25  import org.eclipse.jdt.core.ICompilationUnit;
26  import org.eclipse.jdt.core.IType;
27  import org.eclipse.jface.action.IAction;
28  import org.eclipse.jface.dialogs.MessageDialog;
29  import org.eclipse.jface.viewers.ISelection;
30  import org.eclipse.jface.viewers.IStructuredSelection;
31  import org.eclipse.swt.widgets.Shell;
32  import org.eclipse.ui.IObjectActionDelegate;
33  import org.eclipse.ui.IWorkbenchPart;
34  
35  
36  /***
37   * action called from object contribution (right click on navigator). Support multiple selections.
38   * @author fgiust
39   * @version $Revision: 1.4 $ ($Author: fgiust $)
40   */
41  public class JavaTypeObjectAction extends JavaTypeAction implements IObjectActionDelegate
42  {
43  
44      /***
45       * selected objects. Can contain instances of IType or ICompilationUnit
46       */
47      private List selected;
48  
49      /***
50       * new JavaTypeObjectAction.
51       */
52      public JavaTypeObjectAction()
53      {
54          this.selected = null;
55      }
56  
57      /***
58       * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
59       */
60      public void setActivePart(IAction action, IWorkbenchPart targetPart)
61      {
62      }
63  
64      /***
65       * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
66       */
67      public void run(IAction action)
68      {
69  
70          IType type = null;
71  
72          if (this.selected != null)
73          {
74  
75              // iterates on selection
76              Iterator selectionIterator = this.selected.iterator();
77  
78              // prepare shell
79              Shell shell = new Shell();
80  
81              while (selectionIterator.hasNext())
82              {
83  
84                  // iterates and check object
85                  Object iteratorObject = selectionIterator.next();
86  
87                  // reset type
88                  type = null;
89  
90                  if (iteratorObject instanceof IType)
91                  {
92                      type = (IType) iteratorObject;
93                  }
94                  else if (iteratorObject instanceof ICompilationUnit)
95                  {
96                      type = ((ICompilationUnit) iteratorObject).findPrimaryType();
97  
98                  }
99                  else
100                 {
101                     MessageDialog.openError(shell, CCMessages.getString("Generator.errortitle"), //$NON-NLS-1$
102                         MessageFormat.format(CCMessages.getString("Generator.unknownobject"), //$NON-NLS-1$
103                             new Object[]{iteratorObject.getClass().getName()}));
104                 }
105 
106                 if (type != null)
107                 {
108                     runAction(action, type, shell);
109                 }
110 
111             }
112 
113         }
114 
115     }
116 
117     /***
118      * file selected from menu.
119      * @see org.eclipse.ui.IActionDelegate#selectionChanged(IAction, ISelection)
120      */
121     public void selectionChanged(IAction action, ISelection selection)
122     {
123         if (selection != null && selection instanceof IStructuredSelection)
124         {
125             IStructuredSelection ss = (IStructuredSelection) selection;
126             if (!ss.isEmpty())
127             {
128                 this.selected = ss.toList();
129             }
130         }
131 
132     }
133 
134 }