001: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
002: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
003: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
004: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
005: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
006: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
007: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
008: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
009: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
010: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
011: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
012: // POSSIBILITY OF SUCH DAMAGE.
013: //
014: // Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
015: package com.metaboss.enterprise.xi.enhydrabarracuda.htmlwidgets;
016:
017: import org.w3c.dom.NodeList;
018: import org.w3c.dom.html.HTMLAnchorElement;
019: import org.w3c.dom.html.HTMLDivElement;
020: import org.w3c.dom.html.HTMLDocument;
021: import org.w3c.dom.html.HTMLElement;
022:
023: import com.metaboss.enterprise.ui.UIUnexpectedProgramConditionException;
024: import com.metaboss.enterprise.xi.enhydrabarracuda.Util;
025: import com.metaboss.enterprise.xi.enhydrabarracuda.Widget;
026: import com.metaboss.enterprise.xi.enhydrabarracuda.WidgetModel;
027: import com.metaboss.util.DOMUtils;
028:
029: /** This class represents an implementation of the page index control. It has
030: * buttons to move to the very first page, previous page, next page, very last page
031: * and shows a number of pages at the each side of the current page. It grays the
032: * buttons which are disabled.
033: * <p>This control is represented on the prototype page by the div element.
034: * If the div element contains an anchor (A) element with href - the href will be taken
035: * as the base href for the page control to return.</p> */
036: public class PageNavigationControl extends Widget {
037: /** This class represents the data model of the text control */
038: public static class Model implements WidgetModel {
039: private String mPageNoParameterName = "PageNo";
040: private String mHRef = null;
041: private long mCurrentPageNumber = 0;
042: private long mTotalNumberOfPages = 0;
043:
044: /** Setter for the base href. */
045: public void setHRef(String pHRef) {
046: mHRef = pHRef;
047: }
048:
049: /** Getter for the base href. */
050: public String getHRef() {
051: return mHRef;
052: }
053:
054: /** Setter for the PageNo parameter name. */
055: public void setPageNoParameterName(String pPageNoParameterName) {
056: mPageNoParameterName = pPageNoParameterName;
057: }
058:
059: /** Getter for the for the PageNo parameter name. */
060: public String getPageNoParameterName() {
061: return mPageNoParameterName;
062: }
063:
064: /** Setter for the value for the current page number. */
065: public void setCurrentPageNumber(long pCurrentPageNumber) {
066: mCurrentPageNumber = pCurrentPageNumber;
067: }
068:
069: /** Getter for the value for the current page number. */
070: public long getCurrentPageNumber() {
071: return mCurrentPageNumber;
072: }
073:
074: /** Setter for the total number of pages controlled by this control. */
075: public void setTotalNumberOfPages(long pTotalNumberOfPages) {
076: mTotalNumberOfPages = pTotalNumberOfPages;
077: }
078:
079: /** Getter for the total number of pages controlled by this control. */
080: public long getTotalNumberOfPages() {
081: return mTotalNumberOfPages;
082: }
083:
084: /** This method is used to apply the values passed from the form back into the model.
085: * @return the model object after the form values have been applied (The model object may just return itself)
086: * @exception UIUnexpectedProgramConditionException thrown when this widget model does not support editing */
087: public void applyFormValues(String[] pValues)
088: throws UIUnexpectedProgramConditionException {
089: throw new UIUnexpectedProgramConditionException(
090: "Text widget does not support editing and can not be used in the form");
091: }
092:
093: /** This method used primarily for debug purposes. It is expected to output a single string value reflecting the content of the model */
094: public String getDebugString() {
095: return "Page " + mCurrentPageNumber + " Of "
096: + mTotalNumberOfPages;
097: }
098: }
099:
100: /** This standard method creates an instance of the model object for this widget */
101: public WidgetModel createModel() {
102: HTMLDivElement lControlNode = (HTMLDivElement) getBoundDocumentElement();
103: Model lModel = new Model();
104: // Grab the url from the first anchor in the sample
105: NodeList lAnchorElements = lControlNode
106: .getElementsByTagName("A");
107: for (int i = 0; i < lAnchorElements.getLength(); i++) {
108: HTMLAnchorElement lAnchorElement = (HTMLAnchorElement) lAnchorElements
109: .item(i);
110: String lHRef = lAnchorElement.getHref();
111: if (lHRef != null && lHRef.length() > 0) {
112: lModel.setHRef(lHRef);
113: break; // Only pick the first one up
114: }
115: }
116: return lModel;
117: }
118:
119: /** This lifecycle method is expected to render the control's image onto the document */
120: public void render() {
121: HTMLDivElement lControlNode = (HTMLDivElement) getBoundDocumentElement();
122: Model lModel = (Model) getModel();
123: // Remove all child nodes first
124: DOMUtils.removeAllChildNodes(lControlNode);
125: // There are less than two pages - there is no need for this control.
126: if (lModel.getTotalNumberOfPages() >= 2) {
127: HTMLDocument lDocument = (HTMLDocument) lControlNode
128: .getOwnerDocument();
129: // Add required nodes one by one
130: // Jump to the first page
131: {
132: if (lModel.getCurrentPageNumber() == 1) {
133: HTMLElement lToLastPage = (HTMLElement) lDocument
134: .createElement("SPAN");
135: lToLastPage.appendChild(lDocument
136: .createTextNode("<<"));
137: lControlNode.appendChild(lToLastPage);
138: } else {
139: HTMLAnchorElement lToFirstPage = (HTMLAnchorElement) lDocument
140: .createElement("A");
141: lToFirstPage.appendChild(lDocument
142: .createTextNode("<<"));
143: if (lModel.getHRef() != null) {
144: String lURL = Util.setURLParameter(lModel
145: .getHRef(), lModel
146: .getPageNoParameterName(), "1");
147: lToFirstPage.setHref(lURL);
148: }
149: lControlNode.appendChild(lToFirstPage);
150: }
151: }
152: // Fill some spaces
153: {
154: HTMLElement lFiller = (HTMLElement) lDocument
155: .createElement("SPAN");
156: lFiller.appendChild(lDocument.createTextNode(" "));
157: lControlNode.appendChild(lFiller);
158: }
159: // Jump to the previous page
160: {
161: if (lModel.getCurrentPageNumber() == 1) {
162: HTMLElement lToLastPage = (HTMLElement) lDocument
163: .createElement("SPAN");
164: lToLastPage.appendChild(lDocument
165: .createTextNode("<"));
166: lControlNode.appendChild(lToLastPage);
167: } else {
168: HTMLAnchorElement lToFirstPage = (HTMLAnchorElement) lDocument
169: .createElement("A");
170: lToFirstPage.appendChild(lDocument
171: .createTextNode("<"));
172: if (lModel.getHRef() != null) {
173: String lURL = Util.setURLParameter(lModel
174: .getHRef(), lModel
175: .getPageNoParameterName(),
176: Long.toString(lModel
177: .getCurrentPageNumber() - 1));
178: lToFirstPage.setHref(lURL);
179: }
180: lControlNode.appendChild(lToFirstPage);
181: }
182: }
183: // Fill some spaces
184: {
185: HTMLElement lFiller = (HTMLElement) lDocument
186: .createElement("SPAN");
187: lFiller.appendChild(lDocument.createTextNode(" "));
188: lControlNode.appendChild(lFiller);
189: }
190: // Jump to the next page
191: {
192: if (lModel.getCurrentPageNumber() == lModel
193: .getTotalNumberOfPages()) {
194: HTMLElement lToLastPage = (HTMLElement) lDocument
195: .createElement("SPAN");
196: lToLastPage.appendChild(lDocument
197: .createTextNode(">"));
198: lControlNode.appendChild(lToLastPage);
199: } else {
200: HTMLAnchorElement lToLastPage = (HTMLAnchorElement) lDocument
201: .createElement("A");
202: lToLastPage.appendChild(lDocument
203: .createTextNode(">"));
204: if (lModel.getHRef() != null) {
205: String lURL = Util.setURLParameter(lModel
206: .getHRef(), lModel
207: .getPageNoParameterName(),
208: Long.toString(lModel
209: .getCurrentPageNumber() + 1));
210: lToLastPage.setHref(lURL);
211: }
212: lControlNode.appendChild(lToLastPage);
213: }
214: }
215: // Fill some spaces
216: {
217: HTMLElement lFiller = (HTMLElement) lDocument
218: .createElement("SPAN");
219: lFiller.appendChild(lDocument.createTextNode(" "));
220: lControlNode.appendChild(lFiller);
221: }
222: // Jump to the last page
223: {
224: if (lModel.getCurrentPageNumber() == lModel
225: .getTotalNumberOfPages()) {
226: HTMLElement lToLastPage = (HTMLElement) lDocument
227: .createElement("SPAN");
228: lToLastPage.appendChild(lDocument
229: .createTextNode(">>"));
230: lControlNode.appendChild(lToLastPage);
231: } else {
232: HTMLAnchorElement lToLastPage = (HTMLAnchorElement) lDocument
233: .createElement("A");
234: lToLastPage.appendChild(lDocument
235: .createTextNode(">>"));
236: if (lModel.getHRef() != null) {
237: String lURL = Util.setURLParameter(lModel
238: .getHRef(), lModel
239: .getPageNoParameterName(), Long
240: .toString(lModel
241: .getTotalNumberOfPages()));
242: lToLastPage.setHref(lURL);
243: }
244: lControlNode.appendChild(lToLastPage);
245: }
246: }
247: }
248: }
249: }
|