net.xoetrope.xui.data
Class XModel

java.lang.Object
  extended by net.xoetrope.xui.data.XModel
Direct Known Subclasses:
DatabaseFieldModel, DatabaseRowModel, XBaseModel, XServiceModelNode

public abstract class XModel
extends java.lang.Object

    

The XModel is designed to support an MVC like structure. The model allows data to be maintained separately from UI code and separately from the control logic. The model supports an XPath like way of naming nodes and accessing data. The model is hierarchical and therefore a variety of data can be stored including simple scalar values to vectors and arrays. The model can be supplemented by adding custom node types and by using adapters and bindings.

The XModel is one of the main ideas behind XUI and central to providing the clean separation of the data. The XUI UI components can be abstractly bound to the model as instances of XModel although the actual nodes may be implemented by a range of classes representing tables or lists of even more complex nodes. All the UI component need know the path to the node in the model.
In some cases an adapter is used to simplify access to the data and perhaps maintain additional information needed by the UI component (e.g. the selected value in a list).

Copyright (c) Xoetrope Ltd., 1998-2003
License: see license.txt


Field Summary
protected  XModel parentModel
          The parent XModel of this instance.
protected  java.lang.String tagName
          The tagname to be output for this XModel if it is being persisted to an XML structure
 
Constructor Summary
XModel()
           
 
Method Summary
 void addModelListener(XModelListener listener, java.lang.String name)
          Add a new listener to this model node
abstract  java.lang.Object append(java.lang.String elementName)
          Append a new node with the specified name.
abstract  void append(XModel childNode)
          Append a node
 void clear()
          Return this node to its initial state by removing its children and attributes
 void fireModelUpdated()
          Notify the listeners that this model node has changed
abstract  java.lang.Object get()
          gets the value attribute
abstract  XModel get(int i)
          Get the XModel at element i
 java.lang.Object get(java.lang.String element)
          Get the value of the element located at the path in the element parameter If the attribName parameter is not null we get the value of the attributeValues
abstract  java.lang.String getAttribName(int i)
          Retrieve the name of the attribute at the specified index
abstract  int getAttribute(java.lang.String attribName)
          returns the index of the attribiteNames array whose value is the same as the attribName
abstract  java.lang.Object getAttribValue(int i)
          Retrieve the value of the attribute at the specified index and return it.
abstract  double getAttribValueAsDouble(int i)
          Retrieve the value of the attribute at the specified index, convert it to a double and return it.
abstract  int getAttribValueAsInt(int i)
          Retrieve the value of the attribute at the specified index, convert it to an int and return it.
abstract  java.lang.String getAttribValueAsString(int i)
          Retrieve the value of the attribute at the specified index, convert it to a String and return it.
abstract  java.lang.String getId()
          Gets the value of the ID attribute
static XModel getInstance()
          Get the root instance of the model.
 int getNumAttributes()
          Gets the number of attributes of this node
 int getNumChildren()
          Gets the number of immediate children of this node
 XModel getParent()
          Retrieve the parent XModel of this XModel instance.
 java.lang.String getTagName()
          Gets the model element tag name, e.g.
abstract  double getValueAsDouble(java.lang.String elementName)
          Retrieve the named child, convert it's value to a double and return it.
abstract  int getValueAsInt(java.lang.String elementName)
          Retrieve the named child, convert it's value to an int and return it.
abstract  java.lang.String getValueAsString(java.lang.String elementName)
          Retrieve the named child, convert it's value to a String and return it.
 boolean hasAutoId()
          Determine if there is no id set for this model in the datasource
 void hasAutoId(boolean b)
          Used for elements which need a name assigned temporarily because one doesn't exist in the DataSource.
abstract  int hashCode()
          Get a hash code for the node.
static java.lang.String prefixOutputPath(java.lang.String path)
          Prefix the output path if the prefix is not already present
 void removeChildren()
          Remove the children of this node
 XModel reset()
          Reset the whole model, giving a new root node and a new hierarchy 'deprecated since 1.0.3
 void resetAttributes()
          Reset the attributes of this node
