KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > component > FormSupport


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.wcf.component;
14
15 import java.util.HashSet JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Set JavaDoc;
18
19 import org.apache.log4j.Logger;
20
21 import com.tonbeller.wcf.controller.RequestContext;
22
23 /**
24  * default implementation of <code>Form</code>. Ensures that
25  * two different <code>FormSupport</code> instances may listen to
26  * each other without endless recursion.
27  * @see Form
28  * @author av
29  */

30 public class FormSupport implements Form {
31   Set JavaDoc listeners = new HashSet JavaDoc();
32   boolean fireing = false;
33   private static Logger logger = Logger.getLogger(FormSupport.class);
34
35   public FormSupport() {
36   }
37
38   /**
39    * calls validate() on all components
40    */

41   public boolean validate(RequestContext context) {
42     logger.info("enter");
43     // avoid endless recursion
44
if (fireing)
45       return true;
46     fireing = true;
47     try {
48       boolean valid = true;
49       for (Iterator JavaDoc it = listeners.iterator(); it.hasNext();) {
50         FormListener listener = (FormListener) it.next();
51         // make sure to validate all components
52
valid = listener.validate(context) && valid;
53       }
54       return valid;
55     } finally {
56       fireing = false;
57     }
58   }
59
60   /**
61    * calls revert() on all listeners.
62    */

63   public void revert(RequestContext context) {
64     logger.info("enter");
65     if (fireing)
66       return;
67     fireing = true;
68     try {
69       for (Iterator JavaDoc it = listeners.iterator(); it.hasNext();) {
70         FormListener listener = (FormListener) it.next();
71         listener.revert(context);
72       }
73     } finally {
74       fireing = false;
75     }
76   }
77
78   public void addFormListener(FormListener listener) {
79     listeners.add(listener);
80   }
81
82   public void removeFormListener(FormListener listener) {
83     listeners.remove(listener);
84   }
85
86 }
87
Popular Tags