--- /dev/null
+/*****************************************************************************
+ * Copyright Igor Barma, Eric Brun, Alexandre Desoubeaux, Christian Martel,
+ * (2 décembre 2008)
+ *
+ * Ce logiciel est un programme informatique servant à l'évaluation des
+ * compétences.
+ *
+ * Ce logiciel est régi par la licence CeCILL soumise au droit français et
+ * respectant les principes de diffusion des logiciels libres. Vous pouvez
+ * utiliser, modifier et/ou redistribuer ce programme sous les conditions
+ * de la licence CeCILL telle que diffusée par le CEA, le CNRS et l'INRIA
+ * sur le site "http://www.cecill.info".
+ *
+ * En contrepartie de l'accessibilité au code source et des droits de copie,
+ * de modification et de redistribution accordés par cette licence, il n'est
+ * offert aux utilisateurs qu'une garantie limitée. Pour les mêmes raisons,
+ * seule une responsabilité restreinte pèse sur l'auteur du programme, le
+ * titulaire des droits patrimoniaux et les concédants successifs.
+ *
+ * A cet égard l'attention de l'utilisateur est attirée sur les risques
+ * associés au chargement, à l'utilisation, à la modification et/ou au
+ * développement et à la reproduction du logiciel par l'utilisateur étant
+ * donné sa spécificité de logiciel libre, qui peut le rendre complexe à
+ * manipuler et qui le réserve donc à des développeurs et des professionnels
+ * avertis possédant des connaissances informatiques approfondies. Les
+ * utilisateurs sont donc invités à charger et tester l'adéquation du
+ * logiciel à leurs besoins dans des conditions permettant d'assurer la
+ * sécurité de leurs systèmes et ou de leurs données et, plus généralement,
+ * à l'utiliser et l'exploiter dans les mêmes conditions de sécurité.
+ *
+ * Le fait que vous puissiez accéder à cet en-tête signifie que vous avez
+ * pris connaissance de la licence CeCILL, et que vous en avez accepté les
+ * termes.
+ *******************************************************************************/
+package com.pentila.evalcomp.client.services.widget;
+
+import com.google.gwt.core.client.GWT;
+import com.gwtext.client.core.Function;
+import com.gwtext.client.util.DelayedTask;
+import com.pentila.evalcomp.client.i18n.Messages;
+import com.pentila.evalcomp.client.services.IServiceWidgetContainerPaging;
+import com.smartgwt.client.types.Alignment;
+import com.smartgwt.client.types.Overflow;
+import com.smartgwt.client.widgets.Label;
+import com.smartgwt.client.widgets.events.ClickEvent;
+import com.smartgwt.client.widgets.events.ClickHandler;
+import com.smartgwt.client.widgets.form.DynamicForm;
+import com.smartgwt.client.widgets.form.FormItemHoverFormatter;
+import com.smartgwt.client.widgets.form.fields.FormItem;
+import com.smartgwt.client.widgets.form.fields.SelectItem;
+import com.smartgwt.client.widgets.form.fields.TextItem;
+import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
+import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;
+import com.smartgwt.client.widgets.toolbar.ToolStrip;
+import com.smartgwt.client.widgets.toolbar.ToolStripButton;
+
+/**
+ * The Class GridPager.
+ */
+public class GridPager extends ToolStrip {
+
+ /** The total count. */
+ int totalCount = 0;
+
+ /** The start. */
+ int start = 0;
+
+ /**
+ * How many row in each pager.
+ */
+ private int pageSize = 50;
+ /**
+ * Current page.
+ */
+ private int pageNum = 1;
+
+ /** The search filter. */
+ private String searchFilter ="";
+ /**
+ * The actual grid.
+ */
+ private final IServiceWidgetContainerPaging grid;
+
+ // visual component
+ /** The first label. */
+ protected ToolStripButton firstLabel;
+
+ /** The next label. */
+ protected ToolStripButton nextLabel;
+
+ /** The page form. */
+ DynamicForm pageForm;
+
+ /** The page text. */
+ protected TextItem pageText;
+
+ /** The search term. */
+ protected TextItem searchTerm;
+
+ /** The previous label. */
+ protected ToolStripButton previousLabel;
+
+ /** The last label. */
+ protected ToolStripButton lastLabel;
+
+ /** The total label. */
+ protected Label totalLabel;
+
+ /** The page form2. */
+ private DynamicForm pageForm2;
+
+ /** The cb item. */
+ private SelectItem cbItem;
+
+ /** The page form3. */
+ private DynamicForm pageForm3;
+
+ /**
+ * Instantiates a new grid pager.
+ *
+ * @param listGrid the list grid
+ * @param pageSize the page size
+ */
+ public GridPager(IServiceWidgetContainerPaging listGrid, int pageSize) {
+ this(listGrid, pageSize, 20, 22);
+ }
+
+ /**
+ * Instantiates a new grid pager.
+ *
+ * @param listGrid the listGrid
+ * @param pagesize the pagesize
+ * @param cellHeight cellHeight for the listGrid.
+ * @param headerHeight headerHeight for the listGrid.
+ */
+ public GridPager(IServiceWidgetContainerPaging listGrid, int pagesize,
+ int cellHeight, int headerHeight) {
+ super();
+ this.grid = listGrid;
+ this.pageSize = pagesize;
+
+ cbItem = new SelectItem();
+ cbItem.setTitle("");
+
+ cbItem.setValueMap("25", "50", "100", "200", "300", "400", "500", "1000", Messages.getString("Tous"));
+ cbItem.setValue("25");
+
+ cbItem.setHeight(20);
+ cbItem.addChangedHandler(new ChangedHandler(){
+
+ public void onChanged(ChangedEvent event) {
+
+ if (event.getValue().toString().equals(Messages.getString("Tous"))){
+ pageSize = 100000;
+ }else{
+
+
+ pageSize = Integer.parseInt(event.getValue().toString());
+
+ }
+ goToPage(-1);
+ }
+
+ });
+
+ final DelayedTask DelayedTask = new DelayedTask();
+
+
+ final Function f = new Function(){
+
+ public void execute() {
+ goToPage(-1);
+
+ }
+
+ };
+
+ searchTerm = new TextItem();
+ searchTerm.setHoverWidth(100);
+ searchTerm.setItemHoverFormatter(new FormItemHoverFormatter(){
+
+ public String getHoverHTML(FormItem item, DynamicForm form) {
+ return Messages.getString("Rechercher_par_nom");
+ }
+
+ });
+ searchTerm.setWidth(100);
+ searchTerm.setHeight(20);
+ searchTerm.setShowTitle(false);
+ searchTerm.addChangedHandler(new ChangedHandler() {
+
+ public void onChanged(ChangedEvent event) {
+ DelayedTask.cancel();
+ DelayedTask.delay(1500, f);
+ }
+
+ });
+
+ firstLabel = new ToolStripButton("<<");
+ firstLabel.setWidth(30);
+
+ firstLabel.addClickHandler(new ClickHandler() {
+ public void onClick(ClickEvent event) {
+ goToPage(1);
+ }
+ });
+
+ previousLabel = new ToolStripButton("<");
+ previousLabel.setWidth(20);
+
+ previousLabel.addClickHandler(new ClickHandler() {
+ public void onClick(ClickEvent event) {
+ goToPage(pageNum - 1);
+ }
+ });
+
+ pageText = new TextItem();
+ pageText.setWidth(30);
+ pageText.setHeight(20);
+ pageText.setShowTitle(false);
+ pageText.addChangedHandler(new ChangedHandler() {
+
+ public void onChanged(ChangedEvent event) {
+ if (event.getValue() != null) {
+ int value = Integer.parseInt(event.getValue().toString());
+ goToPage(value);
+ }
+ }
+ });
+
+ nextLabel = new ToolStripButton(">");
+
+ nextLabel.setWidth(20);
+
+ nextLabel.addClickHandler(new ClickHandler() {
+ public void onClick(ClickEvent event) {
+ goToPage(pageNum + 1);
+ }
+ });
+
+ lastLabel = new ToolStripButton(">>");
+ lastLabel.setWidth(30);
+
+ lastLabel.addClickHandler(new ClickHandler() {
+ public void onClick(ClickEvent event) {
+ goToPage(getTotalPages());
+ }
+ });
+
+ totalLabel = new Label();
+ totalLabel.setWrap(false);
+ totalLabel.setWidth(50);
+
+ setHeight(20);
+ setOverflow(Overflow.VISIBLE);
+
+ pageForm = new DynamicForm();
+ pageForm.setNumCols(1);
+ pageForm.setFields(pageText);
+ pageForm.setWidth(30);
+
+ pageForm2 = new DynamicForm();
+ pageForm2.setNumCols(1);
+ pageForm2.setFields(searchTerm);
+ pageForm2.setWidth(100);
+
+ pageForm3 = new DynamicForm();
+ pageForm3.setNumCols(1);
+
+ pageForm3.setFields(cbItem);
+
+ addMember(pageForm2);
+ addMember(firstLabel);
+ addMember(previousLabel);
+
+ addMember(pageForm);
+ addMember(nextLabel);
+ addMember(lastLabel);
+ addMember(pageForm3);
+ addMember(totalLabel);
+
+ setAlign(Alignment.RIGHT);
+
+ }
+
+ /**
+ * Go to page.
+ *
+ * @param pageNum the page num
+ */
+ public void goToPage(int pageNum) {
+ GWT.log("go to page " + pageNum, null);
+ // clamp to the end of the possible set of pages
+ int pages = getTotalPages();
+
+ String search = null;
+
+ if (pageNum == -1) {
+
+ this.pageNum = -1;
+ }
+
+ if (searchTerm.getValue() != null
+ && !searchTerm.getValue().toString().equals("")) {
+ search = searchTerm.getValue().toString();
+ }
+
+ this.searchFilter = search;
+
+ if (pageNum > pages) {
+ pageNum = pages;
+ }
+ if (pageNum < 1) {
+ pageNum = 1;
+ }
+
+ if (pageNum == this.pageNum) {
+ updatePagerControls(pages);
+ return;
+ }
+ // update the buttons
+ this.pageNum = pageNum;
+ updatePagerControls(pages);
+ start = (pageNum - 1) * pageSize;
+ grid.update(start, pageSize, search);
+
+ }
+
+ public void goToPage(int pageNum, boolean force) {
+ GWT.log("go to page " + pageNum, null);
+ // clamp to the end of the possible set of pages
+ int pages = getTotalPages();
+
+ String search = null;
+
+ if (pageNum == -1) {
+
+ this.pageNum = -1;
+ }
+
+ if (searchTerm.getValue() != null
+ && !searchTerm.getValue().toString().equals("")) {
+ search = searchTerm.getValue().toString();
+ }
+
+ this.searchFilter = search;
+
+ if (pageNum > pages) {
+ pageNum = pages;
+ }
+ if (pageNum < 1) {
+ pageNum = 1;
+ }
+
+ if (pageNum == this.pageNum && !force) {
+ updatePagerControls(pages);
+ return;
+ }
+ // update the buttons
+ this.pageNum = pageNum;
+ updatePagerControls(pages);
+ start = (pageNum - 1) * pageSize;
+ grid.update(start, pageSize, search);
+
+ }
+
+
+ /**
+ * Gets the total pages.
+ *
+ * @return the total pages
+ */
+ public int getTotalPages() {
+
+
+ int total = this.getTotalCount();
+ int pages = (int) Math.ceil(((float) total) / ((float) pageSize));
+ // never return zero pages
+ if (pages == 0) {
+ pages = 1;
+ }
+ return pages;
+ }
+
+ /**
+ * Update pager controls.
+ *
+ * @param total the total
+ */
+ public void updatePagerControls(int total) {
+ pageText.setValue(pageNum);
+ totalLabel.setContents("(" + pageNum + "/" + total + ")");
+
+ }
+
+ /**
+ * Gets the total count.
+ *
+ * @return the total count
+ */
+ public int getTotalCount() {
+ return totalCount;
+ }
+
+ /**
+ * Sets the total count.
+ *
+ * @param totalCount the new total count
+ */
+ public void setTotalCount(int totalCount) {
+ this.totalCount = totalCount;
+ }
+
+ /**
+ * Gets the start.
+ *
+ * @return the start
+ */
+ public int getStart() {
+ return start;
+ }
+
+ /**
+ * Sets the start.
+ *
+ * @param start the new start
+ */
+ public void setStart(int start) {
+ this.start = start;
+ }
+
+ /**
+ * Gets the page size.
+ *
+ * @return the page size
+ */
+ public int getPageSize() {
+ return pageSize;
+ }
+
+ /**
+ * Sets the page size.
+ *
+ * @param pageSize the new page size
+ */
+ public void setPageSize(int pageSize) {
+ this.pageSize = pageSize;
+ }
+
+ /**
+ * Gets the search filter.
+ *
+ * @return the search filter
+ */
+ public String getSearchFilter(){
+ return this.searchFilter;
+ }
+
+}