org.formaria.aria.data
Class BaseModel

java.lang.Object
  extended by org.formaria.aria.data.DataModel
      extended by org.formaria.aria.data.BaseModel
All Implemented Interfaces:
java.lang.Cloneable
Direct Known Subclasses:
TableModel

public class BaseModel
extends DataModel
implements java.lang.Cloneable

The basic implementation of the DataModel is handled by this class. Static data is loaded from an XML file pointed to by the startup.properties file by default using an DataSource. The model is central to Aria and allows the UI components to be separated from the data in an abstract fashion. This model element is designed to support static data, text, list and tabular structures.

Copyright (c) Formaria Ltd., 2008
License: see license.txt


Field Summary
protected  boolean addByDefault
          If set, paths not found will automatically be allocated
protected static boolean appendByDefault
          append DataModel elements which are not found by default.
protected  java.lang.String[] attributeNames
           
protected  java.lang.Object[] attributeValues
           
static int ID_ATTRIBUTE
           
static int NUM_FIXED_ATTRIBUTE
           
static int VALUE_ATTRIBUTE
           
protected  DataModel[] values
          array of Object values
 
Fields inherited from class org.formaria.aria.data.DataModel
parentModel, tagName
 
Constructor Summary
BaseModel()
          null ctor.
BaseModel(DataModel parent)
          Constructs an instance of the model node.
BaseModel(DataModel parent, java.lang.String id, java.lang.Object value)
          Constructor which sets the id and value attributes and appends this DataModel to the parent model.
 
Method Summary
 void append(DataModel childNode)
          Appends a node to the model.
 java.lang.Object append(java.lang.String elementName)
          Append a new node with the specified name.
 void appendAttribute(java.lang.String name, java.lang.Object value)
          Append an attribute, creating a new attribute slot if necessary
 java.lang.Object get()
          gets the value attribute
 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.
 boolean getAddByDefault()
          Get the flags that determines if attributes are added when queried
static boolean getAppendByDefault()
          Get the flags that determines if attributes are added when queried
static java.lang.String getAttribFromPath(java.lang.String path)
          Get the attribute from a path e.g.
 java.lang.String getAttribName(int i)
          Set the name of the attribute located at the specified index
 int getAttribute(java.lang.String attribName)
          Get the index of the attribiteNames array whose value is the same as the attribName
 java.lang.Object getAttribValue(int i)
          Retrive the value of the attribute at the specified index
 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.
 double getAttribValueAsDouble(int i, char decimalSeparator, char groupingSeparator)
          Convert the attribute at the specified index to a double and return it
 int getAttribValueAsInt(int index)
          Convert the attribute at the specified index to an int and return it.
 java.lang.String getAttribValueAsString(int i)
          Convert the attribute at the specified index to a String and return it
static java.lang.String getBaseFromPath(java.lang.String path)
          Get the base path from a path e.g.
 boolean getChildExists(java.lang.String name)
          Check to see if the specified child node exists.
 java.lang.String getId()
          Gets the name attribute
 DataModel getModel(java.lang.String elementName)
          Gets the value attribute of the specified node as an DataModel.
 int getNumAttributes()
          Gets the number of attributes of this node
 int getNumChildren()
          Gets the number of immediate children of this node
 double getValueAsDouble(java.lang.String elementName)
          Gets the value attribute as a Double value
 int getValueAsInt(java.lang.String elementName)
          Gets the value attribute of the specified node as an int.
 java.lang.String getValueAsString(java.lang.String elementName)
          Gets the value attribute of the specified node as a string.
 boolean hasAutoId()
          Determine if the element needs a name assigned temporarily because one doesn't exist in the DataSource.
 void hasAutoId(boolean b)
          Used for elements which need a name assigned temporarily because one doesn't exist in the DataSource.
 int hashCode()
          The hashcode of this instance.
 void insertChildAt(DataModel newNode, int idx)
          Insert a node at a specified index in the list of children
 void remove(DataModel child)
          Remove a child node from this DataModel instance.
 void removeAttributes()
          Remove the nodes attributes and attribute names
 void removeChild(java.lang.String name)
          Remove a child node
 void removeChild(java.lang.String name, java.lang.String value)
          Remove a child node
 void removeChildren()
          Remove the children of this node
 void set(java.lang.Object s)
          Sets the model value of this node.
 void set(java.lang.String elementName, java.lang.Object newObject)
          Set the value of the element in the child DataModel located at the elementName.
 void setAddByDefault(boolean state)
          Set the flags that determines if attributes are added when queried
