1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
76 Iterator selectionIterator = this.selected.iterator();
77
78
79 Shell shell = new Shell();
80
81 while (selectionIterator.hasNext())
82 {
83
84
85 Object iteratorObject = selectionIterator.next();
86
87
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"),
102 MessageFormat.format(CCMessages.getString("Generator.unknownobject"),
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 }