net.xoetrope.xui.data
Class XBaseModel

java.lang.Object
  extended by net.xoetrope.xui.data.XModel
      extended by net.xoetrope.xui.data.XBaseModel
All Implemented Interfaces:
java.lang.Cloneable
Direct Known Subclasses:
XTableModel

public class XBaseModel
extends XModel
implements java.lang.Cloneable

The basic implementation of the XModel is handled by this class. Static data is loaded from an XML file pointed to by the startup.properties file by default using an XDataSource. The model is central to XUI 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) Xoetrope Ltd., 1998-2003
License: see license.txt


Field Summary
protected  boolean addByDefault
          If set, paths not found will automatically be allocated
protected static boolean appendByDefault
          append XModel 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  XModel[] values
          array of Object values
 
Fields inherited from class net.xoetrope.xui.data.XModel
parentModel, tagName
 
Constructor Summary
XBaseModel()
          null ctor.
XBaseModel(XModel parent)
          Constructs an instance of the model node.
XBaseModel(XModel parent, java.lang.String id, java.lang.Object value)
          Constructor which sets the id and value attributes and appends this XModel to the parent model.
 
Method Summary
 java.lang.Object append(java.lang.String elementName)
          Append a new node with the specified name.
 void append(XModel childNode)
          Appends a node to the model.
 java.lang.Object get()
          gets the value attribute
 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.
 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
 XModel getModel(java.lang.String elementName)
          Gets the value attribute of the specified node as an XModel.
 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(XModel newNode, int idx)
          Insert a node at a specified index in the list of children
 void remove(XModel child)
          Remove a child node from this XModel 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 XModel 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(XModel nodeA, XModel nodeB)
          Move a child node up or down in the list of children
 
Methods inherited from class net.xoetrope.xui.data.XModel
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 XModel[] 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 XModel 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

XBaseModel

public XBaseModel(XModel parent)
Constructs an instance of the model node.

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

XBaseModel

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

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

XBaseModel

public XBaseModel()
null ctor. Calls the XBaseModel( XModel parent ) ctor will null parameter

Method Detail

insertChildAt

public void insertChildAt(XModel 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(XModel nodeA,
                      XModel 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 XModel
Parameters:
element - The path to the XModel required
Returns:
The value of the XModel or the attribute

set

public void set(java.lang.String elementName,
                java.lang.Object newObject)
Set the value of the element in the child XModel 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 XModel
Parameters:
elementName - The path to the XModel 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 XModel
Parameters:
attribName - The name of the attribute we are trying to locate
Returns:
The index of the attributeNames array containg the name

get

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

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

get

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

Specified by:
get in class XModel
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 XModel
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 XModel
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 XModel
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 XModel
Returns:
the name attribute

setId

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

Overrides:
setId in class XModel
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 XModel
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 XModel
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 XModel
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 XModel
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 XModel
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

Specified by:
getAttribValueAsInt in class XModel
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 XModel
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 XModel
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 XModel
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 XModel
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 XModel
Parameters:
elementName - The name of the element to be retrieved from this instance
Returns:
the value as a string

getModel

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

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

hashCode

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

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

getNumChildren

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

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

getNumAttributes

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

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

setNumChildren

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

Overrides:
setNumChildren in class XModel
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 XModel
Parameters:
elementName - The immediate path to the XModel required
Returns:
The value of the XModel or the attribute

append

public void append(XModel 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 XModel
Parameters:
childNode - the child node

remove

public void remove(XModel child)
Remove a child node from this XModel 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 XModel

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 XModel
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 )".