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
21 import net.sf.commonclipse.CompareToGenerator;
22 import net.sf.commonclipse.EqualsGenerator;
23 import net.sf.commonclipse.HashcodeGenerator;
24 import net.sf.commonclipse.CCMessages;
25 import net.sf.commonclipse.ToStringGenerator;
26
27 import org.eclipse.jdt.core.IType;
28 import org.eclipse.jdt.core.JavaModelException;
29 import org.eclipse.jface.action.IAction;
30 import org.eclipse.jface.dialogs.MessageDialog;
31 import org.eclipse.swt.widgets.Shell;
32
33
34 /***
35 * base action delegate for action that need an IType to work.
36 * @author fgiust
37 * @version $Revision: 1.4 $ ($Author: fgiust $)
38 */
39 public abstract class JavaTypeAction
40 {
41
42 /***
43 * id for toString() generator.
44 */
45 public static final String ACTION_TOSTRING = "cb.ToString";
46
47 /***
48 * id for hashCode() generator.
49 */
50 public static final String ACTION_HASHCODE = "cb.HashCode";
51
52 /***
53 * id for equals() generator.
54 */
55 public static final String ACTION_EQUALS = "cb.Equals";
56
57 /***
58 * id for compareTo() generator.
59 */
60 public static final String ACTION_COMPARETO = "cb.CompareTo";
61
62 /***
63 * run the action on the given IType.
64 * @param action activated IAction
65 * @param type selected IType
66 * @param shell Shell for messages
67 */
68 protected void runAction(IAction action, IType type, Shell shell)
69 {
70 try
71 {
72 if (!type.isClass())
73 {
74 MessageDialog.openError(new Shell(), CCMessages.getString("Generator.errortitle"),
75 MessageFormat.format(CCMessages.getString("Generator.notaclass"),
76 new Object[]{type.getElementName()}));
77 return;
78 }
79 }
80 catch (JavaModelException e)
81 {
82 MessageDialog.openError(new Shell(), CCMessages.getString("Generator.errortitle"),
83 e.getMessage());
84 return;
85 }
86
87 String id = action.getId();
88
89 if (ACTION_TOSTRING.equals(id))
90 {
91 ToStringGenerator.getInstance().generate(type, shell);
92 }
93 else if (ACTION_HASHCODE.equals(id))
94 {
95 HashcodeGenerator.getInstance().generate(type, shell);
96 }
97 else if (ACTION_EQUALS.equals(id))
98 {
99 EqualsGenerator.getInstance().generate(type, shell);
100 }
101 else if (ACTION_COMPARETO.equals(id))
102 {
103 CompareToGenerator.getInstance().generate(type, shell);
104 }
105 else
106 {
107 MessageDialog.openError(new Shell(), CCMessages.getString("Generator.errortitle"),
108 MessageFormat.format(CCMessages.getString("Generator.unknownaction"),
109 new Object[]{id}));
110 }
111
112 }
113
114 }