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  
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"; //$NON-NLS-1$
46  
47      /***
48       * id for hashCode() generator.
49       */
50      public static final String ACTION_HASHCODE = "cb.HashCode"; //$NON-NLS-1$
51  
52      /***
53       * id for equals() generator.
54       */
55      public static final String ACTION_EQUALS = "cb.Equals"; //$NON-NLS-1$
56  
57      /***
58       * id for compareTo() generator.
59       */
60      public static final String ACTION_COMPARETO = "cb.CompareTo"; //$NON-NLS-1$
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"), //$NON-NLS-1$
75                      MessageFormat.format(CCMessages.getString("Generator.notaclass"), //$NON-NLS-1$
76                          new Object[]{type.getElementName()}));
77                  return;
78              }
79          }
80          catch (JavaModelException e)
81          {
82              MessageDialog.openError(new Shell(), CCMessages.getString("Generator.errortitle"), //$NON-NLS-1$
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"), //$NON-NLS-1$
108                 MessageFormat.format(CCMessages.getString("Generator.unknownaction"), //$NON-NLS-1$
109                     new Object[]{id}));
110         }
111 
112     }
113 
114 }