--- /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.services.widget;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import com.google.gwt.http.client.RequestBuilder;
+import com.google.gwt.user.client.rpc.AsyncCallback;
+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.ServiceAbstract;
+import com.pentila.evalcomp.client.utilities.Tools;
+import com.pentila.evalcomp.client.utilities.UserTools;
+import com.pentila.evalcomp.client.widget.SmartWindow;
+import com.pentila.evalcomp.domain.definition.Competence;
+import com.pentila.evalcomp.domain.definition.Domain;
+import com.pentila.evalcomp.domain.definition.EvaluationObject;
+import com.pentila.evalcomp.domain.definition.EvaluationSubject;
+import com.pentila.evalcomp.domain.definition.Level;
+import com.pentila.evalcomp.domain.definition.MethodEval;
+import com.pentila.evalcomp.domain.definition.Referentiel;
+import com.pentila.evalcomp.domain.instance.ScenarioInstance;
+import com.pentila.evalcomp.domain.transit.TCompositeDataMap;
+import com.pentila.evalcomp.domain.transit.Todo;
+import com.smartgwt.client.core.Function;
+import com.smartgwt.client.data.RecordList;
+import com.smartgwt.client.types.AutoFitWidthApproach;
+import com.smartgwt.client.types.GroupStartOpen;
+import com.smartgwt.client.types.ListGridEditEvent;
+import com.smartgwt.client.types.Overflow;
+import com.smartgwt.client.types.VerticalAlignment;
+import com.smartgwt.client.widgets.Label;
+import com.smartgwt.client.widgets.events.ClickEvent;
+import com.smartgwt.client.widgets.events.ClickHandler;
+import com.smartgwt.client.widgets.form.fields.SelectItem;
+import com.smartgwt.client.widgets.grid.CellFormatter;
+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.CellClickEvent;
+import com.smartgwt.client.widgets.grid.events.CellClickHandler;
+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.MenuItemClickEvent;
+import com.smartgwt.client.widgets.toolbar.ToolStrip;
+import com.smartgwt.client.widgets.toolbar.ToolStripButton;
+
+
+/**
+ * The Class SmartEvalGrid.
+ */
+public class SmartEvalGrid extends VLayout implements IServiceWidgetContainerPaging{
+
+ ListGrid grid;
+
+ /** The read only. */
+ boolean readOnly = false;
+
+ /** The disable fields. */
+ private Set<String> disableFields;
+
+ /** The group field. */
+ private String groupField;
+
+ private GridPager gp;
+
+ Map<String, EvaluationObject> idEO = new HashMap<String, EvaluationObject>();
+
+ private Todo todo;
+
+ private LinkedHashMap<String, Level> mapLevel;
+
+ private Set<String> enableFields;
+
+ private boolean unique;
+
+ protected boolean groupGridOpen = true;
+
+ private ToolStripButton expandButton;
+
+ private boolean pendingCell = false;
+
+ private String login = null;
+
+ public SmartEvalGrid(){
+ super();
+ }
+
+ /**
+ * Instantiates a new eval grid.
+ *
+ * @param cbStore the cb store
+ * @param readOnly the read only
+ * @param disableFields the disable fields
+ * @param groupField the group field
+ */
+ public SmartEvalGrid(Todo todo, boolean readOnly, Set<String> enableFields, Set<String> disableFields, String groupField, boolean unique, String uid) {
+ super();
+
+
+ this.todo = todo;
+ this.readOnly = readOnly;
+ this.enableFields = enableFields;
+ this.disableFields = disableFields;
+ this.groupField = groupField;
+ this.unique = unique;
+ this.login = uid;
+
+
+ initializeInterface();
+
+ }
+
+
+
+ /**
+ * Gets the competence instances.
+ *
+ * @return the competence instances
+ */
+ public TCompositeDataMap getCompetenceInstances() {
+
+ TCompositeDataMap T = new TCompositeDataMap();
+
+ String[][] map = new String[grid.getRecords().length][];
+ int i = 0;
+ for (ListGridRecord lgr : grid.getRecords()){
+
+ String ciId = lgr.getAttributeAsString("ciId");
+ String levelId = lgr.getAttributeAsString("levelId");
+ String evalId = UserTools.getUser().getId().toString();
+
+ map[i++] = new String[]{ciId, levelId, evalId};
+ }
+
+
+ T.setIndexes(Arrays.asList("ciId", "levelId", "evalId"));
+
+ T.setDataMap(map);
+
+ return T;
+
+ }
+
+ /**
+ * Gets the all competence instances.
+ *
+ * @return the all competence instances
+ */
+ public TCompositeDataMap getAllCompetenceInstances() {
+
+
+
+ return getCompetenceInstances();
+ }
+
+
+
+ private void buildMap(EvaluationObject eo){
+
+ idEO.put(eo.getId().toString(), eo);
+
+
+
+ if (eo instanceof Domain){
+ for (Competence c : ((Domain)eo).getCompetences()){
+
+ idEO.put(c.getId().toString(), c);
+ }
+ if (((Domain) eo).getDomains() != null){
+ for (Domain sub : ((Domain) eo).getDomains()){
+ buildMap(sub);
+ }
+ }
+
+ }else if (eo instanceof Referentiel){
+ for (Domain eo1: eo.getDomains()){
+ buildMap(eo1);
+ }
+ }
+
+
+
+
+ }
+
+
+ public void updateGrid(final int start, final int limit){
+ AsyncCallback callback = new AsyncCallback() {
+
+ public void onFailure(Throwable arg0) {
+
+ Tools.hideLoadingRegion(getID());
+ }
+
+ public void onSuccess(Object result) {
+
+ Todo t = (Todo) result;
+
+ update(t, start, limit);
+
+
+ Tools.hideLoadingRegion(getID());
+ }
+
+ };
+
+ todo.setDataMap(null);
+
+ Tools.showLoadingRegion(getID(), Messages.getString("MAJ"), Messages
+ .getString("Chargement"));
+
+
+
+ RequestBuilder rb = ConnexionServer.instanceService.updateTodo(todo,start, limit, gp.getSearchFilter(), this.login,
+ callback);
+ rb.setHeader("code", "A9");
+ ConnexionServer.doSend(rb);
+ }
+
+ public void update(Todo t, int start, int limit) {
+
+ int total = t.getTotalRelatedUsers();
+
+ TCompositeDataMap T = t.getDataMap();
+
+ gp.setTotalCount(total);
+
+ gp.updatePagerControls(gp.getTotalPages());
+
+
+ loadDataSource(T);
+
+ }
+
+ public void initializeInterface(){
+
+
+
+ // Init EO
+ EvaluationSubject es = ((ScenarioInstance)todo.getScenarioInstanceAbstract()).getEvaluationSubjectInstance().getEvaluationSubject();
+ for (EvaluationObject eo : es.getEvaluationObjects()){
+ buildMap(eo);
+ }
+
+ mapLevel = new LinkedHashMap<String, Level>();
+
+ MethodEval me = ((ScenarioInstance)todo.getScenarioInstanceAbstract()).getEvaluationInstance()
+ .getEvaluationDefinition().getMethodEval();
+
+ List<Level> levelsList =new ArrayList<Level>( me.getLevels());
+
+ Collections.sort(levelsList, new Comparator<Level>(){
+
+ public int compare(Level l1, Level l2) {
+ return l1.getValue()-l2.getValue();
+ }
+
+ });
+
+ for (Level l : levelsList){
+
+ mapLevel.put(l.getId().toString(), l);
+ }
+
+ this.setWidth100();
+ this.setHeight100();
+ grid = new ListGrid();
+ grid.setWidth100();
+ grid.setHeight100();
+
+ grid.setShowAllRecords(false);
+
+ grid.setWrapCells(true);
+ grid.setFixedRecordHeights(false);
+ grid.setCanGroupBy(true);
+ grid.setCanFreezeFields(true);
+ grid.setGroupStartOpen(GroupStartOpen.ALL);
+ grid.setGroupByMaxRecords(10000);
+
+ grid.setCanReorderFields(true);
+ grid.setCanCollapseGroup(true);
+
+ grid.setAutoSaveEdits(false);
+
+ ListGridField cc1 = new ListGridField("candName", Messages.getString("candName"));
+ cc1.setCanGroupBy(true);
+ cc1.setWidth(200);
+ ListGridField cc2 = new ListGridField("compName", Messages.getString("Competence"));
+ cc2.setCanGroupBy(true);
+ cc2.setWidth(100);
+ ListGridField cc3 = new ListGridField("levelId", Messages.getString("Note"));
+ cc3.setAutoFitWidth(true);
+ cc3.setAutoFitWidthApproach(AutoFitWidthApproach.BOTH);
+ ListGridField cc4 = new ListGridField("evalName", Messages.getString("evaluateur"));
+ cc4.setWidth(200);
+ ListGridField cc5 = new ListGridField("ciGroupname", Messages.getString("Groupe"));
+ cc5.setWidth(150);
+ ListGridField cc6 = new ListGridField("ciMotiv", Messages.getString("motivation"));
+ cc6.setWidth(150);
+
+ List<ListGridField> l = new ArrayList<ListGridField>();
+
+ if (this.enableFields != null && this.enableFields.contains("candName") || (this.disableFields != null && !this.disableFields.contains("candName")) || (this.enableFields == null && this.disableFields == null) ){
+ cc1.setCanEdit(false);
+ l.add(cc1);
+ }
+
+ if (this.enableFields != null && this.enableFields.contains("compName") || (this.disableFields != null && !this.disableFields.contains("compName")) || (this.enableFields == null && this.disableFields == null) ){
+ cc2.setCanEdit(false);
+ l.add(cc2);
+ }
+ if (this.enableFields != null && this.enableFields.contains("levelId") || (this.disableFields != null && !this.disableFields.contains("levelId")) || (this.enableFields == null && this.disableFields == null) ){
+
+ cc3.setCellFormatter(new CellFormatter(){
+
+ public String format(Object value, ListGridRecord record,
+ int rowNum, int colNum) {
+
+ if (value != null){
+
+ String levelId = (String)value;
+ Level l = mapLevel.get(levelId);
+ String ret = l.getName();
+
+ if (l.isThreshold() || l.isGreaterThanThreshold()){
+ ret = "<span style='color:#008600;'>" + ret + "</span>";
+ }
+
+
+ return ret;
+
+ }
+ return "";
+ }
+
+ });
+
+ l.add(cc3);
+ }
+ if (this.enableFields != null && this.enableFields.contains("evalName") || (this.disableFields != null && !this.disableFields.contains("evalName")) || (this.enableFields == null && this.disableFields == null) ){
+ cc4.setCanEdit(false);
+ l.add(cc4);
+ }
+ if (this.enableFields != null && this.enableFields.contains("ciGroupname") || (this.disableFields != null && !this.disableFields.contains("ciGroupname")) || (this.enableFields == null && this.disableFields == null) ){
+ cc5.setCanEdit(false);
+ l.add(cc5);
+ }
+ if (this.enableFields != null && this.enableFields.contains("ciMotiv") || (this.disableFields != null && !this.disableFields.contains("ciMotiv")) || (this.enableFields == null && this.disableFields == null) ){
+ l.add(cc6);
+ cc6.setCanEdit(false);
+ }
+
+ if (!readOnly){
+ LinkedHashMap<String, String> lhm = new LinkedHashMap<String, String>();
+
+ for( Level level : mapLevel.values()){
+
+ String name = level.getName();
+ if (level.isThreshold() || level.isGreaterThanThreshold() ){
+ name = "<span style='color:#008600;'>" + name + "</span>";
+ }
+
+ lhm.put(level.getId().toString(), name);
+ }
+
+ SelectItem si = new SelectItem();
+
+ si.setValueMap(lhm);
+
+
+
+ cc3.setEditorType(si);
+ cc3.setCanEdit(true);
+
+
+ }
+
+ if (l.size() == 1){
+ l.get(0).setWidth("100%");
+ }
+
+
+
+ ListGridField[] llgf = l.toArray(new ListGridField[l.size()]);
+
+ if (groupField != null){
+ grid.setGroupByField(groupField);
+ }
+
+
+ grid.setFields(llgf);
+
+
+
+ if (!readOnly){
+ grid.setCanEdit(true);
+ grid.setEditEvent(ListGridEditEvent.CLICK);
+ grid.setEditByCell(true);
+
+ grid.setShowCellContextMenus(true);
+ final Menu menu = new Menu();
+ menu.setShowShadow(true);
+ menu.setShadowDepth(10);
+ LinkedHashMap<String, String> lhm = new LinkedHashMap<String, String>();
+
+
+
+
+ for( Level level : mapLevel.values()){
+ lhm.put(level.getId().toString(), level.getName());
+ }
+
+ menu.setTitle(Messages.getString("Pour_toute_la_selection"));
+
+ for (Level level : mapLevel.values()){
+ String name = level.getName();
+ if (level.isThreshold() || level.isGreaterThanThreshold()){
+ name = "<span style='color:#008600;'>" + name + "</span>";
+ }
+
+ MenuItem m = new MenuItem(name);
+ m.setAttribute("levelId", level.getId().toString());
+
+ m.addClickHandler(new com.smartgwt.client.widgets.menu.events.ClickHandler(){
+
+ public void onClick(MenuItemClickEvent event) {
+
+ ListGridRecord[] array = grid.getSelectedRecords();
+
+
+ for (ListGridRecord lgr : array){
+
+ lgr.setAttribute("levelId", event.getItem().getAttribute("levelId"));
+
+
+ }
+
+ grid.refreshFields();
+
+ pendingCell = true;
+
+ }
+
+ });
+ menu.addItem(m);
+ }
+
+
+ grid.setContextMenu(menu);
+
+ grid.addRowContextClickHandler(new RowContextClickHandler(){
+
+ public void onRowContextClick(RowContextClickEvent event) {
+
+ menu.showContextMenu();
+
+ }
+
+ });
+
+ }else{
+ grid.setCanEdit(false);
+ }
+
+ grid.addCellClickHandler(new CellClickHandler(){
+
+ public void onCellClick(CellClickEvent event) {
+ String fieldName = grid.getFieldName(event.getColNum());
+
+ if (fieldName.equals("ciMotiv")){
+
+ ListGridRecord record = event.getRecord();
+
+ Label label = new Label(record.getAttributeAsString("ciMotiv"));
+ label.setWidth100();
+ label.setHeight100();
+ label.setPadding(5);
+ label.setValign(VerticalAlignment.TOP);
+
+ String competenceName = idEO.get(record.getAttributeAsString("cId")).getName();
+ String levelName = mapLevel.get(record.getAttributeAsString("levelId")).getName();
+ String title = Messages.getString("motivation_pour") + " " + competenceName + " | " +levelName;
+
+
+ SmartWindow window = new SmartWindow();
+ window.setAutoSize(true);
+ window.setTitle(title);
+ window.setWidth(500);
+ window.setHeight(300);
+ window.setAutoCenter(true);
+ window.setCanDragReposition(true);
+ window.setCanDragResize(true);
+ window.addItem(label);
+ window.show();
+ }
+
+ }
+
+ });
+ grid.setCanMultiSort(true);
+ grid.setCanSort(true);
+
+ final VLayout compos = new VLayout(0);
+ compos.setWidth100();
+ compos.setHeight100();
+ compos.addMember(grid);
+
+ HLayout hlayout = new HLayout();
+
+
+ expandButton = new ToolStripButton("", "[SKIN]/ListGrid/group_opened.png");
+
+ expandButton.addClickHandler(new ClickHandler() {
+ public void onClick(ClickEvent event) {
+
+ if (grid.getGroupTree() != null){
+
+ if (groupGridOpen){
+ groupGridOpen = false;
+ grid.getGroupTree().closeAll();
+
+ expandButton.setIcon("[SKIN]/ListGrid/group_closed.png");
+
+ }else{
+ groupGridOpen = true;
+ grid.getGroupTree().openAll();
+ expandButton.setIcon("[SKIN]/ListGrid/group_opened.png");
+ }
+ }
+ }
+ });
+
+
+ gp = new GridPager(this, ServiceAbstract.pageSize);
+ gp.setWidth100();
+
+ ToolStrip ts = new ToolStrip();
+ ts.setHeight(20);
+ ts.setOverflow(Overflow.VISIBLE);
+ ts.addButton(expandButton);
+ ts.setWidth(20);
+ hlayout.addMember(ts);
+
+ hlayout.addMember(gp);
+
+
+ compos.addMember(hlayout);
+
+ addMember(compos);
+
+ }
+
+ protected void loadDataSource(TCompositeDataMap t) {
+
+
+ if (!unique || enableFields == null){
+ ListGridRecord[] ll = new ListGridRecord[t.getDataMap().length];
+ int i = 0;
+
+
+
+ int indexComp = t.getIndexes().indexOf("cId");
+
+
+ for (String[] line : t.getDataMap()) {
+
+ ListGridRecord smartgwtRecord = new ListGridRecord();
+ int j = 0;
+ for (String index : t.getIndexes()){
+
+ smartgwtRecord.setAttribute(index, line[j++]);
+
+ }
+ smartgwtRecord.setAttribute("compName", idEO.get(line[indexComp]).getName());
+ ll[i++] = smartgwtRecord;
+
+ }
+
+ grid.setData(ll);
+
+ }else{
+ RecordList rl = new RecordList();
+
+ List<Integer> l = new ArrayList<Integer>();
+
+ for (String en : enableFields){
+ l.add(t.getIndexes().indexOf(en));
+ }
+
+ Set<String> uniqueList = new HashSet<String>();
+
+
+ for (String[] line : t.getDataMap()) {
+
+
+ ListGridRecord smartgwtRecord = new ListGridRecord();
+ int k = 0;
+
+ String key = "";
+ for (String en : enableFields){
+
+ String value = line[l.get(k++)];
+ key = key + value;
+
+ smartgwtRecord.setAttribute(en, value);
+ }
+
+
+ if (!uniqueList.contains(key)){
+ rl.add(smartgwtRecord);
+ uniqueList.add(key);
+ }
+
+
+
+ }
+
+ grid.setData(rl);
+
+ }
+ if (grid.getGroupTree() != null){
+ groupGridOpen = true;
+ grid.getGroupTree().openAll();
+ expandButton.setIcon("[SKIN]/ListGrid/group_opened.png");
+ }
+
+ }
+
+
+
+ public void update() {
+
+ updateGrid(0, ServiceAbstract.pageSize);
+ }
+
+ public void update(int start, int pageSize, String searchFilter) {
+ updateGrid(start, pageSize);
+
+ }
+
+ public boolean hasRecords(){
+ return grid.getRecords().length > 0;
+ }
+
+ public String getUnSettedLevelName() {
+ String idLevelInit = "";
+ for (Level l : mapLevel.values()){
+ if (l.getInitial()){
+ idLevelInit = l.getId().toString();
+ break;
+ }
+ }
+
+ for (ListGridRecord lgr : grid.getRecords()){
+ String idLevel = lgr.getAttributeAsString("levelId");
+ if (idLevel.equals(idLevelInit)){
+ return mapLevel.get(idLevel).getName();
+ }
+ }
+ return null;
+ }
+
+ public boolean hasPendingRecords(){
+
+ return grid.getAllEditRows().length != 0 || pendingCell;
+ }
+
+ public void saveValues(Function callback){
+
+ if (pendingCell && grid.getAllEditRows().length == 0 ){
+ pendingCell = false;
+ callback.execute();
+ }else{
+ pendingCell = false;
+
+
+
+ while (grid.hasChanges()){
+ grid.saveAllEdits();
+ }
+
+ callback.execute();
+
+
+
+
+ }
+ }
+
+}