001: package org.osbl.client.wings.form;
002:
003: import java.text.MessageFormat;
004: import java.util.*;
005:
006: import org.conform.*;
007: import org.osbl.client.wings.shell.Client;
008: import org.osbl.client.wings.shell.Environment;
009:
010: import javax.swing.event.*;
011:
012: /**
013: * @author hengels
014: * @version $Revision$
015: */
016: public class ValidationStatus implements ValidationListener {
017: Environment environment;
018:
019: Set<Issue> issues = new HashSet<Issue>();
020: protected Client.Message message = new Client.Message();
021: StringBuilder detail = new StringBuilder();
022:
023: protected EventListenerList listenerList = new EventListenerList();
024:
025: public ValidationStatus(Environment environment) {
026: this ();
027: this .environment = environment;
028: }
029:
030: public void setEnvironment(Environment environment) {
031: this .environment = environment;
032: updateStatusMessage();
033: }
034:
035: public ValidationStatus() {
036: message.setType(Client.Message.TYPE_ERROR);
037: message.setMessage(Client.getInstance().getResourceProvider()
038: .getMessage("validation.issues"));
039: }
040:
041: public ValidationStatus(String label) {
042: message.setType(Client.Message.TYPE_ERROR);
043: message.setMessage(label
044: + ": "
045: + Client.getInstance().getResourceProvider()
046: .getMessage("validation.issues"));
047: }
048:
049: public void addIssues(ValidationEvent event) {
050: // add or replace
051: issues.removeAll(event.getIssues());
052: issues.addAll(event.getIssues());
053: updateStatusMessage();
054: }
055:
056: public void removeIssues(ValidationEvent event) {
057: issues.removeAll(event.getIssues());
058: updateStatusMessage();
059: }
060:
061: public static String composeMessageDetail(StringBuilder detail,
062: Collection issues) {
063: detail.setLength(0);
064: for (Iterator<Issue> iterator = issues.iterator(); iterator
065: .hasNext();) {
066: Issue issue = iterator.next();
067: PropertyMeta[] metas = issue.getMetas();
068:
069: if (metas == null) {
070: } else if (metas.length == 1) {
071: label(detail, metas[0]);
072: detail.append(":\n");
073: } else {
074: BeanMeta beanMeta = metas[0].getBeanMeta();
075: label(detail, beanMeta);
076: detail.append(" (");
077: for (int i = 0; i < metas.length; i++) {
078: if (i > 0)
079: detail.append(", ");
080: label(detail, metas[i]);
081: }
082: detail.append("):\n");
083: }
084:
085: String code = issue.getCode();
086: Object[] arguments = issue.getArguments();
087:
088: if (code == null)
089: code = "validation.incorrect";
090:
091: code = Client.getInstance().getResourceProvider()
092: .getMessage(code);
093:
094: if (arguments == null)
095: detail.append(" - ").append(code);
096: else
097: detail.append(" - ").append(
098: MessageFormat.format(code, arguments));
099:
100: detail.append('\n');
101: }
102: return detail.toString();
103: }
104:
105: private static void label(StringBuilder detail, Meta meta) {
106: String label = meta.getLabel();
107: if (label == null) {
108: if (meta instanceof PropertyMeta) {
109: String name = meta.getName();
110: meta = ((PropertyMeta) meta).getBeanMeta();
111: meta = Client.getInstance().getBeanMetaProvider()
112: .getBeanMeta(meta.getType());
113: meta = ((BeanMeta) meta).getProperty(name);
114: } else {
115: meta = Client.getInstance().getBeanMetaProvider()
116: .getBeanMeta(meta.getType());
117: }
118: label = meta.getLabel();
119: }
120: detail.append(label != null ? label : meta.getName());
121: }
122:
123: public void updateStatusMessage() {
124: if (issues.size() != 0) {
125: message.setDetail(composeMessageDetail(detail, issues));
126:
127: if (environment != null)
128: environment.setStatusMessage(message);
129: else
130: Client.getInstance().showMessage(message);
131: } else {
132: if (environment != null)
133: environment.setStatusMessage(null);
134: else
135: Client.getInstance().hideMessage(null);
136: }
137: fireChange();
138: }
139:
140: public Set<Issue> getIssues() {
141: return issues;
142: }
143:
144: public void clearIssues(Meta meta) {
145: for (Iterator<Issue> iterator = issues.iterator(); iterator
146: .hasNext();) {
147: Issue issue = iterator.next();
148: PropertyMeta[] issueMetas = issue.getMetas();
149: for (PropertyMeta issueMeta : issueMetas) {
150: if (issueMeta.getBeanMeta() == meta) {
151: iterator.remove();
152: break;
153: }
154: }
155: }
156:
157: updateStatusMessage();
158: }
159:
160: public void addChangeListener(ChangeListener listener) {
161: listenerList.add(ChangeListener.class, listener);
162: }
163:
164: public void removeChangeListener(ChangeListener listener) {
165: listenerList.remove(ChangeListener.class, listener);
166: }
167:
168: public ChangeListener[] getChangeListeners() {
169: return listenerList.getListeners(ChangeListener.class);
170: }
171:
172: public final ChangeEvent event = new ChangeEvent(this );
173:
174: protected void fireChange() {
175: Object[] listeners = listenerList.getListenerList();
176: for (int i = listeners.length - 2; i >= 0; i -= 2) {
177: if (listeners[i] == ChangeListener.class) {
178: ((ChangeListener) listeners[i + 1]).stateChanged(event);
179: }
180: }
181: }
182: }
|