Application Techniques



Dynamically Formatting Data in Views

How to format the data in a view at runtime.

About this technique

Details

Category

Java Client Techniques> Java-based views

Description

You'll learn about:

You can run this technique code from:

NOTE   First make sure that database is running on your localhost SilverStream Server

Related reading

See the chapter on using Java-based views in the Programmer's Guide

In this example, the view displays data from a database table. The view defines two band formats. The value of the data in a particular column determines which band format is used to display the data. All products with a unit price greater than $500 appears red and bold--the format definition of band 2. (See the dataProductAgcData control properties for further details.)

Adding a data control to a form   Top of page

In this example, the data is stored in a table named products. This table is located in Examples3_Java tables. To add a data control to the form:

  1. Click the data icon on the second row of the tool bar and add a data control to the form.

  2. Set the following properties:

  3. Set the appropriate expressions for these column names:

  4. Add a final column expression and set the Column Name to [Bandname]

  5. Set the [Bandname] expression so that when the unit price is greater than 500, the data is displayed using the Band 2 format; otherwise it uses the Band 1 format like this:

      if products.unitprice > 500 then "Band2" else "Band1" endif 
    

Rules for using the [Bandname] property

  1. The definition for the [Bandname] column can be a database column or a true/false expression.

  2. If no bands with the specified names exist, SilverStream uses the first one that is available and the desired formatting will not be used.

  3. When you create the AgcView's band formats, the names must exactly match the band names defined in the AgcData's Property Inspector.

Defining column formats for a band format   Top of page

The following code creates column formats for a band. It defines the properties of each column and appends it to the band. The following shows the code for creating one column (Product ID) of the three columns in band 1, and the same column in band 2. Note that for band 2, the column uses a different text color and a different font from that of band 1.

  //Code fragment in formActivate() 
   
     // Set the width in one place for both bands 
     int iProdIdWidth = 80; 
     int iProdNameWidth = 220; 
     int iUnitPriceWidth = 100; 
      
     // Create the format, band and column variables 
     AgoViewFormat vwFormatObject = new AgoViewFormat();  
     AgoBandFormat bndFormatObject;  
     AgoColumnText colTextObject;  
      
     // Write the code for Band1             
     bndFormatObject = new AgoBandFormat("Band1");  
   
     colTextObject = new AgoColumnText();  
     colTextObject.setPropertyName("productid");  
     colTextObject.setWidth(iProdIdWidth);  
     bndFormatObject.appendColumnFormat(colTextObject); 
     ... 
     vwFormatObject.appendBandFormat(bndFormatObject);  
     
     // Write the code for Band2 
     // this band is a different color and a different font 
     Color colObject = new Color(255, 0, 0); 
     AgoFontIdentifier fntIdObject = new AgoFontIdentifier(colTextObject.getFontIdentifier());  
   
     bndFormatObject = new AgoBandFormat("Band2");  
      
      colTextObject = new AgoColumnText();  
     colTextObject.setWidth(iProdIdWidth);  
     fntIdObject.setStyle(fntIdObject.BOLD);  
     colTextObject.setFontIdentifier(fntIdObject);  
     colTextObject.setPropertyName("productid");  
     colTextObject.setColor(colObject);  
     bndFormatObject.appendColumnFormat(colTextObject); 

Notes about the code

For band 1, create a column and set its name and its width.

For band 2, create a column and set its name, width, font, text style, and color.

Adding band formats to a view format   Top of page

The following code uses appendBandFormat() to append a band to the view format. Call this method after creating each of the two bands.

  //Code fragment in formActivate() 
   
  vwFormatObject.appendBandFormat(bndFormatObject);  

Notes about the code

Initializing a view with dynamic data and a view format   Top of page

The following code dynamically associates the view with data and the format for viewing the data.

  //Code fragment in formActivate() 
   
  vwProducts.initView(dataProduct, vwFormatObject); 

Notes about the code






Copyright © 2000, SilverStream Software, Inc. All rights reserved.