01: package org.conform;
02:
03: import org.apache.commons.logging.LogFactory;
04:
05: import javax.swing.event.EventListenerList;
06: import java.util.*;
07:
08: /**
09: * @author hengels
10: * @version $Revision: 812 $
11: */
12: public abstract class AbstractData implements Data {
13: private static org.apache.commons.logging.Log LOG = LogFactory
14: .getLog(Data.class);
15:
16: EventListenerList listenerList = new EventListenerList();
17: boolean issuePublishingActive = false;
18: Set<Issue> bufferedIssues = new HashSet<Issue>();
19:
20: public void addValidationListener(ValidationListener listener) {
21: if (listener == null) {
22: throw new IllegalArgumentException("Listener is null");
23: }
24: listenerList.add(ValidationListener.class, listener);
25: if (isIssuePublishingActive()) {
26: Iterator iter = bufferedIssues.iterator();
27: while (iter.hasNext()) {
28: Issue issue = (Issue) iter.next();
29: listener.addIssues(new ValidationEvent(this , issue));
30: }
31: }
32: }
33:
34: public void removeValidationListener(ValidationListener listener) {
35: listenerList.remove(ValidationListener.class, listener);
36: }
37:
38: public void fireAddIssue(ValidationEvent event) {
39: LOG.debug("addIssues " + event);
40: bufferedIssues.removeAll(event.getIssues());
41: bufferedIssues.addAll(event.getIssues());
42: if (isIssuePublishingActive()) {
43: Object[] listeners = listenerList.getListenerList();
44: for (int i = listeners.length - 2; i >= 0; i -= 2) {
45: if (listeners[i] == ValidationListener.class) {
46: ((ValidationListener) listeners[i + 1])
47: .addIssues(event);
48: }
49: }
50: }
51: }
52:
53: public void fireRemoveIssue(ValidationEvent event) {
54: LOG.debug("removeIssues " + event);
55: bufferedIssues.removeAll(event.getIssues());
56: if (isIssuePublishingActive()) {
57: Object[] listeners = listenerList.getListenerList();
58: for (int i = listeners.length - 2; i >= 0; i -= 2) {
59: if (listeners[i] == ValidationListener.class) {
60: ((ValidationListener) listeners[i + 1])
61: .removeIssues(event);
62: }
63: }
64: }
65: }
66:
67: public void setIssuePublishingActive(boolean issuePublishingActive) {
68: if (this .issuePublishingActive == issuePublishingActive)
69: return;
70:
71: this .issuePublishingActive = issuePublishingActive;
72:
73: ValidationEvent event = null;
74: Object[] listeners = listenerList.getListenerList();
75: for (int i = listeners.length - 2; i >= 0; i -= 2) {
76: if (listeners[i] == ValidationListener.class) {
77: ValidationListener listener = (ValidationListener) listeners[i + 1];
78: listener.clearIssues(getMeta());
79: if (issuePublishingActive) {
80: if (event == null) {
81: event = new ValidationEvent(this );
82: event.addIssues(bufferedIssues);
83: }
84: listener.addIssues(event);
85: }
86: }
87: }
88: }
89:
90: public boolean isIssuePublishingActive() {
91: return issuePublishingActive;
92: }
93:
94: public abstract Meta getMeta();
95:
96: protected abstract Object validate(Object value)
97: throws ValidationException;
98: }
|