Styling > Introducing Cascading Style Sheets > The CSS Specification

This section contains reference information on CSS. It explains how CSS concepts are used in JViews TGO. It introduces you to:

CSS Syntax

CSS syntax is described more fully in the ILOG JViews Diagrammer documentation, Developing with the SDK, section Using CSS Syntax in the Style Sheet.

The main elements of syntax are:

Applying CSS to Java Objects

You can apply CSS to style Java objects instead of XML documents.

The CSS selector is designed to match HTML or XML documents. It can also be used to match a hierarchy of Java objects accessible through a model interface. The declarations are then sorted for the objects in the model and are used depending on the application that controls the CSS engine. In JViews TGO, declarations create and customize one graphic object for each object in the model.

The input model represents the kernel of the CSS for Java engine. It provides these important categories of information:

The declarations part represents property settings on a target object. The target object depends on the way the CSS engine is used. In JViews TGO, the target object is the rendering object associated with the object in the model. JViews TGO provides a default graphic representation for user-defined business classes and predefined business classes. This graphic representation has a set of properties that are used to customize its appearance.

Instead of using the default graphic representation supplied by JViews TGO, you can define your own rendering object, an IlvGraphic or a JComponent. In this case, the declarations change property values on the graphic object that corresponds to the object matched in the model.

How to Customize the Graphic Representation
object."test.Vehicle"[model=sports] {
    icon: "sports-car.gif";
}

This example matches the object of the class test.Vehicle with the property model (which has the value sports) and fixes the property icon of the graphic object associated with this object to sports-car.gif. (The association of the object with the graphic object is defined elsewhere.)

Note
The enclosing double quotes around test.Vehicle are used so that the dot is not interpreted as a CSS class pattern; that is, an object of type test with CSS class Vehicle.

Property matching can be used to add dynamic behavior. An event that changes attribute values occurring on the model can activate the CSS engine to establish new property values.

How to Add Dynamic Behavior Through Property Matching
object.computer[state=down] {
    foreground: gray;
}

This example changes the foreground color whenever an object of CSS class computer has the value of the property state set to down.

The CSS Engine in JViews TGO

In JViews TGO, the CSS engine is responsible for creating and customizing graphic objects and renderers when the data is loaded. At run time, the engine customizes the graphic objects according to changes in the model.

The left side of a declaration usually represents a JavaBean property of the graphic property. The right side is a literal. If the literal requires type conversion, the method setAsText() is invoked on the property editor associated with the JavaBean property.

Class Property

The class property name is a reserved keyword that indicates the class name of the generated graphic object. JViews TGO provides a predefined representation for the objects in all graphic components, which means that the class property is optional. It can be used when you want to replace the predefined representation.

How to Use the Class Property

The right side of a class declaration could contain:

object {
    class: ilog.views.sdm.graphic.IlvGeneralNode;
    foreground: red;
}
object {
    class: data.beans.gauge;
    foreground: red;
}
The JavaBeans documentation states: "When using the beanName as a serialized object name, we convert the given beanName to a resource pathname and add a trailing `.ser' suffix. We then try to load a serialized object from that resource."
In the example given, the method Beans.instantiate() would try to read a serialized object from the resource data/beans/gauge.ser.

In the network and equipment components, the class declaration is applied only when a creation request occurs. When the model state changes, graphic components are customized by applying only new declarations from the matching rules in the CSS. The class declaration is ignored. To change the class, the object in the model must be removed and then added again.

Model Indirection

The right side of a declaration contains a literal that is converted dynamically through a property editor. If the literal is prefixed by @, the remainder of the string is treated as a model attribute name. The declaration expects the attribute value of the object from the model.

How to Refer to Attribute Values of Model Objects
object."ilog.tgo.model.IltObject" {
    label: @name;
}

The label property is set to the value of the attribute named name in the model.

Besides the standard model attributes, JViews TGO also provides the following attributes that you can use when customizing objects and graphic components:

object {
  toolTipText: @__ID;
}

Resolving URLs

Sometimes declaration values are URLs relative to the style sheet location. A special construct, standard in CSS level 2, allows you to create a URL from the base URL of the current style sheet. For example, the following declaration extends the path of the current style sheet URL with images/icon.gif.

How to Extend the Path of the Current Style Sheet
imageURL: url(images/icon.gif);

This feature is very useful for creating style sheets with images located relative to the style sheet itself, since the URL remains valid even when the CSS is cascaded or imported elsewhere.

CSS Recursion

You may want to specify a Java object as the value of a declaration. A simple convention allows you to recurse in the style sheet; that is, to define a new Java object which has the same style sheet, but is unrelated to the current model.

Prefix the value with @# to create a new JavaBean dynamically.

