







































Module('ca.carleton.gcrc.formula.condition','$Revision: 3655 $',function(mod){






mod.LogicalConditionFactory=Class(function(publ,priv){




publ.__init__=function(
condition_
){

this.condition=condition_;


this.conditionFactories=new Array();;
}





publ.Add=function(conditionFactory_){
this.conditionFactories.push(conditionFactory_);
}






publ.Create=function(context_){
var children=new Array();
var loop;
for(loop=0;loop<this.conditionFactories.length;++loop){
var child=this.conditionFactories[loop].Create(context_);
children.push(child);
}

return new mod.LogicalCondition(this.condition,children);
}
});






mod.LogicalCondition=Class(function(publ,priv){





publ.__init__=function(
condition_
,children_
){

this.condition=condition_;


this.children=children_;


var loop;
for(loop=0;loop<this.children.length;++loop){
this.children[loop].Parent(this);
}


this.value=-3;


this.Update();
}





publ.Parent=function(parent_){
this.parent=parent_;
}





publ.LogicalValue=function(){
return this.value;
}




publ.Update=function(){
var previousValue=this.value;


if('or'==this.condition){
this.value=false;
var loop;
for(loop=0;loop<this.children.length;++loop){
var value=this.children[loop].LogicalValue();
if(value){
this.value=true;
break;
}
}
}else if('and'==this.condition){
this.value=true;
var loop;
for(loop=0;loop<this.children.length;++loop){
var value=this.children[loop].LogicalValue();
if(!value){
this.value=false;
break;
}
}
}else{

this.value=false;
}


if(previousValue!=this.value){
if(null!=this.parent){
this.parent.Update();
}
}
}




publ.Terminate=function(){
var loop;
for(loop=0;loop<this.children.length;++loop){
this.children[loop].Terminate();
}
}
});





mod.NegationFactory=Class(function(publ,priv){




publ.__init__=function(
conditionFactory_
){

this.conditionFactory=conditionFactory_;
}






publ.Create=function(context_)
{
var child=this.conditionFactory.Create(context_);

return new mod.Negation(child);
}
});






mod.Negation=Class(function(publ,priv){




publ.__init__=function(
child_
){

this.child=child_;
this.child.Parent(this);


this.value=-3;


this.Update();
}





publ.Parent=function(parent_){
this.parent=parent_;
}





publ.LogicalValue=function(){
return this.value;
}




publ.Update=function(){
var previousValue=this.value;
this.value=(false==this.child.LogicalValue());


if(previousValue!=this.value){
if(null!=this.parent){
this.parent.Update();
}
}
}




publ.Terminate=function(){
this.child.Terminate()
}
});






mod.FeatureConditionFactory=Class(function(publ,priv){






publ.__init__=function(
condition_
,leftValueFactory_
,rightValueFactory_
){

this.condition=condition_;


this.leftValueFactory=leftValueFactory_;


this.rightValueFactory=rightValueFactory_;
}






publ.Create=function(context_){
var leftNode=this.leftValueFactory.Create(context_);
var rightNode=this.rightValueFactory.Create(context_);

return new mod.FeatureCondition(this.condition,leftNode,rightNode);
}
});






mod.FeatureCondition=Class(function(publ,priv){






publ.__init__=function(
condition_
,leftValue_
,rightValue_
){

this.condition=condition_;


this.leftValue=leftValue_;
this.leftValue.Parent(this);


this.rightValue=rightValue_;
this.rightValue.Parent(this);


this.value=-3;


this.Update();
}





publ.Parent=function(parent_){
this.parent=parent_;
}





publ.LogicalValue=function(){
return this.value;
}




publ.Update=function(){
var previousValue=this.value;


var leftValue=this.leftValue.Values()[0];
var rightValue=this.rightValue.Values()[0];
if(isNaN(leftValue)&&isNaN(rightValue)){
}else{
leftValue=1*this.leftValue.Values()[0];
rightValue=1*this.rightValue.Values()[0];
}


if('equalTo'==this.condition){
if(null==this.leftValue.Values()[0]||null==this.rightValue.Values()[0]){
this.value=false;
}else{
this.value=(leftValue==rightValue);
}
}else if('lessThan'==this.condition){
this.value=(leftValue<rightValue);
}else if('lessThanOrEqualTo'==this.condition){
this.value=(leftValue<=rightValue);
}else if('greaterThan'==this.condition){
this.value=(leftValue>rightValue);
}else if('greaterThanOrEqualTo'==this.condition){
this.value=(leftValue>=rightValue);
}else{

this.value=false;
}

if(previousValue!=this.value){
if(null!=this.parent){
this.parent.Update();
}
}
}




publ.Terminate=function(){
this.leftValue.Terminate();
this.rightValue.Terminate();
}
});






mod.IsNullFactory=Class(function(publ,priv){




publ.__init__=function(
childValueFactory_
){

this.childValueFactory=childValueFactory_;
}






publ.Create=function(context_){
var childNode=this.childValueFactory.Create(context_);

return new mod.IsNullCondition(childNode);
}
});






mod.IsNullCondition=Class(function(publ,priv){




publ.__init__=function(
childValue_
){

this.childValue=childValue_;
this.childValue.Parent(this);


this.Update();
}





publ.Parent=function(parent_){
this.parent=parent_;
}




publ.LogicalValue=function(){
return this.value;
}




publ.Update=function(){
var previousValue=this.value;


var child=this.childValue.Values()[0];


this.value=(child==null);


if(previousValue!=this.value){
if(null!=this.parent){
this.parent.Update();
}
}
}




publ.Terminate=function(){
this.childValue.Terminate();
}
});





mod.StateConditionFactory=Class(function(publ,priv){






publ.__init__=function(
condition_
,layerNameFactory_
,featureNameFactory_
){

this.condition=condition_;


this.layerNameFactory=layerNameFactory_;


this.featureNameFactory=featureNameFactory_;
}






publ.Create=function(context_){
var layerNameNode=null;
if(null!=this.layerNameFactory){
layerNameNode=this.layerNameFactory.Create(context_);
}

var featureNameNode=null;
if(null!=this.featureNameFactory){
featureNameNode=this.featureNameFactory.Create(context_);
}

return new mod.StateCondition(this.condition,context_,layerNameNode,featureNameNode);
}
});





mod.StateCondition=Class(function(publ,priv){







publ.__init__=function(
condition_
,context_
,layerNameNode_
,featureNameNode_
){

this.condition=condition_;


this.context=context_;


this.layerNameNode=layerNameNode_;
if(null!=this.layerNameNode){
this.layerNameNode.Parent(this);
}


this.featureNameNode=featureNameNode_;
if(null!=this.featureNameNode){
this.featureNameNode.Parent(this);
}


this.model=this.context.layerModel.mapModel;


this.currentLayerName=null;


this.currentfeatureName=null;


this.currentFeature=null;


this.currentObservation=null;


this.value=-3;


this.Update();
}





publ.Parent=function(parent_){
this.parent=parent_;
}





publ.LogicalValue=function(){
return this.value;
}




publ.Update=function(){

var effectiveLayerName;
if(null!=this.layerNameNode){
effectiveLayerName=this.layerNameNode.Values()[0];
}else{
effectiveLayerName=this.context.layerModel.name;
}
var effectiveFeatureName;
if(null!=this.featureNameNode){
effectiveFeatureName=this.featureNameNode.Values()[0];
}else{
effectiveFeatureName=this.context.id;
}


var isNewObservationRequired=false;
if(effectiveLayerName!=this.currentLayerName){
isNewObservationRequired=true;
}else if(effectiveFeatureName!=this.currentFeatureName){
isNewObservationRequired=true;
}


if(isNewObservationRequired){

if(null!=this.currentObservation){
this.currentObservation.RemoveObserver();
this.currentObservation=null;
}


this.currentLayerName=effectiveLayerName;
this.currentFeatureName=effectiveFeatureName;

if(null!=effectiveLayerName&&null!=effectiveFeatureName){

var layer=this.model.LayerFromName(effectiveLayerName);
var feature=layer.FeatureFromId(effectiveFeatureName);


this.currentObservation=feature.AddObserver('display_state',this,this.UpdatedState,null);

this.currentFeature=feature;
}else{
this.currentFeature=null;
}
}


this.RecomputeValue();
}




publ.RecomputeValue=function(){
var previousValue=this.value;
if(null!=this.currentFeature){
if('selection'==this.condition){
this.value=this.currentFeature.IsSelected();
}else if('focus'==this.condition){
this.value=this.currentFeature.IsInFocus();
}else{
this.value=false;
}
}else{
this.value=false;
}


if(previousValue!=this.value){
if(null!=this.parent){
this.parent.Update();
}
}
}





publ.UpdatedState=function(event_){
this.RecomputeValue();
}




publ.Terminate=function(){
if(null!=this.layerNameNode){
this.layerNameNode.Terminate();
}
if(null!=this.featureNameNode){
this.featureNameNode.Terminate();
}
if(null!=this.currentObservation){
this.currentObservation.RemoveObserver();
}
}
});
});