001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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.wizards;
011:
012: import java.util.Collection;
013:
014: import org.eclipse.core.runtime.Assert;
015: import org.eclipse.core.runtime.IStatus;
016:
017: import org.eclipse.ui.dialogs.ISelectionStatusValidator;
018:
019: import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
020:
021: /**
022: * Implementation of a <code>ISelectionValidator</code> to validate the
023: * type of an element.
024: * Empty selections are not accepted.
025: */
026: public class TypedElementSelectionValidator implements
027: ISelectionStatusValidator {
028:
029: private IStatus fgErrorStatus = new StatusInfo(IStatus.ERROR, ""); //$NON-NLS-1$
030: private IStatus fgOKStatus = new StatusInfo();
031:
032: private Class[] fAcceptedTypes;
033: private boolean fAllowMultipleSelection;
034: private Collection fRejectedElements;
035:
036: /**
037: * @param acceptedTypes The types accepted by the validator
038: * @param allowMultipleSelection If set to <code>true</code>, the validator
039: * allows multiple selection.
040: */
041: public TypedElementSelectionValidator(Class[] acceptedTypes,
042: boolean allowMultipleSelection) {
043: this (acceptedTypes, allowMultipleSelection, null);
044: }
045:
046: /**
047: * @param acceptedTypes The types accepted by the validator
048: * @param allowMultipleSelection If set to <code>true</code>, the validator
049: * allows multiple selection.
050: * @param rejectedElements A list of elements that are not accepted
051: */
052: public TypedElementSelectionValidator(Class[] acceptedTypes,
053: boolean allowMultipleSelection, Collection rejectedElements) {
054: Assert.isNotNull(acceptedTypes);
055: fAcceptedTypes = acceptedTypes;
056: fAllowMultipleSelection = allowMultipleSelection;
057: fRejectedElements = rejectedElements;
058: }
059:
060: /*
061: * @see org.eclipse.ui.dialogs.ISelectionValidator#isValid(java.lang.Object)
062: */
063: public IStatus validate(Object[] elements) {
064: if (isValid(elements)) {
065: return fgOKStatus;
066: }
067: return fgErrorStatus;
068: }
069:
070: private boolean isOfAcceptedType(Object o) {
071: for (int i = 0; i < fAcceptedTypes.length; i++) {
072: if (fAcceptedTypes[i].isInstance(o)) {
073: return true;
074: }
075: }
076: return false;
077: }
078:
079: private boolean isRejectedElement(Object elem) {
080: return (fRejectedElements != null)
081: && fRejectedElements.contains(elem);
082: }
083:
084: protected boolean isSelectedValid(Object elem) {
085: return true;
086: }
087:
088: private boolean isValid(Object[] selection) {
089: if (selection.length == 0) {
090: return false;
091: }
092:
093: if (!fAllowMultipleSelection && selection.length != 1) {
094: return false;
095: }
096:
097: for (int i = 0; i < selection.length; i++) {
098: Object o = selection[i];
099: if (!isOfAcceptedType(o) || isRejectedElement(o)
100: || !isSelectedValid(o)) {
101: return false;
102: }
103: }
104: return true;
105: }
106: }
|