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.ColumnHeading;
020: import info.jtrac.domain.FilterCriteria.Expression;
021: import info.jtrac.domain.Item;
022: import info.jtrac.domain.ItemRefId;
023: import info.jtrac.domain.ItemSearch;
024: import info.jtrac.domain.Space;
025: import info.jtrac.domain.User;
026: import info.jtrac.exception.InvalidRefIdException;
027: import java.util.Arrays;
028: import java.util.List;
029: import org.apache.wicket.Component;
030: import org.apache.wicket.PageParameters;
031: import org.apache.wicket.ajax.AjaxRequestTarget;
032: import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
033: import org.apache.wicket.markup.html.WebMarkupContainer;
034: import org.apache.wicket.markup.html.basic.Label;
035: import org.apache.wicket.markup.html.form.Button;
036: import org.apache.wicket.markup.html.form.CheckBox;
037: import org.apache.wicket.markup.html.form.DropDownChoice;
038: import org.apache.wicket.markup.html.form.Form;
039: import org.apache.wicket.markup.html.form.IChoiceRenderer;
040: import org.apache.wicket.markup.html.link.Link;
041: import org.apache.wicket.markup.html.list.ListItem;
042: import org.apache.wicket.markup.html.list.ListView;
043: import org.apache.wicket.markup.html.panel.FeedbackPanel;
044: import org.apache.wicket.model.CompoundPropertyModel;
045: import org.apache.wicket.model.PropertyModel;
046:
047: /**
048: * item search form panel
049: */
050: public class ItemSearchFormPanel extends BasePanel {
051:
052: private ItemSearch itemSearch;
053: private boolean expandAll;
054:
055: public ItemSearchFormPanel(String id, User user) {
056: super (id);
057: this .itemSearch = new ItemSearch(user, this );
058: addComponents();
059: }
060:
061: public ItemSearchFormPanel(String id) {
062: super (id);
063: Space s = getCurrentSpace();
064: if (s != null) {
065: this .itemSearch = new ItemSearch(s, this );
066: } else {
067: this .itemSearch = new ItemSearch(getPrincipal(), this );
068: }
069: addComponents();
070: }
071:
072: public ItemSearchFormPanel(String id, ItemSearch itemSearch) {
073: super (id);
074: this .itemSearch = itemSearch;
075: addComponents();
076: }
077:
078: private void addComponents() {
079: final Form form = new Form("form");
080: add(form);
081: form.add(new FeedbackPanel("feedback"));
082: form.setModel(new CompoundPropertyModel(itemSearch));
083: List<Integer> sizes = Arrays.asList(new Integer[] { 5, 10, 15,
084: 25, 50, 100, -1 });
085: DropDownChoice pageSizeChoice = new DropDownChoice("pageSize",
086: sizes, new IChoiceRenderer() {
087: public Object getDisplayValue(Object o) {
088: return ((Integer) o) == -1 ? localize("item_search_form.noLimit")
089: : o.toString();
090: }
091:
092: public String getIdValue(Object o, int i) {
093: return o.toString();
094: }
095: });
096: form.add(pageSizeChoice);
097: form.add(new CheckBox("showHistory"));
098: form.add(new Button("search") {
099: @Override
100: public void onSubmit() {
101: String refId = itemSearch.getRefId();
102: if (refId != null) {
103: if (getCurrentSpace() != null) {
104: // user can save typing by entering the refId number without the space prefixCode
105: try {
106: long id = Long.parseLong(refId);
107: refId = getCurrentSpace().getPrefixCode()
108: + "-" + id;
109: } catch (Exception e) {
110: // oops that didn't work, continue
111: }
112: }
113: try {
114: new ItemRefId(refId);
115: } catch (InvalidRefIdException e) {
116: form
117: .error(localize("item_search_form.error.refId.invalid"));
118: return;
119: }
120: Item item = getJtrac().loadItemByRefId(refId);
121: if (item == null) {
122: form
123: .error(localize("item_search_form.error.refId.notFound"));
124: return;
125: }
126: setCurrentItemSearch(itemSearch);
127: setResponsePage(ItemViewPage.class,
128: new PageParameters("0=" + item.getRefId()));
129: return;
130: }
131: String searchText = itemSearch.getSearchText();
132: if (searchText != null) {
133: if (!getJtrac().validateTextSearchQuery(searchText)) {
134: form
135: .error(localize("item_search_form.error.summary.invalid"));
136: return;
137: }
138: }
139: setCurrentItemSearch(itemSearch);
140: setResponsePage(ItemListPage.class);
141: }
142: });
143: form.add(new Link("expandAll") {
144: public void onClick() {
145: expandAll = true;
146: }
147:
148: @Override
149: public boolean isVisible() {
150: return !expandAll;
151: }
152: });
153: form
154: .add(new ListView("columns", itemSearch
155: .getColumnHeadings()) {
156: protected void populateItem(final ListItem listItem) {
157: final ColumnHeading ch = (ColumnHeading) listItem
158: .getModelObject();
159: String label = ch.isField() ? ch.getLabel()
160: : localize("item_list." + ch.getName());
161: listItem.add(new Label("columnName", label));
162: listItem.add(new CheckBox("visible",
163: new PropertyModel(ch, "visible")));
164: List<Expression> validExpressions = ch
165: .getValidFilterExpressions();
166: DropDownChoice expressionChoice = new IndicatingDropDownChoice(
167: "expression", validExpressions,
168: new IChoiceRenderer() {
169: public Object getDisplayValue(
170: Object o) {
171: String key = ((Expression) o)
172: .getKey();
173: return localize("item_filter."
174: + key);
175: }
176:
177: public String getIdValue(Object o,
178: int i) {
179: return ((Expression) o)
180: .getKey();
181: }
182: });
183: if (ch.getName().equals(ColumnHeading.ID)) {
184: ch.getFilterCriteria().setExpression(
185: Expression.EQ);
186: }
187: Component fragParent = null;
188: if (expandAll) {
189: ch.getFilterCriteria().setExpression(
190: validExpressions.get(0));
191: fragParent = ch
192: .getFilterUiFragment(ItemSearchFormPanel.this );
193: } else {
194: fragParent = getFilterUiFragment(ch);
195: }
196: fragParent.setOutputMarkupId(true);
197: listItem.add(fragParent);
198: expressionChoice.setModel(new PropertyModel(ch
199: .getFilterCriteria(), "expression"));
200: expressionChoice.setNullValid(true);
201: listItem.add(expressionChoice);
202: expressionChoice
203: .add(new AjaxFormComponentUpdatingBehavior(
204: "onChange") {
205: protected void onUpdate(
206: AjaxRequestTarget target) {
207: if (!ch
208: .getFilterCriteria()
209: .requiresUiFragmentUpdate()) {
210: return;
211: }
212: Component fragment = getFilterUiFragment(ch);
213: fragment
214: .setOutputMarkupId(true);
215: listItem.replace(fragment);
216: target.addComponent(fragment);
217: target
218: .appendJavascript("document.getElementById('"
219: + fragment
220: .getMarkupId()
221: + "').focus()");
222: }
223: });
224: }
225: });
226: }
227:
228: private Component getFilterUiFragment(ColumnHeading ch) {
229: if (ch.getFilterCriteria().getExpression() == null) {
230: return new WebMarkupContainer("fragParent");
231: }
232: return ch.getFilterUiFragment(ItemSearchFormPanel.this);
233: }
234:
235: }
|