KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > actions > GenerateConstructorUsingFieldsValidator


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.actions;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
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 JavaDoc 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 JavaDoc[] 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 JavaDoc buffer, Object JavaDoc[] 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 JavaDoc getExistingConstructorSignatures() {
71         List JavaDoc existing= new ArrayList JavaDoc();
72         IMethodBinding[] methods= fType.getDeclaredMethods();
73         for (int index= 0; index < methods.length; index++) {
74             if (methods[index].isConstructor()) {
75                 StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
76                 createSignature(methods[index], buffer, null);
77                 existing.add(buffer.toString());
78             }
79         }
80         return existing;
81     }
82
83     public IStatus validate(Object JavaDoc[] selection) {
84         StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
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 JavaDoc[] { String.valueOf(countSelectedFields(selection)), String.valueOf(fEntries)}));
90     }
91 }
92
Popular Tags