KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

33    private static String JavaDoc EMPLOYEE = "employee";
34    private static String JavaDoc PROJECT = "project";
35
36    public void run(HttpPresentationComms comms) throws HttpPresentationException, IOException JavaDoc {
37       ControlsHTML page=new ControlsHTML();
38
39       String JavaDoc employee = comms.request.getParameter(EMPLOYEE);
40       HTMLSelectElement sel=page.getElementCboEmployee();
41       fillEmployeeSelection(page,sel,employee);
42
43       String JavaDoc project = comms.request.getParameter(PROJECT);
44       sel=page.getElementCboProject();
45       fillProjectSelection(page,sel,project);
46
47       comms.response.writeDOM(page);
48    }
49
50    private void fillEmployeeSelection (ControlsHTML page,HTMLSelectElement cboEmployees,
51          String JavaDoc selectedEmployeeID) throws ProjectManagementPresentationException {
52       HTMLOptionElement optEmployee = page.getElementOptEmployee();
53     
54  try {
55      EmployeeManager employeeManager = EmployeeManagerFactory.getEmployeeManager("projectmanagement.business.employee.EmployeeManagerImpl");
56      Employee[] employees=employeeManager.getAllEmployees();
57  
58       // Remove the dummy storyboard text from the prototype HTML
59
optEmployee.removeChild(optEmployee.getFirstChild());
60
61     
62          // set the text to select all employees - default option
63
HTMLOptionElement clonedOption = (HTMLOptionElement)optEmployee.cloneNode(true);
64          clonedOption.setValue("");
65          String JavaDoc all="All";
66          Node optionTextNode = clonedOption.getOwnerDocument().
67             createTextNode(all);
68          clonedOption.appendChild(optionTextNode);
69           clonedOption.setAttribute("selected","selected");
70
71          // Do only a shallow copy of the option as we don't want the text child
72
// of the node option
73
cboEmployees.appendChild(clonedOption);
74
75         
76  
77        // set all employees
78
if (employees!=null) {
79             for (int i=0; i<employees.length; i++) {
80                Employee e=employees[i];
81                // Now populate the combo with employees
82
// This algorithm is obscure because options
83
// are not normal HTML elements
84
// First populate the option value (the employee database ID).
85
// Then append a text child as the option
86
// text, which is what is displayed as the text
87
// in each row of the select box
88
clonedOption = (HTMLOptionElement)optEmployee.cloneNode(true);
89                clonedOption.setValue(e.getHandle());
90                optionTextNode = clonedOption.getOwnerDocument().
91                    createTextNode(e.getFirstName()+" "+e.getLastName());
92                clonedOption.appendChild(optionTextNode);
93                if (selectedEmployeeID!=null && e.getHandle().equals(selectedEmployeeID)) {
94                   clonedOption.setAttribute("selected","selected");
95
96                }
97                // Do only a shallow copy of the option as we don't want the text child
98
// of the node option
99
cboEmployees.appendChild(clonedOption);
100             }
101          }
102          cboEmployees.removeChild(optEmployee);
103       } catch(NullPointerException JavaDoc ex) {
104       } catch (Exception JavaDoc ex) {
105          //this.writeDebugMsg("Error populating list of employees: " + ex);
106
throw new ProjectManagementPresentationException("Error populating employee list: ", ex);
107       }
108
109       
110    }
111
112    private void fillProjectSelection (ControlsHTML page,HTMLSelectElement cboProjects,
113          String JavaDoc selectedProjectID) throws ProjectManagementPresentationException {
114       HTMLOptionElement optProject = page.getElementOptProject();
115    
116
117   try {
118
119         ProjectManager projectManager = ProjectManagerFactory.getProjectManager("projectmanagement.business.project.ProjectManagerImpl");
120           Project[] projects=projectManager.getAllProjects();
121    
122        // Remove the dummy storyboard text from the prototype HTML
123
optProject.removeChild(optProject.getFirstChild());
124
125          // set the text to select all projects - default option
126
HTMLOptionElement clonedOption = (HTMLOptionElement)optProject.cloneNode(true);
127          clonedOption.setValue("");
128          String JavaDoc all="All";
129          Node optionTextNode = clonedOption.getOwnerDocument().
130             createTextNode(all);
131          clonedOption.appendChild(optionTextNode);
132          clonedOption.setAttribute("selected","selected");
133
134          // Do only a shallow copy of the option as we don't want the text child
135
// of the node option
136
cboProjects.appendChild(clonedOption);
137
138          // set all projects
139

140         
141          if (projects!=null) {
142             for (int i=0; i<projects.length; i++) {
143                Project p=projects[i];
144                // Now populate the combo with projects
145
// This algorithm is obscure because options
146
// are not normal HTML elements
147
// First populate the option value (the project database ID).
148
// Then append a text child as the option
149
// text, which is what is displayed as the text
150
// in each row of the select box
151
clonedOption = (HTMLOptionElement)optProject.cloneNode(true);
152                clonedOption.setValue(p.getHandle());
153                optionTextNode = clonedOption.getOwnerDocument().
154                    createTextNode(p.getName());
155                clonedOption.appendChild(optionTextNode);
156                if (selectedProjectID!=null && p.getHandle().equals(selectedProjectID)) {
157                clonedOption.setAttribute("selected","selected");
158
159                }
160                // Do only a shallow copy of the option as we don't want the text child
161
// of the node option
162
cboProjects.appendChild(clonedOption);
163             }
164          }
165          
166       cboProjects.removeChild(optProject);
167       
168       } catch(NullPointerException JavaDoc ex) {
169       } catch (Exception JavaDoc ex) {
170          //this.writeDebugMsg("Error populating list of projects: " + ex);
171
throw new ProjectManagementPresentationException("Error populating project list: ", ex);
172       }
173
174    }
175
176 }
177
Popular Tags