1 11 package org.eclipse.jdt.internal.ui.actions; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 16 import org.eclipse.core.runtime.IStatus; 17 18 import org.eclipse.ui.dialogs.ISelectionStatusValidator; 19 20 import org.eclipse.jdt.core.dom.IMethodBinding; 21 import org.eclipse.jdt.core.dom.ITypeBinding; 22 import org.eclipse.jdt.core.dom.IVariableBinding; 23 24 import org.eclipse.jdt.internal.corext.util.Messages; 25 26 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo; 27 28 public class GenerateConstructorUsingFieldsValidator implements ISelectionStatusValidator { 29 30 private GenerateConstructorUsingFieldsSelectionDialog fDialog; 31 32 private final int fEntries; 33 34 private List fSignatures; 35 36 private ITypeBinding fType= null; 37 38 public GenerateConstructorUsingFieldsValidator(GenerateConstructorUsingFieldsSelectionDialog dialog, ITypeBinding type, int entries) { 39 fEntries= entries; 40 fDialog= dialog; 41 fType= type; 42 fSignatures= getExistingConstructorSignatures(); 43 } 44 45 public GenerateConstructorUsingFieldsValidator(int entries) { 46 fEntries= entries; 47 fType= null; 48 } 49 50 private int countSelectedFields(Object [] selection) { 51 int count= 0; 52 for (int index= 0; index < selection.length; index++) { 53 if (selection[index] instanceof IVariableBinding) 54 count++; 55 } 56 return count; 57 } 58 59 private void createSignature(final IMethodBinding constructor, StringBuffer buffer, Object [] selection) { 60 ITypeBinding types[]= constructor.getParameterTypes(); 61 for (int index= 0; index < types.length; index++) 62 buffer.append(types[index].getName()); 63 if (selection != null) { 64 for (int index= 0; index < selection.length; index++) 65 if (selection[index] instanceof IVariableBinding) 66 buffer.append(((IVariableBinding) selection[index]).getType().getErasure().getName()); 67 } 68 } 69 70 private List getExistingConstructorSignatures() { 71 List existing= new ArrayList (); 72 IMethodBinding[] methods= fType.getDeclaredMethods(); 73 for (int index= 0; index < methods.length; index++) { 74 if (methods[index].isConstructor()) { 75 StringBuffer buffer= new StringBuffer (); 76 createSignature(methods[index], buffer, null); 77 existing.add(buffer.toString()); 78 } 79 } 80 return existing; 81 } 82 83 public IStatus validate(Object [] selection) { 84 StringBuffer buffer= new StringBuffer (); 85 final IMethodBinding constructor= fDialog.getSuperConstructorChoice(); 86 createSignature(constructor, buffer, selection); 87 if (fSignatures.contains(buffer.toString())) 88 return new StatusInfo(IStatus.WARNING, ActionMessages.GenerateConstructorUsingFieldsAction_error_duplicate_constructor); 89 return new StatusInfo(IStatus.INFO, Messages.format(ActionMessages.GenerateConstructorUsingFieldsAction_fields_selected, new Object [] { String.valueOf(countSelectedFields(selection)), String.valueOf(fEntries)})); 90 } 91 } 92 | Popular Tags |