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.Item;
020: import info.jtrac.domain.ItemSearch;
021: import info.jtrac.exception.InvalidRefIdException;
022: import info.jtrac.wicket.yui.YuiPanel;
023: import org.apache.wicket.PageParameters;
024: import org.apache.wicket.ajax.AjaxRequestTarget;
025: import org.apache.wicket.ajax.markup.html.form.AjaxSubmitButton;
026: import org.apache.wicket.markup.html.form.Form;
027: import org.apache.wicket.markup.html.form.TextField;
028: import org.apache.wicket.markup.html.panel.FeedbackPanel;
029: import org.apache.wicket.model.PropertyModel;
030:
031: /**
032: * view by ref id form panel
033: * the submit is done over ajax and validation errors also
034: * are shown using ajax
035: */
036: public class ItemRefIdFormPanel extends BasePanel {
037:
038: private TextField refIdField;
039: private YuiPanel yuiPanel;
040:
041: // TODO nice frameworky way to do this
042: public void setYuiPanel(YuiPanel yuiPanel) {
043: this .yuiPanel = yuiPanel;
044: }
045:
046: public String getFocusScript() {
047: return "document.getElementById('" + refIdField.getMarkupId()
048: + "').focus();";
049: }
050:
051: public ItemRefIdFormPanel(String id) {
052: super (id);
053: add(new ItemRefIdForm());
054: }
055:
056: /**
057: * wicket form
058: */
059: private class ItemRefIdForm extends Form {
060:
061: private String refId;
062:
063: public String getRefId() {
064: return refId;
065: }
066:
067: public void setRefId(String refId) {
068: this .refId = refId;
069: }
070:
071: public ItemRefIdForm() {
072: super ("form");
073: final FeedbackPanel feedback;
074: feedback = new FeedbackPanel("feedback");
075: add(feedback);
076: feedback.setOutputMarkupId(true);
077: refIdField = new TextField("refId", new PropertyModel(this ,
078: "refId"));
079: refIdField.setOutputMarkupId(true);
080: refIdField.add(new ErrorHighlighter());
081: add(refIdField);
082: add(new AjaxSubmitButton("submit", this ) {
083: @Override
084: protected void onError(AjaxRequestTarget target,
085: Form form) {
086: target.addComponent(feedback);
087: // hack for IE, flip visibility will re-size panel to accomodate area size change
088: target.appendJavascript(yuiPanel.getHideScript()
089: + yuiPanel.getShowScript()
090: + getFocusScript());
091: }
092:
093: protected void onSubmit(AjaxRequestTarget target,
094: Form form) {
095: target.addComponent(feedback);
096: if (refId == null) {
097: refIdField
098: .error(localize("item_search_form.error.refId.invalid"));
099: return;
100: }
101: Item item = null;
102: try {
103: item = getJtrac().loadItemByRefId(refId);
104: } catch (InvalidRefIdException e) {
105: refIdField
106: .error(localize("item_search_form.error.refId.invalid"));
107: return;
108: }
109: if (item == null) {
110: refIdField
111: .error(localize("item_search_form.error.refId.notFound"));
112: return;
113: }
114: ItemSearch itemSearch = getCurrentItemSearch();
115: if (itemSearch == null
116: || itemSearch.getRelatingItemRefId() == null) {
117: setCurrentItemSearch(null); // disable back link for item view
118: }
119: setResponsePage(ItemViewPage.class,
120: new PageParameters("0=" + refId));
121: }
122: });
123: }
124:
125: }
126:
127: }
|