--- /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.evaluation;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import com.google.gwt.user.client.rpc.AsyncCallback;
+import com.gwtext.client.core.EventObject;
+import com.gwtext.client.data.ArrayReader;
+import com.gwtext.client.data.DataProxy;
+import com.gwtext.client.data.FieldDef;
+import com.gwtext.client.data.MemoryProxy;
+import com.gwtext.client.data.ObjectFieldDef;
+import com.gwtext.client.data.Reader;
+import com.gwtext.client.data.Record;
+import com.gwtext.client.data.RecordDef;
+import com.gwtext.client.data.SimpleStore;
+import com.gwtext.client.data.Store;
+import com.gwtext.client.data.StringFieldDef;
+import com.gwtext.client.dd.DragData;
+import com.gwtext.client.dd.DragSource;
+import com.gwtext.client.dd.DropTarget;
+import com.gwtext.client.dd.DropTargetConfig;
+import com.gwtext.client.util.Format;
+import com.gwtext.client.widgets.grid.CellMetadata;
+import com.gwtext.client.widgets.grid.ColumnConfig;
+import com.gwtext.client.widgets.grid.ColumnModel;
+import com.gwtext.client.widgets.grid.GridPanel;
+import com.gwtext.client.widgets.grid.Renderer;
+import com.gwtext.client.widgets.grid.event.GridRowListenerAdapter;
+import com.gwtext.client.widgets.menu.BaseItem;
+import com.gwtext.client.widgets.menu.Item;
+import com.gwtext.client.widgets.menu.Menu;
+import com.gwtext.client.widgets.menu.event.BaseItemListenerAdapter;
+import com.gwtext.client.widgets.tree.TreeDragData;
+import com.gwtext.client.widgets.tree.TreeNode;
+import com.pentila.evalcomp.client.ConnexionServer;
+import com.pentila.evalcomp.client.i18n.Messages;
+import com.pentila.evalcomp.client.utilities.GridPanelDropable;
+import com.pentila.evalcomp.client.utilities.Tools;
+import com.pentila.evalcomp.client.utilities.WsItemNode;
+import com.pentila.evalcomp.domain.RessourceDefinition;
+import com.pentila.evalcomp.domain.definition.EvaluationSubject;
+import com.pentila.evalcomp.domain.transit.Activity;
+import com.pentila.evalcomp.domain.transit.RessourceItem;
+
+// TODO: Auto-generated Javadoc
+/**
+ * The Class RoleDefWidget.
+ */
+public class AssociateRessourceExtWidget extends GridPanelDropable {
+
+ /** The activity. */
+ private Activity activity = null;
+
+ /** The cfg column model ressource. */
+ private ColumnModel cfgColumnModelRessourceExt;
+
+ /** The cfg columns ressource. */
+ private ColumnConfig[] cfgColumnsRessourceExt;
+
+ /** The evaluationSubject. */
+ private EvaluationSubject es = null;
+
+ /** The menu ressources. */
+ private Menu menuRessource;
+
+ /** The render ressource. */
+ private Renderer renderRessourceExt = new Renderer() {
+ public String render(Object value, CellMetadata cellMetadata,
+ Record record, int rowIndex, int colNum, Store store) {
+ String icon = "<img src=\"" + Tools.getBaseURL() + "images/{0}\"> {1}"; //$NON-NLS-1$
+ String imgIcon = "";
+ RessourceDefinition rd = (RessourceDefinition) record
+ .getAsObject("rDefObject");
+ if (rd.getType().equals("RessourceFile")) {
+ imgIcon = "leaf16.gif";
+ }
+ if (rd.getType().equals("RessourceMoodleCourse")) {
+ imgIcon = "moodle20.png";
+ }
+ return Format.format(icon,
+ new String[] { imgIcon, record.getAsString("rDefName") });
+ }
+ };
+
+ /** The store ressources. */
+ private Store storeRessource;
+
+ /**
+ * Instantiates a new ressource association widget.
+ *
+ * @param es
+ * the evaluationSubject
+ * @param act
+ * the activity
+ */
+ public AssociateRessourceExtWidget(EvaluationSubject es, Activity act) {
+ super();
+ this.es = es;
+ this.activity = act;
+
+ cfgColumnsRessourceExt = new ColumnConfig[] { new ColumnConfig(
+ Messages.getString("Ressources"), "rDefName", 140, true, renderRessourceExt,
+ "rDefName") };
+ cfgColumnModelRessourceExt = new ColumnModel(cfgColumnsRessourceExt);
+
+ initializeInterface();
+
+ this.setDragDropText(Messages.getString("x_ressourcex_selectionneex"));
+
+ // this.updateRessourcesGrid();
+
+ }
+
+ /**
+ * Delete ressource.
+ */
+ private void deleteRessources() {
+
+ Record[] rows = this.getSelectionModel().getSelections();
+
+ final Set<RessourceDefinition> srd = new HashSet<RessourceDefinition>();
+
+ for (Record row : rows) {
+ this.getStore().remove(row);
+ srd.add((RessourceDefinition) row.getAsObject("rDefObject"));
+ }
+
+ AsyncCallback callback = new AsyncCallback() {
+
+ public void onFailure(Throwable caught) {
+ System.out.println("Probleme dans Suppression des ressources"); //$NON-NLS-1$
+ }
+
+ public void onSuccess(Object result) {
+ // on supprime les rdef du es
+ es.getRessourceDefinitions().removeAll(srd);
+ }
+ };
+ ConnexionServer.definitionService.removeRessourceDefinitions(srd,
+ callback);
+
+ }
+
+ /**
+ * Initialize interface.
+ */
+ private void initializeInterface() {
+
+
+ storeRessource = new SimpleStore(0, new FieldDef[] {
+ new StringFieldDef("rDefID"), new StringFieldDef("rDefName"),
+ new ObjectFieldDef("rDefObject") }, new Object[][] {});
+ storeRessource.load();
+
+ this.setStore(storeRessource);
+ this.setColumnModel(cfgColumnModelRessourceExt);
+ this.setEnableDragDrop(true);
+ this.setDdGroup("evalCompDDGroup");
+ this.setWidth(150);
+ this.setHeight(100);
+ this.setHideColumnHeader(true);
+ this.setEnableHdMenu(false);
+ this.setEnableColumnHide(true);
+ this.setHideBorders(true);
+ }
+
+ /**
+ * Initialize the listener on the tab.
+ */
+ public void initializeListener() {
+
+ this.addGridRowListener(new GridRowListenerAdapter() {
+ @Override
+ public void onRowContextMenu(GridPanel grid, int rowIndex,
+ EventObject e) {
+ e.stopEvent();
+ showDelRessourceContextMenu(e);
+ }
+ });
+
+ DropTargetConfig cfg = new DropTargetConfig();
+ cfg.setdDdGroup("evalCompDDGroup");
+
+ new DropTarget(this, cfg) {
+ @Override
+ public boolean notifyDrop(DragSource source, EventObject e,
+ DragData data) {
+
+ try {
+ TreeDragData myData = (TreeDragData) data;
+ TreeNode node = myData.getTreeNode();
+ WsItemNode wsin = (WsItemNode) node
+ .getAttributeAsObject("object");
+ RessourceItem ri = wsin.getResourceItem();
+ RessourceDefinition myRDef = new RessourceDefinition();
+ myRDef.setActivityId(activity.getId());
+ // myRDef.setEvaluationSubject(es);
+ myRDef.setPluginKey(ri.getPluginKey());
+ myRDef.setType(ri.getType());
+ myRDef.setName(ri.getName());
+ myRDef.setPath(ri.getRessourceKey());
+
+ AsyncCallback callback = new AsyncCallback() {
+
+ public void onFailure(Throwable caught) {
+ System.out
+ .println("Probleme dans ajout des ressources"); //$NON-NLS-1$
+ }
+
+ public void onSuccess(Object result) {
+
+ Set<RessourceDefinition> rDefs = (Set<RessourceDefinition>) result;
+
+ Set<RessourceDefinition> allRDefs = es
+ .getRessourceDefinitions();
+
+ Object[][] objList = new Object[rDefs.size()][];
+ for (int i = 0; i < rDefs.size(); i++) {
+ RessourceDefinition rd = (RessourceDefinition) rDefs
+ .toArray()[i];
+ // on conserve les nouveaux rdef dans le ES
+ allRDefs.add(rd);
+
+ Object[] monObjet = new Object[] {
+ rd.getId().toString(), rd.getName(), rd };
+ objList[i] = monObjet;
+ }
+
+ es.setRessourceDefinitions(allRDefs);
+
+ RecordDef recordDef = new RecordDef(new FieldDef[] {
+ new StringFieldDef("rDefID"), //$NON-NLS-1$
+ new StringFieldDef("rDefName"), //$NON-NLS-1$
+ new ObjectFieldDef("rDefObject") }); //$NON-NLS-1$
+ DataProxy proxy = new MemoryProxy(objList);
+ Reader reader = new ArrayReader(recordDef);
+ Store tmpStore = new Store(proxy, reader);
+ tmpStore.load();
+ for (Record rec : tmpStore.getRecords()) {
+ getStore().addSorted(rec);
+ }
+ }
+ };
+
+ Set<RessourceDefinition> myResSet = new HashSet<RessourceDefinition>();
+ myResSet.add(myRDef);
+ ConnexionServer.definitionService
+ .addRessourceDefinitionsToES(myResSet, es.getId(),
+ callback);
+ return true;
+ } catch (Exception ex) {
+
+ ex.printStackTrace();
+
+ }
+ return false;
+ }
+
+ @Override
+ public String notifyOver(DragSource source, EventObject e,
+ DragData data) {
+ try {
+ TreeDragData myData = (TreeDragData) data;
+ TreeNode node = myData.getTreeNode();
+ WsItemNode wsin = (WsItemNode) node
+ .getAttributeAsObject("object");
+ if (wsin != null) {
+ return "x-dd-drop-ok";
+ }
+ } catch (Exception ex) {
+
+ ex.printStackTrace();
+
+ }
+ return "";
+ }
+ };
+
+ }
+
+ /**
+ * Show del ressource context menu.
+ *
+ * @param e
+ * the e
+ */
+ private void showDelRessourceContextMenu(EventObject e) {
+ if (menuRessource == null) {
+ menuRessource = new Menu();
+ Item deleteItem = new Item(
+ Messages.getString("Supprimer_cette_ressource"), //$NON-NLS-1$
+ new BaseItemListenerAdapter() {
+ @Override
+ public void onClick(BaseItem item, EventObject e) {
+ deleteRessources();
+ }
+ });
+ deleteItem.setIconCls("menu-delete"); //$NON-NLS-1$
+ menuRessource.addItem(deleteItem);
+ }
+ menuRessource.showAt(e.getXY());
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.client.utilities.GridPanelDropable#trashedElements()
+ */
+ @Override
+ public void trashedElements(Object...objects) {
+ deleteRessources();
+ }
+
+ /**
+ * Update role def grid.
+ */
+ public void updateRessourcesGrid() {
+ if (es != null) {
+ int nbRD = 0;
+ Set<RessourceDefinition> allRDefs = es.getRessourceDefinitions();
+
+ for (int i = 0; i < allRDefs.size(); i++) {
+ RessourceDefinition rd = (RessourceDefinition) allRDefs
+ .toArray()[i];
+ if (rd.getActivityId().equals(activity.getId())) {
+ nbRD = nbRD + 1;
+ }
+ }
+ if (nbRD != 0) {
+ Object[][] objList = new Object[nbRD][];
+ int count = 0;
+ for (int i = 0; i < allRDefs.size(); i++) {
+ RessourceDefinition myRD = (RessourceDefinition) allRDefs
+ .toArray()[i];
+
+ if (myRD.getActivityId().equals(activity.getId())) {
+ Object[] monObjet = new Object[] {
+ myRD.getId().toString(), myRD.getName(), myRD, };
+ objList[count] = monObjet;
+ count = count + 1;
+ }
+ }
+
+ RecordDef recordDef = new RecordDef(new FieldDef[] {
+ new StringFieldDef("rDefID"),
+ new StringFieldDef("rDefName"),
+ new ObjectFieldDef("rDefObject") });
+ DataProxy proxy = new MemoryProxy(objList);
+ Reader reader = new ArrayReader(recordDef);
+ Store tmpStore = new Store(proxy, reader);
+ tmpStore.load();
+ tmpStore.sort("rDefName");
+
+ this.reconfigure(tmpStore, cfgColumnModelRessourceExt);
+ }
+ }
+ }
+
+}