1 11 package org.eclipse.pde.internal.ui.editor.plugin; 12 13 import org.eclipse.core.runtime.Assert; 14 import org.eclipse.core.runtime.IStatus; 15 import org.eclipse.core.runtime.Status; 16 import org.eclipse.pde.internal.ui.PDEPlugin; 17 import org.eclipse.ui.dialogs.ISelectionStatusValidator; 18 23 public class JarSelectionValidator implements ISelectionStatusValidator { 24 25 private Class [] fAcceptedTypes; 26 private boolean fAllowMultipleSelection; 27 28 29 34 public JarSelectionValidator(Class [] acceptedTypes, boolean allowMultipleSelection) { 35 Assert.isNotNull(acceptedTypes); 36 fAcceptedTypes= acceptedTypes; 37 fAllowMultipleSelection= allowMultipleSelection; 38 } 39 40 41 44 public IStatus validate(Object [] elements) { 45 if (isValidSelection(elements)) { 46 return new Status( 47 IStatus.OK, 48 PDEPlugin.getPluginId(), 49 IStatus.OK, 50 "", null); 52 } 53 return new Status( 54 IStatus.ERROR, 55 PDEPlugin.getPluginId(), 56 IStatus.ERROR, 57 "", null); 59 } 60 61 private boolean isValidSelection(Object [] selection) { 62 if (selection.length == 0) { 63 return false; 64 } 65 66 if (!fAllowMultipleSelection && selection.length != 1) { 67 return false; 68 } 69 70 for (int i= 0; i < selection.length; i++) { 71 Object o= selection[i]; 72 if (!isValid(o)) { 73 return false; 74 } 75 } 76 return true; 77 } 78 79 public boolean isValid(Object element) { 80 for (int i= 0; i < fAcceptedTypes.length; i++) { 81 if (fAcceptedTypes[i].isInstance(element)) { 82 return true; 83 } 84 } 85 return false; 86 } 87 } 88 | Popular Tags |