aboutsummaryrefslogtreecommitdiffstats
path: root/docs/examples/bean/BoundPoint.java
blob: 36c85819c90e0ff6af87b65b039bb02be2c0e475 (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
/*
 * Copyright (c) 1998-2002 Xerox Corporation,
 *               2004 Contributors.  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 declare a field on Point to hold the property
   * change support object.  `this' is a reference to a Point object.
   */
  private PropertyChangeSupport Point.support = new PropertyChangeSupport(this);

  /*
   * Declare property change registration methods on Point,
   * and 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;

  /**
   * Send property change event after X setter completes normally.
   * Use around advice to keep the old value on the stack.
   */
  void around(Point p): execution(void Point.setX(int)) && target(p) {
      int oldValue = p.getX();
      proceed(p);
      firePropertyChange(p, "x", oldValue, p.getX());
  }

  /**
   * Send property change event after Y setter completes normally.
   * Use around advice to keep the old value on the stack.
   */
  void around(Point p): execution(void Point.setY(int)) && target(p) {
      int oldValue = p.getY();
      proceed(p);
      firePropertyChange(p, "y", oldValue, 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));
  }
}