]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/validators/MultiValidator.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / validators / MultiValidator.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 /*
13  * Created on 6.10.2005
14  * @author Toni Kalajainen 
15  */
16 package org.simantics.utils.ui.validators;
17
18 import org.eclipse.jface.dialogs.IInputValidator;
19
20 /**
21  * Validator Multiplexer. Allows to combine multiple validators
22  * 
23  */
24 public class MultiValidator implements IInputValidator {
25
26     private final IInputValidator validators[];
27
28     private final AcceptType acceptType;
29
30     public enum AcceptType {
31         ALL_MUST_PASS, ONE_MUST_PASS
32     };
33
34     public MultiValidator(IInputValidator validators[], AcceptType acceptType) {
35         this.acceptType = acceptType;
36         this.validators = new IInputValidator[validators.length];
37         for (int i = 0; i < validators.length; i++)
38             this.validators[i] = validators[i];
39     }
40
41     public String isValid(String newText) {
42         if (acceptType == AcceptType.ALL_MUST_PASS) {
43             for (int i = 0; i < validators.length; i++) {
44                 String result = validators[i].isValid(newText);
45                 if (result != null)
46                     return result;
47             }
48             return null;
49         }
50         
51         if (acceptType == AcceptType.ONE_MUST_PASS) {
52             String error = null;
53             for (int i = 0; i < validators.length; i++) {
54                 String result = validators[i].isValid(newText);                
55                 if (result != null)
56                     error = result;
57                 else
58                     return null;
59             }
60             return error;
61         }
62         return null;
63     }
64
65 }