KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > contrib > dbroggisch > repopulation > SelectFormValidator


1 /*
2  * Copyright (C) 2003 Diez B. Roggisch [deets@web.de]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: SelectFormValidator.java,v 1.4 2004/02/01 05:16:27 christianc Exp $
19  */

20 package org.enhydra.barracuda.contrib.dbroggisch.repopulation;
21
22 import java.util.List JavaDoc;
23 import java.util.ArrayList JavaDoc;
24
25 import org.enhydra.barracuda.core.forms.DefaultFormValidator;
26 import org.enhydra.barracuda.core.comp.ListModel;
27 import org.enhydra.barracuda.core.comp.DefaultListModel;
28 import org.apache.log4j.Logger;
29 import org.enhydra.barracuda.core.comp.ListSelectionModel;
30 import org.enhydra.barracuda.core.forms.ValidationException;
31 import org.enhydra.barracuda.core.forms.FormElement;
32 import org.enhydra.barracuda.core.forms.FormValidator;
33 import org.enhydra.barracuda.core.comp.ItemMap;
34
35 public class SelectFormValidator extends DefaultFormValidator {
36     protected static final Logger logger = Logger.getLogger(SelectFormValidator.class.getName());
37
38     int selMode = -1;
39     String JavaDoc ignore;
40
41     public void setSelectionMode(int mode) {
42         selMode = mode == ListSelectionModel.MULTIPLE_INTERVAL_SELECTION ?
43             mode : mode == ListSelectionModel.SINGLE_INTERVAL_SELECTION ?
44             mode : mode == ListSelectionModel.SINGLE_SELECTION ?
45             mode : selMode;
46     }
47
48     public SelectFormValidator(String JavaDoc ignore) {
49         //super();
50
this.ignore = ignore;
51     }
52
53     public SelectFormValidator(String JavaDoc ignore, String JavaDoc message) {
54         super(message);
55         this.ignore = ignore;
56     }
57
58     public SelectFormValidator() {
59         //super();
60
}
61
62     public SelectFormValidator(int selMode) {
63         //super();
64
setSelectionMode(selMode);
65     }
66
67     public SelectFormValidator(int selMode, String JavaDoc message) {
68         super(message);
69         setSelectionMode(selMode);
70     }
71
72     public SelectFormValidator(int selMode, String JavaDoc ignore, String JavaDoc message) {
73         super(message);
74         this.ignore = ignore;
75         setSelectionMode(selMode);
76     }
77
78
79     public int getSelectionMode() {
80         return selMode;
81     }
82
83     public void setIgnore(String JavaDoc val) {
84         ignore = val;
85     }
86
87     public String JavaDoc getIgnore() {
88         return ignore;
89     }
90
91
92     public void validateFormElement(Object JavaDoc val, FormElement element, boolean deferExceptions) throws ValidationException {
93         if(logger.isDebugEnabled()) {
94             logger.debug("Begin validation.");
95         }
96         if(!(element instanceof SelectFormElement)) {
97             logger.error("Not a SelectFormElement" + element);
98             throw this.generateException(element, deferExceptions, "Element must be SelectFormElement");
99         }
100
101         boolean allContained = true;
102         SelectFormElement selEl = (SelectFormElement)element;
103         ListModel lm = selEl.getListModel();
104         List JavaDoc selected = new ArrayList JavaDoc();
105         Object JavaDoc origVal = selEl.getOrigVal();
106
107         // Get the list of selected values - as a list
108
if(origVal != null) {
109             if(logger.isDebugEnabled()) {
110                 logger.debug("OrigVal class is:" + origVal.getClass());
111             }
112             if(origVal instanceof List JavaDoc) {
113                 selected = (List JavaDoc)origVal;
114             } else {
115                 selected.add(origVal);
116             }
117         }
118
119         if(ignore != null) {
120             selected.remove(ignore);
121         }
122
123
124         // Get the keys/values from the ListModel - as Strings!
125
List JavaDoc values = new ArrayList JavaDoc(lm.getSize());
126         for(int i = 0; i < lm.getSize(); i++) {
127             String JavaDoc key;
128             String JavaDoc value;
129             Object JavaDoc item = lm.getItemAt(i);
130             if(item instanceof ItemMap) {
131                 key = ((ItemMap)item).getKey().toString();
132                 value = ((ItemMap)item).getValue().toString();
133             } else {
134                 value = key = item.toString();
135             }
136             if(!(ignore != null && ignore.equals(value)))
137                 values.add(key);
138         }
139         // Every selected value _must_ be among the allowed values. Otherwise - error!
140
for(int i = 0;i < selected.size(); i++) {
141             if(!values.contains(selected.get(i))) {
142                 allContained = false;
143                 break;
144             }
145         }
146         if(!allContained) {
147             if(logger.isDebugEnabled()) {
148                 logger.debug("Not all selected options are among the permitted values.");
149             }
150             throw this.generateException(element, deferExceptions, getErrorMessage() != null ? getErrorMessage() : "Not all selected options are among the permitted values.");
151         }
152         // Do we have a value at all? If not,
153
// throw an error.
154
if(ignore != null && selected.size() == 0) {
155             if(logger.isDebugEnabled()) {
156                 logger.debug("The selected value was the ignored one.");
157             }
158             throw this.generateException(element, deferExceptions, getErrorMessage() != null ? getErrorMessage() : "The selected value was the ignored one.");
159         }
160         if(selMode != -1 && SelectFormElement.calcSelectionMode(values, selected) != selMode) {
161             if(logger.isDebugEnabled()) {
162                 logger.debug("The selection mode differs.");
163             }
164             throw this.generateException(element, deferExceptions, getErrorMessage() != null ? getErrorMessage() : "The selection mode differs.");
165         }
166     }
167 }
168
Popular Tags