How to Create a New JavaBean Dynamically
object."Alarm/creationTime" {
    toolTipText: "@#tooltipFormatBean";
    label: @creationTime;
}
Subobject#toolTipFormatBean {
    class:"ilog.cpl.util.text.IlpSimpleDataFormat";
    pattern: "HH:mm:ss z";
}

The @# operator extends the current data model by adding a dummy model object as the child of the current object. The object ID of the dummy object is the remainder of the string, beyond the @# operator. The type of the dummy object is Subobject. The dummy object inherits CSS classes and attributes from its parent.

The CSS engine creates and customizes a new subobject according to the declarations it finds for the dummy object. This means, in particular, that the Java class of the subobject is determined by the value of the class property. The newly created subobject becomes the value of the @# expression. In the declarations for the subobject, attribute references through the @ operator refer to the attributes of the parent object.

Once the subobject is completed, the previous model is restored, so that normal processing is resumed.

In the example, an IlpSimpleDateFormat object is created, with the pattern property set to HH:mm:ss z, and is assigned to the toolTipText property of the object.

There are two refinements to the @#ID operator:

The need for these refinements arises from a performance issue. The @# operator creates a new object each time a declaration is resolved. Usually, a declaration is applied when properties change. Under certain circumstances, the creation of objects may lead to expensive processing. These optional mechanisms minimize the creation of objects.

CSS Expressions and Functions

The values of CSS declarations can be literals, @# constructions, or attributes from the model (prefixed by @). In addition, you can also declare a value with @|, which considers the rest of the value as an expression.

The syntax of the expression after the @| prefix is close to Java syntax. Expressions can be, for example, arithmetic (int, long, float, or double), Boolean, or String.

Table 1.1 Syntax of Expression Values
Expression Syntax 
Meaning 
@|3+2*5 
13 
@|true&&(true||!true) 
true 
@|foo+bar 
"foobar" 

Expressions can also refer to attributes in the model. The syntax used is the normal one.

Table 1.2 Syntax of Expressions Referring to Attributes in the Model
Expression Syntax 
Meaning 
@|@speed/100+@drift 
1/100 of the value of speed plus value of drift, where speed and drift are attributes of the current object. 
'@|"name is: " + @name' 
"name is: Bob", if the value of the current object attribute name is Bob. Note the use of double quotes to retain the space characters. 

The usual functions are accepted:

For example:

@|3+sin(pi/2)

which results in the value 4.

CSS also supports functions as part of expressions.

JViews TGO Functions

JViews TGO provides a set of functions that can be used directly in your CSS files.

