1 package org.whatsitcalled.webflange.webapp;
2
3 import java.util.Set;
4
5 import org.apache.wicket.AttributeModifier;
6 import org.apache.wicket.behavior.SimpleAttributeModifier;
7 import org.apache.wicket.markup.html.basic.Label;
8 import org.apache.wicket.markup.html.form.Form;
9 import org.apache.wicket.markup.html.form.RequiredTextField;
10 import org.apache.wicket.markup.html.form.TextArea;
11 import org.apache.wicket.markup.html.link.Link;
12 import org.apache.wicket.markup.html.panel.Panel;
13 import org.apache.wicket.markup.repeater.Item;
14 import org.apache.wicket.markup.repeater.data.DataView;
15 import org.apache.wicket.model.AbstractReadOnlyModel;
16 import org.apache.wicket.model.CompoundPropertyModel;
17 import org.apache.wicket.model.IModel;
18 import org.whatsitcalled.webflange.ResourceFactory;
19 import org.whatsitcalled.webflange.model.LoadTest;
20 import org.whatsitcalled.webflange.model.Project;
21 import org.whatsitcalled.webflange.webapp.model.ProjectDataProvider;
22
23 public class ProjectPanel extends Panel {
24 AddProjectForm projectForm;
25
26 public ProjectPanel(String id) {
27 super(id);
28 add(new Link("newProject") {
29 public void onClick() {
30 projectForm.setVisible(true);
31 setProject(new Project());
32 }
33 });
34 add(new Label("selectHdr", "Action"));
35 add(new Label("projectHdr", "Project Name"));
36 add(new Label("descHdr", "Description"));
37
38 add(new DataView("projects", new ProjectDataProvider()) {
39 protected void populateItem(final Item item) {
40 final Project project = (Project) item.getModelObject();
41 item.add(new Link("edit") {
42 public void onClick() {
43 setProject((Project) getParent().getModelObject());
44 projectForm.setVisible(true);
45 }
46 });
47
48
49 Link deleteLink = new Link("delete") {
50 public void onClick() {
51 Project project = (Project) getParent()
52 .getModelObject();
53 ResourceFactory.getProjectDAO().removeProject(
54 project.getId());
55 }
56 };
57
58
59 Set<LoadTest> tests = project.getLoadTests();
60 String testString = "\\n";
61 for (LoadTest test : tests) {
62 testString = testString + test.getName() + "\\n";
63 }
64
65 if (tests != null && tests.size() > 0) {
66 deleteLink
67 .add(new SimpleAttributeModifier(
68 "onclick",
69 "alert('This project cannot be deleted, as it is being used by the following LoadTests: "
70 + testString + "'); return false;"));
71
72 } else {
73 deleteLink.add(new SimpleAttributeModifier("onclick",
74 "return confirm('Delete project "
75 + project.getName() + "?');"));
76 }
77 item.add(deleteLink);
78
79 item.add(new Label("name", project.getName()));
80 item.add(new Label("description", project.getDescription()));
81
82 item.add(new AttributeModifier("class", true,
83 new AbstractReadOnlyModel() {
84 public Object getObject() {
85 return (item.getIndex() % 2 == 1) ? "even"
86 : "odd";
87 }
88 }));
89 }
90 });
91
92
93 projectForm = new AddProjectForm("addProjectForm",
94 new CompoundPropertyModel(getProject()));
95 projectForm.setVisible(false);
96 add(projectForm);
97
98 }
99
100 private Project getProject() {
101 Project project = (Project) getModelObject();
102 if (project == null) {
103 project = new Project();
104 setProject(project);
105 }
106 return project;
107 }
108
109 private void setProject(Project project) {
110 setModel(new CompoundPropertyModel(project));
111 }
112
113 public void onModelChanged() {
114 if ((projectForm != null) && this.getModel() != null) {
115 projectForm.setModel(this.getModel());
116 }
117 }
118
119 public final class AddProjectForm extends Form {
120
121
122
123
124
125
126 public AddProjectForm(final String id, IModel model) {
127
128
129 super(id, new CompoundPropertyModel(getProject()));
130
131 add(new RequiredTextField("name"));
132 add(new TextArea("description"));
133 }
134
135
136
137
138 public final void onSubmit() {
139 final Project project = (Project) getModelObject();
140
141 ResourceFactory.getProjectService().saveProject(project);
142
143 this.setModelObject(new CompoundPropertyModel(new Project()));
144 projectForm.setVisible(false);
145 }
146 }
147 }