aboutsummaryrefslogtreecommitdiffstats
path: root/docs/examples/bean
diff options
context:
space:
mode:
Diffstat (limited to 'docs/examples/bean')
-rw-r--r--docs/examples/bean/BoundPoint.java90
-rw-r--r--docs/examples/bean/Demo.java80
-rw-r--r--docs/examples/bean/Point.java69
-rw-r--r--docs/examples/bean/files.lst3
4 files changed, 242 insertions, 0 deletions
diff --git a/docs/examples/bean/BoundPoint.java b/docs/examples/bean/BoundPoint.java
new file mode 100644
index 000000000..36c85819c
--- /dev/null
+++ b/docs/examples/bean/BoundPoint.java
@@ -0,0 +1,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));
+ }
+}
diff --git a/docs/examples/bean/Demo.java b/docs/examples/bean/Demo.java
new file mode 100644
index 000000000..e16be245e
--- /dev/null
+++ b/docs/examples/bean/Demo.java
@@ -0,0 +1,80 @@
+/*
+Copyright (c) Xerox Corporation 1998-2002. 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.
+*/
+
+package bean;
+
+import java.beans.*;
+import java.io.*;
+
+public class Demo implements PropertyChangeListener {
+
+ static final String fileName = "test.tmp";
+
+ /**
+ * when Demo is playing the listener role,
+ * this method reports that a propery has changed
+ */
+ public void propertyChange(PropertyChangeEvent e){
+ System.out.println("Property " + e.getPropertyName() + " changed from " +
+ e.getOldValue() + " to " + e.getNewValue() );
+ }
+
+ /**
+ * main: test the program
+ */
+ public static void main(String[] args){
+ Point p1 = new Point();
+ p1.addPropertyChangeListener(new Demo());
+ System.out.println("p1 =" + p1);
+ p1.setRectangular(5,2);
+ System.out.println("p1 =" + p1);
+ p1.setX( 6 );
+ p1.setY( 3 );
+ System.out.println("p1 =" + p1);
+ p1.offset(6,4);
+ System.out.println("p1 =" + p1);
+ save(p1, fileName);
+ Point p2 = (Point) restore(fileName);
+ System.out.println("Had: " + p1);
+ System.out.println("Got: " + p2);
+ }
+
+ /**
+ * Save a serializable object to a file
+ */
+ static void save(Serializable p, String fn){
+ try {
+ System.out.println("Writing to file: " + p);
+ FileOutputStream fo = new FileOutputStream(fn);
+ ObjectOutputStream so = new ObjectOutputStream(fo);
+ so.writeObject(p);
+ so.flush();
+ } catch (Exception e) {
+ System.out.println(e);
+ System.exit(1);
+ }
+ }
+
+ /**
+ * Restore a serializable object from the file
+ */
+ static Object restore(String fn){
+ try {
+ Object result;
+ System.out.println("Reading from file: " + fn);
+ FileInputStream fi = new FileInputStream(fn);
+ ObjectInputStream si = new ObjectInputStream(fi);
+ return si.readObject();
+ } catch (Exception e) {
+ System.out.println(e);
+ System.exit(1);
+ }
+ return null;
+ }
+}
diff --git a/docs/examples/bean/Point.java b/docs/examples/bean/Point.java
new file mode 100644
index 000000000..a6ea703cd
--- /dev/null
+++ b/docs/examples/bean/Point.java
@@ -0,0 +1,69 @@
+/*
+Copyright (c) Xerox Corporation 1998-2002. 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;
+
+class Point {
+
+ protected int x = 0;
+ protected int y = 0;
+
+ /**
+ * Return the X coordinate
+ */
+ public int getX(){
+ return x;
+ }
+
+ /**
+ * Return the y coordinate
+ */
+ public int getY(){
+ return y;
+ }
+
+ /**
+ * Set the x and y coordinates
+ */
+ public void setRectangular(int newX, int newY){
+ setX(newX);
+ setY(newY);
+ }
+
+ /**
+ * Set the X coordinate
+ */
+ public void setX(int newX) {
+ x = newX;
+ }
+
+ /**
+ * set the y coordinate
+ */
+ public void setY(int newY) {
+ y = newY;
+ }
+
+ /**
+ * Move the point by the specified x and y offset
+ */
+ public void offset(int deltaX, int deltaY){
+ setRectangular(x + deltaX, y + deltaY);
+ }
+
+ /**
+ * Make a string of this
+ */
+ public String toString(){
+ return "(" + getX() + ", " + getY() + ")" ;
+ }
+}
diff --git a/docs/examples/bean/files.lst b/docs/examples/bean/files.lst
new file mode 100644
index 000000000..12ac0eb39
--- /dev/null
+++ b/docs/examples/bean/files.lst
@@ -0,0 +1,3 @@
+BoundPoint.java
+Point.java
+Demo.java