Subversion Repositories eFlore/Applications.coel

Compare Revisions

Ignore whitespace Rev 984 → Rev 985

/trunk/src/org/tela_botanica/client/vues/commentaire/CommentaireListeVue.java
New file
0,0 → 1,301
package org.tela_botanica.client.vues.commentaire;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
 
import org.tela_botanica.client.Mediateur;
import org.tela_botanica.client.RegistreId;
import org.tela_botanica.client.i18n.Constantes;
import org.tela_botanica.client.images.Images;
import org.tela_botanica.client.interfaces.Rafraichissable;
import org.tela_botanica.client.modeles.Information;
import org.tela_botanica.client.modeles.Utilisateur;
import org.tela_botanica.client.modeles.Valeur;
import org.tela_botanica.client.modeles.ValeurListe;
import org.tela_botanica.client.modeles.aDonnee;
import org.tela_botanica.client.modeles.commentaire.Commentaire;
import org.tela_botanica.client.modeles.commentaire.CommentaireListe;
 
import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.SortDir;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.GroupingStore;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.grid.GridGroupRenderer;
import com.extjs.gxt.ui.client.widget.grid.GridSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.GroupColumnData;
import com.extjs.gxt.ui.client.widget.grid.GroupingView;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.core.client.GWT;
 
public class CommentaireListeVue extends ContentPanel implements Rafraichissable {
 
private Mediateur mediateur = null;
private Constantes i18nC = null;
 
private Grid<Commentaire> grille = null;
private GroupingStore<Commentaire> store = null;
private ColumnModel modeleDesColonnes = null;
private Button ajouter;
private Button modifier;
private Button supprimer;
private CommentaireListe commentaires = null;
protected boolean commentairesChargementOk = false;
protected HashMap<String, Valeur> ontologie = null;
protected boolean ontologieChargementOk = false;
public CommentaireListeVue(Mediateur mediateurCourant) {
mediateur = mediateurCourant;
i18nC = Mediateur.i18nC;
setLayout(new FitLayout());
setHeading(i18nC.menuCommentaire());
// Gestion de l'ontologie
ontologie = new HashMap<String, Valeur>();
mediateur.obtenirListeValeurEtRafraichir(this, "typeCommentaireCollection");
// Gestion de la barre d'outil
ToolBar toolBar = new ToolBar();
ajouter = new Button(i18nC.ajouter());
ajouter.setIcon(Images.ICONES.ajouter());
ajouter.addSelectionListener(new SelectionListener<ButtonEvent>() {
public void componentSelected(ButtonEvent be) {
mediateur.clicAjouterCommentaire();
}
});
toolBar.add(ajouter);
 
modifier = new Button(i18nC.modifier());
modifier.setIcon(Images.ICONES.formModifier());
modifier.addSelectionListener(new SelectionListener<ButtonEvent>() {
public void componentSelected(ButtonEvent be) {
mediateur.clicModifierCommentaire(grille.getSelectionModel().getSelectedItems());
}
});
toolBar.add(modifier);
supprimer = new Button(i18nC.supprimer());
supprimer.setIcon(Images.ICONES.supprimer());
supprimer.addSelectionListener(new SelectionListener<ButtonEvent>() {
public void componentSelected(ButtonEvent be) {
mediateur.clicSupprimerCommentaire(grille.getSelectionModel().getSelectedItems());
}
});
toolBar.add(supprimer);
setTopComponent(toolBar);
 
// Gestion de la grille
List<ColumnConfig> colonnes = new ArrayList<ColumnConfig>();
// ATTENTION : les noms des colonnes doivent correspondre aux noms variables de la classe utilisée dans la liste
colonnes.add(new ColumnConfig("_collection_nom_", i18nC.commentaireCollection(), 150));
colonnes.add(creerColonneType());
colonnes.add(new ColumnConfig("_titre_", i18nC.commentaireTitre(), 150));
colonnes.add(new ColumnConfig("_ponderation_", i18nC.commentairePonderation(), 30));
colonnes.add(creerColonneAcces());
modeleDesColonnes = new ColumnModel(colonnes);
 
GridSelectionModel<Commentaire> modeleDeSelection = new GridSelectionModel<Commentaire>();
modeleDeSelection.addSelectionChangedListener(new SelectionChangedListener<Commentaire>() {
public void selectionChanged(SelectionChangedEvent<Commentaire> event) {
Commentaire commentaire = (Commentaire) event.getSelectedItem();
clicListe(commentaire);
}
});
store = new GroupingStore<Commentaire>();
store.sort("cmhl_date_modification", SortDir.ASC);
store.groupBy("_collection_nom_");
GroupingView vueDeGroupe = new GroupingView();
vueDeGroupe.setShowGroupedColumn(false);
vueDeGroupe.setForceFit(true);
vueDeGroupe.setGroupRenderer(new GridGroupRenderer() {
@Override
public String render(GroupColumnData data) {
String f = modeleDesColonnes.getColumnById(data.field).getHeader();
String l = data.models.size() == 1 ? i18nC.commentaireSingulier() : i18nC.commentairePluriel();
return f + ": " + data.group + " (" + data.models.size() + " " + l + ")";
}
});
grille = new Grid<Commentaire>(store, modeleDesColonnes);
grille.setView(vueDeGroupe);
grille.setWidth("100%");
grille.setAutoExpandColumn("_titre_");
grille.getView().setAutoFill(true);
grille.getView().setForceFit(true);
grille.setSelectionModel(modeleDeSelection);
grille.addListener(Events.ViewReady, new Listener<BaseEvent>() {
@Override
public void handleEvent(BaseEvent be) {
grille.getSelectionModel().select(0, false);
}
});
grille.addListener(Events.OnDoubleClick, new Listener<BaseEvent>(){
@Override
public void handleEvent(BaseEvent be) {
modifier.fireEvent(Events.Select);
}
});
add(grille);
}
private ColumnConfig creerColonneType() {
GridCellRenderer<Commentaire> typeRendu = new GridCellRenderer<Commentaire>() {
@Override
public String render(Commentaire model, String property, ColumnData config, int rowIndex, int colIndex, ListStore<Commentaire> store, Grid<Commentaire> grid) {
// FIXME : créer une classe Ontologie qui mixe le code ci-dessous et tout ce qui concerne l'ontologie dans DetailVue
String type = "";
String[] valeurs = model.getCollectionACommentaire().getType().split(aDonnee.SEPARATEUR_VALEURS);
int nbreValeurs = valeurs.length;
int indexAvtDernier = nbreValeurs - 1;
if (nbreValeurs > 0) {
for (int i = 0; i < nbreValeurs; i++) {
String valeur = valeurs[i];
if (valeur.matches("^[0-9]+$")) {
if (valeur.equals("0")) {
valeur = "";
} else if (ontologie != null) {
Valeur valeurOntologie = ontologie.get(valeur);
if (valeurOntologie != null) {
valeur = valeurOntologie.getNom();
}
}
}
if (i != indexAvtDernier) {
type += valeur+", ";
} else {
type += valeur;
}
}
}
model.set("_type_", type);
return type;
}
};
ColumnConfig typeColonne = new ColumnConfig("_type_", i18nC.commentaireType(), 100);
typeColonne.setRenderer(typeRendu);
return typeColonne;
}
private ColumnConfig creerColonneAcces() {
GridCellRenderer<Commentaire> accesRendu = new GridCellRenderer<Commentaire>() {
@Override
public String render(Commentaire model, String property, ColumnData config, int rowIndex, int colIndex, ListStore<Commentaire> store, Grid<Commentaire> grid) {
String acces = (model.etrePublic() ? i18nC.donneePublic() : i18nC.donneePrivee());
model.set("_public_", acces);
return acces;
}
};
ColumnConfig accesColonne = new ColumnConfig("_public_", i18nC.commentairePublic(), 30);
accesColonne.setRenderer(accesRendu);
return accesColonne;
}
private void clicListe(Commentaire commentaire) {
if (commentaire != null && store.getCount() > 0) {
mediateur.clicListeCommentaire(commentaire);
}
}
 
private void gererEtatActivationBouton() {
int nbreElementDuMagazin = store.getCount();
ajouter.enable();
if (nbreElementDuMagazin <= 0) {
supprimer.disable();
modifier.disable();
} else if (nbreElementDuMagazin > 0) {
modifier.enable();
if (((Utilisateur) Registry.get(RegistreId.UTILISATEUR_COURANT)).isIdentifie()) {
supprimer.enable();
}
}
}
public void rafraichir(Object nouvellesDonnees) {
if (nouvellesDonnees instanceof CommentaireListe) {
commentaires = (CommentaireListe) nouvellesDonnees;
commentairesChargementOk = true;
} else if (nouvellesDonnees instanceof ValeurListe) {
ValeurListe listeValeursReceptionnee = (ValeurListe) nouvellesDonnees;
Iterator<String> it = listeValeursReceptionnee.keySet().iterator();
while (it.hasNext()) {
String cle = it.next();
Valeur valeur = listeValeursReceptionnee.get(cle);
if (valeur != null) {
ontologie.put(cle, valeur);
}
}
ontologieChargementOk = true;
} else if (nouvellesDonnees instanceof Information) {
Information info = (Information) nouvellesDonnees;
if (info.getType().equals("maj_utilisateur")) {
gererEtatActivationBouton();
} else if (info.getType().equals("suppression_commentaire")) {
String message = info.toString();
if (info.getDonnee(0) != null) {
message = (String) info.getDonnee(0);
}
Info.display(i18nC.commentaireTitreSuppression(), message);
supprimerCommentairesSelectionnees();
gererEtatActivationBouton();
}
} else {
GWT.log(Mediateur.i18nM.erreurRafraichir(nouvellesDonnees.getClass(), this.getClass()), null);
}
if (etrePretAAfficherListe()) {
chargerListe();
}
layout();
}
private boolean etrePretAAfficherListe() {
boolean ok = false;
if (commentairesChargementOk && ontologieChargementOk) {
ok = true;
}
return ok;
}
private void chargerListe() {
if (commentaires != null) {
List<Commentaire> liste = commentaires.toList();
store.removeAll();
store.add(liste);
mediateur.actualiserPanneauCentral();
}
}
private void supprimerCommentairesSelectionnees() {
List<Commentaire> commentairesSelectionnees = grille.getSelectionModel().getSelectedItems();
Iterator<Commentaire> it = commentairesSelectionnees.iterator();
while (it.hasNext()) {
grille.getStore().remove(it.next());
}
layout(true);
}
}