001: /*
002: * Copyright 2002-2005 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package info.jtrac.wicket;
018:
019: import info.jtrac.domain.User;
020: import java.util.Arrays;
021: import java.util.List;
022: import org.apache.wicket.behavior.HeaderContributor;
023: import org.apache.wicket.behavior.SimpleAttributeModifier;
024: import org.apache.wicket.markup.html.IHeaderContributor;
025: import org.apache.wicket.markup.html.IHeaderResponse;
026: import org.apache.wicket.markup.html.WebMarkupContainer;
027: import org.apache.wicket.markup.html.basic.Label;
028: import org.apache.wicket.markup.html.form.DropDownChoice;
029: import org.apache.wicket.markup.html.form.Form;
030: import org.apache.wicket.markup.html.form.IChoiceRenderer;
031: import org.apache.wicket.markup.html.form.TextField;
032: import org.apache.wicket.markup.html.link.Link;
033: import org.apache.wicket.markup.html.list.ListItem;
034: import org.apache.wicket.markup.html.list.ListView;
035: import org.apache.wicket.model.CompoundPropertyModel;
036: import org.apache.wicket.model.LoadableDetachableModel;
037: import org.apache.wicket.model.PropertyModel;
038:
039: /**
040: * user management page
041: */
042: public class UserListPage extends BasePage {
043:
044: private long selectedUserId;
045:
046: private String searchText = "";
047: private String searchOn = "name";
048:
049: public void setSelectedUserId(long selectedUserId) {
050: this .selectedUserId = selectedUserId;
051: }
052:
053: public UserListPage() {
054: add(new Link("create") {
055: public void onClick() {
056: UserFormPage page = new UserFormPage();
057: page.setPrevious(UserListPage.this );
058: setResponsePage(page);
059: }
060: });
061:
062: add(new SearchForm("form"));
063:
064: LoadableDetachableModel userListModel = new LoadableDetachableModel() {
065: protected Object load() {
066: if (searchText == null) {
067: return getJtrac().findAllUsers();
068: } else if (searchText.equals("")) {
069: // first time page loaded, don't query
070: return null;
071: } else {
072: return getJtrac().findUsersMatching(searchText,
073: searchOn);
074: }
075: }
076: };
077:
078: final SimpleAttributeModifier sam = new SimpleAttributeModifier(
079: "class", "alt");
080:
081: ListView listView = new ListView("users", userListModel) {
082: protected void populateItem(ListItem listItem) {
083: final User user = (User) listItem.getModelObject();
084: if (selectedUserId == user.getId()) {
085: listItem.add(new SimpleAttributeModifier("class",
086: "selected"));
087: } else if (listItem.getIndex() % 2 == 1) {
088: listItem.add(sam);
089: }
090: listItem.add(new Label("name", new PropertyModel(user,
091: "name")));
092: listItem.add(new Label("loginName", new PropertyModel(
093: user, "loginName")));
094: listItem.add(new Label("email", new PropertyModel(user,
095: "email")));
096: listItem.add(new Label("locale", new PropertyModel(
097: user, "locale")));
098: listItem.add(new WebMarkupContainer("locked")
099: .setVisible(user.isLocked()));
100: listItem.add(new Link("edit") {
101: public void onClick() {
102: UserFormPage page = new UserFormPage(user);
103: page.setPrevious(UserListPage.this );
104: setResponsePage(page);
105: }
106: });
107: listItem.add(new Link("allocate") {
108: public void onClick() {
109: setResponsePage(new UserAllocatePage(user
110: .getId(), UserListPage.this ));
111: }
112: });
113: }
114: };
115:
116: add(listView);
117:
118: }
119:
120: /**
121: * wicket form
122: */
123: private class SearchForm extends Form {
124:
125: public String getSearchText() {
126: return searchText;
127: }
128:
129: public void setSearchText(String searchText) {
130: UserListPage.this .searchText = searchText;
131: }
132:
133: public String getSearchOn() {
134: return searchOn;
135: }
136:
137: public void setSearchOn(String searchOn) {
138: UserListPage.this .searchOn = searchOn;
139: }
140:
141: public SearchForm(String id) {
142: super (id);
143: setModel(new CompoundPropertyModel(this ));
144: List<String> searchOnOptions = Arrays.asList(new String[] {
145: "name", "loginName", "email" });
146: DropDownChoice searchOnChoice = new DropDownChoice(
147: "searchOn", searchOnOptions, new IChoiceRenderer() {
148: public Object getDisplayValue(Object o) {
149: String s = (String) o;
150: if (s.equals("name")) {
151: s = "userName"; // to match i18 key
152: }
153: return localize("user_list." + s);
154: }
155:
156: public String getIdValue(Object o, int i) {
157: return o.toString();
158: }
159: });
160: add(searchOnChoice);
161: final TextField searchTextField = new TextField(
162: "searchText");
163: searchTextField.setOutputMarkupId(true);
164: add(searchTextField);
165: add(new HeaderContributor(new IHeaderContributor() {
166: public void renderHead(IHeaderResponse response) {
167: response
168: .renderOnLoadJavascript("document.getElementById('"
169: + searchTextField.getMarkupId()
170: + "').focus()");
171: }
172: }));
173: }
174:
175: @Override
176: protected void onSubmit() {
177: // setResponsePage(UserListPage.this);
178: }
179:
180: }
181:
182: }
|