KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > frontend > editor > PartRequiredValidator


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.frontend.editor;
17
18 import org.apache.cocoon.forms.validation.WidgetValidator;
19 import org.apache.cocoon.forms.validation.ValidationError;
20 import org.apache.cocoon.forms.formmodel.Widget;
21 import org.apache.cocoon.forms.formmodel.Field;
22 import org.outerj.daisy.frontend.HtmlHelper;
23
24 /**
25  * Validates that the part has a value, if required & if validation enabled.
26  */

27 class PartRequiredValidator implements WidgetValidator {
28     private DocumentEditorForm documentEditorForm;
29     private boolean isRequired;
30     private boolean htmlMode;
31
32     public PartRequiredValidator(DocumentEditorForm documentEditorForm, boolean isRequired, boolean htmlMode) {
33         this.documentEditorForm = documentEditorForm;
34         this.isRequired = isRequired;
35         this.htmlMode = htmlMode;
36     }
37
38     public boolean validate(Widget widget) {
39         boolean success = true;
40         if (documentEditorForm.getValidateOnSave() && isRequired) {
41             if (htmlMode) {
42                 if (HtmlHelper.isEmpty((String JavaDoc)widget.getValue())) {
43                     ((Field)widget).setValidationError(new ValidationError("editdoc.part-required", true));
44                     success = false;
45                 }
46             } else {
47                 if (widget.validate() && widget.getValue() == null) {
48                     ((Field)widget).setValidationError(new ValidationError("editdoc.part-required", true));
49                     success = false;
50                 }
51             }
52         }
53         return success;
54     }
55 }
56
Popular Tags