1 11 package org.eclipse.jdt.internal.ui.wizards; 12 13 import java.util.Collection ; 14 15 import org.eclipse.core.runtime.Assert; 16 import org.eclipse.core.runtime.IStatus; 17 18 19 import org.eclipse.ui.dialogs.ISelectionStatusValidator; 20 21 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo; 22 23 28 public class TypedElementSelectionValidator implements ISelectionStatusValidator { 29 30 private IStatus fgErrorStatus= new StatusInfo(IStatus.ERROR, ""); private IStatus fgOKStatus= new StatusInfo(); 32 33 private Class [] fAcceptedTypes; 34 private boolean fAllowMultipleSelection; 35 private Collection fRejectedElements; 36 37 42 public TypedElementSelectionValidator(Class [] acceptedTypes, boolean allowMultipleSelection) { 43 this(acceptedTypes, allowMultipleSelection, null); 44 } 45 46 52 public TypedElementSelectionValidator(Class [] acceptedTypes, boolean allowMultipleSelection, Collection rejectedElements) { 53 Assert.isNotNull(acceptedTypes); 54 fAcceptedTypes= acceptedTypes; 55 fAllowMultipleSelection= allowMultipleSelection; 56 fRejectedElements= rejectedElements; 57 } 58 59 62 public IStatus validate(Object [] elements) { 63 if (isValid(elements)) { 64 return fgOKStatus; 65 } 66 return fgErrorStatus; 67 } 68 69 private boolean isOfAcceptedType(Object o) { 70 for (int i= 0; i < fAcceptedTypes.length; i++) { 71 if (fAcceptedTypes[i].isInstance(o)) { 72 return true; 73 } 74 } 75 return false; 76 } 77 78 private boolean isRejectedElement(Object elem) { 79 return (fRejectedElements != null) && fRejectedElements.contains(elem); 80 } 81 82 protected boolean isSelectedValid(Object elem) { 83 return true; 84 } 85 86 private boolean isValid(Object [] selection) { 87 if (selection.length == 0) { 88 return false; 89 } 90 91 if (!fAllowMultipleSelection && selection.length != 1) { 92 return false; 93 } 94 95 for (int i= 0; i < selection.length; i++) { 96 Object o= selection[i]; 97 if (!isOfAcceptedType(o) || isRejectedElement(o) || !isSelectedValid(o)) { 98 return false; 99 } 100 } 101 return true; 102 } 103 } 104 | Popular Tags |