aboutsummaryrefslogtreecommitdiffstats
path: root/docs/dist/doc/examples/bean/BoundPoint.java
blob: c247ca9cd4a1fa1123db7c0e29edfa9aa799c158 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
 * Copyright (c) 1998-2002 Xerox Corporation.  All rights reserved.
 *
 * Use and copying of this software and preparation of derivative works based
 * upon this software are permitted.  Any distribution of this software or
 * derivative works must comply with all applicable United States export
 * control laws.
 *
 * This software is made available AS IS, and Xerox Corporation makes no
 * warranty about the software, its performance or its conformity to any
 * specification.
 */
package bean;

import java.beans.*;
import java.io.Serializable;

/*
 * Add bound properties and serialization to point objects 
 */

aspect BoundPoint {
  /*
   * privately introduce a field into Point to hold the property
   * change support object.  `this' is a reference to a Point object. 
   */
  private PropertyChangeSupport Point.support = new PropertyChangeSupport(this);

  /*
   * Introduce the property change registration methods into Point.
   * also introduce implementation of the Serializable interface.
   */
  public void Point.addPropertyChangeListener(PropertyChangeListener listener){
    support.addPropertyChangeListener(listener);
  }

  public void Point.addPropertyChangeListener(String propertyName, 
                                              PropertyChangeListener listener){
	
    support.addPropertyChangeListener(propertyName, listener);
  }

  public void Point.removePropertyChangeListener(String propertyName, 
                                                 PropertyChangeListener listener) {
    support.removePropertyChangeListener(propertyName, listener);
  }

  public void Point.removePropertyChangeListener(PropertyChangeListener listener) {
    support.removePropertyChangeListener(listener);
  }

  public void Point.hasListeners(String propertyName) {
    support.hasListeners(propertyName);
  }

  declare parents: Point implements Serializable;

  /**
   * Pointcut describing the set<property> methods on Point.
   * (uses a wildcard in the method name)
   */
  pointcut setter(Point p): call(void Point.set*(*)) && target(p);

  /**
   * Advice to get the property change event fired when the
   * setters are called. It's around advice because you need 
   * the old value of the property.
   */
  void around(Point p): setter(p) {
	String propertyName = 
      thisJoinPointStaticPart.getSignature().getName().substring("set".length());
	int oldX = p.getX();
	int oldY = p.getY();
	proceed(p);
	if (propertyName.equals("X")){
      firePropertyChange(p, propertyName, oldX, p.getX());
	} else {
      firePropertyChange(p, propertyName, oldY, p.getY());
	} 
  }

  /*
   * Utility to fire the property change event.
   */
  void firePropertyChange(Point p, 
                          String property, 
                          double oldval, 
                          double newval) {
	p.support.firePropertyChange(property, 
                                 new Double(oldval), 
                                 new Double(newval));
  }
}