1 11 package org.jboss.portlet.forums.helper; 12 13 14 19 public class GotoWithOffset 20 extends Range 21 { 22 private int perPage; 23 private String URL; 24 private int offset; 25 private int size; 26 private String gotoPageLabel; 27 private String previousLabel; 28 private String nextLabel; 29 30 41 public GotoWithOffset(int size, 42 int offset, 43 int perPage, 44 String URL, 45 String gotoPageLabel, 46 String previousLabel, 47 String nextLabel) 48 { 49 super(3, 3, 3); 50 this.size = size; 51 this.offset = offset; 52 this.perPage = perPage; 53 this.URL = URL; 54 this.gotoPageLabel = gotoPageLabel; 55 this.previousLabel = previousLabel; 56 this.nextLabel = nextLabel; 57 } 58 59 64 public void setOffset(int offset) 65 { 66 this.offset = offset; 67 } 68 69 74 protected final void start(StringBuffer buffer) 75 { 76 buffer.append(gotoPageLabel); 77 buffer.append(" "); 78 int offset = (int) Math.floor((double) this.offset / (double) perPage); 79 if (offset > 0) 80 { 81 offset = (offset - 1) * perPage; 82 buffer.append(" <a HREF=\"").append(URL).append(offset).append("\">").append(previousLabel).append("</a> "); 83 } 84 } 85 86 91 protected final void end(StringBuffer buffer) 92 { 93 int nextOffset = (int) (Math.floor((double) this.offset / (double) perPage) + 1) * perPage; 94 if (nextOffset < size) 95 { 96 buffer.append(" <a HREF=\"").append(URL).append(nextOffset).append("\">").append(nextLabel) 97 .append("</a> "); 98 } 99 } 100 101 107 protected final void normal(StringBuffer buffer, 108 int index) 109 { 110 lastNormal(buffer, index); 111 buffer.append(", "); 112 } 113 114 120 protected final void enhanced(StringBuffer buffer, 121 int index) 122 { 123 lastEnhanced(buffer, index); 124 buffer.append(", "); 125 } 126 127 133 protected final void lastNormal(StringBuffer buffer, 134 int index) 135 { 136 buffer.append("<a HREF=\"").append(URL).append(index * perPage).append("\">").append(index + 1).append("</a>"); 137 } 138 139 145 protected final void lastEnhanced(StringBuffer buffer, 146 int index) 147 { 148 buffer.append("<b>").append(index + 1).append("</b>"); 149 } 150 151 156 public String generate() 157 { 158 if (size > perPage) 159 { 160 return build((int) Math.ceil(size / (double) perPage), (int) Math.floor(offset / (double) perPage)); 161 } 162 else 163 { 164 return ""; 165 } 166 } 167 } | Popular Tags |