You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by brazz <al...@man.eu> on 2011/07/14 09:03:21 UTC

Custom PagingNavigation Back-Button -Problem

I wrote a custom PagingNavigation which renders page links in the form:
[1-5...11-15 *16-20* 21-25...196-200]. The page size is adjustable by a
DropDownChoice. 

Everything works fine until i hit the back-button and then click on one of
the links (e.g. 11-15). What happens then is that in class
"RequestListenerInterface" the request doesn't get processed in the invoke
method, which results in an empty DataView (because my custom
PagingNavigation class does not get called):

if (!component.isEnabledInHierarchy() || !component.isVisibleInHierarchy())
		{
			// just return so that we have a silent fail and just re-render the
			// page
			log.warn("component not enabled or visible; ignoring call. Component: " +
component);
			return;
		}

I already searched the mailing list and found, that this has been introduced
because of a security problem. On the one hand i removed every code line
which sets components visibility or enablement to false. That didn't solve
the problem. 

On the other hand even if that worked, i'd have no idea how to get around
this problem, as i  have to set the visibility of loopItems (i overwrote
method "populateItem" in class PagingNavigation) because not every page has
a corresponding link and when instead i call "remove()" on the loopItems,
wicket exits with an exception that the component is "null". 

Thanks for any suggestions!!!

Here's the code of my customized PagingNavigation class:

public class CustomPagingNavigation extends PagingNavigation  {

	private static final long serialVersionUID = -3614988044938070942L;

	private BrowsingItems f;

	private PagingNavigationLink<Void> link;
	
	private int currentPage;

	private BrowsingItems.BrowsingItem selectedBi;

	private BrowsingItems.BrowsingItem pageMatchingBi;
	
	private IPageable pageable;
	
	private int offset = 1;
	
	private IASPPagingDimensionsProvider dimensionsProvider;
	
	private BrowsingItems.BrowsingItem nextBrowsingItem = null;

	public CustomPagingNavigation(String id, IPageable pageable,
			IPagingLabelProvider labelProvider, IASPPagingDimensionsProvider
dimensionsProvider) {
		super(id, pageable, labelProvider);
		this.dimensionsProvider = dimensionsProvider;
		this.pageable = pageable;
		
		setViewSize(dimensionsProvider.getRecordCount() /
dimensionsProvider.getItemsPerPage());
		f = newBrowsingItems();
		f.setOffset(1);
	}

	
	@Override
	protected void populateItem(LoopItem loopItem) {
		setViewSize(dimensionsProvider.getRecordCount() /
dimensionsProvider.getItemsPerPage());
		if (f.getCurrentPage() >= f.getPages())
			f.setOffset(f.getLastPageOffset());
		if (f.getOffset() < 0)
			f.setOffset(1);
		if (f.getOffset() > f.getRecordCount())
			f.setOffset(f.getLastPageOffset());
		currentPage = loopItem.getIteration();
		pageMatchingBi = findMatchingPage(f.getBrowsingItems(), currentPage);
		if (pageMatchingBi != null) {
			boolean selected = false;
			if (selectedBi == null) {
				selectedBi = f.getBrowsingItems()[0];
			}
			if (selectedBi != null && selectedBi.equals(pageMatchingBi)) {
				selected = true;
			}
			link = new ASPBrowsingItemPageNavigationLink<Void>("pageLink",
					pageable, currentPage, pageMatchingBi, selected) {
				private static final long serialVersionUID = 7412763652723598668L;

				@Override
				public void onClick() {
					super.onClick();
					selectedBi = this.getBrowsingItem();
					offset = this.getBrowsingItem().getFrom();
					newBrowsingItems();
					getPage().setRedirect(true);
					// Return the current page.
					setResponsePage(getPage());
				}
			};
			loopItem.add(link);
			loopItem.add(new Label("separator", "&nbsp;")
				.setEscapeModelStrings(false));
			nextBrowsingItem = f.findNextBrowsingItem(pageMatchingBi);
			if(nextBrowsingItem != null && nextBrowsingItem.isDots()){
				loopItem.add(newDotsLabel());
			} else {
				loopItem.add(newBlankLabel());
			}
		} else {
			//Even if i don't do this but instead insert the components above i get
the same
			//error message
			loopItem.setVisibilityAllowed(false);
		}
	}
	