static void setAppendByDefault(boolean state)
          Set the flags that determines if attributes are added when queried
 void setAttribValue(int index, java.lang.Object value)
          Sets the attribute value
 void setAttribValue(int index, java.lang.String attribName, java.lang.Object value)
          Sets the attribute value
 void setId(java.lang.String newName)
          Sets the name attribute
 void setNumAttributes(int num)
          Setup the attributeNames and attributeValues arrays.
 void setNumChildren(int num)
          Set the number of children of this node
 void swapNodes(DataModel nodeA, DataModel nodeB)
          Move a child node up or down in the list of children
 
Methods inherited from class org.formaria.aria.data.DataModel
addModelListener, clear, fireModelUpdated, getParent, getTagName, prefixOutputPath, reset, resetAttributes, setParent, setTagName
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

values

protected DataModel[] values
array of Object values


attributeValues

protected java.lang.Object[] attributeValues

attributeNames

protected java.lang.String[] attributeNames

addByDefault

protected boolean addByDefault
If set, paths not found will automatically be allocated


appendByDefault

protected static boolean appendByDefault
append DataModel elements which are not found by default.


VALUE_ATTRIBUTE

public static final int VALUE_ATTRIBUTE
See Also:
Constant Field Values

ID_ATTRIBUTE

public static final int ID_ATTRIBUTE
See Also:
Constant Field Values

NUM_FIXED_ATTRIBUTE

public static final int NUM_FIXED_ATTRIBUTE
See Also:
Constant Field Values
Constructor Detail

BaseModel

public BaseModel(DataModel parent)
Constructs an instance of the model node.

Parameters:
parent - The DataModel to which this instance will be appended

BaseModel

public BaseModel(DataModel parent,
                 java.lang.String id,
                 java.lang.Object value)
Constructor which sets the id and value attributes and appends this DataModel to the parent model.

Parameters:
parent - the DataModel instance which will be the parent of this instance
id - the id of this instance
value - the initial value of this instance

BaseModel

public BaseModel()
null ctor. Calls the BaseModel( DataModel parent ) ctor will null parameter

Method Detail

insertChildAt

public void insertChildAt(DataModel newNode,
                          int idx)
Insert a node at a specified index in the list of children

Parameters:
newNode - the new model node
idx - the index at which to insert

swapNodes

public void swapNodes(DataModel nodeA,
                      DataModel nodeB)
Move a child node up or down in the list of children

Parameters:
nodeA - the child node to move
nodeB - the child node to move

getChildExists

public boolean getChildExists(java.lang.String name)
Check to see if the specified child node exists. Doing a get() creates the named node by design, so it will always result in a value

Parameters:
name - the name of the child we are looking for
Returns:
boolean indicating the existance of the child.

get

public java.lang.Object get(java.lang.String element)
Get the value of the element located at the path in the element parameter. The element name parameter can include an attribute name by appending '@attributeName' to the path, where attributeName is the name of the attribute.

Overrides:
get in class DataModel
Parameters:
element - The path to the DataModel required
Returns:
The value of the DataModel or the attribute

set

public void set(java.lang.String elementName,
                java.lang.Object newObject)
