View Javadoc
1   /*
2    * Copyright 2014 James Pether Sörling
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   *	$Id$
17   *  $HeadURL$
18  */
19  package com.hack23.cia.web.impl.ui.application.views.admin.system.pagemode;
20  
21  import com.hack23.cia.web.impl.ui.application.views.common.pagemode.AbstractPageModContentFactoryImpl;
22  import com.hack23.cia.web.impl.ui.application.views.common.sizing.ContentRatio;
23  import com.vaadin.ui.AbstractOrderedLayout;
24  import com.vaadin.ui.HorizontalLayout;
25  import com.vaadin.ui.Label;
26  import com.vaadin.ui.Link;
27  
28  /**
29   * The Class AbstractAdminSystemPageModContentFactoryImpl.
30   */
31  abstract class AbstractAdminSystemPageModContentFactoryImpl extends AbstractPageModContentFactoryImpl {
32  
33  	/** The Constant LIMIT_FOR_DISPLAYING_START_END_LINKS. */
34  	private static final int LIMIT_FOR_DISPLAYING_START_END_LINKS = 5;
35  
36  	/** The Constant PAGE_ONE. */
37  	private static final int PAGE_ONE = 1;
38  
39  	/** The Constant PAGE_SEPARATOR. */
40  	private static final char PAGE_SEPARATOR = '/';
41  
42  	/** The Constant SHOW. */
43  	private static final String SHOW = " :: Show ";
44  
45  	/** The Constant RESULTS_PER_PAGE. */
46  	private static final String RESULTS_PER_PAGE = " results per page:";
47  
48  	/** The Constant PAGES_TOTAL_RESULTS. */
49  	private static final String PAGES_TOTAL_RESULTS = " pages. Total results:";
50  
51  	/** The Constant PAGE_HEADER. */
52  	private static final String PAGE_HEADER = "Page: ";
53  
54  	/** The Constant NEXT_PAGE. */
55  	private static final String NEXT_PAGE = "next page";
56  
57  	/** The Constant FIRST_PAGE. */
58  	private static final String FIRST_PAGE = "first page";
59  
60  	/** The Constant LAST_PAGE. */
61  	private static final String LAST_PAGE = "last page";
62  
63  	/** The Constant PREVIOUS_PAGE. */
64  	private static final String PREVIOUS_PAGE = "previous page";
65  
66  	/** The Constant DEFAULT_RESULTS_PER_PAGE. */
67  	public static final int DEFAULT_RESULTS_PER_PAGE=250;
68  
69  	/**
70  	 * Instantiates a new abstract admin system page mod content factory impl.
71  	 */
72  	AbstractAdminSystemPageModContentFactoryImpl() {
73  		super();
74  	}
75  
76  	/**
77  	 * Creates the paging controls.
78  	 *
79  	 * @param content
80  	 *            the content
81  	 * @param name
82  	 *            the name
83  	 * @param pageId
84  	 *            the page id
85  	 * @param size
86  	 *            the size
87  	 * @param pageNr
88  	 *            the page nr
89  	 * @param resultPerPage
90  	 *            the result per page
91  	 */
92  	protected final void createPagingControls(final AbstractOrderedLayout content,final String name,final String pageId, final Long size, final int pageNr, final int resultPerPage) {
93  		final HorizontalLayout pagingControls = new HorizontalLayout();
94  		pagingControls.setSpacing(true);
95  		pagingControls.setMargin(true);
96  
97  		final int maxPages = (int) ((size +(resultPerPage-1)) / resultPerPage);
98  
99  		final StringBuilder stringBuilder = new StringBuilder();
100 		stringBuilder.append(PAGE_HEADER)
101 		.append(pageNr)
102 		.append(PAGE_SEPARATOR)
103 		.append(maxPages)
104 		.append(PAGES_TOTAL_RESULTS)
105 		.append(size)
106 		.append(RESULTS_PER_PAGE)
107 		.append(resultPerPage)
108 		.append(SHOW);
109 		final Label pageInfo = new Label(stringBuilder.toString());
110 		pagingControls.addComponent(pageInfo);
111 		pagingControls.setExpandRatio(pageInfo, ContentRatio.SMALL);
112 
113 
114 		if (pageNr > PAGE_ONE) {
115 			addPagingLink(PREVIOUS_PAGE,name, pageId, pageNr -1,pagingControls);
116 		}
117 
118 		if (maxPages > PAGE_ONE && pageNr < maxPages) {
119 			addPagingLink(NEXT_PAGE,name, pageId, pageNr +1,pagingControls);
120 		}
121 
122 		if (maxPages > LIMIT_FOR_DISPLAYING_START_END_LINKS && pageNr > PAGE_ONE) {
123 			addPagingLink(FIRST_PAGE,name, pageId, 1,pagingControls);
124 		}
125 
126 		if (maxPages > LIMIT_FOR_DISPLAYING_START_END_LINKS && pageNr < maxPages) {
127 			addPagingLink(LAST_PAGE,name, pageId, maxPages,pagingControls);
128 		}
129 
130 		content.addComponent(pagingControls);
131 		content.setExpandRatio(pagingControls, ContentRatio.SMALL2);
132 
133 	}
134 
135 
136 	/**
137 	 * Adds the paging link.
138 	 *
139 	 * @param label
140 	 *            the label
141 	 * @param name
142 	 *            the name
143 	 * @param pageId
144 	 *            the page id
145 	 * @param pageNr
146 	 *            the page nr
147 	 * @param pagingControls
148 	 *            the paging controls
149 	 */
150 	private void addPagingLink(final String label,final String name, final String pageId, final int pageNr, final HorizontalLayout pagingControls) {
151 		final Link previousPageLink = getPageLinkFactory().createAdminPagingLink(label,name, pageId, String.valueOf(pageNr));
152 		pagingControls.addComponent(previousPageLink);
153 		pagingControls.setExpandRatio(previousPageLink, ContentRatio.SMALL);
154 	}
155 
156 }