KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > plugin > JarSelectionValidator


1 /*******************************************************************************
2  * Copyright (c) 2003, 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.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     /**
19      * Implementation of a <code>ISelectionValidator</code> to validate the
20      * type of an element.
21      * Empty selections are not accepted.
22      */

23 public class JarSelectionValidator implements ISelectionStatusValidator {
24
25         private Class JavaDoc[] fAcceptedTypes;
26         private boolean fAllowMultipleSelection;
27
28     
29         /**
30          * @param acceptedTypes The types accepted by the validator
31          * @param allowMultipleSelection If set to <code>true</code>, the validator
32          * allows multiple selection.
33          */

34         public JarSelectionValidator(Class JavaDoc[] acceptedTypes, boolean allowMultipleSelection) {
35             Assert.isNotNull(acceptedTypes);
36             fAcceptedTypes= acceptedTypes;
37             fAllowMultipleSelection= allowMultipleSelection;
38         }
39     
40
41         /*
42          * @see org.eclipse.ui.dialogs.ISelectionValidator#isValid(java.lang.Object)
43          */

44         public IStatus validate(Object JavaDoc[] elements) {
45             if (isValidSelection(elements)) {
46                 return new Status(
47                     IStatus.OK,
48                     PDEPlugin.getPluginId(),
49                     IStatus.OK,
50                     "", //$NON-NLS-1$
51
null);
52             }
53             return new Status(
54                 IStatus.ERROR,
55                 PDEPlugin.getPluginId(),
56                 IStatus.ERROR,
57                 "", //$NON-NLS-1$
58
null);
59         }
60
61         private boolean isValidSelection(Object JavaDoc[] 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 JavaDoc o= selection[i];
72                 if (!isValid(o)) {
73                     return false;
74                 }
75             }
76             return true;
77         }
78         
79         public boolean isValid(Object JavaDoc 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