Package com.peterhi.almostworking

Source Code of com.peterhi.almostworking.ObjectTest$Person

package com.peterhi.almostworking;

import com.peterhi.almostworking.ObjectModel;
import com.peterhi.almostworking.ObjectModelPropertiesSupport;
import com.peterhi.almostworking.ObjectModelPropertyListener;
import com.peterhi.almostworking.ObjectModelPropertyValueEvent;
import com.peterhi.almostworking.ObjectModelPropertyValueListener;
import com.peterhi.almostworking.ObjectModelUtilities;

public class ObjectTest {

  public static interface Person extends ObjectModel {
    String PROPERTY_NAME = "name";
   
    String getName();
    void setName(String newName);
  }
 
  public static class Pet implements ObjectModel {
    public static final String PROPERTY_NAME = "name";
    public static final String PROPERTY_AGE = "age";
   
    private final ObjectModelPropertiesSupport support;
    private String name;
    private int age;
   
    public Pet() {
      support = new ObjectModelPropertiesSupport(this);
    }
   
    public String getName() {
      return name;
    }
   
    public void setName(String newName) {
      String oldName = name;
      newName = (String )support.fireValueChanging(PROPERTY_NAME, oldName, newName);
      name = newName;
      support.fireValueChanged(PROPERTY_NAME, oldName, newName);
    }
   
    public int getAge() {
      return age;
    }
   
    public void setAge(int newAge) {
      int oldAge = age;
      newAge = ((Integer )support.fireValueChanging(PROPERTY_AGE, new Integer(oldAge), new Integer(newAge))).intValue();
      age = newAge;
      support.fireValueChanged(PROPERTY_AGE, new Integer(oldAge), new Integer(newAge));
    }
   
    public void addPropertyChangeListener(String propertyName, ObjectModelPropertyListener listener) {
      support.addListener(propertyName, listener);
    }
   
    public void removePropertyChangeListener(String propertyName, ObjectModelPropertyListener listener) {
      support.removeListener(propertyName, listener);
    }
  }
 
  public static void main(String[] args) {
    Person person = (Person )ObjectModelUtilities.createLocalObjectModel(Person.class);
    System.out.println(person);
    System.out.println(person.equals(person));
    System.out.println(person.equals(new Object()));
   
    ObjectModelUtilities.addPropertyListener(person, "name", new ObjectModelPropertyValueListener() {
      @Override
      public void valueChanging(ObjectModelPropertyValueEvent event) {
        System.out.println("changing " + event.getProperty() + " from " + event.getOldValue() + " to " + event.getNewValue());
        event.setNewValue("HAHA!");
      }
      @Override
      public void valueChanged(ObjectModelPropertyValueEvent event) {
        System.out.println("changed " + event.getProperty() + " from " + event.getOldValue() + " to " + event.getNewValue());
      }
    });
   
    ObjectModelUtilities.setPropertyValue(person, "name", "Jason");
    System.out.println(ObjectModelUtilities.getPropertyValue(person, "name"));

    Pet pet = new Pet();
    ObjectModelUtilities.addPropertyListener(pet, "age", new ObjectModelPropertyValueListener() {
      @Override
      public void valueChanging(ObjectModelPropertyValueEvent event) {
        System.out.println("changing " + event.getProperty() + " from " + event.getOldValue() + " to " + event.getNewValue());
        event.setNewValue(new Integer(6));
      }
      @Override
      public void valueChanged(ObjectModelPropertyValueEvent event) {
        System.out.println("changed " + event.getProperty() + " from " + event.getOldValue() + " to " + event.getNewValue());
      }
    });
   
    pet.setAge(4);
    System.out.println(pet.getAge());
  }
 
}
TOP

Related Classes of com.peterhi.almostworking.ObjectTest$Person

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.