Developer

Sample Custom-Menu State

The contents of SendSampleMenu.java and SampleBean.java illustrate how to create a custom-menu state.

SendSampleMenu.java

Some details from this sample have been omitted, because they are similar to those in nonmenu custom-state implementations.
// Package name and imports have been omitted for clarity
        
public class SendSampleMenu extends AbstractStateMenuImpl {
  
  // Other omissions include input and output variable declarations,
  // getRevisionString, getStateId, getStateName, and getStateNotes

  @Override 
  protected int getMaxMenuItems () {           
    return 4;       
  }                

  // Similar implementation as getStateAttributes       

  @Override       
  protected Attribute[] getStateAttributeList() {        

    // Assume stateAttr has been defined        
    return stateAttr.clone();       
  }           

  @Override       
  protected SmappState init(SmappStateProcessingAction action) 
           throws DBException {
    try {             
      // Get the menu list from the source: database or service
      // Convert it to the SampleBean list             
      // See SampleBean class below               

      List<SampleBean> sampleList = getSampleMenuList();                 

      // Store the list in the session variable               
      setMenuListToSession(sampleList);          
    }          
    catch (DBException dbex) {           
      return continueFail();          
    }          
    catch (Exception ex) {           
      return continueFail();          
    }                              
    return null;       
  }               

  @Override       
  protected List<KeyValuePair<String, String>> constructMenuList() 
           throws DBException {
    List<KeyValuePair<String, String>> menuList = 
           new ArrayList<KeyValuePair<String, String>>(); 
    
    for (SampleBean sb : getMenuListFromSession(new SampleBean())) {          
      keyValuePair = new KeyValuePair<String, String>();          
      keyValuePair.setKey(sb.getId());          
      keyValuePair.setValue(sb.getStatus());          
      menuList.add(keyValuePair);        
    }        
    return menuList;       
  }           

  @Override       
  protected SmappState saveSessionVariables(String key, String value) 
           throws DBException {
    int selectedKey = Integer.parseInt(key);              

  }

SampleBean.java

// Package name and imports have been omitted for clarity 
       
public class SampleBean implements BeanConverterInterface<SampleBean> {

  protected String id;       
  protected String status;           
  
  public static SampleBean parse (String id, String status) {        
    SampleBean sb = new SampleBean();        
    sb.id = id;        
    sb.status = status;       
  }           

  @Override       
  public String convert(SampleBean sb) {        
    StringBuilder sb = new StringBuilder();        
    sb.append(sb.getId());        
    sb.append("|");        
    sb.append(sb.getStatus());        
    return sb.toString();       
  }       

  @Override       
  public SampleBean convert(String value) {        
    String[] values = value.split("\\|");        
    Return SampleBean.parse(values[0], values[1]);       
  }           

  public String getId() {        
    return id;       
  }           

  public String getStatus() {        
    return status;       
  }    
}