Set the value of the element in the child DataModel located at the elementName. The child node matching the element is first retrieved and then the named attribute is updated. The attributeName can be specified by appending '@attributeName' to the path, where attributeName is the name of the attribute (e.g. '/fonts/arial/@bold' where 'bold' is the attributeName. If the attributeName is not specified the retreived node's value is updated.
To update this node's value use set( newObject )

Specified by:
set in class DataModel
Parameters:
elementName - The path to the DataModel in the format 'base/foo
newObject - The new value of the attribute

getAttribute

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

Specified by:
getAttribute in class DataModel
Parameters:
attribName - The name of the attribute we are trying to locate
Returns:
The index of the attributeNames array containg the name

get

public DataModel get(int i)
Get the DataModel at element i

Specified by:
get in class DataModel
Parameters:
i - The index of the values array
Returns:
The DataModel at location i

get

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

Specified by:
get in class DataModel
Returns:
the value of the model

set

public void set(java.lang.Object s)
Sets the model value of this node.

Specified by:
set in class DataModel
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.

Overrides:
hasAutoId in class DataModel
Parameters:
b - true if there was no name in the DataSource

hasAutoId

public boolean hasAutoId()
Determine if the element needs a name assigned temporarily because one doesn't exist in the DataSource.

Overrides:
hasAutoId in class DataModel
Returns:
true if there was no name for the element in the DataSource

getId

public java.lang.String getId()
Gets the name attribute

Specified by:
getId in class DataModel
Returns:
the name attribute

setId

public void setId(java.lang.String newName)
Sets the name attribute

Overrides:
setId in class DataModel
Parameters:
newName - the new name

getAttribName

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

Specified by:
getAttribName in class DataModel
Parameters:
i - The index of the attributeNames array whose value we want
Returns:
The string value of the attributeNames array at position i

getAttribValue

public java.lang.Object getAttribValue(int i)
Retrive the value of the attribute at the specified index

Specified by:
getAttribValue in class DataModel
Parameters:
i - The index of the attributeValues array whose value we want
Returns:
The (Object) value of the attributeValues array at position i

getAttribValueAsString

public java.lang.String getAttribValueAsString(int i)
Convert the attribute at the specified index to a String and return it

Specified by:
getAttribValueAsString in class DataModel
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 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.

Convert the attribute at the specified index to a double and return it

Specified by:
getAttribValueAsDouble in class DataModel
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 double getAttribValueAsDouble(int i,
                                     char decimalSeparator,
                                     char groupingSeparator)
Convert the attribute at the specified index to a double and return it

Specified by:
getAttribValueAsDouble in class DataModel
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 int getAttribValueAsInt(int index)
Convert the attribute at the specified index to an int and return it. If the attribute is null 0 is returned

Specified by:
getAttribValueAsInt in class DataModel
Parameters:
index - The index of the attributeValues array whose value we want
Returns:
The int value of the attributeValues array at position i

setAttribValue

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

Specified by:
setAttribValue in class DataModel
Parameters:
index - The index of the attributeValues array whose value we want
value - the value object

setAttribValue

public void setAttribValue(int index,
                           java.lang.String attribName,
                           java.lang.Object value)
Sets the attribute value

Specified by:
setAttribValue in class DataModel
Parameters:
index - The index of the attributeValues array whose value we want
attribName - the name of the attribute
value - the value object

getValueAsDouble

public double getValueAsDouble(java.lang.String elementName)
Gets the value attribute as a Double value

Specified by:
getValueAsDouble in class DataModel
Parameters:
elementName - The name of the element to be retrieved from this instance
Returns:
the value as a double

getValueAsInt

public int getValueAsInt(java.lang.String elementName)
Gets the value attribute of the specified node as an int.

Specified by:
getValueAsInt in class DataModel
Parameters:
elementName - The name of the element to be retrieved from this instance
Returns:
the value as an int

getValueAsString

public java.lang.String getValueAsString(java.lang.String elementName)
Gets the value attribute of the specified node as a string.

Specified by:
getValueAsString in class DataModel
Parameters:
elementName - The name of the element to be retrieved from this instance
Returns:
the value as a string

getModel

public DataModel getModel(java.lang.String elementName)
Gets the value attribute of the specified node as an DataModel.

Parameters:
elementName - The name of the element to be retrieved from this instance
Returns:
the value as an DataModel

hashCode

public int hashCode()
The hashcode of this instance. Based on the ID String

Specified by:
hashCode in class DataModel
Returns:
The hashcode of this instance

getNumChildren

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

Overrides:
getNumChildren in class DataModel
Returns:
the number of child nodes

getNumAttributes

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

Overrides:
getNumAttributes in class DataModel
Returns:
the number of attributes

setNumChildren

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

Overrides:
setNumChildren in class DataModel
Parameters:
num - the new number of children

append

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

Specified by:
append in class DataModel
Parameters:
elementName - The immediate path to the DataModel required
Returns:
The value of the DataModel or the attribute

append

public void append(DataModel childNode)
Appends a node to the model. If a node of the same name is found it is replaced. If there is insufficient space to store the new node then the storage is automatically expanded.
The child nodes should be named uniquely or not at all (i.e. they should be annonymous). If the new childNode has a name then it is compared to the existing nodes and will replace a node of the same name if one exists.

Specified by:
append in class DataModel
Parameters:
childNode - the child node

appendAttribute

public void appendAttribute(java.lang.String name,
                            java.lang.Object value)
Append an attribute, creating a new attribute slot if necessary

Parameters:
name - the name of the attribute
value -

remove

public void remove(DataModel child)
Remove a child node from this DataModel instance. Squeeze the following children so that they are contiguous.

Parameters:
child - the child to be removed.

removeAttributes

public void removeAttributes()
Remove the nodes attributes and attribute names


removeChildren

public void removeChildren()
Remove the children of this node

Overrides:
removeChildren in class DataModel

removeChild

public void removeChild(java.lang.String name)
Remove a child node

Parameters:
name - the ID or name of the node

removeChild

public void removeChild(java.lang.String name,
                        java.lang.String value)
Remove a child node

Parameters:
name - the ID or name of the node
value - the value of the matching node to remove

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.

Overrides:
setNumAttributes in class DataModel
Parameters:
num - The new size of the array

getAttribFromPath

public static java.lang.String getAttribFromPath(java.lang.String path)
Get the attribute from a path e.g.
returns 'attrib' from 'a/b/c/@attrib' returns null from 'a/b/c/'

Parameters:
path - the path to split
Returns:
the attribute name

getBaseFromPath

public static java.lang.String getBaseFromPath(java.lang.String path)
Get the base path from a path e.g.
returns 'a/b/c/' from 'a/b/c/@attrib' returns 'a/b/c/' from 'a/b/c/' returns null from 'a/b/c/@attrib'

Parameters:
path - the path to split
Returns:
the path stripped of attributes

setAddByDefault

public void setAddByDefault(boolean state)
Set the flags that determines if attributes are added when queried

Parameters:
state - true to add an attribute if it is missing, false to return -1 when querying a node attribute with "getAttribute( name )".

getAddByDefault

public boolean getAddByDefault()
Get the flags that determines if attributes are added when queried

Returns:
true if an attribute is added if it is missing when queried, or false to return -1 when querying a node attribute with "getAttribute( name )".

setAppendByDefault

public static void setAppendByDefault(boolean state)
Set the flags that determines if attributes are added when queried

Parameters:
state - true to add an attribute if it is missing, false to return -1 when querying a node attribute with "getAttribute( name )".

getAppendByDefault

public static boolean getAppendByDefault()
Get the flags that determines if attributes are added when queried

Returns:
true if an attribute is added if it is missing when queried, or false to return -1 when querying a node attribute with "getAttribute( name )".