Table 1.3 JViews TGO Functions for Direct Use in CSS Files 
Function Name 
Description 
Class Name 
Usage 
image 
Retrieves an image from the Image Repository service registered in your application context. 
Parameter:  
image location  
Example
@|image("ilog/tgo/ilt_busy.png") 
resource 
Retrieves a resource value from a given ResourceBundle
Parameters
ResourceBundle name (mandatory) 
Message identifier (mandatory) 
Default message value (optional). Returned if the message requested was not found in the given resource bundle. 
Example
@|resource("ilog.tgo.messages.JTGOMessages", "ilog.tgo.Operational_State") 
valuemap 
Retrieves a value from an IlpValueMap that corresponds to the given key. 
Parameters
IlpValueMap instance 
Object key 
Example
@|valuemap(@#valuemap, @severity); 
format 
Applies a format to the given values. 
Parameters
java.text.Format instance 
Arguments of the Format 
Example
@|format(@#formatBean, @attribute) 
blinkingcolor 
Creates a blinking color. 
Parameters
on color 
off color 
on period 
off period 
Example
@|blinkingcolor(black, white,1000, 500) 
pattern 
Creates a pattern description, IlPattern, which can be used to customize the representation of JViews TGO predefined business objects. 
Parameter
Pattern type, which can have one of the following values: 
Grid, SkewGrid, Dots, ThinHatching
It can also generate patterns defined in IlvPattern, for example, LIGHT_VERTICAL
Depending on the pattern type, other arguments can be passed to configure the pattern instance. 
Grid patterns accept two integer arguments: xPeriod and yPeriod
SkewGrid patterns accept two integer arguments: uPeriod and vPeriod
Examples
@|pattern("Grid", 3, 2) 
@|pattern("SkewGrid", 2, 2) 
@|pattern("LIGHT_VERTICAL") 
acknowledgedAlarmCount 
Returns the number of acknowledged alarms for a given business object. 
Parameter
One of the following possibilities: 
Default: all raw severities or traps 
Impact: all impact alarm severities 
Alarm severity name: for example, Raw.Major, Impact.MajorHigh 
Examples
label: '@|acknowledgedAlarmCount("Impact")'; 
label: '@|acknowledgedAlarmCount("Impact.MajorHigh")'; 
acknowledgedAlarmSummary 
Returns a summary of acknowledged alarms for a given business object. The returned value is a String listing the number of acknowledged alarms for each chosen severity. 
Parameter
One of the following possibilities: 
Default: all raw severities or traps 
Impact: all impact alarm severities 
Alarm severity name: for example, Raw.Major, Impact.MajorHigh 
Examples
label: '@|acknowledgedAlarmSummary("Impact")'; 
label: '@|acknowledgedAlarmSummary("Raw.Major")'; 
alarmCount 
Returns the number of outstanding alarms for a given business object. 
Parameter
One of the following possibilities: 
Default: all raw severities or traps 
Impact: all impact alarm severities 
Alarm severity name: for example, Raw.Major, Impact.MajorHigh 
Examples
label: '@|alarmCount("Impact")'; 
label: '@|alarmCount("Raw.Major")'; 
alarmSummary 
Returns the summary of new and acknowledged alarms for a given business object. 
Parameter
One of the following possibilities: 
Default: all raw severities or traps 
Impact: all impact alarm severities 
Alarm severity name: for example, Raw.Major, Impact.MajorHigh 
Examples
label: '@|alarmSummary()'; ///Equivalent to "Default" 
label: '@|alarmSummary("Impact")'; ///Impact alarms 
label: '@|alarmSummary("Raw.Major")'; ///Consider only raw major alarms 
highestAcknowledgedSeverity 
Returns the highest severity of acknowledged alarms for a given business object. 
Parameter
One of the following possibilities: 
Default: all raw alarm severities or traps 
Impact: all impact alarm severities 
Alarm severity name: for example, Raw.Major, Impact.MajorHigh 
Examples
label: '@|highestAcknowledgedSeverity()'; ///Equivalent to "Default" 
label: '@|highestAcknowledgedSeverity("Impact")'; ///Impact alarms 
label: '@|highestAcknowledgedSeverity("Raw.Major")'; ///Consider only raw major alarms 
highestNewSeverity 
Returns the highest severity of new alarms for a given business object. 
Parameter
One of the following possibilities: 
Default: all raw alarm severities or traps 
Impact: all impact alarm severities 
Alarm severity name: for example, Raw.Major, Impact.MajorHigh 
Examples
label: '@|highestNewSeverity()'; ///Equivalent to "Default" 
label: '@|highestNewSeverity("Impact")'; ///Impact alarms 
label: '@|highestNewSeverity("Raw.Major")'; ///Consider only raw major alarms 
highestSeverity 
Returns the highest severity of outstanding alarms for a given business object. 
Parameter
One of the following possibilities: 
Default: all raw alarm severities or traps 
Impact: all impact alarm severities 
Alarm severity name: for example, Raw.Major, Impact.MajorHigh 
Examples
label: '@|highestSeverity()'; ///Equivalent to "Default" 
label: '@|highestSeverity("Impact")'; ///Impact alarms 
label: '@|highestSeverity("Raw.Major")'; ///Consider only raw major alarms 
newAlarmCount 
Returns the number of new alarms for a given business object. 
Parameter
One of the following possibilities: 
Default: all raw alarm severities or traps 
Impact: all impact alarm severities 
Alarm severity name: for example, Raw.Major, Impact.MajorHigh 
Examples
label: '@|newAlarmCount()'; ///Equivalent to "Default" 
label: '@|newAlarmCount("Impact")'; ///Impact alarms 
label: '@|newAlarmCount("Impact.MajorLow")'; ///Consider only raw major alarms 
newAlarmSummary 
Returns the summary of new alarms for a given business object. 
Parameter
One of the following possibilities: 
Default: all raw alarm severities or traps 
Impact: all impact alarm severities 
Alarm severity name: for example, Raw.Major, Impact.MajorHigh 
Examples
label: '@|newAlarmSummary()'; ///Equivalent to "Default" 
label: '@|newAlarmSummary("Impact")'; ///Impact alarms 
label: '@|newAlarmSummary("Impact.MajorLow")'; ///Consider only raw major alarms 
primaryStateSummary 
Returns the summary of the primary state information for a given business object. 
Example
label: '@|primaryStateSummary()';  
secondaryStateSummary 
Returns the summary of the secondary state information for a given business object. 
Example
label: '@|secondaryStateSummary()';  
settings 
Parameter
Setting key 
Example
icon: '@|settings("Link.Media.Fiber.Icon")';  
severityColor 
Returns the color corresponding to a given alarm severity. 
Parameter
Alarm severity 
Examples
labelBackgroundColor: '@|severityColor(@|highestNewSeverity())'; 
labelBackgroundColor: '@|severityColor("Raw.Major")';  
severityBrightColor 
Returns the bright color corresponding to a given alarm severity. 
Parameter
Alarm severity 
Example
alarmBrightColor: '@|severityBrightColor(@|highestNewSeverity())'; 
severityDarkColor 
Returns the dark color corresponding to a given alarm severity. 
Parameter
Alarm severity 
Example
alarmDarkColor: '@|severityDarkColor(@|highestNewSeverity())'; 
severityIcon 
Returns the icon corresponding to a given alarm severity. 
Parameter
IltAlarmSeverity or the String representation of an IltAlarmSeverity
Example
alarmIcon: '@|severityIcon("Raw.Major")'; 
alarmIcon: '@|severityIcon(@|highestSeverity())'; 
alarmIcon: '@|severityIcon(@|highestSeverity("Impact"))'; 

How to Create New CSS Functions

JViews TGO allows you to create and register new functions to be used in CSS files to customize the representation of your business objects. These functions should implement the interface IlpCSSFunction. This interface defines the following methods:

The signature of the main method is:

public Object call (Object[] args,
                    Class type,
                    IlpContext appc,
                    IlpGraphicView view,
                    IlpRepresentationObject ro,
                    IlpAttribute attribute);

When a function is evaluated, the parameters are first resolved as subexpressions. Then, the final values of the parameters are passed to the args array.

The parameter type is the expected type of the function when it is known. A null value is possible. The implementation should be careful to return an object of the appropriate type. Otherwise, a simple conversion is applied, if conversion is possible, that is, between primitive types or to a String.

The other parameters provide information about the application context, graphic view, representation object, and attribute at the time when the call is made. Stateless expressions do not need these parameters.

If an error occurs during a call, an exception will be reported and the current property setting will be canceled.

Please refer to sample <installdir>samples/framework/datasource-explorer2 , file ByteFunction.java, which shows how to implement and register a new function.

Functions are registered in the CSS file with one of the following properties:

How to Register a Function in a CSS File
StyleSheet {
    functionList: "test.function.FirstFunction,test.function.SecondFunction";
}

or

StyleSheet {
    functions[0]: "test.function.FirstFunction";
    functions[1]: "test.function.SecondFunction";
}

Divergences from CSS2

Java objects are not HTML documents. The differences lead to an adaptation of the CSS, so that its power can be fully exploited. The CSS2 syntax is retained. Therefore, a CSS editor can still be used to create the style sheet.

Cascading

Cascading is explicit. The API provides a means of cascading the style sheets. The !important and inherit tags are not supported. They have not been implemented for the sake of simplicity.

Pseudo-classes and Pseudo-elements

Pseudo-classes are minimal building blocks which match model objects according to an external context. The syntax is like a CSS class, but uses a colon (:) instead of a dot (.). For example, :link-visited matches a link element only if it is already visited. Only the browser can resolve this pseudo-class at run time.

The pseudo-classes are fully implemented and are used by all JViews TGO renderers. JViews TGO recognizes the pseudo-classes :selected, :focus, and :expanded by default.

A pseudo-class has the same specificity as a CSS class.

Pseudo-elements are metaclasses like pseudo-classes, but match document structure instead of the browser state; for example, :first-child.

The CSS2 predefined pseudo-elements and pseudo-classes (:link, :hover, and so forth) are not implemented: they have no meaning in Java.

Attribute Matching

Each attribute value must be converted to String. This conversion is done using the Type converter specified in your application context.

The attribute pattern in CSS2 checks only the presence [att], equality [att=val], and inclusion [att~=val] of strings. The |= operator is disabled. In Java, numeric comparators (>, >=, <>, <=, <) have been added, with the usual semantics.

Syntax Enhancement

CSS for Java requires the use of quotation marks when a token contains special characters such as dot (.), colon (:), commercial at sign (@), hash sign (#), space ( ), and so on. Quotes can be used almost everywhere, in particular to delimit a declaration value, an element type, or a CSS class with reserved characters. The closing semicolon (;) is optional.

Null Value

Sometimes it makes sense to specify a null value in a declaration. By convention, null is a zero-length string '' or "".

How to Specify a Null Value
object {
    labelBackground: '';
}

The notation '' is also used to denote a null array for properties that expect an array of values.

Empty String

The null syntax does not distinguish the ability to write an empty string in the style sheets. If an empty string is required, it is easy to create it dynamically.

How to Create an Empty String
object {
    label: @#emptyString;
}
 
Subobject#emptyString {
    class: 'java.lang.String';
}

Note
You can use the sharing mechanism to avoid creating several strings. The @= construct creates the empty string the first time only and reuses the same instance for all other occurrences of @=emptyString.