	private Label newBlankLabel() {
		Label lbl = new Label("dots", "");
		return lbl;
	}
	
	private Label newDotsLabel() {
		Label lbl = new Label("dots", "&hellip;");
		lbl.add(new SimpleAttributeModifier("class", "active"));
		lbl.setEscapeModelStrings(false);
		return lbl;
	}

	public void setSelectedBi(BrowsingItems.BrowsingItem browsingItem) {
		this.selectedBi = browsingItem;
	}

	/**
	 * new BrowsingItems.
	 * 
	 * @return
	 */
	private BrowsingItems newBrowsingItems() {
		f = new BrowsingItems();
		f.setRecordCount(dimensionsProvider.getRecordCount());
		f.setPageLength(dimensionsProvider.getItemsPerPage());
		f.setOffset(offset);
		return f;
	}

	/**
	 * finds the corresponding BrowsingItem for a given page.
	 * 
	 * @param bi
	 * @param page
	 * @return
	 */
	private BrowsingItems.BrowsingItem findMatchingPage(
			BrowsingItems.BrowsingItem[] bi, int page) {
		page = page + 1;
		for (int i = 0; i < bi.length; i++) {
			if (page == bi[i].getPage(dimensionsProvider.getItemsPerPage())) {
				return bi[i];
			}
		}
		return null;
	}

	/**
	 * marks the next browsing item when clicking the next button
	 */
	public void markNextLink() {
		if (selectedBi != null) {
			newBrowsingItems();
			f.setOffset(offset);
			int nextOffset = f.getNextOffset();
			f.setOffset(nextOffset);
			this.offset = nextOffset;
			BrowsingItems.BrowsingItem nextItem = f
					.findNextBrowsingItem(selectedBi);
			this.selectedBi = nextItem;
		}
	}

	/**
	 *  marks the previous browsing item when clicking the next button
	 */
	public void markPreviousLink() {
		if (selectedBi != null) {
			newBrowsingItems();
			f.setOffset(offset);
			int prevOffset = f.getPreviousOffset();
			f.setOffset(prevOffset);
			this.offset = prevOffset;
			BrowsingItems.BrowsingItem prevItem = f
					.findPrevBrowsingItem(selectedBi);
			this.selectedBi = prevItem;
		}
	}
}


--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Custom-PagingNavigation-Back-Button-Problem-tp3666779p3666779.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Custom PagingNavigation Back-Button -Problem

Posted by brazz <al...@man.eu>.
Solved the problem. My assumption about the RequestListenerInterface was
wrong. What i am doing now:

In my page i prevent Browser Caching:
protected void configureResponse() {
	       super.configureResponse();
	       WebResponse response = getWebRequestCycle().getWebResponse();
	       response.setHeader("Cache-Control",
	             "no-cache, max-age=0,must-revalidate, no-store");
	       response.setHeader("Expires","-1");
	       response.setHeader("Pragma","no-cache");
	   }

The problem was located in my DataProvider:
public int size() {
		//That solved my problem:
		if(technicalInfoRepository.countAllEntities() == 0)
			technicalInfoRepository.load("", null, null, null, null);
		return filteringStrategy.getFiltered(
				new LinkedList <ITechnicalInfo>(technicalInfoRepository
						.findAll(all(ITechnicalInfo.class)))).size();
	}

When i clicked on the back-button my Repository was empty. No i am loading
the repository again if its size is null.

By the way: 
I am using the InMemoryRepositories of the sourceforge project "domian". I
am using this kind of repository because my Application solely uses Web
Services as its data source. In order to get a good compromise between
caching and the number of web service calls i'm saving a reference to the
repositories in the DataProvider or in the Page (Panel, Component, 
whatever). Whenever possible i use a transient reference (but that wasn't
the problem in this case). So this is just a thin cache for user specific
data.

For data that is reusable for all users i use ehcache. 

If you've got any suggestions on alternative approaches i would really
appreciate that.






--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Custom-PagingNavigation-Back-Button-Problem-tp3666779p3677549.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org