KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > projectmanagement > presentation > projects > Controls


1 /*
2  * projectManagement
3  *
4  * Enhydra super-servlet presentation object
5  *
6  */

7
8 package projectmanagement.presentation.projects;
9
10 import projectmanagement.presentation.*;
11 import projectmanagement.spec.customer.*;
12
13 import org.w3c.dom.*;
14 import org.w3c.dom.html.*;
15
16 // Enhydra SuperServlet imports
17
import com.lutris.appserver.server.httpPresentation.HttpPresentation;
18 import com.lutris.appserver.server.httpPresentation.HttpPresentationComms;
19 import com.lutris.appserver.server.httpPresentation.HttpPresentationException;
20
21 // Standard imports
22
import java.io.IOException JavaDoc;
23
24 /**
25 * Generates the blank HTML page.
26 */

27 public class Controls implements HttpPresentation {
28
29    /**
30     * Constants
31     */

32    private static String JavaDoc CUSTOMER = "customer";
33
34    public void run(HttpPresentationComms comms) throws HttpPresentationException, IOException JavaDoc {
35       ControlsHTML page=new ControlsHTML();
36
37       String JavaDoc customer = comms.request.getParameter(CUSTOMER);
38       HTMLSelectElement sel=page.getElementCboCustomers();
39       fillSelection(page,sel,customer);
40
41       comms.response.writeDOM(page);
42    }
43
44    private void fillSelection (ControlsHTML page,HTMLSelectElement cboCustomers,
45          String JavaDoc selectedCustomerID) throws ProjectManagementPresentationException {
46       HTMLOptionElement optCustomer = page.getElementOptCustomer();
47  
48   try {
49     
50      CustomerManager customerManager = CustomerManagerFactory.getCustomerManager("projectmanagement.business.customer.CustomerManagerImpl");
51      Customer[] customers = customerManager.getAllCustomers();
52     
53       // Remove the dummy storyboard text from the prototype HTML
54
optCustomer.removeChild(optCustomer.getFirstChild());
55
56      
57          // set the text to select all customers - default option
58
HTMLOptionElement clonedOption = (HTMLOptionElement)optCustomer.cloneNode(true);
59          clonedOption.setValue("");
60          String JavaDoc all="All";
61          Node optionTextNode = clonedOption.getOwnerDocument().
62             createTextNode(all);
63          clonedOption.appendChild(optionTextNode);
64           clonedOption.setAttribute("selected","selected");
65          // Do only a shallow copy of the option as we don't want the text child
66
// of the node option
67
cboCustomers.appendChild(clonedOption);
68
69         
70          
71          // set all customers
72

73          if (customers!=null) {
74             for (int i=0; i<customers.length; i++) {
75                Customer c=customers[i];
76                // Now populate the combo with customers
77
// This algorithm is obscure because options
78
// are not normal HTML elements
79
// First populate the option value (the customer database ID).
80
// Then append a text child as the option
81
// text, which is what is displayed as the text
82
// in each row of the select box
83
clonedOption = (HTMLOptionElement)optCustomer.cloneNode(true);
84                clonedOption.setValue(c.getHandle());
85                optionTextNode = clonedOption.getOwnerDocument().
86                    createTextNode(c.getCompanyName());
87                clonedOption.appendChild(optionTextNode);
88                if (selectedCustomerID!=null && c.getHandle().equals(selectedCustomerID)) {
89                    clonedOption.setAttribute("selected","selected");
90                }
91                // Do only a shallow copy of the option as we don't want the text child
92
// of the node option
93
cboCustomers.appendChild(clonedOption);
94             }
95          }
96           cboCustomers.removeChild(optCustomer);
97          
98 /* Catch Null pointer exception ( we canot make a instances of classes from business layer when we run ProjectManagement_pres )
99  * We need to allow ProjectManagement_pres to be functional , response
100  * will be default HTML page
101  */

102       } catch(NullPointerException JavaDoc ex) {
103       
104       } catch (Exception JavaDoc ex) {
105          //this.writeDebugMsg("Error populating list of customers: " + ex);
106
throw new ProjectManagementPresentationException("Error populating customer list: ", ex);
107       }
108
109      
110    }
111
112 }
113
Popular Tags