120 |
gduche |
1 |
package org.tela_botanica.del.client.pagination;
|
|
|
2 |
|
|
|
3 |
import java.util.Iterator;
|
|
|
4 |
import java.util.Vector;
|
|
|
5 |
|
147 |
benjamin |
6 |
import org.tela_botanica.del.client.cache.CacheClient;
|
|
|
7 |
|
120 |
gduche |
8 |
import com.google.gwt.event.dom.client.ChangeEvent;
|
|
|
9 |
import com.google.gwt.event.dom.client.ChangeHandler;
|
|
|
10 |
import com.google.gwt.event.dom.client.ClickEvent;
|
|
|
11 |
import com.google.gwt.event.dom.client.ClickHandler;
|
|
|
12 |
import com.google.gwt.user.client.ui.Button;
|
|
|
13 |
import com.google.gwt.user.client.ui.HasWidgets;
|
|
|
14 |
import com.google.gwt.user.client.ui.HorizontalPanel;
|
|
|
15 |
import com.google.gwt.user.client.ui.ListBox;
|
|
|
16 |
|
|
|
17 |
public abstract class PaginationPresenteur {
|
|
|
18 |
|
|
|
19 |
// Singleton
|
|
|
20 |
private static PaginationPresenteur presenteur;
|
|
|
21 |
|
|
|
22 |
private final PaginationVue vue;
|
|
|
23 |
private int nbElementsTotal = 0;
|
|
|
24 |
private int nbPage;
|
|
|
25 |
private Vector<Button> liens;
|
|
|
26 |
|
|
|
27 |
public PaginationPresenteur(int nbElements) {
|
|
|
28 |
presenteur = this;
|
|
|
29 |
this.nbElementsTotal = nbElements;
|
|
|
30 |
actualiserNbPage();
|
|
|
31 |
vue = new PaginationVue();
|
147 |
benjamin |
32 |
setPas(CacheClient.getInstance().getPasPageRechercheImage());
|
120 |
gduche |
33 |
}
|
|
|
34 |
|
137 |
gduche |
35 |
private void setBoutonActif() {
|
|
|
36 |
Iterator<Button> it = liens.iterator();
|
|
|
37 |
while (it.hasNext()) {
|
|
|
38 |
Button boutonCourant = it.next();
|
147 |
benjamin |
39 |
if (boutonCourant.getText().equals(String.valueOf(CacheClient.getInstance().getNumPageRechercheImage()))) {
|
137 |
gduche |
40 |
boutonCourant.setStyleName("actif");
|
|
|
41 |
} else {
|
|
|
42 |
boutonCourant.setStyleName("inactif");
|
|
|
43 |
}
|
|
|
44 |
}
|
|
|
45 |
}
|
|
|
46 |
|
120 |
gduche |
47 |
private void actualiserNbPage() {
|
147 |
benjamin |
48 |
Double a = Math.ceil((double) nbElementsTotal / (double) CacheClient.getInstance().getPasPageRechercheImage());
|
120 |
gduche |
49 |
this.nbPage = a.intValue();
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
public void setPas(int pas) {
|
147 |
benjamin |
53 |
CacheClient.getInstance().setPasPageRechercheImage(pas);
|
120 |
gduche |
54 |
actualiserNbPage();
|
|
|
55 |
creerLiens();
|
147 |
benjamin |
56 |
changerPage(CacheClient.getInstance().getPasPageRechercheImage() * CacheClient.getInstance().getNumPageRechercheImage(), CacheClient.getInstance().getPasPageRechercheImage() * (CacheClient.getInstance().getNumPageRechercheImage() + 1));
|
120 |
gduche |
57 |
}
|
|
|
58 |
|
|
|
59 |
public void creerLiens() {
|
|
|
60 |
HorizontalPanel zoneLiens = vue.getZoneLiens();
|
|
|
61 |
zoneLiens.clear();
|
|
|
62 |
liens = new Vector<Button>();
|
|
|
63 |
Button precedent = new Button();
|
|
|
64 |
precedent.setText("<");
|
|
|
65 |
precedent.addClickHandler(new ClickHandler() {
|
|
|
66 |
|
|
|
67 |
@Override
|
|
|
68 |
public void onClick(ClickEvent event) {
|
|
|
69 |
pagePrecedente();
|
|
|
70 |
}
|
|
|
71 |
});
|
|
|
72 |
|
|
|
73 |
liens.add(precedent);
|
|
|
74 |
vue.getZoneLiens().add(precedent);
|
|
|
75 |
|
137 |
gduche |
76 |
// TODO : gérer le nombre de pages excessives
|
120 |
gduche |
77 |
for (int i = 0; i < nbPage; i++) {
|
|
|
78 |
Button bouton = new Button();
|
|
|
79 |
bouton.setText(String.valueOf(i));
|
|
|
80 |
liens.add(bouton);
|
|
|
81 |
zoneLiens.add(bouton);
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
Button suivant = new Button();
|
|
|
85 |
suivant.setText(">");
|
|
|
86 |
suivant.addClickHandler(new ClickHandler() {
|
|
|
87 |
|
|
|
88 |
@Override
|
|
|
89 |
public void onClick(ClickEvent event) {
|
|
|
90 |
pageSuivante();
|
|
|
91 |
}
|
|
|
92 |
});
|
|
|
93 |
|
|
|
94 |
liens.add(suivant);
|
|
|
95 |
vue.getZoneLiens().add(suivant);
|
|
|
96 |
|
|
|
97 |
handleEvents();
|
137 |
gduche |
98 |
setBoutonActif();
|
120 |
gduche |
99 |
}
|
|
|
100 |
|
|
|
101 |
public void initNbElements() {
|
|
|
102 |
|
|
|
103 |
ListBox nbElements = vue.getNbElements();
|
|
|
104 |
for (int i = 0; i < 20; i += 5) {
|
|
|
105 |
int valeur = i;
|
|
|
106 |
if (i == 0) {
|
|
|
107 |
valeur = 1;
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
nbElements.addItem(String.valueOf(valeur));
|
|
|
111 |
}
|
147 |
benjamin |
112 |
nbElements.setSelectedIndex(CacheClient.getInstance().getPasPageRechercheImage() / 5);
|
120 |
gduche |
113 |
nbElements.addChangeHandler(new ChangeHandler() {
|
|
|
114 |
|
|
|
115 |
@Override
|
|
|
116 |
public void onChange(ChangeEvent event) {
|
|
|
117 |
ListBox nbElements = (ListBox) event.getSource();
|
|
|
118 |
int index = nbElements.getSelectedIndex();
|
|
|
119 |
setPas(Integer.parseInt(nbElements.getValue(index)));
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
});
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
public void go(HasWidgets container) {
|
|
|
126 |
initNbElements();
|
|
|
127 |
creerLiens();
|
|
|
128 |
container.add(vue);
|
|
|
129 |
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
public void handleEvents() {
|
|
|
133 |
Iterator<Button> iterator = liens.iterator();
|
|
|
134 |
while (iterator.hasNext()) {
|
|
|
135 |
Button bouton = iterator.next();
|
|
|
136 |
|
|
|
137 |
bouton.addClickHandler(new ClickHandler() {
|
|
|
138 |
|
|
|
139 |
@Override
|
|
|
140 |
public void onClick(ClickEvent event) {
|
|
|
141 |
Button bouton = (Button) event.getSource();
|
147 |
benjamin |
142 |
CacheClient.getInstance().setNumPageRechercheImage(Integer.parseInt(bouton.getText()));
|
|
|
143 |
int debut = Integer.parseInt(bouton.getText()) * CacheClient.getInstance().getPasPageRechercheImage();
|
|
|
144 |
int fin = (debut + CacheClient.getInstance().getPasPageRechercheImage());
|
137 |
gduche |
145 |
setBoutonActif();
|
120 |
gduche |
146 |
changerPage(debut, fin);
|
|
|
147 |
|
|
|
148 |
}
|
|
|
149 |
});
|
|
|
150 |
|
|
|
151 |
}
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
public void pageSuivante() {
|
147 |
benjamin |
155 |
if (CacheClient.getInstance().getNumPageRechercheImage() < nbPage - 1) {
|
|
|
156 |
CacheClient.getInstance().setNumPageRechercheImage(CacheClient.getInstance().getNumPageRechercheImage() + 1);
|
|
|
157 |
int debut = CacheClient.getInstance().getNumPageRechercheImage() * CacheClient.getInstance().getPasPageRechercheImage();
|
|
|
158 |
int fin = debut + CacheClient.getInstance().getPasPageRechercheImage();
|
120 |
gduche |
159 |
changerPage(debut, fin);
|
|
|
160 |
}
|
137 |
gduche |
161 |
setBoutonActif();
|
120 |
gduche |
162 |
}
|
|
|
163 |
|
|
|
164 |
public void pagePrecedente() {
|
|
|
165 |
|
147 |
benjamin |
166 |
if (CacheClient.getInstance().getNumPageRechercheImage() > 0) {
|
|
|
167 |
CacheClient.getInstance().setNumPageRechercheImage(CacheClient.getInstance().getNumPageRechercheImage() - 1);
|
|
|
168 |
int debut = CacheClient.getInstance().getNumPageRechercheImage() * CacheClient.getInstance().getPasPageRechercheImage();
|
|
|
169 |
int fin = debut + CacheClient.getInstance().getPasPageRechercheImage();
|
120 |
gduche |
170 |
changerPage(debut, fin);
|
|
|
171 |
}
|
137 |
gduche |
172 |
setBoutonActif();
|
120 |
gduche |
173 |
}
|
|
|
174 |
|
|
|
175 |
public abstract void changerPage(int debut, int fin);
|
|
|
176 |
|
|
|
177 |
public PaginationPresenteur getInstance() {
|
|
|
178 |
return presenteur;
|
|
|
179 |
}
|
|
|
180 |
}
|