KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > wizards > TypedElementSelectionValidator


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.wizards;
12
13 import java.util.Collection JavaDoc;
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 /**
24  * Implementation of a <code>ISelectionValidator</code> to validate the
25  * type of an element.
26  * Empty selections are not accepted.
27  */

28 public class TypedElementSelectionValidator implements ISelectionStatusValidator {
29
30     private IStatus fgErrorStatus= new StatusInfo(IStatus.ERROR, ""); //$NON-NLS-1$
31
private IStatus fgOKStatus= new StatusInfo();
32
33     private Class JavaDoc[] fAcceptedTypes;
34     private boolean fAllowMultipleSelection;
35     private Collection JavaDoc fRejectedElements;
36     
37     /**
38      * @param acceptedTypes The types accepted by the validator
39      * @param allowMultipleSelection If set to <code>true</code>, the validator
40      * allows multiple selection.
41      */

42     public TypedElementSelectionValidator(Class JavaDoc[] acceptedTypes, boolean allowMultipleSelection) {
43         this(acceptedTypes, allowMultipleSelection, null);
44     }
45     
46     /**
47      * @param acceptedTypes The types accepted by the validator
48      * @param allowMultipleSelection If set to <code>true</code>, the validator
49      * allows multiple selection.
50      * @param rejectedElements A list of elements that are not accepted
51      */

52     public TypedElementSelectionValidator(Class JavaDoc[] acceptedTypes, boolean allowMultipleSelection, Collection JavaDoc rejectedElements) {
53         Assert.isNotNull(acceptedTypes);
54         fAcceptedTypes= acceptedTypes;
55         fAllowMultipleSelection= allowMultipleSelection;
56         fRejectedElements= rejectedElements;
57     }
58     
59     /*
60      * @see org.eclipse.ui.dialogs.ISelectionValidator#isValid(java.lang.Object)
61      */

62     public IStatus validate(Object JavaDoc[] elements) {
63         if (isValid(elements)) {
64             return fgOKStatus;
65         }
66         return fgErrorStatus;
67     }
68
69     private boolean isOfAcceptedType(Object JavaDoc 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 JavaDoc elem) {
79         return (fRejectedElements != null) && fRejectedElements.contains(elem);
80     }
81     
82     protected boolean isSelectedValid(Object JavaDoc elem) {
83         return true;
84     }
85     
86     private boolean isValid(Object JavaDoc[] 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 JavaDoc o= selection[i];
97             if (!isOfAcceptedType(o) || isRejectedElement(o) || !isSelectedValid(o)) {
98                 return false;
99             }
100         }
101         return true;
102     }
103 }
104
Popular Tags