Rev 851 | Go to most recent revision | Blame | Last modification | View Log | RSS feed
package org.tela_botanica.client.vues;
import java.util.ArrayList;
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.Projet;
import org.tela_botanica.client.modeles.ProjetListe;
import org.tela_botanica.client.modeles.Utilisateur;
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.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
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.GridSelectionModel;
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;
import com.google.gwt.user.client.Window;
public class ProjetListeVue extends ContentPanel implements Rafraichissable {
private Mediateur mediateur = null;
private Constantes i18nC = null;
private Grid<Projet> grille = null;
private ListStore<Projet> store = null;
private ColumnModel modeleDesColonnes = null;
private Button ajouter;
private Button modifier;
private Button supprimer;
private boolean enSuppression = false;
public ProjetListeVue(Mediateur mediateurCourant) {
super();
mediateur = mediateurCourant;
i18nC = Mediateur.i18nC;
setLayout(new FitLayout());
setHeading("Projets");
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.clicAjouterProjet();
}
});
toolBar.add(ajouter);
modifier = new Button(i18nC.modifier());
modifier.setIcon(Images.ICONES.formModifier());
toolBar.add(modifier);
modifier.addSelectionListener(new SelectionListener<ButtonEvent>() {
public void componentSelected(ButtonEvent be) {
int tailleSel = grille.getSelectionModel().getSelectedItems().size();
if(tailleSel > 0) {
if(grille.getSelectionModel().getSelectedItems().size() > 1) {
Window.alert("Veuillez ne sélectionner qu'un seul projet à modifier");
} else {
mediateur.clicModifierProjet(grille.getSelectionModel().getSelectedItems());
}
}
}
});
supprimer = new Button(i18nC.supprimer());
supprimer.setIcon(Images.ICONES.supprimer());
toolBar.add(supprimer);
supprimer.addSelectionListener(new SelectionListener<ButtonEvent>() {
public void componentSelected(ButtonEvent be) {
mediateur.clicSupprimerProjet(grille.getSelectionModel().getSelectedItems());
}
});
setTopComponent(toolBar);
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("id_projet", i18nC.id(), 20));
colonnes.add(new ColumnConfig("nom", i18nC.nom(), 200));
colonnes.add(new ColumnConfig("abreviation", i18nC.projetAbreviation(), 200));
colonnes.add(new ColumnConfig("resume", i18nC.projetResume(), 300));
colonnes.add(new ColumnConfig("url", i18nC.projetUrl(), 200));
colonnes.add(new ColumnConfig("mots_cles", i18nC.projetMotsCles(), 280));
modeleDesColonnes = new ColumnModel(colonnes);
GridSelectionModel<Projet> modeleDeSelection = new GridSelectionModel<Projet>();
modeleDeSelection.addSelectionChangedListener(new SelectionChangedListener<Projet>() {
public void selectionChanged(SelectionChangedEvent<Projet> event) {
Projet projet = (Projet) event.getSelectedItem();
clicListe(projet);
}
});
store = new ListStore<Projet>();
store.sort("titre", SortDir.ASC);
grille = new Grid<Projet>(store, modeleDesColonnes);
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);
}
public ListStore<Projet> getStore() {
return store;
}
private void clicListe(Projet projet) {
mediateur.clicListeProjet(projet);
}
private void gererEtatActivationBouton() {
int nbreElementDuMagazin = store.getCount();
ajouter.enable();
if(!((Utilisateur)Registry.get(RegistreId.UTILISATEUR_COURANT)).isIdentifie()) {
supprimer.disable();
modifier.disable();
} else {
if (nbreElementDuMagazin <= 0) {
supprimer.disable();
modifier.disable();
} else {
supprimer.enable();
modifier.enable();
}
}
}
public void rafraichir(Object nouvellesDonnees) {
if (nouvellesDonnees instanceof ProjetListe) {
ProjetListe projets = (ProjetListe) nouvellesDonnees;
if (projets != null) {
List<Projet> liste = projets.toList();
store = new ListStore<Projet>();
store.removeAll();
store.add(liste);
grille.reconfigure(store, modeleDesColonnes);
grille.getSelectionModel().select(0, false);
repaint();
}
//mediateur.actualiserPanneauCentral();
gererEtatActivationBouton();
layout(true);
} else if (nouvellesDonnees instanceof Information) {
Information info = (Information) nouvellesDonnees;
if (info.getType().equals("maj_utilisateur")) {
gererEtatActivationBouton();
}
if (info.getType().equals("suppression_projet")) {
supprimerProjetsSelectionnees();
enSuppression = true;
}
if (info.getType().equals("suppression_auteur") && enSuppression) {
supprimerProjetsSelectionnees();
enSuppression = false;
}
} else {
GWT.log(mediateur.i18nM.erreurRafraichir(nouvellesDonnees.getClass(), this.getClass()), null);
}
}
public void supprimerProjetsSelectionnees() {
List<Projet> selPub = grille.getSelectionModel().getSelectedItems();
GWT.log("Le résultat dans supprimer est : "+grille.getSelectionModel().getSelection().size()+" ", null);
for(Iterator<Projet> it = selPub.iterator(); it.hasNext();) {
GWT.log("Le résultat dans rafraichir est : "+grille.getSelectionModel().getSelection().size()+" ", null);
grille.getStore().remove(it.next());
}
grille.reconfigure(store, modeleDesColonnes);
layout(true);
}
}