Rev 26 | Blame | Last modification | View Log | RSS feed
/**David Delon david.delon@clapas.net 2007*//** InventoryItemList.java (Composite de Panel)** Cas d'utilisation :** Affichage de releve** 1 : Recherche du nombre de releves associe au navigateur ou a l'utilisateur est connecte, en fonction des criteres de selection* 2 : Recherche des releves correspondant au critere precedent* 3 : Affichage des releves avec positionnement sur le dernier releve** Selection de releve** 1 : L'utilisateur selectionne un releve : lancement de l'affichage detaille pour le releve selectionne*** Pagination :** 1 : Avancement ou recul d'une page*** C(R)UD Element d'inventaire : (TODO : creer un nouvel objet pour cela)** 1 : Ajoute d'un element* 2 : Modification d'un element* 3 : Suppression d'un element*//* Actions declenchees :** onInventoryItemSelected(numero d'ordre de la ligne selectionne) : selection d'une ligne* onInventoryItemUnselected(numero d'ordre de la ligne selectionne) : deselection d'une ligne* onInventoryUpdated(location) : action suite a la modification, suppression, creation d'un element d'inventaire**/package org.tela_botanica.client;import java.util.Iterator;import java.util.Vector;import com.google.gwt.http.client.URL;import com.google.gwt.json.client.JSONArray;import com.google.gwt.json.client.JSONNumber;import com.google.gwt.json.client.JSONParser;import com.google.gwt.json.client.JSONString;import com.google.gwt.json.client.JSONValue;import com.google.gwt.user.client.HTTPRequest;import com.google.gwt.user.client.ResponseTextHandler;import com.google.gwt.user.client.ui.Composite;import com.google.gwt.user.client.ui.FlexTable;import com.google.gwt.user.client.ui.Grid;import com.google.gwt.user.client.ui.HTML;import com.google.gwt.user.client.ui.HorizontalPanel;import com.google.gwt.user.client.ui.Image;import com.google.gwt.user.client.ui.SourcesTableEvents;import com.google.gwt.user.client.ui.TableListener;import com.google.gwt.user.client.ui.VerticalPanel;import com.google.gwt.user.client.ui.Button;import com.google.gwt.user.client.ui.CheckBox;import com.google.gwt.user.client.ui.Widget;import com.google.gwt.user.client.ui.ClickListener;import com.google.gwt.user.client.ui.HasHorizontalAlignment;import com.google.gwt.user.client.ui.HasVerticalAlignment;public class InventoryItemList extends Composite{// Debut Barre de navigationprivate class NavBar extends Composite implements ClickListener {public final Button gotoFirst = new Button("<<", this);public final Button gotoNext = new Button(">", this);public final Button gotoPrev = new Button("<", this);public final Button gotoEnd = new Button(">>", this);public final HTML status = new HTML();public NavBar() {HorizontalPanel bar = new HorizontalPanel();bar.setStyleName("navbar");status.setStyleName("status");bar.add(status);bar.setCellHorizontalAlignment(status, HasHorizontalAlignment.ALIGN_RIGHT);bar.setCellVerticalAlignment(status, HasVerticalAlignment.ALIGN_MIDDLE);bar.setCellWidth(status, "100%");bar.add(gotoFirst);bar.add(gotoPrev);bar.add(gotoNext);bar.add(gotoEnd);initWidget(bar);}public void onClick(Widget sender) {if (sender == gotoNext) {// Move forward a page.startIndex += VISIBLE_TAXON_COUNT;if (startIndex >= count)startIndex -= VISIBLE_TAXON_COUNT;} else {if (sender == gotoPrev) {// Move back a page.startIndex -= VISIBLE_TAXON_COUNT;if (startIndex < 0)startIndex = 0;} else {if (sender == gotoEnd) {gotoEnd();} else {if (sender == gotoFirst) {startIndex = 0;}}}}update();}}// Fin Barre de navigation// Conteneur (header et table sont dans panel)private Grid header = new Grid(1, 3);private FlexTable table = new FlexTable();private VerticalPanel panel = new VerticalPanel();// Servicesprivate String serviceBaseUrl = null;private String user;private Mediator mediator = null;// Navigationprivate int startIndex = 0;private int count = 0;private static final int VISIBLE_TAXON_COUNT = 15;private NavBar navBar=null;private int selectedRow = -1;// Filtre par defaut :private String location = "all";private String date = "all";private String search = "all";private String station = "all";public InventoryItemList(Mediator med) {// Traitement contexte utilisateur et servicemediator=med;mediator.registerInventoryItemList(this);user=mediator.getUser();serviceBaseUrl = mediator.getServiceBaseUrl();// Barre navigation integree au headernavBar = new NavBar();// Mise en forme du headerheader.setCellSpacing(0);header.setCellPadding(2);header.setWidth("100%");header.setStyleName("inventoryItem-ListHeader");header.setWidget(0, 2, navBar);// Mise en forme de la table (contenu)table.setCellSpacing(0);table.setBorderWidth(0);table.setCellPadding(2);table.setWidth("100%");table.setStyleName("inventoryItem-List");panel.add(header);panel.add(table);table.addTableListener(new TableListener () {public void onCellClicked(SourcesTableEvents sender, int row, int cell) {if ((table.getWidget(row, 0)!=null) && (cell>0)){if (row!=selectedRow) {selectRow(row);// Numero d'ordremediator.onInventoryItemSelected(table.getText(row, 5));}// Deselectionelse {styleRow(row, false);selectedRow=-1;// Numero d'ordremediator.onInventoryItemUnselected(table.getText(row, 5));}}}});initWidget(panel);}/*** Gestion affichage selection/deselection d'un element d'inventaire**/private void selectRow(int row) {styleRow(selectedRow, false);styleRow(row, true);selectedRow = row;}private void styleRow(int row, boolean selected) {if (row != -1) {if (selected)table.getRowFormatter().addStyleName(row, "inventoryItem-SelectedRow");elseif (row < table.getRowCount()) {table.getRowFormatter().removeStyleName(row, "inventoryItem-SelectedRow");}}}/*** Lancement de la mise a jour d'une ligne d'inventaire (appele par Mediator.onModifyInventoryItem())**/public void updateElement() {if (mediator.inventoryItemIsValid()) {final InventoryItem inventoryItem=mediator.getInventoryItem();setStatusDisabled();// Modification d'un nom faisant parti du referentiel : recherche du nom valide correspondantif (inventoryItem.getNomenclaturalNumber() !=null) {HTTPRequest.asyncGet(serviceBaseUrl + "/NameValid/" + inventoryItem.getNomenclaturalNumber(),new ResponseTextHandler() {public void onCompletion(String strcomplete) {JSONValue jsonValue = JSONParser.parse(strcomplete);JSONArray jsonArray;if ((jsonArray = jsonValue.isArray()) != null) {// Nom retenu, Num Nomen nom retenu, Num Taxon, FamilleupdateElement(inventoryItem.getOrdre(),inventoryItem.getName(), inventoryItem.getNomenclaturalNumber(),Util.toCelString(((JSONString) jsonArray.get(0)).toString()),((JSONString) jsonArray.get(1)).stringValue(),((JSONString) jsonArray.get(2)).stringValue(),Util.toCelString(((JSONString) jsonArray.get(3)).toString()),inventoryItem.getLocation(),inventoryItem.getLocation_id(),inventoryItem.getDate(),inventoryItem.getMilieu(),inventoryItem.getComment());}}});}// Modification d'un nom ne faisant pas parti du referentiel (saisie libre)else {updateElement(inventoryItem.getOrdre(),inventoryItem.getName(), " ", " ", " ", " ", " ",inventoryItem.getLocation(),inventoryItem.getLocation_id(),inventoryItem.getDate(),inventoryItem.getMilieu(),inventoryItem.getComment());}}else {// TODO : message d'erreurreturn;}}/*** Lancement de la creation d'une ligne d'inventaire (appele par Mediator.onAddInventoryItem())**/public void addelement() {if (mediator.inventoryItemIsValid()) {final InventoryItem inventoryItem=mediator.getInventoryItem();setStatusDisabled();// Creation d'un nom faisant parti du referentiel : recherche du nom valide correspondantif (inventoryItem.getNomenclaturalNumber() !=null) {HTTPRequest.asyncGet(serviceBaseUrl + "/NameValid/" + inventoryItem.getNomenclaturalNumber(),new ResponseTextHandler() {public void onCompletion(String strcomplete) {JSONValue jsonValue = JSONParser.parse(strcomplete);JSONArray jsonArray;if ((jsonArray = jsonValue.isArray()) != null) {// Nom retenu, Num Nomen nom retenu, Num Taxon,// FamilleaddElement(inventoryItem.getName(), inventoryItem.getNomenclaturalNumber(),Util.toCelString(((JSONString) jsonArray.get(0)).toString()),((JSONString) jsonArray.get(1)).stringValue(),((JSONString) jsonArray.get(2)).stringValue(),Util.toCelString(((JSONString) jsonArray.get(3)).toString()),inventoryItem.getLocation(),inventoryItem.getLocation_id(),inventoryItem.getDate(),inventoryItem.getMilieu(),inventoryItem.getComment());}}});}// Saisie libreelse {addElement(inventoryItem.getName(), " ", " ", " ", " ", " ",inventoryItem.getLocation(),inventoryItem.getLocation_id(),inventoryItem.getDate(),inventoryItem.getMilieu(),inventoryItem.getComment());}}else {// TODO : message d'erreurreturn;}}/*** Ajoute effectif d'un element a l'inventaire (appel interne)** @param nom_sel :* nom selectionne* @param num_nom_sel :* numero nomenclatural nom selectionne* @param nom_ret :* nom retenu* @param num_nom_ret :* numero nomenclaturel nom retenu* @param num_taxon :* numero taxonomique* @param famille :* famille*/private void addElement(String nom_sel, String num_nom_sel, String nom_ret,String num_nom_ret, String num_taxon, String famille,final String loc, String id_location,String dat, String milieu, String comment) {count++;HTTPRequest.asyncPost(serviceBaseUrl + "/Inventory/", "identifiant="+ user + "&nom_sel=" + URL.encodeComponent(nom_sel) + "&num_nom_sel=" + num_nom_sel+ "&nom_ret=" + URL.encodeComponent(nom_ret) + "&num_nom_ret=" + num_nom_ret+ "&num_taxon=" + num_taxon + "&famille=" + URL.encodeComponent(famille) + "&location=" + URL.encodeComponent(loc) + "&id_location=" + id_location + "&date_observation=" + dat+ "&station="+ URL.encodeComponent(milieu) + "&commentaire="+ URL.encodeComponent(comment),new ResponseTextHandler() {public void onCompletion(String str) {location=loc;if (location.compareTo("")==0) {location="000null";}mediator.onInventoryUpdated(location);}});}/*** Modification effective d'un element de l'inventaire (appel interne)** @param ordre : numero d'ordre* @param nom_sel :* nom selectionne* @param num_nom_sel :* numero nomenclatural nom selectionne* @param nom_ret :* nom retenu* @param num_nom_ret :* numero nomenclaturel nom retenu* @param num_taxon :* numero taxonomique* @param famille :* famille*/private void updateElement(String ordre, String nom_sel, String num_nom_sel, String nom_ret,String num_nom_ret, String num_taxon, String famille,final String loc, String id_location,String dat, String milieu, String comment) {HTTPRequest.asyncPost(serviceBaseUrl + "/Inventory/" + user + "/" +ordre + "/","&nom_sel=" + URL.encodeComponent(nom_sel) + "&num_nom_sel=" + num_nom_sel+ "&nom_ret=" + URL.encodeComponent(nom_ret) + "&num_nom_ret=" + num_nom_ret+ "&num_taxon=" + num_taxon + "&famille=" + URL.encodeComponent(famille) + "&location=" + URL.encodeComponent(loc) + "&id_location=" + id_location + "&date_observation=" + dat+ "&station="+ URL.encodeComponent(milieu) + "&commentaire="+ URL.encodeComponent(comment),new ResponseTextHandler() {public void onCompletion(String str) {mediator.onInventoryUpdated(location);}});}/*** Suppression effective d'un element lde l'inventaire, a partir de son numero d'ordre**/public void deleteElement() {setStatusDisabled();Vector parseChecked = new Vector();for (int i = table.getRowCount() - 1; i >= 0; i--) {if (table.getWidget(i, 0)!=null) {if (((CheckBox) table.getWidget(i, 0)).isChecked()) {// Numero ordreparseChecked.add(table.getText(i, 5));count--;}}}StringBuffer ids=new StringBuffer();for (Iterator it = parseChecked.iterator(); it.hasNext();) {ids.append((String)it.next());if (it.hasNext()) ids.append(",");}if (ids.length()>0) {HTTPRequest.asyncPost(serviceBaseUrl + "/Inventory/" + user+ "/" + ids.toString(), "action=DELETE",new ResponseTextHandler() {public void onCompletion(String str) {mediator.onInventoryUpdated(location);}});}setStatusEnabled();}/*** Transmission de releve a Tela (TODO : a appeler par Mediator)*/public void transmitElement() {setStatusDisabled();Vector parseChecked = new Vector();for (int i = table.getRowCount() - 1; i >= 0; i--) {if (table.getWidget(i, 0)!=null) {if (((CheckBox) table.getWidget(i, 0)).isChecked()) {// Numero ordreparseChecked.add(table.getText(i, 5));}}}StringBuffer ids=new StringBuffer();for (Iterator it = parseChecked.iterator(); it.hasNext();) {ids.append((String)it.next());if (it.hasNext()) ids.append(",");}if (ids.length()>0) {HTTPRequest.asyncPost(serviceBaseUrl + "/InventoryTransmit/" + user+ "/" + ids.toString(), "transmission=1",new ResponseTextHandler() {public void onCompletion(String str) {update(); // Pour affichage logo}});}setStatusEnabled();}/*** Selection/Deselection de l'ensemble des elements affiches**/public void selectAll() {for (int i = table.getRowCount() - 1; i >= 0; i--) {if (table.getWidget(i, 0)!=null)((CheckBox) table.getWidget(i, 0)).setChecked(true);}}public void deselectAll() {for (int i = table.getRowCount() - 1; i >= 0; i--) {if (table.getWidget(i, 0)!=null)((CheckBox) table.getWidget(i, 0)).setChecked(false);}}/*** Recherche nombre d'enregistrement pour l'utilisateur et la localite en cours**/public void updateCount () {setStatusDisabled();// Transformation de la date selectionne vers date time stampString adate="all";if (date.compareTo("all")!=0) {adate=date.substring(6,10)+"-"+date.substring(3,5)+"-"+date.substring(0,2)+" 00:00:00";}HTTPRequest.asyncGet(serviceBaseUrl + "/InventoryItemList/" + user + "/" + URL.encodeComponent(location) + "/" + adate + "/" + URL.encodeComponent(search) + "/" + URL.encodeComponent(station),new ResponseTextHandler() {public void onCompletion(String str) {JSONValue jsonValue = JSONParser.parse(str);JSONNumber jsonNumber;if ((jsonNumber = jsonValue.isNumber()) != null) {count = (int) jsonNumber.getValue();// if (location.compareTo("")==0) location="000null";gotoEnd(); // Derniere pageupdate();}}});}// Utilitaire affichageprivate String subLeft(String text, int length) {return (text.length() < length) ? text : text.substring(0, length)+ " ...";}/*** Mise a jour de l'affichage, a partir des donnaes d'inventaire deja* saisies. La valeur de this.startIndex permet de determiner quelles* donnaes seront affichees**/public void update() {// table.setBorderWidth(1); // Debug// Ligne d'informationsetStatusDisabled();String adate="all";if (date.compareTo("all")!=0) {adate=date.substring(6,10)+"-"+date.substring(3,5)+"-"+date.substring(0,2)+" 00:00:00";}HTTPRequest.asyncGet(serviceBaseUrl + "/InventoryItemList/" + user + "/" + URL.encodeComponent(location) +"/" + adate + "/" + URL.encodeComponent(search) + "/" + URL.encodeComponent(station) + "/"+ startIndex + "/" + VISIBLE_TAXON_COUNT,new ResponseTextHandler() {public void onCompletion(String str) {JSONValue jsonValue = JSONParser.parse(str);JSONArray jsonArray;JSONArray jsonArrayNested;int row=0;int i=0;if ((jsonArray = jsonValue.isArray()) != null) {StringBuffer left=new StringBuffer();StringBuffer center=new StringBuffer();StringBuffer right=new StringBuffer();int arraySize = jsonArray.size();for (i = 0; i < arraySize; ++i) {if ((jsonArrayNested = jsonArray.get(i).isArray()) != null) {// Optimisationif (i>=table.getRowCount()) {row = table.insertRow(table.getRowCount());}else {row = i;}left=new StringBuffer();center=new StringBuffer();right=new StringBuffer();// Case a cochertable.setWidget(row, 0, new CheckBox());// Statut Observation transmise ?String atransmit=((JSONString) jsonArrayNested .get(11)).stringValue();if (atransmit.compareTo("1")==0) {table.setWidget(row,1,new Image("tela.gif"));}else {table.setWidget(row,1,new HTML(" "));}// Nom saisileft.append("<b>"+Util.toCelString(((JSONString) jsonArrayNested .get(0)).toString())+"</b>");// Nom retenuString aname=Util.toCelString(((JSONString) jsonArrayNested .get(2)).toString());if (aname.compareTo("null")==0) {}else {center.append(aname+", ");}// Num nomenclaturalString ann=((JSONString) jsonArrayNested .get(3)).stringValue();if (ann.compareTo("0")!=0) {center.append(""+ann+"-");}else {center.append("0-");}// Num TaxonomiqueString ant=((JSONString) jsonArrayNested .get(4)).stringValue();if (ant.compareTo("0")!=0) {center.append(ant+", ");}else {center.append("0, ");}// FamilleString afamily=Util.toCelString(((JSONString) jsonArrayNested .get(5)).toString());if (afamily.compareTo("null")==0) {//}else {center.append(afamily+", ");}String aloc=Util.toCelString(((JSONString) jsonArrayNested .get(6)).toString());// Localisation - Lieuif (aloc.compareTo("000null")==0) {if (center.length()==0) {center.append("Commune absente");}else {center.append("commune absente");}}else {if (center.length()==0) {center.append("Commune de "+aloc);}else {center.append("commune de "+aloc);}}String alieudit=Util.toCelString(((JSONString) jsonArrayNested .get(9)).toString());// Localisation - Lieu ditif (alieudit.compareTo("000null")!=0) {center.append(", "+alieudit);}String acomment=Util.toCelString(((JSONString) jsonArrayNested .get(10)).toString());// Commentaireif (acomment.compareTo("null")!=0) {center.append(", "+acomment);}String adate=((JSONString) jsonArrayNested .get(8)).stringValue();// Dateif (adate.compareTo("0000-00-00 00:00:00")!=0) {right.append("<b>"+adate+"</b>");}else {// right.append("<b>00/00/0000</b>");}table.setHTML(row, 2, subLeft(" "+left,40));table.setHTML(row, 3, subLeft(" "+center,120));table.setHTML(row, 4, subLeft(" "+right,25));table.getRowFormatter().removeStyleName(row, "inventoryItem-SelectedRow");table.getCellFormatter().setWidth(row,0,"2%");table.getCellFormatter().setWidth(row,1,"2%");table.getCellFormatter().setWordWrap(row,2,false);table.getCellFormatter().setWidth(row,2,"10%");table.getCellFormatter().setWordWrap(row,3,false);table.getCellFormatter().setWordWrap(row,4,false);table.getCellFormatter().setWidth(row,4,"7%");String aordre=((JSONString) jsonArrayNested.get(7)).stringValue();// Numero d'ordre (cache)table.setText(row, 5, aordre);table.getCellFormatter().setVisible(row, 5, false);}}}// Suppression fin ancien affichagefor (int j=i;j<VISIBLE_TAXON_COUNT;j++) {table.setHTML(j,0," ");table.setHTML(j,1," ");table.setHTML(j,2," ");table.setHTML(j,3," ");table.setHTML(j,4," ");table.setHTML(j,5," ");table.getCellFormatter().setVisible(j, 5, false);table.getRowFormatter().removeStyleName(j, "inventoryItem-SelectedRow");}setStatusEnabled();}});}/*** Affichage message d'attente et desactivation navigation** @param* @return void*/private void setStatusDisabled() {navBar.gotoFirst.setEnabled(false);navBar.gotoPrev.setEnabled(false);navBar.gotoNext.setEnabled(false);navBar.gotoEnd.setEnabled(false);navBar.status.setText("Patientez ...");}/*** Affichage numero de page et gestion de la navigation**/private void setStatusEnabled() {// Il y a forcemment un disabled avant d'arriver iciif (count > 0) {if (startIndex >= VISIBLE_TAXON_COUNT) { // Au dela de la// premiere pagenavBar.gotoPrev.setEnabled(true);navBar.gotoFirst.setEnabled(true);if (startIndex < (count - VISIBLE_TAXON_COUNT)) { // Pas la// derniere// pagenavBar.gotoNext.setEnabled(true);navBar.gotoEnd.setEnabled(true);navBar.status.setText((startIndex + 1) + " - "+ (startIndex + VISIBLE_TAXON_COUNT) + " sur " + count );} else { // Derniere pagenavBar.status.setText((startIndex + 1) + " - " + count + " sur " + count );}} else { // Premiere pageif (count > VISIBLE_TAXON_COUNT) { // Des pages derrieresnavBar.gotoNext.setEnabled(true);navBar.gotoEnd.setEnabled(true);navBar.status.setText((startIndex + 1) + " - "+ (startIndex + VISIBLE_TAXON_COUNT) + " sur " + count);} else {navBar.status.setText((startIndex + 1) + " - " + count + " sur " + count);}}}else { // Pas d'inventaire, pas de navigationnavBar.status.setText("0 - 0 sur 0");}}/** Positionnement index de parcours (this.startIndex) pour affichage de la* derniere page** @param* @return void*/private void gotoEnd() {if ((count == 0) || (count % VISIBLE_TAXON_COUNT) > 0) {startIndex = count - (count % VISIBLE_TAXON_COUNT);} else {startIndex = count - VISIBLE_TAXON_COUNT;}}/** Recherche en cours**/public void setSearch(String search) {this.search = search;}/** Localite en cours**/public void setLocation(String location) {this.location = location;}/** Station en cours**/public void setStation(String station) {this.station = station;}/** Date en cours**/public void setDate(String date) {this.date = date;}/** Utilisateur en cours**/public void setUser(String user) {this.user = user;}public String getDate() {return date;}public String getLocation() {return location;}public String getSearch() {return search;}public String getStation() {return station;}public Grid getHeader() {return header;}public void displayFilter() {// Mise a jour boutton export feuille de calculmediator.getActionPanel().getExportButton().setHTML("<a href=\""+mediator.getServiceBaseUrl()+"/InventoryExport/"+ user + "/"+ URL.encodeComponent(location) + "/"+ URL.encodeComponent(station)+ "/"+ URL.encodeComponent(search) + "/"+ date +"\">"+"Export tableur</a>");// Mise a jour ligne de selectionString com;if (location.compareTo("all")==0) {com="Toutes communes";}else {if (location.compareTo("000null")==0) {com="Communes non renseignées";}else {com="Commune de "+location;}}String dat;if (date.compareTo("all")==0) {dat=", toutes périodes";}else {if (date.compareTo("00/00/0000")==0) {dat=", périodes non renseignées";}else {dat=", le "+ date;}}String stat;if (station.compareTo("all")==0) {stat=", toutes stations";}else {if (station.compareTo("000null")==0) {stat=", stations non renseignées";}else {stat=", station "+ station;}}header.setHTML(0, 0, com + dat + stat );}}/* +--Fin du code ---------------------------------------------------------------------------------------+* $Log$* Revision 1.7 2007-09-17 19:25:34 ddelon* Documentation**/