@doc alias="context" hierarchy="GMLDOM">
An object that holds the active context, a representation of the unit, element,
or field currently in focus.
Instances of this class are created automatically by the system, and should
not be created directly. To access the active context use the
$ENV.context property.
(c) SAP AG 2003-2006. All rights reserved.
///////////////////////////////////////////////////////////////////////
// CLASS HEADER
Class ModelContext inherit Object;
<@doc scope="private">
Constructs and initializes a new model context object
The model for which the context object is created
constructor(model)
this.level = 1;
this.model = model;
this.unit = model.root;
this.backgroundUnit = null;
this.element = null;
this.field = null;
this.object = model.root;
this.isEmpty = false;
this.isEditable = model.root.isEnabled(); // used to be isEditable(0, but now a readOnly model can be enabled for editing.
this.board = '';
end
///////////////////////////////////////////////////////////////////////
// PROPERTIES
<@doc>Gets the id of the board in context
readonly property board = '';
<@doc>Gets the clipboard collection
readonly property clipboard = ^gml:GmlObject[id];
<@doc>Gets the unit in context
readonly property unit = ^gml:Unit;
<@doc>Set a context unit in the background for cases when the unit is not
loaded to the WS (like GlobaEnumeration or Service in Service Wizard).
In the case of setting this property the $ENV.contextUnit will return this property.
readonly property backgroundUnit = ^gml:Unit;
<@doc>Gets the element in context
readonly property element = ^gml:Element;
<@doc>Gets the field in context
readonly property field = ^gml:Field;
<@doc>Indicates whether the context is editable
readonly property isEditable = false;
<@doc>Indicates whether the context is empty (i.e., level == CTX_NONE)
readonly property isEmpty = true;
<@doc>Gets the context level (CTX_NONE, CTX_UNIT, CTX_ELEMENT, CTX_FIELD)
readonly property level = 0;
<@doc>Gets the model in context
readonly property model = ^gml:Model;
<@doc>Gets the object in direct context (@unit, @element, or @field, depending on @level)
readonly property object = ^gml:GmlObject;
///////////////////////////////////////////////////////////////////////
// METHODS
<@doc>
Clears the context
If the context has actually been changed, then the @env:Environment!onContextSwitch event will also be triggered.
method clear()
$WIN.beforeUnitSwitch(this.unit !== null);
var changes=0;
if (this.unit) changes |= #[CTX_UNIT];
if (this.element) changes |= #[CTX_ELEMENT];
if (this.field) changes |= #[CTX_FIELD];
if (this.board) changes |= #[CTX_BOARD];
this.level = 0;
this.isEmpty = true;
this.isEditable = false;
this.unit = null;
this.backgroundUnit = null;
this.element = null;
this.field = null;
this.object = null;
this.board = '';
if (changes) $ENV.fireContextSwitch(changes);
end
<@doc>
Checks whether a given object and the active context coincide on a specified level
The object to test
The level to match (1=unit, 2=element, or 4=field)
The test result
method coincides(object, level)
if (!this.object || !object || typeof object.isa != 'function') return false;
var l=level||1, L=this.level, D=null, E=null, F=null, M=this.model;
try {
if (object.isa(M.unitClassifier)) {
D=object;
} else if (object.isa(M.elementClassifier)) {
D=object.unit; E=object;
} else if (object.isa(M.fieldClassifier)) {
D=object.element.unit; E=object.element; F=object;
}
return (l==#[CTX_UNIT] && (L >= #[CTX_UNIT]) && D==this.unit) ||
(l==#[CTX_ELEMENT] && (L >= #[CTX_ELEMENT]) && E==this.element) ||
(l==#[CTX_FIELD] && (L >= #[CTX_FIELD]) && F==this.field);
} catch(e) {
return false;
}
end
<@doc>
Checks whether a given object is contained by or equal to the active context
The object to test
The test result
method contains(object)
if (!this.object || !object || typeof object.isa != 'function') return false;
var M=this.model;
try {
var L=this.level, O=this.object;
if (object.isa(M.unitClassifier)) {
return (L==1 && O==object);
} else if (object.isa(M.elementClassifier)) {
return (L==1 && O==object.unit) ||
(L==2 && O==object);
} else if (object.isa(M.fieldClassifier)) {
return (L==1 && O==object.element.unit) ||
(L==2 && O==object.element) ||
(L==4 && O==object);
} else {
return false;
}
} catch(e) {
return false;
}
end
<@doc>
Checks whether a given object is the same as the active context
The object to test
The test result
method equals(object)
return (this.object ? object === this.object : !object);
end
<@doc>
Tests whether the active context object is an instance of a specified GML class/prototype
The qualified class/prototype name to test (if the namespace is omitted then "~core.gml:" is assumed)
Returns ~true iff the active context is an instance of %0
override method isa(className)
return (this.object && this.object.isa(className));
end
<@doc>
Refreshes the active context
This method causes the @env:Environment!onContextSwitch to be triggered unconditionally
method refresh()
$ENV.fireContextSwitch(#[CTX_OBJECT|CTX_BOARD]);
end
<@doc>
Sets the context to the given object and board
The new object in context
The new board in context
Indicates whether the context switch is due to history traversal
This method might throw an exception if the object cannot be put into context.
If the context has actually been changed, then the @env:Environment!onContextSwitch event will also be triggered.
method setContext(object, board, history)
var ochange = (object !== -1);
var vchange = (board !== -1);
var changes = 0;
if ((!ochange || this.object === object || typeof object.isa != 'function') && (!vchange || this.board === board)) return;
if (ochange) {
if (object.canActivate && !object.canActivate()) throw new Error('#TEXT[XMSG_OBJECT_TYPE]'.replace('{0}', object.Class.name));
var oldUnit=this.unit, oldElement=this.element, oldField=this.field, M=this.model;
if (object.isa(M.unitClassifier)) {
$WIN.beforeUnitSwitch(this.unit !== object);
this.level = #[CTX_UNIT];
this.unit = object;
this.element = null;
this.field = null;
} else if (object.isa(M.elementClassifier)) {
$WIN.beforeUnitSwitch(this.unit !== object.unit);
this.level = #[CTX_ELEMENT];
this.unit = object.unit;
this.element = object;
this.field = null;
} else if (object.isa(M.fieldClassifier)) {
$WIN.beforeUnitSwitch(this.unit !== object.element.unit);
this.level = #[CTX_FIELD];
this.unit = object.element.unit;
this.element = object.element;
this.field = object;
} else {
return this.clear();
}
this.object = object;
this.isEmpty = false;
this.isEditable = this.unit && this.unit.isEnabled(); // used to be isEditable(0, but now a readOnly model can be enabled for editing.
if (oldUnit !== this.unit) changes |= #[CTX_UNIT];
if (oldElement !== this.element) changes |= #[CTX_ELEMENT];
if (oldField !== this.field) changes |= #[CTX_FIELD];
}
if (vchange) {
var oldBoard = this.board;
this.board = UPPER(board);
if (!(this.board in $ENV.getBoardsTable())) this.board = '';
if (oldBoard != this.board) changes |= #[CTX_BOARD];
}
if (history) changes |= #[CTX_HISTORY];
if (this.backgroundUnit) this.backgroundUnit = null;
$ENV.fireContextSwitch(changes);
end
<@doc>
Sets the context to the given object, without changing the board
The new object in context
This method might throw an exception if the object cannot be put into context.
If the context has actually been changed, then the @env:Environment!onContextSwitch event will also be triggered.
method setObject(object)
if (typeof object == 'string') object = this.model.getUnit(object);
if (!object) this.clear(); else this.setContext(object, -1);
end
<@doc>
Sets the context to the given board, without changing the object
The new board in context
This method might throw an exception if the object cannot be put into context.
If the context has actually been changed, then the @env:Environment!onContextSwitch event will also be triggered.
method setBoard(board)
this.setContext(-1, board);
end
<@doc>
Gets the context board's definition object
The context board definition object
method getBoardDef()
return $ENV.getBoard(this.board) || null;
end
<@doc>
Gets the context board's aspect role
The context board aspect role
method getBoardAspect()
var boardDef = $ENV.getBoard(this.board);
return boardDef && CLASS(boardDef.aspect) || null;
end