1 15 package org.apache.tapestry.contrib.table.components; 16 17 import org.apache.tapestry.IRequestCycle; 18 import org.apache.tapestry.contrib.table.model.ITableModelSource; 19 import org.apache.tapestry.util.ComponentAddress; 20 21 35 public abstract class TablePages extends AbstractTableViewComponent 36 { 37 public abstract int getPagesDisplayed(); 39 40 private int m_nDisplayPage; 42 43 47 public int getDisplayPage() 48 { 49 return m_nDisplayPage; 50 } 51 52 56 public void setDisplayPage(int displayPage) 57 { 58 m_nDisplayPage = displayPage; 59 } 60 61 public int getCurrentPage() 62 { 63 return getTableModelSource().getTableModel().getPagingState().getCurrentPage() + 1; 64 } 65 66 public int getPageCount() 67 { 68 return getTableModelSource().getTableModel().getPageCount(); 69 } 70 71 public boolean getCondBack() 72 { 73 return getCurrentPage() > 1; 74 } 75 76 public boolean getCondFwd() 77 { 78 return getCurrentPage() < getPageCount(); 79 } 80 81 public boolean getCondCurrent() 82 { 83 return getDisplayPage() == getCurrentPage(); 84 } 85 86 public int getStartPage() 87 { 88 int nCurrent = getCurrentPage(); 89 int nPagesDisplayed = getPagesDisplayed(); 90 91 int nRightMargin = nPagesDisplayed / 2; 92 int nStop = nCurrent + nRightMargin; 93 int nLastPage = getPageCount(); 94 95 int nLeftAddon = 0; 96 if (nStop > nLastPage) 97 nLeftAddon = nStop - nLastPage; 98 99 int nLeftMargin = (nPagesDisplayed - 1) / 2 + nLeftAddon; 100 int nStart = nCurrent - nLeftMargin; 101 int nFirstPage = 1; 102 if (nStart < nFirstPage) 103 nStart = nFirstPage; 104 return nStart; 105 } 106 107 public int getStopPage() 108 { 109 int nCurrent = getCurrentPage(); 110 int nPagesDisplayed = getPagesDisplayed(); 111 112 int nLeftMargin = (nPagesDisplayed - 1) / 2; 113 int nStart = nCurrent - nLeftMargin; 114 int nFirstPage = 1; 115 116 int nRightAddon = 0; 117 if (nStart < nFirstPage) 118 nRightAddon = nFirstPage - nStart; 119 120 int nRightMargin = nPagesDisplayed / 2 + nRightAddon; 121 int nStop = nCurrent + nRightMargin; 122 int nLastPage = getPageCount(); 123 if (nStop > nLastPage) 124 nStop = nLastPage; 125 return nStop; 126 } 127 128 public Integer [] getPageList() 129 { 130 int nStart = getStartPage(); 131 int nStop = getStopPage(); 132 133 Integer [] arrPages = new Integer [nStop - nStart + 1]; 134 for (int i = nStart; i <= nStop; i++) 135 arrPages[i - nStart] = new Integer (i); 136 137 return arrPages; 138 } 139 140 public Object [] getFirstPageContext() 141 { 142 ComponentAddress objAddress = new ComponentAddress(getTableModelSource()); 143 return new Object [] { objAddress, new Integer (1)}; 144 } 145 146 public Object [] getLastPageContext() 147 { 148 ComponentAddress objAddress = new ComponentAddress(getTableModelSource()); 149 return new Object [] { objAddress, new Integer (getPageCount())}; 150 } 151 152 public Object [] getBackPageContext() 153 { 154 ComponentAddress objAddress = new ComponentAddress(getTableModelSource()); 155 return new Object [] { objAddress, new Integer (getCurrentPage() - 1)}; 156 } 157 158 public Object [] getFwdPageContext() 159 { 160 ComponentAddress objAddress = new ComponentAddress(getTableModelSource()); 161 return new Object [] { objAddress, new Integer (getCurrentPage() + 1)}; 162 } 163 164 public Object [] getDisplayPageContext() 165 { 166 ComponentAddress objAddress = new ComponentAddress(getTableModelSource()); 167 return new Object [] { objAddress, new Integer (m_nDisplayPage)}; 168 } 169 170 public void changePage(IRequestCycle objCycle) 171 { 172 Object [] arrParameters = objCycle.getListenerParameters(); 173 if (arrParameters.length != 2 174 && !(arrParameters[0] instanceof ComponentAddress) 175 && !(arrParameters[1] instanceof Integer )) 176 { 177 return; 179 } 180 181 ComponentAddress objAddress = (ComponentAddress) arrParameters[0]; 182 ITableModelSource objSource = (ITableModelSource) objAddress.findComponent(objCycle); 183 setCurrentPage(objSource, ((Integer ) arrParameters[1]).intValue()); 184 185 objSource.fireObservedStateChange(); 187 } 188 189 public void setCurrentPage(ITableModelSource objSource, int nPage) 190 { 191 objSource.getTableModel().getPagingState().setCurrentPage(nPage - 1); 192 } 193 194 } 195 | Popular Tags |