001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.actions;
011:
012: import java.util.ArrayList;
013: import java.util.List;
014:
015: import org.eclipse.core.runtime.IStatus;
016:
017: import org.eclipse.ui.dialogs.ISelectionStatusValidator;
018:
019: import org.eclipse.jdt.core.dom.IMethodBinding;
020: import org.eclipse.jdt.core.dom.ITypeBinding;
021: import org.eclipse.jdt.core.dom.IVariableBinding;
022:
023: import org.eclipse.jdt.internal.corext.util.Messages;
024:
025: import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
026:
027: public class GenerateConstructorUsingFieldsValidator implements
028: ISelectionStatusValidator {
029:
030: private GenerateConstructorUsingFieldsSelectionDialog fDialog;
031:
032: private final int fEntries;
033:
034: private List fSignatures;
035:
036: private ITypeBinding fType = null;
037:
038: public GenerateConstructorUsingFieldsValidator(
039: GenerateConstructorUsingFieldsSelectionDialog dialog,
040: ITypeBinding type, int entries) {
041: fEntries = entries;
042: fDialog = dialog;
043: fType = type;
044: fSignatures = getExistingConstructorSignatures();
045: }
046:
047: public GenerateConstructorUsingFieldsValidator(int entries) {
048: fEntries = entries;
049: fType = null;
050: }
051:
052: private int countSelectedFields(Object[] selection) {
053: int count = 0;
054: for (int index = 0; index < selection.length; index++) {
055: if (selection[index] instanceof IVariableBinding)
056: count++;
057: }
058: return count;
059: }
060:
061: private void createSignature(final IMethodBinding constructor,
062: StringBuffer buffer, Object[] selection) {
063: ITypeBinding types[] = constructor.getParameterTypes();
064: for (int index = 0; index < types.length; index++)
065: buffer.append(types[index].getName());
066: if (selection != null) {
067: for (int index = 0; index < selection.length; index++)
068: if (selection[index] instanceof IVariableBinding)
069: buffer.append(((IVariableBinding) selection[index])
070: .getType().getErasure().getName());
071: }
072: }
073:
074: private List getExistingConstructorSignatures() {
075: List existing = new ArrayList();
076: IMethodBinding[] methods = fType.getDeclaredMethods();
077: for (int index = 0; index < methods.length; index++) {
078: if (methods[index].isConstructor()) {
079: StringBuffer buffer = new StringBuffer();
080: createSignature(methods[index], buffer, null);
081: existing.add(buffer.toString());
082: }
083: }
084: return existing;
085: }
086:
087: public IStatus validate(Object[] selection) {
088: StringBuffer buffer = new StringBuffer();
089: final IMethodBinding constructor = fDialog
090: .getSuperConstructorChoice();
091: createSignature(constructor, buffer, selection);
092: if (fSignatures.contains(buffer.toString()))
093: return new StatusInfo(
094: IStatus.WARNING,
095: ActionMessages.GenerateConstructorUsingFieldsAction_error_duplicate_constructor);
096: return new StatusInfo(
097: IStatus.INFO,
098: Messages
099: .format(
100: ActionMessages.GenerateConstructorUsingFieldsAction_fields_selected,
101: new Object[] {
102: String
103: .valueOf(countSelectedFields(selection)),
104: String.valueOf(fEntries) }));
105: }
106: }
|