abstract  void set(java.lang.Object s)
          Sets the model value
abstract  void set(java.lang.String elementName, java.lang.Object newObject)
          Set the named attribute value of this XModel node.
abstract  void setAttribValue(int i, java.lang.Object value)
          Sets the attribute value
abstract  void setAttribValue(int i, java.lang.String attribName, java.lang.Object value)
          Sets the attribute name and value
 void setId(java.lang.String newId)
          Sets the ID attribute
 void setNumAttributes(int num)
          Setup the attributeNames and attributeValues arrays.
 void setNumChildren(int num)
          Set the number of children of this node
 void setParent(XModel parent)
          Set the XModel parent of this instance
 void setTagName(java.lang.String name)
          Sets the model element tag name, e.g.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

tagName

protected java.lang.String tagName
The tagname to be output for this XModel if it is being persisted to an XML structure


parentModel

protected XModel parentModel
The parent XModel of this instance. null if this instance has no parent

Constructor Detail

XModel

public XModel()
Method Detail

getInstance

public static XModel getInstance()
Get the root instance of the model. This class now delegates to the project manager.

Returns:
the root XModel instance. 'deprecated since 1.0.3

reset

public XModel reset()
Reset the whole model, giving a new root node and a new hierarchy 'deprecated since 1.0.3

Returns:
The base model

clear

public void clear()
Return this node to its initial state by removing its children and attributes


resetAttributes

public void resetAttributes()
Reset the attributes of this node


removeChildren

public void removeChildren()
Remove the children of this node


setTagName

public void setTagName(java.lang.String name)
Sets the model element tag name, e.g. 'Component' from the XML fragment

Parameters:
name - Set the name of the tag which should be set when outputting the XModel

getTagName

public java.lang.String getTagName()
Gets the model element tag name, e.g. 'Component' from the XML fragment

Returns:
the model name

hasAutoId

public boolean hasAutoId()
Determine if there is no id set for this model in the datasource

Returns:
true if there was no name for the element in the DataSource, an example of this is the annonymouse nodes used to represent the record of a table e.g.
... ...

getId

public abstract java.lang.String getId()
Gets the value of the ID attribute

Returns:
the ID attribute

setId

public void setId(java.lang.String newId)
Sets the ID attribute

Parameters:
newId - the new name

get

public java.lang.Object get(java.lang.String element)
Get the value of the element located at the path in the element parameter If the attribName parameter is not null we get the value of the attributeValues

Parameters:
element - The path to the XModel we require
Returns:
The value of the XModel or the attribute

getParent

public XModel getParent()
Retrieve the parent XModel of this XModel instance. Not all models need implement this behavior and the method may possible return null for a valid node.

Returns:
The parent XModel to which this instance belongs

setParent

public void setParent(XModel parent)
Set the XModel parent of this instance

Parameters:
parent - The new XModel parent of this instance

set

public abstract void set(java.lang.String elementName,
                         java.lang.Object newObject)
Set the named attribute value of this XModel node. If the attribName is null then this node's value is updated.

Parameters:
elementName - The path to the XModel in the format 'base/foo
newObject - The new value of the XModel

getAttribute

public abstract int getAttribute(java.lang.String attribName)
returns the index of the attribiteNames array whose value is the same as the attribName

Parameters:
attribName - The name of the attribute we are trying to locate
Returns:
The index of the attributeNames array containg the name

setAttribValue

public abstract void setAttribValue(int i,
                                    java.lang.Object value)
Sets the attribute value

Parameters:
i - The index of the attributeValues array whose value we want
value - the value object

setAttribValue

public abstract void setAttribValue(int i,
                                    java.lang.String attribName,
                                    java.lang.Object value)
Sets the attribute name and value

Parameters:
i - The index of the attributeValues array whose value we want
attribName - the name of the attribute
value - the value object

get

