001: /*
002: * $Id: PropertyChangeReport.java,v 1.1 2006/11/01 16:23:07 kleopatra Exp $
003: *
004: * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
005: * Santa Clara, California 95054, U.S.A. All rights reserved.
006: */
007:
008: package org.jdesktop.test;
009:
010: import java.beans.PropertyChangeEvent;
011: import java.beans.PropertyChangeListener;
012: import java.util.HashMap;
013: import java.util.Iterator;
014: import java.util.LinkedList;
015: import java.util.List;
016: import java.util.Map;
017:
018: /**
019: * A PropertyChangeListener that stores the received PropertyChangeEvents.
020: *
021: * modified ("beanified") from JGoodies PropertyChangeReport.
022: *
023: */
024: public class PropertyChangeReport implements PropertyChangeListener {
025:
026: /**
027: * Holds a list of all received PropertyChangeEvents.
028: */
029: protected List events = new LinkedList();
030: protected Map eventMap = new HashMap();
031:
032: //------------------------ implement PropertyChangeListener
033:
034: public void propertyChange(PropertyChangeEvent evt) {
035: events.add(0, evt);
036: if (evt.getPropertyName() != null) {
037: eventMap.put(evt.getPropertyName(), evt);
038: }
039: }
040:
041: public int getEventCount() {
042: return events.size();
043: }
044:
045: public void clear() {
046: events.clear();
047: eventMap.clear();
048: }
049:
050: public boolean hasEvents() {
051: return !events.isEmpty();
052: }
053:
054: public int getEventCount(String property) {
055: if (property == null)
056: return getMultiCastEventCount();
057: int count = 0;
058: for (Iterator iter = events.iterator(); iter.hasNext();) {
059: PropertyChangeEvent event = (PropertyChangeEvent) iter
060: .next();
061: if (property.equals(event.getPropertyName())) {
062: count++;
063: }
064: }
065: return count;
066: }
067:
068: public boolean hasEvents(String property) {
069: return eventMap.get(property) != null;
070: }
071:
072: public int getMultiCastEventCount() {
073: int count = 0;
074: for (Iterator i = events.iterator(); i.hasNext();) {
075: PropertyChangeEvent event = (PropertyChangeEvent) i.next();
076: if (event.getPropertyName() == null)
077: count++;
078: }
079: return 0;
080: }
081:
082: public int getNamedEventCount() {
083: return getEventCount() - getMultiCastEventCount();
084: }
085:
086: public PropertyChangeEvent getLastEvent() {
087: return events.isEmpty() ? null : (PropertyChangeEvent) events
088: .get(0);
089: }
090:
091: public PropertyChangeEvent getLastEvent(String property) {
092: return (PropertyChangeEvent) eventMap.get(property);
093: }
094:
095: public Object getLastOldValue() {
096: PropertyChangeEvent last = getLastEvent();
097: return last != null ? last.getOldValue() : null;
098: }
099:
100: public Object getLastNewValue() {
101: PropertyChangeEvent last = getLastEvent();
102: return last != null ? last.getNewValue() : null;
103: }
104:
105: public Object getLastOldValue(String property) {
106: PropertyChangeEvent last = getLastEvent(property);
107: return last != null ? last.getOldValue() : null;
108: }
109:
110: public Object getLastNewValue(String property) {
111: PropertyChangeEvent last = getLastEvent(property);
112: return last != null ? last.getNewValue() : null;
113: }
114:
115: public boolean getLastOldBooleanValue() {
116: return ((Boolean) getLastOldValue()).booleanValue();
117: }
118:
119: public boolean getLastNewBooleanValue() {
120: return ((Boolean) getLastNewValue()).booleanValue();
121: }
122:
123: }
|