Created
November 9, 2012 16:25
-
-
Save uklance/4046654 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.lazan.t5components.components; | |
import java.util.ArrayList; | |
import java.util.Iterator; | |
import java.util.List; | |
import org.apache.tapestry5.Block; | |
import org.apache.tapestry5.annotations.InjectComponent; | |
import org.apache.tapestry5.annotations.OnEvent; | |
import org.apache.tapestry5.annotations.Parameter; | |
import org.apache.tapestry5.annotations.Property; | |
import org.apache.tapestry5.annotations.SetupRender; | |
import org.apache.tapestry5.corelib.components.Zone; | |
import org.lazan.t5components.model.GalleryDataModel; | |
public class Gallery<T> { | |
@Parameter(required=true) | |
@Property | |
private GalleryDataModel<T> source; | |
@Parameter(required=true) | |
private int columnCount; | |
@Parameter("prop:source.size()") | |
private Integer pageSize; | |
@Parameter("block:defaultValueBlock") | |
@Property | |
private Block valueBlock; | |
@Property | |
private int currentPage; | |
@Property | |
private List<T> row; | |
@Parameter | |
@Property | |
private T value; | |
@InjectComponent | |
private Zone galleryZone; | |
@SetupRender | |
void setupRender() { | |
currentPage = 0; | |
} | |
public List<List<T>> getRows() { | |
int startIndex = currentPage * pageSize; | |
Iterator<T> iterator = source.getItems(startIndex, pageSize).iterator(); | |
List<List<T>> rows = new ArrayList<List<T>>(); | |
while (iterator.hasNext()) { | |
List<T> row = new ArrayList<T>(columnCount); | |
for (int i = 0; i < columnCount; ++ i) { | |
if (iterator.hasNext()) { | |
row.add(iterator.next()); | |
} else { | |
break; | |
} | |
} | |
rows.add(row); | |
} | |
return rows; | |
} | |
public int getNextPage() { | |
return currentPage + 1; | |
} | |
public int getPrevPage() { | |
return currentPage - 1; | |
} | |
public boolean isShowNext() { | |
int nextIndex = (currentPage + 1) * pageSize; | |
return nextIndex < source.size(); | |
} | |
public boolean isShowPrev() { | |
return currentPage > 0; | |
} | |
@OnEvent("changePage") | |
public Block onChangePage(int page) { | |
currentPage = page; | |
return galleryZone.getBody(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<t:zone t:id="galleryZone" xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd"> | |
<t:block t:id="defaultValueBlock"> | |
${value} | |
</t:block> | |
<table> | |
<tbody> | |
<tr t:type="loop" source="rows" value="row"> | |
<td t:type="loop" source="row" value="value"> | |
<t:delegate to="valueBlock" /> | |
</td> | |
</tr> | |
</tbody> | |
</table> | |
<div class="galleryControls"> | |
<t:if test="showPrev"> | |
<t:eventlink event="changePage" context="prevPage" zone="^"><button>${message:prevPage}</button></t:eventlink> | |
</t:if> | |
<t:if test="showNext"> | |
<t:eventlink event="changePage" context="nextPage" zone="^"><button>${message:nextPage}</button></t:eventlink> | |
</t:if> | |
</div> | |
</t:zone> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.lazan.t5components.model; | |
import java.util.List; | |
public interface GalleryDataModel<T> { | |
int size(); | |
List<T> getItems(int startIndex, int maxItems); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.lazan.t5components.demo.pages; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.apache.tapestry5.Asset; | |
import org.apache.tapestry5.annotations.Property; | |
import org.apache.tapestry5.ioc.annotations.Inject; | |
import org.apache.tapestry5.services.AssetSource; | |
import org.lazan.t5components.model.GalleryDataModel; | |
public class GalleryDemo { | |
@Inject | |
private AssetSource assetSource; | |
@Property | |
private Asset image; | |
public GalleryDataModel<Asset> getImages() { | |
return new GalleryDataModel<Asset>() { | |
public int size() { | |
return 15; | |
} | |
public List<Asset> getItems(int startIndex, int maxItems) { | |
List<Asset> assets = new ArrayList<Asset>(); | |
int itemCount = Math.min(size() - startIndex, maxItems); | |
for (int i = 0; i < itemCount; ++ i) { | |
String path = String.format("images/%s.jpg", i + startIndex); | |
assets.add(assetSource.getContextAsset(path, null)); | |
} | |
return assets; | |
} | |
}; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd" xmlns:p="tapestry:parameter"> | |
<t:lazan.gallery source="images" columnCount="literal:3" pageSize="literal:6" value="image"> | |
<p:valueBlock> | |
<img src="${image}" /> | |
</p:valueBlock> | |
</t:lazan.gallery> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment