Subversion Repositories eFlore/Applications.del

Rev

Rev 264 | Rev 281 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 264 Rev 269
Line 1... Line 1...
1
package org.tela_botanica.del.client.composants.pagination;
1
package org.tela_botanica.del.client.composants.pagination;
Line 2... Line 2...
2
 
2
 
3
import java.util.Iterator;
3
import java.util.Iterator;
Line 4... Line -...
4
import java.util.Vector;
-
 
5
 
-
 
6
import org.tela_botanica.del.client.cache.CacheClient;
4
import java.util.Vector;
7
 
5
 
8
import com.google.gwt.event.dom.client.ChangeEvent;
6
import com.google.gwt.event.dom.client.ChangeEvent;
9
import com.google.gwt.event.dom.client.ChangeHandler;
7
import com.google.gwt.event.dom.client.ChangeHandler;
-
 
8
import com.google.gwt.event.dom.client.ClickEvent;
10
import com.google.gwt.event.dom.client.ClickEvent;
9
import com.google.gwt.event.dom.client.ClickHandler;
11
import com.google.gwt.event.dom.client.ClickHandler;
10
import com.google.gwt.user.client.Window;
12
import com.google.gwt.user.client.ui.Button;
11
import com.google.gwt.user.client.ui.Button;
13
import com.google.gwt.user.client.ui.HasWidgets;
12
import com.google.gwt.user.client.ui.HasWidgets;
Line -... Line 13...
-
 
13
import com.google.gwt.user.client.ui.HorizontalPanel;
-
 
14
import com.google.gwt.user.client.ui.ListBox;
-
 
15
 
-
 
16
/**
-
 
17
 * @author gregoire
-
 
18
 * Pagination GENERIQUE qui permet d'afficher un nombre donné d'éléments (pas)
-
 
19
 * La méthode changerPage est abstraite et doit être définie lors de l'instanciation 
-
 
20
 * de la classe pagination pour permettre d'afficher la suite des éléments
14
import com.google.gwt.user.client.ui.HorizontalPanel;
21
 * La méthode actualiserPasCache est abstraite car le pas est différent en 
Line 15... Line 22...
15
import com.google.gwt.user.client.ui.ListBox;
22
 * fonction de la page où l'on se trouve
16
 
-
 
17
public abstract class PaginationPresenteur {
23
 * */
18
 
-
 
19
	private final PaginationVue vue;
-
 
20
	private int nbElementsTotal = 0;
24
public abstract class PaginationPresenteur {
-
 
25
 
-
 
26
	private final PaginationVue vue;
Line -... Line 27...
-
 
27
	private int nbPage;
-
 
28
	private int pageCourante = 1;
-
 
29
	private int nbElementsTotal = 0;
-
 
30
	private int pas = 10;
-
 
31
	
21
	private int nbPage;
32
	/**
22
	private Vector<Button> liens;
33
	 * Constructeur de l'application
23
	private int pas = 10;
34
	 * @param nbElementsTotal : le nombre total des éléments à paginer
24
	private int pageCourante = 1;
35
	 * @param pas : le nombre d'éléments à afficher par page
25
	
-
 
26
	public PaginationPresenteur(int nbElements, int pas) {
-
 
27
		this.nbElementsTotal = nbElements;
-
 
28
		actualiserNbPage();
-
 
29
		vue = new PaginationVue();
-
 
30
		setPas(pas);
-
 
31
	}
-
 
32
 
-
 
33
	private void setBoutonActif() {
-
 
34
		Iterator<Button> it = liens.iterator();
-
 
35
		while (it.hasNext()) {
-
 
36
			Button boutonCourant = it.next();
-
 
37
			if (boutonCourant.getText().equals(String.valueOf(pageCourante))) {
-
 
38
				boutonCourant.setStyleName("actif");
36
	 * */
39
			} else {
37
	public PaginationPresenteur(int nbElementsTotal, int pas) {
-
 
38
		this.nbElementsTotal = nbElementsTotal;
40
				boutonCourant.setStyleName("inactif");
39
		changerPas(pas);
41
			}
40
		vue = new PaginationVue();
42
		}
41
	}
43
	}
42
	
Line -... Line 43...
-
 
43
	/** Met à jour le nombre de page en fonction du pas */
-
 
44
	private void actualiserNbPage() {
44
 
45
		Double nombrePages = Math.ceil((double) nbElementsTotal / (double) pas);
45
	private void actualiserNbPage() {
46
		this.nbPage = nombrePages.intValue();
46
		Double a = Math.ceil((double) nbElementsTotal / (double) pas);
-
 
47
		this.nbPage = a.intValue();
47
	}
48
	}
-
 
49
 
48
 
50
	public void setPas(int pas) {
49
	/** Changer le pas de la pagination 
51
		actualiserNbPage();
50
	 *  @param pas : le nombre d'éléments à afficher par page */
-
 
51
	public void changerPas(int pas) {
-
 
52
		actualiserNbPage();
-
 
53
		changerPage(pas * pageCourante, pas * (pageCourante + 1));
-
 
54
		actualiserPasCache(pas);
-
 
55
	}
-
 
56
	
-
 
57
	/** Methode Go du modèle MVP 
-
 
58
	 * @param container : la vue ou éxécuter l'affichage */
-
 
59
	public void go(HasWidgets container) {
52
		creerLiens();
60
		actualiserLiens();
53
		changerPage(pas * pageCourante, pas * (pageCourante + 1));
61
		container.add(vue);
54
		
62
	}
-
 
63
	
-
 
64
	/** Actualiser les liens pour n'afficher 5 avant et 5 après
55
		actualiserPasCache(pas);
65
	 * */
56
	}
66
	public void actualiserLiens() {
57
 
-
 
58
	public void creerLiens() {
-
 
59
		HorizontalPanel zoneLiens = vue.getZoneLiens();
67
		HorizontalPanel zoneLiens = vue.zoneLiens;
60
		zoneLiens.clear();
68
		zoneLiens.clear();
61
		liens = new Vector<Button>();
69
		
62
		Button precedent = new Button();
70
		
63
		precedent.setText("<");
71
		int pageDebut = pagesAvant();
-
 
72
		int pageFin = pagesApres();
-
 
73
		
64
		precedent.addClickHandler(new ClickHandler() {
74
		for (int i = pageDebut; i < pageFin; i++) {
65
 
75
			Button bouton = new Button(String.valueOf(i));
66
			@Override
76
			zoneLiens.add(bouton);
67
			public void onClick(ClickEvent event) {
-
 
68
				pagePrecedente();
-
 
69
			}
77
		}
70
		});
78
		setBoutonActif();
71
 
79
		gererEvenements();
72
		liens.add(precedent);
80
	}
-
 
81
	
-
 
82
	private int pagesAvant() {
73
		vue.getZoneLiens().add(precedent);
83
		int pageDebut = pageCourante - 5;
-
 
84
		if (pageDebut < 1) {
74
 
85
			pageDebut = 1;
75
		int pageDebut = pageCourante - 5;
86
		}
76
		if (pageDebut < 1) {
87
		return pageDebut;
77
			pageDebut = 1;
88
	}
78
		}
-
 
79
		
-
 
80
		int pageFin = pageDebut + 5;
-
 
81
		if (pageFin > nbPage) {
-
 
82
			pageFin = nbPage;
-
 
83
		}
-
 
84
		
-
 
85
		for (int i = pageDebut; i < pageFin; i++) {
-
 
86
			Button bouton = new Button();
-
 
87
			bouton.setText(String.valueOf(i));
-
 
88
			liens.add(bouton);
-
 
89
			zoneLiens.add(bouton);
-
 
90
		}
-
 
91
 
-
 
92
		Button suivant = new Button();
-
 
93
		suivant.setText(">");
-
 
94
		suivant.addClickHandler(new ClickHandler() {
-
 
95
 
-
 
96
			@Override
-
 
97
			public void onClick(ClickEvent event) {
-
 
98
				pageSuivante();
-
 
99
			}
-
 
100
		});
89
	
101
 
90
	private int pagesApres() {
102
		liens.add(suivant);
91
		int pageFin = pageCourante + 5;
103
		vue.getZoneLiens().add(suivant);
92
		if (pageFin > nbPage) {
104
 
-
 
105
		handleEvents();
93
			pageFin = nbPage;
106
		setBoutonActif();
94
		}
-
 
95
		return pageFin;
-
 
96
	}
107
	}
97
	
108
 
