New file |
0,0 → 1,595 |
package org.tela_botanica.client.vues; |
|
import java.util.Comparator; |
import java.util.Iterator; |
|
import org.tela_botanica.client.interfaces.Filtrable; |
import org.tela_botanica.client.interfaces.Rafraichissable; |
import org.tela_botanica.client.modeles.DateObservation; |
import org.tela_botanica.client.modeles.EntiteGeographiqueObservation; |
import org.tela_botanica.client.modeles.ListeDateObservation; |
import org.tela_botanica.client.modeles.ListeEntiteGeographiqueObservation; |
import org.tela_botanica.client.modeles.ListeObservation; |
import org.tela_botanica.client.modeles.Observation; |
import org.tela_botanica.client.observation.ObservationMediateur; |
|
import com.google.gwt.core.client.GWT; |
import com.google.gwt.json.client.JSONArray; |
import com.google.gwt.json.client.JSONObject; |
import com.google.gwt.user.client.Window; |
import com.google.gwt.user.client.ui.Label; |
import com.gwtext.client.data.Node; |
import com.gwtext.client.data.NodeTraversalCallback; |
import com.gwtext.client.data.Tree; |
import com.gwtext.client.data.event.NodeListenerAdapter; |
import com.gwtext.client.widgets.Component; |
import com.gwtext.client.widgets.Panel; |
import com.gwtext.client.widgets.event.PanelListenerAdapter; |
import com.gwtext.client.widgets.tree.TreeNode; |
import com.gwtext.client.widgets.tree.TreePanel; |
import com.gwtext.client.widgets.tree.event.TreeNodeListenerAdapter; |
import com.gwtext.client.widgets.tree.event.TreePanelListenerAdapter; |
import com.gwtext.client.core.EventObject; |
|
/** |
* fenêtre de recherche affichant l'arbre des mots clés en lecture et un bouton |
* cliquable |
* |
* @author aurelien |
* |
*/ |
public class ArbreDateObservationFiltreVue extends Panel implements Rafraichissable, |
Filtrable { |
|
/** |
* Le médiateur associé à la vue |
*/ |
private ObservationMediateur observationMediateur = null; |
|
/** |
* Les localites en cours |
*/ |
private String donneesDateEnCours = ""; |
|
/** |
* Le treepanel qui affiche l'arbre |
*/ |
private TreePanel arbreDonneesDates = null; |
|
|
|
/** |
* La structure de donnees qui stocke l'arbre. Utilisee a ce niveau car trop liee a la vue |
*/ |
|
private Tree donneesDates = new Tree(); |
|
/** |
* booléen d'initialisation |
*/ |
private boolean estInstancie = false; |
|
/** |
* booléen d'etat |
*/ |
private boolean filtreModifie = false; |
|
private boolean arbreCharge = false ; |
|
private String nomFiltre = "" ; |
|
/** |
* Constructeur sans argument (privé car ne doit pas être utilisé) |
*/ |
@SuppressWarnings("unused") |
private ArbreDateObservationFiltreVue() { |
super(); |
} |
|
/** |
* Constructeur avec paramètres |
* |
* @param im |
* le médiateur à associer |
*/ |
public ArbreDateObservationFiltreVue(ObservationMediateur obs) { |
|
// on crée le panel |
super("Dates"); |
|
this.observationMediateur = obs; |
|
arbreDonneesDates = new TreePanel(); |
|
this.setPaddings(5); |
|
this.setBorder(false); |
this.setCollapsible(true); |
this.setAutoWidth(true); |
|
|
// on ajoute les listeners |
ajouterListenersPanel(); |
estInstancie = false; |
} |
|
/** |
* Ajoute les listeners pour le rendu du panel |
*/ |
private void ajouterListenersPanel() { |
this.addListener(new PanelListenerAdapter() { |
|
// on instancie réellement les composants au moment du rendu pour |
// accélérer l'affichage |
// et éviter des bugs |
public void onRender(Component component) { |
|
// on interdit le drag and drop dans l'arbre |
arbreDonneesDates.setEnableDD(false); |
arbreDonneesDates.setId("x-view-tree-filter-date"); |
arbreDonneesDates.setAutoWidth(false); |
|
if (GWT.isScript()) { |
arbreDonneesDates.setAutoScroll(true); |
} |
|
arbreDonneesDates.setBorder(false); |
|
// on crée une racine pour l'arbre |
TreeNode root = new TreeNode("Dates"); |
root.setId("racine_date"); |
String[] usObject = { "Dates" }; |
root.setUserObject(usObject); |
|
arbreDonneesDates.setRootNode(root); |
arbreDonneesDates.setRootVisible(true); |
arbreDonneesDates.setBorder(false); |
root.setExpandable(true) ; |
|
add(arbreDonneesDates); |
|
// on ajoute les listeners d'évenements |
ajouterListeners(); |
|
|
// enfin on considère le composant comme instancié |
estInstancie = true; |
|
|
} |
|
}); |
} |
|
|
|
/** |
* ajoute les listeners pour les boutons et le cochage des entites |
*/ |
private void ajouterListeners() { |
|
arbreDonneesDates.addListener(new TreePanelListenerAdapter() { |
|
public void onClick(TreeNode node, EventObject e) { |
|
nomFiltre = "" ; |
donneesDateEnCours = "" ; |
String nomPere = "" ; |
String nomGrandPere = "" ; |
|
switch(node.getDepth()) |
{ |
case 0: |
if(!arbreCharge) |
{ |
arbreCharge = true ; |
observationMediateur.obtenirDatesObservation() ; |
} |
else |
{ |
observationMediateur.obtenirNombreObservation() ; |
} |
return ; |
case 3: nomFiltre += "annee,mois,jour"; |
nomPere = ((String[])node.getParentNode().getUserObject())[0] ; |
nomGrandPere = ((String[])node.getParentNode().getParentNode().getUserObject())[0] ; |
donneesDateEnCours += nomGrandPere+","+nomPere+","+((String[])node.getUserObject())[0] ; |
break; |
case 2: nomFiltre += "annee,mois"; |
nomPere = ((String[])node.getParentNode().getUserObject())[0] ; |
donneesDateEnCours += nomPere+","+((String[])node.getUserObject())[0] ; |
break; |
case 1: nomFiltre += "annee"; |
donneesDateEnCours += ((String[])node.getUserObject())[0] ; |
break; |
default: |
break; |
} |
|
filtreModifie = true ; |
observationMediateur.obtenirNombreObservation() ; |
|
} |
|
}) ; |
|
arbreDonneesDates.getRootNode().addListener(new TreeNodeListenerAdapter() { |
|
public void onExpand(Node node) { |
if(!arbreCharge) |
{ |
observationMediateur.obtenirDatesObservation() ; |
arbreCharge = true ; |
} |
} |
|
}) ; |
} |
|
/** |
* Méthode héritée de l'interface rafraichissable |
*/ |
public void rafraichir(Object nouvelleDonnees, |
boolean repandreRaffraichissement) { |
|
if (nouvelleDonnees instanceof ListeDateObservation) { |
|
String annee=null; |
String mois=null; |
String jour=null; |
|
ListeDateObservation data = (ListeDateObservation) nouvelleDonnees ; |
|
// on crée un arbre vide |
TreeNode root = new TreeNode(); |
root.setId("racine_date"); |
root.setText("Dates"); |
String[] usObjRoot = { "Dates"}; |
root.setUserObject(usObjRoot); |
donneesDates.setRootNode(root); |
|
// on la parse et on récupère les informations qui nous interessent |
for (Iterator<String> it= data.keySet().iterator(); it.hasNext();) { |
|
DateObservation ent=(DateObservation) data.get(it.next()); |
|
|
annee= ent.getAnnee() ; |
mois= ent.getMois() ; |
String moisLettre = renvoyerMois(Integer.parseInt(mois)) ; |
jour= ent.getJour() ; |
|
if(annee.contains("0000")) { |
annee="Inconnue" ; |
} |
|
if(moisLettre.contains("00")) { |
mois="Inconnue" ; |
} |
|
if(jour.contains("00")) { |
jour="Inconnue" ; |
} |
|
Node noeudMemeId = donneesDates.getNodeById(""+annee); |
// si la région existe déjà |
if(noeudMemeId != null) |
{ |
// on teste si la localité existe |
Node noeudMemeLoc = donneesDates.getNodeById(""+(annee+mois)); |
if(noeudMemeLoc != null) |
{ |
// enfin on teste si le lieu dit existe |
Node noeudMemeLieu = donneesDates.getNodeById(""+(annee+mois+jour)); |
if(noeudMemeLieu != null) |
{ |
// tous les noeuds existent déjà, normalement ça ne devrait pas arriver |
} |
else |
{ |
// enfin on ne crée que le noeud du lieu dit |
TreeNode node_lieu = new TreeNode(); |
node_lieu.setId(""+(annee+mois+jour)); |
node_lieu.setText(jour); |
noeudMemeLoc.appendChild(node_lieu) ; |
String[] usObj = {jour}; |
node_lieu.setUserObject(usObj); |
} |
} |
else |
{ |
TreeNode node_loc = new TreeNode(); |
node_loc.setId(""+(annee+mois)); |
node_loc.setText(moisLettre); |
noeudMemeId.appendChild(node_loc) ; |
String[] usObj = {mois}; |
node_loc.setUserObject(usObj); |
|
TreeNode node_lieu = new TreeNode(); |
node_lieu.setId(""+(annee+mois+jour)); |
node_lieu.setText(jour); |
node_loc.appendChild(node_lieu) ; |
String[] usObj2 = {jour}; |
node_lieu.setUserObject(usObj2); |
|
} |
} |
else |
{ |
TreeNode node_id_loc = new TreeNode(); |
node_id_loc.setId(""+annee); |
node_id_loc.setText(annee); |
root.appendChild(node_id_loc) ; |
String[] usObj = {annee}; |
node_id_loc.setUserObject(usObj); |
|
TreeNode node_loc = new TreeNode(); |
node_loc.setId(""+(annee+mois)); |
node_loc.setText(moisLettre); |
node_id_loc.appendChild(node_loc) ; |
String[] usObj2 = {mois}; |
node_loc.setUserObject(usObj2); |
|
TreeNode node_lieu = new TreeNode(); |
node_lieu.setId(""+(annee+mois+jour)); |
node_lieu.setText(jour); |
node_loc.appendChild(node_lieu) ; |
String[] usObj3 = {jour}; |
node_lieu.setUserObject(usObj3); |
} |
|
} |
|
// on vide tous les noeuds |
arbreDonneesDates.getRootNode().eachChild(new NodeTraversalCallback() { |
|
public boolean execute(Node node) { |
|
node.remove(); |
return true; |
} |
|
}); |
|
// et on recopie le nouvel arbre |
copierFilsNoeud(donneesDates.getRootNode(), arbreDonneesDates |
.getRootNode()); |
|
// si l'arbre n'était pas encore considéré comme instancié |
if (!estInstancie) { |
// on signale que oui |
estInstancie = true; |
} |
|
// l'état du filtre est réinitialisé |
filtreModifie = false; |
//show() ; |
doLayout(); |
|
} |
|
if(nouvelleDonnees instanceof DateObservation) |
{ |
DateObservation ent = (DateObservation)nouvelleDonnees ; |
|
String annee= ent.getAnnee() ; |
String mois= ent.getMois() ; |
String moisLettre = renvoyerMois(Integer.parseInt(mois)) ; |
|
String jour= ent.getJour() ; |
|
Node root = arbreDonneesDates.getRootNode() ; |
|
if(annee.contains("0000") || annee.equals(null)) { |
annee="Inconnue" ; |
} |
|
if(mois.contains("00") || mois.equals(null)) { |
mois="Inconnue" ; |
} |
|
if(jour.contains("00") || jour.equals(null)) { |
jour="Inconnue" ; |
} |
|
Node noeudMemeId = donneesDates.getNodeById(""+annee); |
// si la région existe déjà |
if(noeudMemeId != null) |
{ |
// on teste si la localité existe |
Node noeudMemeLoc = donneesDates.getNodeById(""+(annee+mois)); |
if(noeudMemeLoc != null) |
{ |
// enfin on teste si le lieu dit existe |
Node noeudMemeLieu = donneesDates.getNodeById(""+(annee+mois+jour)); |
if(noeudMemeLieu != null) |
{ |
// tous les noeuds existent déjà, normalement ça ne devrait pas arriver |
} |
else |
{ |
// enfin on ne crée que le noeud du lieu dit |
TreeNode node_lieu = new TreeNode(); |
node_lieu.setId(""+(annee+mois+jour)); |
node_lieu.setText(jour); |
String[] usObj = {jour}; |
node_lieu.setUserObject(usObj); |
noeudMemeLoc.appendChild(node_lieu) ; |
} |
} |
else |
{ |
TreeNode node_loc = new TreeNode(); |
node_loc.setId(""+(annee+mois)); |
node_loc.setText(mois); |
String[] usObj = {moisLettre}; |
node_loc.setUserObject(usObj); |
noeudMemeId.appendChild(node_loc) ; |
|
TreeNode node_lieu = new TreeNode(); |
node_lieu.setId(""+(annee+mois+jour)); |
node_lieu.setText(jour); |
String[] usObj2 = {jour}; |
node_lieu.setUserObject(usObj2); |
node_loc.appendChild(node_lieu) ; |
|
} |
} |
else |
{ |
// TODO: Pourquoi l'ajout ne marche que sur la racine ? |
|
TreeNode node_id_loc = new TreeNode(); |
node_id_loc.setId(""+annee); |
node_id_loc.setText(annee); |
String[] usObj = {annee}; |
node_id_loc.setUserObject(usObj); |
root.appendChild(node_id_loc) ; |
|
TreeNode node_loc = new TreeNode(); |
node_loc.setId(""+(annee+mois)); |
node_loc.setText(mois); |
String[] usObj2 = {moisLettre}; |
node_loc.setUserObject(usObj2); |
node_id_loc.appendChild(node_loc) ; |
|
TreeNode node_lieu = new TreeNode(); |
node_lieu.setId(""+(annee+mois+jour)); |
node_lieu.setText(jour); |
String[] usObj3 = {jour}; |
node_lieu.setUserObject(usObj3); |
node_loc.appendChild(node_lieu) ; |
|
// TODO : améliorer la compararaison des noeuds |
root.sort(comparerNoeuds()) ; |
} |
|
arbreDonneesDates.doLayout() ; |
} |
} |
|
|
/** |
* Accesseur pour le panneau contenant l'arbre |
* |
* @return le panneau de l'arbre des mots clés |
*/ |
public TreePanel getArbreMotsCles() { |
return arbreDonneesDates; |
} |
|
/** |
* Méthode héritée de Filtrable renvoie le nom du filtre |
*/ |
public String renvoyerNomFiltre() { |
|
return "Dates"; |
} |
|
/** |
* Renvoie un tableau contenant le nom du champ à filtrer et la valeur |
* |
* @return un tableau contenant le nom du champ à filtrer et sa valeur |
*/ |
public String[] renvoyerValeursAFiltrer() { |
|
valider(); |
|
String valeursFiltrees[] = {nomFiltre, donneesDateEnCours } ; |
|
return valeursFiltrees; |
} |
|
/** |
* Fonction récursive qui prend deux noeuds d'arbre en paramètre et crée un |
* copie du sous arbre du premier noeud, qu'elle concatène au deuxième |
* |
* @param ndPereOriginal |
* le père des noeuds de l'arbre original |
* @param ndPereCopie |
* le père qui va recevoir les copies |
*/ |
private void copierFilsNoeud(Node ndPereOriginal, TreeNode ndPereCopie) { |
if (ndPereCopie != null && ndPereOriginal != null) { |
Node[] ndNodeFils = ndPereOriginal.getChildNodes(); |
|
for (int i = 0; i < ndNodeFils.length; i++) { |
|
String[] usObj = (String[]) ndNodeFils[i].getUserObject(); |
TreeNode child = new TreeNode(usObj[0]); |
child.setUserObject(usObj); |
ndPereCopie.appendChild(child); |
|
if (!ndNodeFils[i].isLeaf()) { |
copierFilsNoeud(ndNodeFils[i], child); |
} |
|
} |
} |
} |
|
/** |
* Méthode héritée de Filtrable Renvoie l'état du filtre (modifié ou non) |
*/ |
public boolean renvoyerEtatFiltre() { |
|
return filtreModifie; |
} |
|
public void valider() { |
|
if (estInstancie) { |
|
} |
} |
|
public Comparator<TreeNode> comparerNoeuds() |
{ |
return new Comparator<TreeNode>() { |
|
public int compare(TreeNode o1, TreeNode o2) { |
|
String n1 = ((String[])o1.getUserObject())[0] ; |
String n2 = ((String[])o2.getUserObject())[0] ; |
|
return n1.compareTo(n2) ; |
} |
|
} ; |
} |
|
public String renvoyerMois(int numMois) { |
|
switch (numMois) { |
case 1: |
return "janvier" ; |
case 2: |
return "fevrier" ; |
case 3: |
return "mars" ; |
case 4: |
return "avril" ; |
case 5: |
return "mai" ; |
case 6: |
return "juin" ; |
case 7: |
return "juillet" ; |
case 8: |
return "août" ; |
case 9: |
return "septembre" ; |
case 10: |
return "octobre" ; |
case 11: |
return "novembre" ; |
case 12: |
return "décembre" ; |
default: |
return "Inconnue" ; |
} |
|
} |
|
|
} |