Subversion Repositories eFlore/Applications.coel

Rev

Rev 543 | Rev 843 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
61 jpm 1
package org.tela_botanica.client.composants;
2
 
66 jpm 3
import org.tela_botanica.client.ComposantId;
4
import org.tela_botanica.client.RegistreId;
498 jp_milcent 5
import org.tela_botanica.client.images.Images;
66 jpm 6
import org.tela_botanica.client.modeles.Configuration;
324 jp_milcent 7
import org.tela_botanica.client.modeles.Information;
66 jpm 8
import org.tela_botanica.client.modeles.Menu;
543 jp_milcent 9
import org.tela_botanica.client.util.Pattern;
311 jp_milcent 10
import org.tela_botanica.client.util.Print;
66 jpm 11
 
498 jp_milcent 12
import com.extjs.gxt.ui.client.event.Events;
66 jpm 13
import com.extjs.gxt.ui.client.Registry;
543 jp_milcent 14
import com.extjs.gxt.ui.client.Style.ButtonScale;
61 jpm 15
import com.extjs.gxt.ui.client.Style.LayoutRegion;
66 jpm 16
import com.extjs.gxt.ui.client.Style.Scroll;
498 jp_milcent 17
import com.extjs.gxt.ui.client.event.ButtonEvent;
66 jpm 18
import com.extjs.gxt.ui.client.event.Listener;
69 jpm 19
import com.extjs.gxt.ui.client.event.SelectionListener;
498 jp_milcent 20
import com.extjs.gxt.ui.client.event.TreePanelEvent;
66 jpm 21
import com.extjs.gxt.ui.client.store.Store;
22
import com.extjs.gxt.ui.client.store.TreeStore;
61 jpm 23
import com.extjs.gxt.ui.client.util.Margins;
24
import com.extjs.gxt.ui.client.widget.ContentPanel;
25
import com.extjs.gxt.ui.client.widget.Dialog;
66 jpm 26
import com.extjs.gxt.ui.client.widget.HtmlContainer;
324 jp_milcent 27
import com.extjs.gxt.ui.client.widget.Info;
343 jp_milcent 28
import com.extjs.gxt.ui.client.widget.LayoutContainer;
498 jp_milcent 29
import com.extjs.gxt.ui.client.widget.form.StoreFilterField;
61 jpm 30
import com.extjs.gxt.ui.client.widget.button.Button;
31
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
32
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
343 jp_milcent 33
import com.extjs.gxt.ui.client.widget.layout.FlowLayout;
34
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
543 jp_milcent 35
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
498 jp_milcent 36
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel;
66 jpm 37
import com.google.gwt.core.client.GWT;
324 jp_milcent 38
import com.google.gwt.http.client.Request;
39
import com.google.gwt.http.client.RequestBuilder;
40
import com.google.gwt.http.client.RequestCallback;
41
import com.google.gwt.http.client.RequestException;
42
import com.google.gwt.http.client.Response;
43
import com.google.gwt.json.client.JSONArray;
44
import com.google.gwt.json.client.JSONObject;
45
import com.google.gwt.json.client.JSONParser;
46
import com.google.gwt.json.client.JSONValue;
66 jpm 47
import com.google.gwt.user.client.Event;
61 jpm 48
 
49
public class AideFenetre extends Dialog {
50
 
311 jp_milcent 51
	private static final String SERVICE_NOM = "CoelAide";
543 jp_milcent 52
	private static final String PAGE_SOMMAIRE_CODE = "AideCOELSommaire";
53
	private static final String PAGE_A_OUVRIR_CODE = "AideCOELPresentationGenerale";
311 jp_milcent 54
	private static Menu pagePrincipale = null;
498 jp_milcent 55
	private static Menu menuAOuvrirParDefaut = null;
56
	private TreePanel<Menu> arbre = null;
69 jpm 57
	private TreeStore<Menu> magazin = null;
58
	private StoreFilterField<Menu> filtre = null;
324 jp_milcent 59
	private Menu sommaire = null;
343 jp_milcent 60
	private ContentPanel sommairePanneau = null;
66 jpm 61
	private ContentPanel contenuPanneau = null;
324 jp_milcent 62
	private HtmlContainer conteneurDuHtml;
343 jp_milcent 63
	private LayoutContainer entetePanneau;
543 jp_milcent 64
	private ToolBar sommaireBarreOutils;
324 jp_milcent 65
 
61 jpm 66
	public AideFenetre() {
67
		setBodyBorder(false);
69 jpm 68
		setButtons(Dialog.OK);
516 jp_milcent 69
		setIcon(Images.ICONES.appPanneauListe());
61 jpm 70
		setHeading("COEL - Aide");
324 jp_milcent 71
		setWidth(675);
72
		setHeight(400);
61 jpm 73
		setHideOnButtonClick(true);
324 jp_milcent 74
		setLayout(new BorderLayout());
61 jpm 75
 
543 jp_milcent 76
		creerPanneauEntete();
77
		creerPanneauSommaire();
78
		getSommaireArbreModele();
79
		creerPanneauContenu();
80
	}
81
 
82
	private void creerPanneauEntete() {
343 jp_milcent 83
		entetePanneau = new LayoutContainer();
84
		entetePanneau.setLayout(new FlowLayout());
85
		BorderLayoutData enteteDisposition = new BorderLayoutData(LayoutRegion.NORTH, 30);
86
		add(entetePanneau, enteteDisposition);
543 jp_milcent 87
	}
88
 
89
	private void creerPanneauSommaire() {
343 jp_milcent 90
		inititialiserSommaireArbreFiltre();
543 jp_milcent 91
		creerSommaire();
343 jp_milcent 92
 
93
		sommairePanneau = new ContentPanel();
324 jp_milcent 94
		sommairePanneau.setScrollMode(Scroll.AUTO);
543 jp_milcent 95
		sommairePanneau.setHeight("100%");
96
		sommairePanneau.setHeaderVisible(false);
97
 
98
		creerBarreOutilSommaire();
99
 
358 jp_milcent 100
		BorderLayoutData sommaireDisposition = new BorderLayoutData(LayoutRegion.WEST, 230, 175, 350);
66 jpm 101
		sommaireDisposition.setMargins(new Margins(0, 5, 0, 0));
102
		sommaireDisposition.setSplit(true);
103
		sommaireDisposition.setFloatable(true);
543 jp_milcent 104
		add(sommairePanneau, sommaireDisposition);
105
	}
106
 
107
	private void creerBarreOutilSommaire() {
108
		sommaireBarreOutils = new ToolBar();
109
		// FIXME : on peut utiliser l'alignement à droite car cela pose un problème de rendu dans le Hosted Mode
110
		//sommaireBarreOutils.setAlignment(HorizontalAlignment.RIGHT);
111
		creerBoutonPlierDeplier();
112
		sommairePanneau.setTopComponent(sommaireBarreOutils);
113
	}
114
 
115
	private void creerBoutonPlierDeplier() {
116
		Button plierDeplierToutBtn = new Button();
117
		plierDeplierToutBtn.addSelectionListener(new SelectionListener<ButtonEvent>() {
498 jp_milcent 118
			public void componentSelected(ButtonEvent be) {
119
				Button boutonPlierDeplierTout = (Button) be.getSource();
120
				if (boutonPlierDeplierTout.getIcon().equals(Images.ICONES.deplierTout())) {
543 jp_milcent 121
					boutonPlierDeplierTout.setIcon(Images.ICONES.replierTout());
343 jp_milcent 122
					arbre.expandAll();
498 jp_milcent 123
				} else if (boutonPlierDeplierTout.getIcon().equals(Images.ICONES.replierTout())) {
543 jp_milcent 124
					boutonPlierDeplierTout.setIcon(Images.ICONES.deplierTout());
343 jp_milcent 125
					arbre.collapseAll();
126
				}
543 jp_milcent 127
				boutonPlierDeplierTout.repaint();
343 jp_milcent 128
			}
129
		});
498 jp_milcent 130
		plierDeplierToutBtn.setIcon(Images.ICONES.deplierTout());
543 jp_milcent 131
		plierDeplierToutBtn.setScale(ButtonScale.SMALL);
343 jp_milcent 132
		plierDeplierToutBtn.setToolTip("Étendre le sommaire");
543 jp_milcent 133
		sommaireBarreOutils.add(plierDeplierToutBtn);
134
	}
135
 
136
	private void creerPanneauContenu() {
137
		contenuPanneau = new ContentPanel();
138
		contenuPanneau.setScrollMode(Scroll.AUTO);
343 jp_milcent 139
 
543 jp_milcent 140
		creerBarreOutilContenu();
61 jpm 141
 
543 jp_milcent 142
		BorderLayoutData contenuDisposition = new BorderLayoutData(LayoutRegion.CENTER);
143
		add(contenuPanneau, contenuDisposition);
144
	}
145
 
146
	private void creerBarreOutilContenu() {
498 jp_milcent 147
		Button imprimerBtn = new Button(null, new SelectionListener<ButtonEvent>() {
148
			public void componentSelected(ButtonEvent be) {
324 jp_milcent 149
				Print.it(conteneurDuHtml.el().getInnerHtml());
311 jp_milcent 150
			}
69 jpm 151
		});
516 jp_milcent 152
		imprimerBtn.setIcon(Images.ICONES.imprimer());
69 jpm 153
		contenuPanneau.getHeader().insertTool(imprimerBtn, 0);
61 jpm 154
	}
543 jp_milcent 155
 
498 jp_milcent 156
	private void etendreArbre(Menu menuAOuvrir) {
543 jp_milcent 157
		if (menuAOuvrir != null) {
158
			arbre.setExpanded(menuAOuvrir, true);
159
			arbre.getSelectionModel().select(menuAOuvrir, false);
160
		}
311 jp_milcent 161
	}
162
 
498 jp_milcent 163
	private void etendreArbre(String menuCode) {
164
		Menu menuAOuvrir = magazin.findModel("code", menuCode);
543 jp_milcent 165
		etendreArbre(menuAOuvrir);
311 jp_milcent 166
	}
167
 
498 jp_milcent 168
	private void creerSommaire() {
516 jp_milcent 169
		magazin = new TreeStore<Menu>();
66 jpm 170
 
171
		filtre.bind(magazin);
343 jp_milcent 172
 
516 jp_milcent 173
		arbre = new TreePanel<Menu>(magazin);
543 jp_milcent 174
		arbre.setAutoLoad(true);
175
		arbre.setHeight("100%");
176
		arbre.setDisplayProperty("nom");
516 jp_milcent 177
		arbre.getStyle().setLeafIcon(Images.ICONES.aide());
498 jp_milcent 178
		arbre.addListener(Events.OnClick, new Listener<TreePanelEvent<Menu>>(){
179
			public void handleEvent(TreePanelEvent<Menu> tpe) {
543 jp_milcent 180
				Menu menuSelectionne = arbre.getSelectionModel().getSelectedItem();
181
				if (menuSelectionne.get("code") != null) {
182
					selectionSommaire((String) menuSelectionne.get("code"));
66 jpm 183
				}
184
			}
325 jp_milcent 185
		});
66 jpm 186
	}
187
 
498 jp_milcent 188
	private void definirMenuAOuvrirParDefaut() {
189
		menuAOuvrirParDefaut = magazin.findModel("code", PAGE_A_OUVRIR_CODE);
311 jp_milcent 190
	}
191
 
66 jpm 192
	private void selectionSommaire(String page) {
193
		String serviceUrl = ((Configuration) Registry.get(RegistreId.CONFIG)).getServiceBaseUrl();
311 jp_milcent 194
		String aidePageUrl = serviceUrl+SERVICE_NOM+"/"+page;
66 jpm 195
		GWT.log("Sélection : "+aidePageUrl, null);
196
		chargerPageAide(aidePageUrl);
197
	}
198
 
199
	private void chargerPageAide(String url) {
200
		GWT.log("Charger : "+url, null);
324 jp_milcent 201
		conteneurDuHtml = new HtmlContainer() {
66 jpm 202
			public void onBrowserEvent(Event e) {
203
				// Nous vérifions que l'évenement est un clic et qu'il a lieu sur un lien
543 jp_milcent 204
				if (e.getTypeInt() == Event.ONCLICK && e.getEventTarget().toString().startsWith("http://")) {
66 jpm 205
					e.preventDefault();
498 jp_milcent 206
					String urlPageAideCible = e.getEventTarget().toString();
311 jp_milcent 207
					chargerPageAide(urlPageAideCible);
208
					String codePageAideCible = urlPageAideCible.substring(urlPageAideCible.lastIndexOf("/")+1);
209
					etendreArbre(codePageAideCible);
210
					GWT.log("Clic : "+urlPageAideCible+"::"+codePageAideCible, null);
211
				} else {
543 jp_milcent 212
					GWT.log("Event target:"+e.getEventTarget().toString()+" - type :"+e.getTypeInt()+"="+Event.ONCLICK, null);
66 jpm 213
				}
214
			}
215
		};
311 jp_milcent 216
 
217
		conteneurDuHtml.setId(ComposantId.PANNEAU_AIDE);
218
		conteneurDuHtml.setWidth(400);
219
		conteneurDuHtml.sinkEvents(Event.ONCLICK);
220
		conteneurDuHtml.setUrl(url);
221
		conteneurDuHtml.recalculate();
222
 
66 jpm 223
		contenuPanneau.removeAll();
311 jp_milcent 224
		contenuPanneau.add(conteneurDuHtml);
66 jpm 225
		contenuPanneau.layout();
226
	}
227
 
228
	private void inititialiserSommaireArbreFiltre() {
69 jpm 229
		filtre = new StoreFilterField<Menu>() {
66 jpm 230
			@Override
69 jpm 231
			protected boolean doSelect(Store<Menu> magazin, Menu parent, Menu enregistrement, String propriete, String filtre) {
311 jp_milcent 232
				Boolean retour = false;
69 jpm 233
				// Seul les feuilles sont traitées par le filtre
311 jp_milcent 234
				String nomMenu = enregistrement.getNom();
235
				String nomMenuMinuscule = nomMenu.toLowerCase();
236
				String nomFiltreMinuscule = filtre.toLowerCase();
543 jp_milcent 237
				String nomFiltreMinusculeProtege = Pattern.quote(nomFiltreMinuscule);
238
				if (nomMenuMinuscule.matches(".*"+nomFiltreMinusculeProtege+".*")) {
311 jp_milcent 239
					retour = true;
69 jpm 240
				}
343 jp_milcent 241
				return retour;
66 jpm 242
			}
243
		};
343 jp_milcent 244
		filtre.setFieldLabel("Chercher");
245
		filtre.setLabelStyle("font-weight:normal;");
246
		filtre.setToolTip("Filtrer le sommaire");
247
		filtre.setWidth(200);
248
 
249
		FormLayout fl = new FormLayout();
250
		fl.setLabelWidth(55);
251
 
252
		ContentPanel fp = new ContentPanel();
253
		fp.setHeaderVisible(false);
254
		fp.setLayout(fl);
255
		fp.add(filtre);
256
		entetePanneau.add(fp);
66 jpm 257
	}
258
 
324 jp_milcent 259
	public void getSommaireArbreModele() {
260
		String serviceUrl = ((Configuration) Registry.get(RegistreId.CONFIG)).getServiceBaseUrl();
261
		String sommairePageUrl = serviceUrl+SERVICE_NOM+"/"+PAGE_SOMMAIRE_CODE+"/sommaire";
262
		RequestBuilder rb = new RequestBuilder(RequestBuilder.GET, sommairePageUrl);
263
		try {
264
			rb.sendRequest(null, new RequestCallback() {
66 jpm 265
 
324 jp_milcent 266
				public void onError(Request request, Throwable exception) {
267
					// Gestion des exceptions déclenchées par l'exécution de la requête
268
					GWT.log("Erreur à l'exécution du service "+SERVICE_NOM+" (selection)", exception);
269
					Info.display("Erreur de Requête", "Une erreur s'est produite lors de l'exécution de la requête.");
270
				}
271
 
272
				public void onErrorHTTP(Request request, Response reponse) {
273
					// Gestion des erreurs HTTP renvoyé par Apache ou JRest
274
					Information info = new Information("erreur_jrest", JSONParser.parse(reponse.getText()).isArray());
275
					GWT.log("Erreur JREST - Code "+reponse.getStatusCode()+"\n"+info.getMessages().toString(), null);
276
					Info.display("Erreur JREST - Code "+reponse.getStatusCode(), info.toString());
277
				}
278
 
279
				public void onResponseReceived(Request request, Response response) {
280
					// Si le code de réponse HTTP ne vaut pas 200 OK, on lance le mécanise d'erreur HTTP
281
					if (response.getStatusCode() != 200) {
282
						onErrorHTTP(request, response);
283
					} else {
284
						if (response.getText().length() != 0 && response.getText() != null) {
285
							final JSONValue responseValue = JSONParser.parse(response.getText());
286
							JSONArray jsonArray = responseValue.isArray();
287
 
288
							if (jsonArray != null) {
289
								sommaire = new Menu("Sommaire");
290
								ajouterMenuRecursivement(sommaire, jsonArray);
66 jpm 291
 
516 jp_milcent 292
								magazin.removeAll();
324 jp_milcent 293
								magazin.add(sommaire, true);
294
 
498 jp_milcent 295
								definirMenuAOuvrirParDefaut();
325 jp_milcent 296
 
324 jp_milcent 297
								sommairePanneau.add(arbre);
498 jp_milcent 298
								sommairePanneau.layout();
343 jp_milcent 299
 
324 jp_milcent 300
								// Chargement de la page racine
301
								selectionSommaire(pagePrincipale.getCode());
498 jp_milcent 302
								etendreArbre(menuAOuvrirParDefaut);
324 jp_milcent 303
							} else {
304
								GWT.log("\n\tLa réponse n'est pas un objet ou un talbeau JSON et vaut : "+responseValue.toString(), null);
305
							}
306
						}
307
					}
308
				}
309
			});
310
		} catch (RequestException e) {
311
			e.printStackTrace();
66 jpm 312
		}
324 jp_milcent 313
	}
314
 
315
	private void ajouterMenuRecursivement(Menu brancheSommaire, JSONArray tableau) {
316
		final int tailleMax = tableau.size();
69 jpm 317
 
324 jp_milcent 318
		for (int i = 0; i < tailleMax; i++) {
319
			JSONObject pageCourante = tableau.get(i).isObject() ;
320
			if (pageCourante != null) {
321
				Menu menuCourrant = new Menu();
322
 
323
				if (pageCourante.get("code") != null) {
543 jp_milcent 324
					String codeCourant = pageCourante.get("code").isString().stringValue();
325
					menuCourrant.setCode(codeCourant);
324 jp_milcent 326
				}
327
				if (pageCourante.get("txt") != null) {
328
					menuCourrant.setNom(pageCourante.get("txt").isString().stringValue());
329
				}
330
 
331
				if (menuCourrant.getNom() == null && menuCourrant.getCode() == null) {
332
					menuCourrant = (Menu) brancheSommaire.getChild((brancheSommaire.getChildCount() - 1));
333
				} else {
334
					if (brancheSommaire != null) {
335
						brancheSommaire.add(menuCourrant);
336
						if (pagePrincipale == null) {
337
							pagePrincipale = menuCourrant;
338
						}
339
					}
340
				}
341
				if (pageCourante.get("liste") != null) {
342
					ajouterMenuRecursivement(menuCourrant, pageCourante.get("liste").isArray());
343
				}
344
			}
345
		}
66 jpm 346
	}
61 jpm 347
}