98
	private void setBoutonActif() {
109
	public void initNbElements() {
99
		int nbLiens = vue.zoneLiens.getWidgetCount();
110
 
100
		for (int i=0; i < nbLiens; i++) {
111
		ListBox nbElements = vue.getNbElements();
-
 
112
		for (int i = 0; i < 20; i += 5) {
-
 
113
			int valeur = i;
101
			Button boutonCourant = (Button) vue.zoneLiens.getWidget(i);
114
			if (i == 0) {
-
 
115
				valeur = 1;
-
 
-
 
102
			if (boutonCourant.getText().equals(String.valueOf(pageCourante))) {
Line -... Line 103...
-
 
103
				boutonCourant.setStyleName("actif");
-
 
104
			} else {
-
 
105
				boutonCourant.setStyleName("inactif");
-
 
106
			}
116
			}
107
		}
117
 
108
	}
118
			nbElements.addItem(String.valueOf(valeur));
109
 
119
		}
110
	/** Gérer les évènements sur les boutons
120
		nbElements.setSelectedIndex(pas / 5);
111
	 * */
121
		nbElements.addChangeHandler(new ChangeHandler() {
112
	public void gererEvenements() {
Line 122... Line 113...
122
 
113
		vue.selecteurPas.addChangeHandler(new ChangeHandler() {
123
			@Override
-
 
124
			public void onChange(ChangeEvent event) {
114
			@Override
125
				ListBox nbElements = (ListBox) event.getSource();
115
			public void onChange(ChangeEvent event) {
126
				int index = nbElements.getSelectedIndex();
116
				ListBox nbElements = (ListBox) event.getSource();
127
				setPas(Integer.parseInt(nbElements.getValue(index)));
117
				int index = nbElements.getSelectedIndex();
128
			}
118
				changerPas(Integer.parseInt(nbElements.getValue(index)));
129
 
119
			}
130
		});
120
 
Line -... Line 121...
-
 
121
		});
-
 
122
		
131
	}
123
		vue.boutonPrecedent.addClickHandler(new ClickHandler() {
-
 
124
			@Override
-
 
125
			public void onClick(ClickEvent event) {
-
 
126
				pagePrecedente();
-
 
127
			}
-
 
128
		});
132
 
129
 
133
	public void go(HasWidgets container) {
130
		vue.boutonSuivant.addClickHandler(new ClickHandler() {
134
		initNbElements();
131
			@Override
Line 135... Line 132...
135
		creerLiens();
132
			public void onClick(ClickEvent event) {
Line 136... Line 133...
136
		container.add(vue);
133
				pageSuivante();
137
 
134
			}
138
	}
135
		});
139
 
136
		
140
	public void handleEvents() {
137
		
141
		Iterator<Button> iterator = liens.iterator();
138
		int nbLiens = vue.zoneLiens.getWidgetCount();
142
		while (iterator.hasNext()) {
139
		for (int i=0; i < nbLiens; i++) {
143
			Button bouton = iterator.next();
140
			Button boutonCourant = (Button) vue.zoneLiens.getWidget(i);
-
 
141
 
144
 
142
			boutonCourant.addClickHandler(new ClickHandler() {
145
			bouton.addClickHandler(new ClickHandler() {
143
 
146
 
-
 
147
				@Override
144
				@Override
148
				public void onClick(ClickEvent event) {
145
				public void onClick(ClickEvent event) {
Line -... Line 146...
-
 
146
					Button boutonCourant = (Button) event.getSource();
-
 
147
					pageCourante = Integer.parseInt(boutonCourant.getText());
149
					Button bouton = (Button) event.getSource();
148
					int debut = Integer.parseInt(boutonCourant.getText()) * pas;
150
					pageCourante = Integer.parseInt(bouton.getText());
149
					int fin = (debut + pas);
151
					int debut = Integer.parseInt(bouton.getText()) * pas;
150
					setBoutonActif();
152
					int fin = (debut + pas);
151
					changerPage(debut, fin);
153
					setBoutonActif();
152
					actualiserLiens();
154
					changerPage(debut, fin);
153
				}
-
 
154
			});
155
				}
155
		}
156
			});
-
 
157
 
156
	}
158
		}
157
 
-
 
158
	/** Retourne la limite supèrieur du nombre de pages à afficher
-
 
159
	 * */
159
	}
160
	public void pageSuivante() {
160
 
-
 
161
	public void pageSuivante() {
161
		if (pageCourante < nbPage - 1) {
162
		if (pageCourante < nbPage - 1) {
162
			pageCourante++;
163
			pageCourante++;
163
			int debut = pageCourante * pas;
164
			int debut = pageCourante * pas;
164
			int fin = debut + pas;
165
			int fin = debut + pas;
165
			changerPage(debut, fin);
-
 
166
			actualiserLiens();
166
			changerPage(debut, fin);
167
		}
167
		}
-
 
168
		setBoutonActif();
168
	}
Line 169... Line 169...
169
	}
169
	
170
 
170
	/** Retourne la limite infèrieure du nombre de pages à afficher
171
	public void pagePrecedente() {
171
	 * */