org.formaria.aria.data
Class DataModel

java.lang.Object
  extended by org.formaria.aria.data.DataModel
Direct Known Subclasses:
BaseModel, FieldModel, FormInfoTreeTableAdapter, PojoModel, PojoModelVis, RowModel, VisualiserDebuggerModel

public abstract class DataModel
extends java.lang.Object

The DataModel 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 Path 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 DataModel is one of the main ideas behind Aria and central to providing the clean separation of the data. The Aria UI components can be abstractly bound to the model as instances of DataModel 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) Formaria Ltd., 2008
License: see license.txt


Field Summary
protected  DataModel parentModel
          The parent DataModel of this instance.
protected  java.lang.String tagName
          The tagname to be output for this DataModel if it is being persisted to an XML structure
 
Constructor Summary
DataModel()
           
 
Method Summary
 void addModelListener(ModelListener listener, java.lang.String name)
          Add a new listener to this model node
abstract  void append(DataModel childNode)
          Append a node
abstract  java.lang.Object append(java.lang.String elementName)
          Append a new node with the specified name.
 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  DataModel get(int i)
          Get the DataModel 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)
          Deprecated. use getAttribValueAsDouble( i, decimalSeparator, groupingSeparator ) instead, if the locale is different from the locale used to write the values to the model, then the parsed value may be incorrect.
abstract  double getAttribValueAsDouble(int i, char decimalSeparator, char groupingSeparator)
          Convert the attribute at the specified index 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
 int getNumAttributes()
          Gets the number of attributes of this node
 int getNumChildren()
          Gets the number of immediate children of this node
 DataModel getParent()
          Retrieve the parent DataModel of this DataModel 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
 DataModel reset(Project currentProject)
          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 DataModel 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(DataModel parent)
          Set the DataModel 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 DataModel if it is being persisted to an XML structure


parentModel

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

Constructor Detail

DataModel

public DataModel()
Method Detail

reset

public DataModel reset(Project currentProject)
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 DataModel

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 DataModel we require
Returns:
The value of the DataModel or the attribute

getParent

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

Returns:
The parent DataModel to which this instance belongs

setParent

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

Parameters:
parent - The new DataModel parent of this instance

set

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

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

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 DataModel get(int i)
Get the DataModel at element i

Parameters:
i - The index of the values array
Returns:
The DataModel 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)
Deprecated. use getAttribValueAsDouble( i, decimalSeparator, groupingSeparator ) instead, if the locale is different from the locale used to write the values to the model, then the parsed value may be incorrect.

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

getAttribValueAsDouble

public abstract double getAttribValueAsDouble(int i,
                                              char decimalSeparator,
                                              char groupingSeparator)
Convert the attribute at the specified index to a double and return it

Parameters:
i - The index of the attributeValues array whose value we want
decimalSeparator - the decimal separator
groupingSeparator - the grouping (thousands) separator
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(DataModel 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 DataModel required
Returns:
The value of the DataModel 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(ModelListener 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