[Javaclient-cvs] javaclient/net/sf/javaclient/model CallbackModel.java,NONE,1.1 ListModel.java,1.1,1
Status: Alpha
Brought to you by:
rimmeraj
|
From: Dave S. <rim...@us...> - 2004-10-08 03:15:01
|
Update of /cvsroot/javaclient/javaclient/net/sf/javaclient/model In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv11774/net/sf/javaclient/model Modified Files: ListModel.java Model.java Added Files: CallbackModel.java Log Message: Added tests for basic model handling and list handling. Now we need to handle the callbacks and updates from the List Index: Model.java =================================================================== RCS file: /cvsroot/javaclient/javaclient/net/sf/javaclient/model/Model.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** Model.java 7 Oct 2004 20:41:01 -0000 1.1 --- Model.java 8 Oct 2004 03:14:45 -0000 1.2 *************** *** 3,10 **** import java.util.HashMap; import java.util.ArrayList; import java.beans.PropertyVetoException; public class Model ! implements ObjectReplication { public Model(Class aModelClass) --- 3,14 ---- import java.util.HashMap; import java.util.ArrayList; + import java.util.Iterator; import java.beans.PropertyVetoException; + import java.lang.reflect.Method; + import java.lang.reflect.InvocationHandler; + import java.lang.reflect.Proxy; public class Model ! implements ObjectReplication,InvocationHandler { public Model(Class aModelClass) *************** *** 12,43 **** modelClass = aModelClass; values = new HashMap(); ! lists = new ArrayList(); ! parent=null; } ! public void addListClass(String attribute,Model m) ! { ! m.parent = this; ! ListModel list = new ListModel(m); ! lists.add(list); ! values.put(attribute,lists); ! } ! ! public Object getValue(String attribute) { ! return(values.get(attribute)); } ! public void setValue(String attribute,Object value) { ! values.put(attribute,value); } public HashMap getChangeEvents() { } public void updateModel(HashMap changed) { } public void setServerId(Long aServerId) { --- 16,97 ---- modelClass = aModelClass; values = new HashMap(); ! changeEvent = new HashMap(); } + public Object getProxy() + { + return(Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[] {modelClass,ObjectReplication.class},this)); + } ! public Object invoke(Object proxy, Method method, Object[] args) ! throws Throwable ! { ! String name = method.getName(); ! if(name.startsWith("get") && args == null) ! { ! return(getProperty(name.substring(3))); ! } ! else if(name.startsWith("set") && args.length == 1) ! { ! setProperty(name.substring(3),args[0]); ! return(null); ! } ! return(null); ! } ! public Object getProperty(String property) { ! return(values.get(property)); } ! ! public void setProperty(String property,Object value) ! throws PropertyVetoException { ! Object oldVal = values.get(property); ! if(value == null && oldVal == null) ! { ! return; ! } ! if(value != null && value.equals(oldVal)) ! { ! return; ! } ! if(!isUpdate) ! { ! firePropertyVetoChange(property,oldVal,value); ! } ! values.put(property,value); ! changeEvent.put(property,value); ! firePropertyChange(property,oldVal,value); } public HashMap getChangeEvents() { + HashMap returnVal = changeEvent; + changeEvent = new HashMap(); + return(returnVal); } + public void updateModel(HashMap changed) { + isUpdate=true; + Iterator it = changed.keySet().iterator(); + while(it.hasNext()) + { + String key = (String)it.next(); + try + { + setProperty(key,changed.get(key)); + } + catch(PropertyVetoException err) + { + System.out.println("This should not happen"); + err.printStackTrace(); + } + } + changeEvent = new HashMap(); + isUpdate=false; } + + public void setServerId(Long aServerId) { *************** *** 56,71 **** clientId=aClientId; } - // new<Attibute>Item() - // new<Attibute>Item(int pos) - // delete<Attribute>Item(int pos) - // clear<Attribute>Item() - // size<Attribute>Item() - // get<Attribute>Item(int pos) ! private HashMap values; private Class modelClass; - private Model parent; - private ArrayList lists; private Long serverId,clientId; } --- 110,121 ---- clientId=aClientId; } ! public void firePropertyVetoChange(String property,Object oldVal,Object newVal) throws PropertyVetoException {} ! public void firePropertyChange(String property,Object oldVal,Object newVal) {} ! ! private HashMap values,changeEvent; private Class modelClass; private Long serverId,clientId; + private boolean isUpdate=false; } --- NEW FILE: CallbackModel.java --- package net.sf.javaclient.model; import java.lang.reflect.Method; import java.lang.reflect.InvocationTargetException; import java.beans.PropertyVetoException; import java.util.HashMap; import java.util.ArrayList; import java.util.Iterator; public class CallbackModel extends Model { public CallbackModel(Class aModelClass) { super(aModelClass); changeListeners = new HashMap(); vetoableChangeListeners = new HashMap(); } public void addPropertyChangeListener(String property,Object target,Method method) { ArrayList l = (ArrayList)changeListeners.get(property); if(l == null) { l = new ArrayList(); changeListeners.put(property,l); } l.add(new ListenEvent(method,target)); } public void addVetoableChangeListener(String property,Object target,Method method) { ArrayList l = (ArrayList)vetoableChangeListeners.get(property); if(l == null) { l = new ArrayList(); vetoableChangeListeners.put(property,l); } l.add(new ListenEvent(method,target)); } public void firePropertyVetoChange(String property,Object oldVal,Object newVal) throws PropertyVetoException { ArrayList l = (ArrayList)vetoableChangeListeners.get(property); if(l == null) { return; } Iterator it = l.iterator(); while(it.hasNext()) { ((ListenEvent)it.next()).invoke(oldVal,newVal); } } public void firePropertyChange(String property,Object oldVal,Object newVal) { ArrayList l = (ArrayList)changeListeners.get(property); if(l == null) { return; } try { Iterator it = l.iterator(); while(it.hasNext()) { ((ListenEvent)it.next()).invoke(oldVal,newVal); } } catch(Exception e) { e.printStackTrace(); } } private static class ListenEvent { public ListenEvent(Method aMethod,Object aTarget) { method = aMethod; target = aTarget; } public void invoke(Object oldVal,Object newVal) throws PropertyVetoException { try { method.invoke(target,new Object[] {oldVal,newVal}); } catch(InvocationTargetException e) { if(e.getTargetException() instanceof PropertyVetoException) { throw (PropertyVetoException)e.getTargetException(); } e.printStackTrace(); } catch(Exception err) { err.printStackTrace(); } } private Method method; private Object target; } private HashMap changeListeners,vetoableChangeListeners; } Index: ListModel.java =================================================================== RCS file: /cvsroot/javaclient/javaclient/net/sf/javaclient/model/ListModel.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** ListModel.java 7 Oct 2004 20:41:00 -0000 1.1 --- ListModel.java 8 Oct 2004 03:14:45 -0000 1.2 *************** *** 1,33 **** package net.sf.javaclient.model; public class ListModel { ! public ListModel(Model aListType) { } public Object newListItem() { ! return(null); } public Object newListItem(int pos) { ! return(null); } ! public void delete(int pos) { } public void clear() { } public int size() { ! return(0); } public Object get(int pos) { ! return(null); } ! // needs to note changes ! // new Model needs to know parent } --- 1,43 ---- package net.sf.javaclient.model; + import java.util.ArrayList; + public class ListModel { ! public ListModel(Class aListClass) { + list = new ArrayList(); + listClass = aListClass; } public Object newListItem() { ! return(newListItem(list.size())); } public Object newListItem(int pos) { ! Object o = (new Model(listClass)).getProxy(); ! list.add(pos,o); ! return(o); } ! public void remove(int pos) { + list.remove(pos); } public void clear() { + list.clear(); } public int size() { ! return(list.size()); } public Object get(int pos) { ! return(list.get(pos)); } ! ! private ArrayList list; ! private Class listClass; } + |