Application Techniques



Manipulating Views

How to navigate and manipulate 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

The example lists employees in a selected department. It shows how to query the database, navigate the rows, get and set cell values, and update the database. A choice control filters which department is shown in the view. A slider control lets you change all the displayed salaries by a percentage rate. You can permanently save the changes with a Save button.

Setting a default department in the choice control   Top of page

This code illustrates how to set up a default department in the department choice control.

  protected void formLoaded() 
    { 
      // Select the first department 
      cmbxDepartments.setSelectedIndex(0); 
    } 

Notes about the code

Handling a user selection from the choice control   Top of page

This code illustrates how to translate a selection made by you from the choice control into a department id by which the employee data is filtered.

  private void handle_cmbxDepartments_valueChanged(AgoPropertyChangeEvent evt) 
    { 
      try 
      { 
        // Get the view to refresh the list of employees  
        // for the department selected 
        vwViewManipulation.query("employees.deptid =" + 
          cmbxDepartments.getValue()); 
     
        // Set the slider back to 0 
        sldrRate.setValue(0); 
     
        // Display 0% 
        lblRate.setText("0%"); 
      } 
      catch (AgoApiException e) 
      { 
        agDialog.displayError(e); 
        return; 
      }     
    } 

Notes about the code

Handling the rate percentage slider   Top of page

The following code shows how the slider control is handled when its value is changed.

  private void handle_sldrRate_valueChanged(AgoPropertyChangeEvent 
    evt) 
    { 
      // User has changed the slider value, so get the new value 
      Integer intRate = new Integer(sldrRate.getValue()); 
     
      // Display the new silder value 
      lblRate.setText(intRate.toString() + "%"); 
     
      // Re-calculate the salaries in the view 
      changeRate(); 
       
    } 
   

Notes about the code

Making the salary rate change   Top of page

The following code illustrates a custom method which changes the displayed salaries for the employees of the selected department. This method is called whenever you moves the rate percentage slider. The changes are not permanent until you clicks the Save button.

  public void changeRate() 
    { 
      // We may already have calculated new salaries based on  
      // some other percentage, so refresh the view so that we have 
     // the current salary for each employee 
      try 
      { 
        vwViewManipulation.query("employees.deptid = " + 
        cmbxDepartments.getValue()); 
      } 
      catch (AgoApiException e) 
      { 
        agDialog.displayError(" **** " ,e); 
        return; 
      } 
      // Get the slider value to be used as a percentage increase  
      // in salary 
      double dbRate = sldrRate.getValue() * 1.0; 
     
      // Now that the view has been refreshed, we need to get 
      // a cursor pointing to the first row in the view 
      AgiRowCursor employees = 
        vwViewManipulation.getSelectedRowCursor(); 
     
      // Got a row cursor? We won't if the department has  
      // no employees, so return 
      if (employees == null) return; 
     
      try 
      { 
        // Loop through all the rows and calculate the new  
        // salary for each employee 
        if (employees.gotoFirst())  
          do  
          { 
            // Get the employee's current salary 
            BigDecimal bdSalary = (BigDecimal) 
              employees.getProperty("salary"); 
            // Got a salary? 
            if (bdSalary != null) 
            { 
              // Yes, so apply the increase 
              double dbNewSalary = ((dbRate / 100) + 1.00) 
                * bdSalary.doubleValue(); 
              // New salary as a BigDecimal 
              bdSalary = new BigDecimal(dbNewSalary); 
              // Update the view with the employee's new salary 
              employees.setProperty("salary", bdSalary); 
            } 
          } 
          while (employees.gotoNext()); 
      } 
      catch (AgoApiException e) 
      { 
        agDialog.displayError(e); 
        return; 
      } 
    } 

Notes about the code

Saving the changes   Top of page

The following code illustrates changes are actually updated in the database.

  private void handle_jbtnSave_actionPerformed(ActionEvent evt) 
    { 
        // Save changes to the database 
      try 
      { 
        vwViewManipulation.updateRows(); 
      } 
      catch (AgoApiException e) 
      { 
        agDialog.displayError(e); 
        return; 
      } 
    } 

Notes about the code






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