--- /dev/null
+/*****************************************************************************
+ * Copyright Igor Barma, Eric Brun, Alexandre Desoubeaux, Christian Martel,
+ * (2 décembre 2008)
+ *
+ * Ce logiciel est un programme informatique servant à l'évaluation des
+ * compétences.
+ *
+ * Ce logiciel est régi par la licence CeCILL soumise au droit français et
+ * respectant les principes de diffusion des logiciels libres. Vous pouvez
+ * utiliser, modifier et/ou redistribuer ce programme sous les conditions
+ * de la licence CeCILL telle que diffusée par le CEA, le CNRS et l'INRIA
+ * sur le site "http://www.cecill.info".
+ *
+ * En contrepartie de l'accessibilité au code source et des droits de copie,
+ * de modification et de redistribution accordés par cette licence, il n'est
+ * offert aux utilisateurs qu'une garantie limitée. Pour les mêmes raisons,
+ * seule une responsabilité restreinte pèse sur l'auteur du programme, le
+ * titulaire des droits patrimoniaux et les concédants successifs.
+ *
+ * A cet égard l'attention de l'utilisateur est attirée sur les risques
+ * associés au chargement, à l'utilisation, à la modification et/ou au
+ * développement et à la reproduction du logiciel par l'utilisateur étant
+ * donné sa spécificité de logiciel libre, qui peut le rendre complexe à
+ * manipuler et qui le réserve donc à des développeurs et des professionnels
+ * avertis possédant des connaissances informatiques approfondies. Les
+ * utilisateurs sont donc invités à charger et tester l'adéquation du
+ * logiciel à leurs besoins dans des conditions permettant d'assurer la
+ * sécurité de leurs systèmes et ou de leurs données et, plus généralement,
+ * à l'utiliser et l'exploiter dans les mêmes conditions de sécurité.
+ *
+ * Le fait que vous puissiez accéder à cet en-tête signifie que vous avez
+ * pris connaissance de la licence CeCILL, et que vous en avez accepté les
+ * termes.
+ *******************************************************************************/
+package com.pentila.evalcomp.client.examen;
+
+import java.util.Arrays;
+import java.util.Date;
+import java.util.HashSet;
+import java.util.Set;
+
+import com.google.gwt.http.client.RequestBuilder;
+import com.google.gwt.user.client.rpc.AsyncCallback;
+import com.gwtent.reflection.client.ClassType;
+import com.gwtent.reflection.client.TypeOracle;
+import com.pentila.evalcomp.client.ConnexionServer;
+import com.pentila.evalcomp.client.i18n.Messages;
+import com.pentila.evalcomp.client.services.IServiceWidgetContainerPaging;
+import com.pentila.evalcomp.client.services.widget.GridPager;
+import com.pentila.evalcomp.client.utilities.ListGridDropable;
+import com.pentila.evalcomp.client.utilities.TrashableContainer;
+import com.pentila.evalcomp.client.utilities.UserTools;
+import com.pentila.evalcomp.domain.EObject;
+import com.pentila.evalcomp.domain.examen.Examen;
+import com.pentila.evalcomp.domain.transit.TListEObject;
+import com.smartgwt.client.core.Function;
+import com.smartgwt.client.data.Record;
+import com.smartgwt.client.types.ListGridEditEvent;
+import com.smartgwt.client.util.BooleanCallback;
+import com.smartgwt.client.util.SC;
+import com.smartgwt.client.widgets.events.ShowContextMenuEvent;
+import com.smartgwt.client.widgets.events.ShowContextMenuHandler;
+import com.smartgwt.client.widgets.grid.GroupValueFunction;
+import com.smartgwt.client.widgets.grid.ListGrid;
+import com.smartgwt.client.widgets.grid.ListGridField;
+import com.smartgwt.client.widgets.grid.ListGridRecord;
+import com.smartgwt.client.widgets.grid.events.EditorExitEvent;
+import com.smartgwt.client.widgets.grid.events.EditorExitHandler;
+import com.smartgwt.client.widgets.grid.events.RecordClickEvent;
+import com.smartgwt.client.widgets.grid.events.RecordClickHandler;
+import com.smartgwt.client.widgets.grid.events.RowContextClickEvent;
+import com.smartgwt.client.widgets.grid.events.RowContextClickHandler;
+import com.smartgwt.client.widgets.layout.HLayout;
+import com.smartgwt.client.widgets.layout.VLayout;
+import com.smartgwt.client.widgets.menu.Menu;
+import com.smartgwt.client.widgets.menu.MenuItem;
+import com.smartgwt.client.widgets.menu.events.ClickHandler;
+import com.smartgwt.client.widgets.menu.events.MenuItemClickEvent;
+
+public class ExamenTree extends VLayout implements IServiceWidgetContainerPaging, IAddDeleteWidget, TrashableContainer{
+
+ private GridPager gp;
+
+ private int pageSize = 50;
+
+ ClassType<Examen> classType = TypeOracle.Instance.getClassType(Examen.class);
+
+ ListGridDropable grid;
+
+ private ListFieldData atGlobal = new ListFieldData(
+ Arrays.asList(
+ new FieldData("id", "id", FieldData.HIDE),
+ new FieldData("nom_examen", "nom_examen", FieldData.SHOW)
+
+ ));
+
+ private ExamenLayout examenLayout;
+
+ public ExamenTree(){
+ super();
+ }
+
+ public ExamenTree(ExamenLayout examenLayout){
+ super();
+
+ this.examenLayout = examenLayout;
+ }
+
+
+
+
+
+ public void initializeInterface(){
+
+ grid = new ListGridDropable(this);
+ grid.setWidth100();
+ grid.setHeight100();
+ grid.setEditEvent(ListGridEditEvent.DOUBLECLICK);
+
+ //grid.setAutoFetchData(true);
+ grid.setShowRowNumbers(true);
+
+
+
+ grid.setShowAllRecords(true);
+ grid.setCanEdit(true);
+
+ grid.setShowCellContextMenus(true);
+
+ grid.setCanDragRecordsOut(true);
+ grid.setDragType("EXAMEN");
+
+
+ final Menu menu = new Menu();
+ menu.setShowShadow(true);
+ menu.setShadowDepth(10);
+
+ MenuItem addItem = new MenuItem("Ajouter", "[SKIN]../actions/add.png", "Ctrl+N");
+ MenuItem removeItem = new MenuItem("Supprimer", "[SKIN]../actions/remove.png", "Ctrl+O");
+
+
+ addItem.addClickHandler(new ClickHandler(){
+
+ public void onClick(MenuItemClickEvent event) {
+
+ addFunction();
+
+ }
+
+ });
+
+ removeItem.addClickHandler(new ClickHandler(){
+
+ public void onClick(MenuItemClickEvent event) {
+
+ deleteFunction();
+
+ }
+
+ });
+
+ menu.setItems(addItem, removeItem);
+
+ grid.setContextMenu(menu);
+
+ grid.addRowContextClickHandler(new RowContextClickHandler(){
+
+ public void onRowContextClick(RowContextClickEvent event) {
+ // TODO Auto-generated method stub
+ Boolean b = menu.showContextMenu();
+
+ }
+
+ });
+
+
+ grid.addShowContextMenuHandler(new ShowContextMenuHandler(){
+
+ public void onShowContextMenu(ShowContextMenuEvent event) {
+
+
+
+
+ }
+
+ });
+
+ ListGridField[] llgf = new ListGridField[atGlobal.size()];
+
+
+ //llgf[0] = llll;
+
+ int i = 0;
+ for (final FieldData e : atGlobal) {
+
+ ListGridField cc1 = new ListGridField(e.getFieldName(),
+ Messages.getString(e.getLabel()));
+
+ cc1.setHidden(!e.getShowable());
+
+ if (e.getCellFormatter() != null){
+
+ cc1.setGroupValueFunction(new GroupValueFunction(){
+
+ public Object getGroupValue(Object value,
+ ListGridRecord record, ListGridField field,
+ String fieldName, ListGrid grid) {
+ // TODO Auto-generated method stub
+ return e.getCellFormatter().format(value, record, 0, 0);
+ }
+
+ });
+
+
+ cc1.setCellFormatter(e.getCellFormatter());
+
+ }
+
+
+ llgf[i++] = cc1;
+
+ }
+
+ grid.setFields(llgf);
+
+
+ grid.addEditorExitHandler(new EditorExitHandler(){
+
+ public void onEditorExit(EditorExitEvent event) {
+
+ Record record = event.getRecord();
+
+ Examen examen = (Examen) record.getAttributeAsObject("object");
+
+
+ String fieldName = grid.getFieldName(event.getColNum());
+
+ classType.invoke(examen, getMethodName(fieldName, "set"),
+ event.getNewValue());
+
+ saveExamen(examen, null);
+
+ }
+
+ });
+
+ grid.addRecordClickHandler(new RecordClickHandler(){
+
+ public void onRecordClick(RecordClickEvent event) {
+
+
+ Record record = event.getRecord();
+
+ Examen examen = (Examen)record.getAttributeAsObject("object");
+
+
+
+ examenLayout.showExamen(examen);
+
+ }
+
+ });
+
+ final VLayout compos = new VLayout(0);
+
+ compos.addMember(grid);
+
+ HLayout tools = new HLayout();
+
+ gp = new GridPager(this, 50);
+
+
+
+ tools.addMember(gp);
+
+ compos.addMember(tools);
+
+
+
+ addMember(compos);
+
+ }
+
+ protected void deleteExamen(Set<Long> qIds, final Function callbackFunction) {
+ AsyncCallback callback = new AsyncCallback(){
+
+ public void onFailure(Throwable arg0) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void onSuccess(Object arg0) {
+ // TODO Auto-generated method stub
+ if (callbackFunction != null){
+ callbackFunction.execute();
+ }
+ }
+
+ };
+
+
+ ConnexionServer.examenService.deleteExamens(qIds, callback);
+
+ }
+
+ public void saveExamen(Examen newe, final Function function) {
+
+
+ AsyncCallback callback = new AsyncCallback() {
+
+ public void onFailure(Throwable arg0) {
+
+ }
+
+ public void onSuccess(Object arg0) {
+ if (function != null){
+ function.execute();
+ }
+ }
+
+ };
+
+
+
+
+ ConnexionServer.examenService.saveExamen(newe, callback);
+ }
+
+ public void update(){
+ this.update(0, this.pageSize, null);
+ }
+
+ protected void loadDataSource(TListEObject t) {
+ ListGridRecord[] ll = new ListGridRecord[t.getListOfEObjects().size()];
+ int i = 0;
+
+ for (EObject o : t.getListOfEObjects()) {
+
+ ListGridRecord smartgwtRecord = new ListGridRecord();
+
+ Examen e = (Examen) o;
+
+ for (FieldData field : atGlobal) {
+
+ String fieldName = field.getFieldName();
+
+ Object ee = apply("get", field, e, null);
+
+ smartgwtRecord.setAttribute(fieldName, ee);
+
+ }
+
+ smartgwtRecord.setAttribute("object", e);
+
+
+ ll[i++] = smartgwtRecord;
+
+ }
+
+
+ grid.setData(ll);
+
+ }
+
+
+ private String getMethodName(String fieldName, String type) {
+
+ String f = fieldName.substring(0, 1).toUpperCase();
+ String end = fieldName.substring(1);
+
+ String methodName = type + f + end;
+
+ return methodName;
+
+ }
+
+
+ private Object apply(String type, FieldData field, Object object, Object ...args){
+
+ Object o = null;
+ try{
+ o = classType.invoke(object,
+ getMethodName(field.getFieldName(), type), args);
+ }catch (Exception e){
+ //e.printStackTrace();
+ }
+ return o;
+
+ }
+
+ public void addFunction() {
+ Examen newe = new Examen();
+
+ newe.setNom_examen("new");
+ newe.setAuteur("");
+ newe.setOwner(UserTools.getUser());
+ newe.setTs_datecreation(new Date());
+
+
+ saveExamen(newe, new Function(){
+
+ public void execute() {
+
+
+ gp.goToPage(gp.getTotalPages(), true);
+ }
+
+ });
+
+ }
+
+ public void deleteFunction() {
+ final ListGridRecord[] selectedRecords = grid.getSelectedRecords();
+
+
+ if (selectedRecords != null && selectedRecords.length > 0){
+
+
+ SC.confirm(Messages.getString("confirm_suppression"), new BooleanCallback() {
+ public void execute(Boolean value) {
+ if (value != null && value) {
+ Set<Long> eIds = new HashSet<Long>();
+
+ for (ListGridRecord lgr : selectedRecords){
+
+ Examen examen = (Examen)lgr.getAttributeAsObject("object");
+
+ eIds.add(examen.getId());
+
+ }
+
+ Function callback = new Function(){
+
+ public void execute() {
+
+ update(gp.getStart(), gp.getPageSize(), "");
+
+ }
+
+ };
+
+ deleteExamen(eIds, callback);
+ }
+ }
+ });
+
+
+ }
+ }
+
+ public void update(int start, int pageSize, String searchFilter) {
+ AsyncCallback callback = new AsyncCallback() {
+
+ public void onFailure(Throwable caught) {
+
+ //Tools.hideLoadingRegion(getId());
+ }
+
+ public void onSuccess(Object result) {
+
+ TListEObject T = (TListEObject) result;
+
+ int totalCount = T.getTotal();
+
+ gp.setTotalCount(totalCount);
+ gp.updatePagerControls(gp.getTotalPages());
+ loadDataSource(T);
+
+ }
+ };
+
+
+ RequestBuilder rb = ConnexionServer.examenService.getExamens(start, pageSize, callback);
+ rb.setHeader("code", "A27");
+ ConnexionServer.doSend(rb);
+ }
+
+ public boolean isTrashable(Object... objects) {
+
+ return true;
+ }
+
+ public void trashedElements(Object... objects) {
+ deleteFunction();
+
+ }
+
+ @Override
+ public void saveObject(Object object, Function Function) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void refreshObject(Object object) {
+ // TODO Auto-generated method stub
+
+ }
+}