001: package org.osbl.client.wings.form;
002:
003: import org.conform.*;
004: import org.osbl.client.wings.shell.Environment;
005:
006: import java.beans.PropertyChangeListener;
007: import java.beans.PropertyChangeEvent;
008: import java.util.*;
009:
010: /**
011: * Created by IntelliJ IDEA.
012: * User: hengels
013: * Date: Aug 8, 2006
014: * Time: 12:30:12 PM
015: * To change this template use File | Settings | File Templates.
016: */
017: public class ChangeStatus implements PropertyChangeListener {
018: Environment environment;
019:
020: boolean active;
021: Map<String, PropertyChangeEvent> changes = new HashMap<String, PropertyChangeEvent>();
022: StringBuilder detail = new StringBuilder();
023:
024: public ChangeStatus() {
025: }
026:
027: public ChangeStatus(Environment environment) {
028: this .environment = environment;
029: }
030:
031: public void propertyChange(PropertyChangeEvent evt) {
032: if (!active)
033: return;
034:
035: BeanData source = (BeanData) evt.getSource();
036: if (changes.put(source.getBeanMeta().getName() + "."
037: + evt.getPropertyName(), evt) == null
038: && environment != null)
039: environment.setHasChanges(true);
040: }
041:
042: public boolean hasChanges() {
043: return changes.size() > 0;
044: }
045:
046: public Map<String, PropertyChangeEvent> getChanges() {
047: return changes;
048: }
049:
050: public void clearChanges() {
051: boolean cleared = changes.size() > 0;
052: changes.clear();
053: if (cleared && environment != null)
054: environment.setHasChanges(false);
055: }
056:
057: public void setActive(boolean active) {
058: this .active = active;
059: if (environment != null)
060: environment.setHasChanges(changes.size() > 0);
061: }
062:
063: public String composeMessageDetail() {
064: detail.setLength(0);
065:
066: List<PropertyChangeEvent> changes = new ArrayList<PropertyChangeEvent>(
067: this .changes.values());
068: Collections.sort(changes,
069: new Comparator<PropertyChangeEvent>() {
070: public int compare(PropertyChangeEvent o1,
071: PropertyChangeEvent o2) {
072: BeanData bd1 = (BeanData) o1.getSource();
073: BeanData bd2 = (BeanData) o2.getSource();
074: return bd1.getBeanMeta().getName().compareTo(
075: bd2.getBeanMeta().getName());
076: }
077: });
078:
079: BeanData lastBeanData = null;
080: for (PropertyChangeEvent change : changes) {
081: BeanData beanData = (BeanData) change.getSource();
082: BeanMeta beanMeta = beanData.getBeanMeta();
083: PropertyMeta propertyMeta = beanMeta.getProperty(change
084: .getPropertyName());
085:
086: if (lastBeanData != beanData) {
087: lastBeanData = beanData;
088: String label = beanMeta.getLabel();
089: detail.append(label != null ? label : beanMeta
090: .getName());
091: detail.append(":\n");
092: }
093:
094: String label = propertyMeta.getLabel();
095: detail.append(" - ");
096: detail.append(label != null ? label : propertyMeta
097: .getName());
098: detail.append("\n");
099: }
100: return detail.toString();
101: }
102: }
|