FileDocCategorySizeDatePackage
SubscribeView.javaAPI DocExample3020Tue Jun 08 11:26:42 BST 2004com.mycompany.newsservice.views

SubscribeView.java

package com.mycompany.newsservice.views;

import javax.faces.application.Application;
import javax.faces.component.UICommand;
import javax.faces.component.UIForm;
import javax.faces.component.UIInput;
import javax.faces.component.UIOutput;
import javax.faces.component.UIPanel;
import javax.faces.component.UISelectItems;
import javax.faces.component.UISelectMany;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.faces.el.MethodBinding;
import javax.faces.el.ValueBinding;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mycompany.jsf.pl.View;

/**
 * This class is an example of a pure Java view representation for
 * a fictitious newsletter subscription service subscription interface,
 * used by the example ClassViewHandler implementation.
 *
 * @author Hans Bergsten, Gefion Software <hans@gefionsoftware.com>
 * @version 1.0
 */
public class SubscribeView implements View {
    public UIViewRoot createView(FacesContext context) {
        Application application = context.getApplication();
        UIViewRoot viewRoot = new UIViewRoot();

        UIForm form = new UIForm();
        viewRoot.getChildren().add(form);

        UIPanel grid = new UIPanel();
        grid.setRendererType("javax.faces.Grid");
        grid.getAttributes().put("columns", "2");

        UIOutput emailLabel = new UIOutput();
        emailLabel.setValue("Email Address:");
        grid.getChildren().add(emailLabel);
        UIInput email = new UIInput();
        ValueBinding emailAddr = 
            application.createValueBinding("#{subscr.emailAddr}");
        email.setValueBinding("value", emailAddr);
        grid.getChildren().add(email);

        UIOutput subsLabel = new UIOutput();
        subsLabel.setValue("Newsletters:");
        grid.getChildren().add(subsLabel);
        UISelectMany subs = new UISelectMany();
        subs.setRendererType("javax.faces.Checkbox");
        ValueBinding subIds = 
            application.createValueBinding("#{subscr.subscriptionIds}");
        subs.setValueBinding("value", subIds);
        UISelectItems sis = new UISelectItems();
        List choices = new ArrayList();
        choices.add(new SelectItem("1", "JSF News"));
        choices.add(new SelectItem("2", "IT Industry News"));
        choices.add(new SelectItem("3", "Company News"));
        sis.setValue(choices);
        subs.getChildren().add(sis);
        grid.getChildren().add(subs);
        form.getChildren().add(grid);

        UICommand command = new UICommand();
        command.setValue("Save");
        MethodBinding action = 
            application.createMethodBinding("#{subscrHandler.saveSubscriber}",
                null);
        command.setAction(action);
        form.getChildren().add(command);
        viewRoot.getChildren().add(form);

        return viewRoot;
    }
}