public abstract XModel get(int i)
Get the XModel at element i

Parameters:
i - The index of the values array
Returns:
The XModel at location i

get

public abstract java.lang.Object get()
gets the value attribute

Returns:
the value of the model

set

public abstract void set(java.lang.Object s)
Sets the model value

Parameters:
s - the new value

hasAutoId

public void hasAutoId(boolean b)
Used for elements which need a name assigned temporarily because one doesn't exist in the DataSource.

Parameters:
b - true if there was no name in the DataSource

getAttribName

public abstract java.lang.String getAttribName(int i)
Retrieve the name of the attribute at the specified index

Parameters:
i - The index of the attributeNames array whose value we want
Returns:
The string value of the attributeNames array at position i

getAttribValueAsString

public abstract java.lang.String getAttribValueAsString(int i)
Retrieve the value of the attribute at the specified index, convert it to a String and return it.

Parameters:
i - The index of the attributeValues array whose value we want
Returns:
The string value of the attributeValues array at position i

getAttribValue

public abstract java.lang.Object getAttribValue(int i)
Retrieve the value of the attribute at the specified index and return it.

Parameters:
i - The index of the attributeValues array whose value we want
Returns:
The string value of the attributeValues array at position i

getAttribValueAsDouble

public abstract double getAttribValueAsDouble(int i)
Retrieve the value of the attribute at the specified index, convert it to a double and return it.

Parameters:
i - The index of the attributeValues array whose value we want
Returns:
The double value of the attributeValues array at position i

getAttribValueAsInt

public abstract int getAttribValueAsInt(int i)
Retrieve the value of the attribute at the specified index, convert it to an int and return it.

Parameters:
i - The index of the attributeValues array whose value we want
Returns:
The int value of the attributeValues array at position i

getValueAsDouble

public abstract double getValueAsDouble(java.lang.String elementName)
Retrieve the named child, convert it's value to a double and return it.

Parameters:
elementName - The name of the child whose value is required
Returns:
the value as a double

getValueAsInt

public abstract int getValueAsInt(java.lang.String elementName)
Retrieve the named child, convert it's value to an int and return it.

Parameters:
elementName - The name of the child to be retrieved
Returns:
the value as an int

getValueAsString

public abstract java.lang.String getValueAsString(java.lang.String elementName)
Retrieve the named child, convert it's value to a String and return it.

Parameters:
elementName - The name of the child whose value is required
Returns:
the value as a string

hashCode

public abstract int hashCode()
Get a hash code for the node.

Overrides:
hashCode in class java.lang.Object
Returns:
the node's hash code

getNumChildren

public int getNumChildren()
Gets the number of immediate children of this node

Returns:
the number of child nodes

getNumAttributes

public int getNumAttributes()
Gets the number of attributes of this node

Returns:
the number of attributes

setNumChildren

public void setNumChildren(int num)
Set the number of children of this node

Parameters:
num - the new number of children

append

public abstract void append(XModel childNode)
Append a node

Parameters:
childNode - the child node

append

public abstract java.lang.Object append(java.lang.String elementName)
Append a new node with the specified name. This method does not replace any existing nodes.

Parameters:
elementName - The immediate path to the XModel required
Returns:
The value of the XModel or the attribute

setNumAttributes

public void setNumAttributes(int num)
Setup the attributeNames and attributeValues arrays. If not already initialised set the size of each to 2 otherwise store them temporarily and reassign to the increased size arrays.

Parameters:
num - The new size of the array

addModelListener

public void addModelListener(XModelListener listener,
                             java.lang.String name)
Add a new listener to this model node

Parameters:
listener - the listener object
name - the name of the metod to be invoked

fireModelUpdated

public void fireModelUpdated()
Notify the listeners that this model node has changed


prefixOutputPath

public static java.lang.String prefixOutputPath(java.lang.String path)
Prefix the output path if the prefix is not already present

Parameters:
path - The path to which the prefix will be applied
Returns:
The prefixed path