--- /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.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import com.google.gwt.user.client.rpc.AsyncCallback;
+import com.gwtext.client.core.EventObject;
+import com.gwtext.client.data.Node;
+import com.gwtext.client.dd.DragData;
+import com.gwtext.client.dd.DragDrop;
+import com.gwtext.client.widgets.MessageBox;
+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.menu.event.MenuListenerAdapter;
+import com.gwtext.client.widgets.tree.DropNodeCallback;
+import com.gwtext.client.widgets.tree.TreeDragData;
+import com.gwtext.client.widgets.tree.TreeNode;
+import com.gwtext.client.widgets.tree.TreePanel;
+import com.gwtext.client.widgets.tree.TreeSorter;
+import com.gwtext.client.widgets.tree.TreeSorterConfig;
+import com.gwtext.client.widgets.tree.event.TreeNodeListenerAdapter;
+import com.gwtext.client.widgets.tree.event.TreePanelListenerAdapter;
+import com.pentila.evalcomp.client.ConnexionServer;
+import com.pentila.evalcomp.client.i18n.Messages;
+import com.pentila.evalcomp.client.plan.widget.TrashableTreePanel;
+import com.pentila.evalcomp.client.utilities.Tools;
+import com.pentila.evalcomp.client.utilities.WsItemNode;
+import com.pentila.evalcomp.client.widget.documenteditor.Page;
+import com.pentila.evalcomp.domain.RessourceDefinition;
+import com.pentila.evalcomp.domain.definition.EvaluationDefinition;
+import com.pentila.evalcomp.domain.definition.EvaluationSubject;
+import com.pentila.evalcomp.domain.definition.ScenarioDefinition;
+import com.pentila.evalcomp.domain.transit.Activity;
+import com.pentila.evalcomp.domain.transit.RessourceItem;
+
+public class AssociateRessourceExtTreeWidget extends TrashableTreePanel
+{
+ private EvaluationDefinition currentEvalShown;
+
+ TreeNode ctxNode;
+ TreeNode ctxNodeAdd;
+ private Menu menuNode;
+
+
+ private Menu menuNodeAdd;
+
+
+ Set<Long> esIdsFilter = null;
+ Set<String> activityIdsFilter = null;
+
+ Boolean hideEmpty = null;
+
+ public AssociateRessourceExtTreeWidget(EvaluationDefinition eval, Set<Long> esIdsFilter, Set<String> activityIdsFilter, Boolean hideEmpty){
+ this.currentEvalShown = eval;
+ this.esIdsFilter = esIdsFilter;
+ this.activityIdsFilter = activityIdsFilter;
+ this.hideEmpty = hideEmpty;
+ initializeInterface();
+ }
+
+ public void initializeInterface(){
+
+ rootNode = new TreeNode();
+ rootNode.setAttribute("nodeType", "root");
+ rootNode.setAttribute("treePanelRef", this);
+
+ this.expandAll();
+ this.setUseArrows(false);
+ this.setRootVisible(false);
+ this.setRootNode(rootNode);
+ this.setEnableDD(true);
+ this.setDdGroup("evalCompDDGroup");
+ this.setCls("associateRessourceExtTreeWidget");
+ this.setBorder(true);
+ this.setAutoScroll(true);
+
+
+ this.addListener(new TreePanelListenerAdapter(){
+
+
+ public boolean doBeforeNodeDrop(TreePanel treePanel,
+ final TreeNode target, DragData dragData, String point,
+ DragDrop source, TreeNode dropNode,
+ DropNodeCallback dropDropNodeCallback) {
+
+ if (target.getAttribute("nodeType").equals("activityNode"))
+ {
+ final EvaluationSubject es = (EvaluationSubject)target.getParentNode().getAttributeAsObject("es");
+
+ final Activity activity = (Activity)target.getAttributeAsObject("activity");
+
+ TreeDragData myData = (TreeDragData) dragData;
+ myData.getTreeNode();
+ WsItemNode wsin = (WsItemNode) dropNode.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) {
+ // do some UI stuff to show failure
+ System.out
+ .println("Probleme dans l'ajout des ressources");
+ Tools.hideLoadingRegion(getId());
+ }
+
+ public void onSuccess(Object result) {
+
+ Set<RessourceDefinition> rDefs = (HashSet<RessourceDefinition>) result;
+
+ Set<RessourceDefinition> allRDefs = es.getRessourceDefinitions();
+
+ for (int i = 0; i < rDefs.size(); i++) {
+ final RessourceDefinition rd = (RessourceDefinition) rDefs
+ .toArray()[i];
+ // on conserve les nouveaux rdef dans le ES
+
+ final TreeNode newRessource = new TreeNode(rd.getName());
+ newRessource.setAttribute("nodeType", "rDefNode");
+ newRessource.setAttribute("ressource", rd);
+ newRessource.addListener(new TreeNodeListenerAdapter() {
+
+ public void onContextMenu(Node node, EventObject e) {
+
+ e.stopEvent();
+ showMenu(newRessource, e);
+
+ }
+
+ @Override
+ public void onDblClick(Node node, EventObject e) {
+ // Tools.redirect(baseURL + resourceFile.getPath());
+ String url = "";
+
+ String path = Tools.encodePath(rd.getPath());
+
+ url = Tools.getRessourceAccessURL() + "?pluginKey="
+ + rd.getPluginKey() + "&ressourceKey="
+ + path+ "&mode=";
+
+ Tools.displayRessourceUrl(url, path, rd.getName(), getIconCls());
+ }
+
+ });
+
+ target.appendChild(newRessource);
+ target.expand();
+
+ allRDefs.add(rd);
+
+ }
+
+ es.setRessourceDefinitions(allRDefs);
+ Tools.hideLoadingRegion(getId());
+ }
+
+ };
+ Tools
+ .showLoadingRegion(getId(),
+ Messages.getString("MAJ"), Messages.getString("Chargement"));
+
+ Set<RessourceDefinition> myResSet = new HashSet<RessourceDefinition>();
+ myResSet.add(myRDef);
+ ConnexionServer.definitionService
+ .addRessourceDefinitionsToES(myResSet, es.getId(),
+ callback);
+
+ }
+
+ removeClass("targetDragRed");
+ return false;
+ }
+
+ public boolean onNodeDragOver(TreePanel treePanel, TreeNode target,
+ DragData dragData, java.lang.String point, DragDrop source,
+ TreeNode dropNode) {
+
+ if (!point.equals("append")) {
+ // empeche l insertion entre deux noeuds
+ return false;
+ }
+
+ // On autorise le drop uniquement sur les activityNode
+ try {
+
+ if (target.getAttribute("nodeType").equals("activityNode")){
+ return true;
+ }
+ } catch (Exception e) {
+ }
+ return false;
+ }
+ });
+ }
+
+ public void updateInterface(){
+ ScenarioDefinition sd = (ScenarioDefinition) currentEvalShown
+ .getScenarioDefinitions().toArray()[0];
+
+ List<EvaluationSubject> listES = new ArrayList<EvaluationSubject>(
+ currentEvalShown.getEvaluationSubjects());
+
+ List<Activity> listActivity = new ArrayList<Activity>(
+ sd.getActivities());
+
+ Collections.sort(listES, new Comparator<EvaluationSubject>() {
+ public int compare(EvaluationSubject es1, EvaluationSubject es2) {
+ return es1.getComputedName().compareTo(es2.getComputedName());
+ }
+ });
+
+ Collections.sort(listActivity, new Comparator<Activity>() {
+ public int compare(Activity act1, Activity act2) {
+ return act1.getId().compareTo(act2.getId());
+ }
+ });
+
+ for(EvaluationSubject es : listES){
+
+ if (this.esIdsFilter != null && !this.esIdsFilter.contains(es.getId())){
+ continue;
+ }
+
+
+ TreeNode groupeCompetences = new TreeNode(es.getComputedName());
+ groupeCompetences.setAttribute("nodeType", "esNode");
+ groupeCompetences.setAttribute("es", es);
+
+ groupeCompetences.setIconCls("iconEvalSubject16");
+
+ for(Activity a : listActivity){
+
+ if (this.activityIdsFilter != null && !this.activityIdsFilter.contains(a.getId())){
+ continue;
+ }else if (this.hideEmpty != null && this.hideEmpty && this.activityIdsFilter != null && this.activityIdsFilter.contains(a.getId())){
+ boolean hasRDOnActivity = false;
+
+ for (RessourceDefinition rd : es.getRessourceDefinitions()){
+
+ if (this.activityIdsFilter.contains(rd.getActivityId())){
+ hasRDOnActivity = true;
+ break;
+ }
+ }
+ if (hasRDOnActivity){
+ continue;
+ }
+ }
+
+
+ final TreeNode activity = new TreeNode(Messages.getString(a.getId()));
+ activity.setAttribute("nodeType", "activityNode");
+ activity.setAttribute("activity", a);
+
+ activity.addListener(new TreeNodeListenerAdapter() {
+
+ public void onContextMenu(Node node, EventObject e) {
+
+ e.stopEvent();
+ showMenuAdd(activity, e);
+
+ }
+ });
+
+ Set<RessourceDefinition> allRDef = es.getRessourceDefinitions();
+
+
+ for(int i = 0; i < allRDef.size(); i ++){
+ final RessourceDefinition rDef = (RessourceDefinition)allRDef.toArray()[i];
+
+ if(rDef.getActivityId().equals(a.getId()))
+ {
+ final TreeNode ressource = new TreeNode(rDef.getName());
+ ressource.setAttribute("nodeType", "rDefNode");
+ ressource.setAttribute("ressource", rDef);
+
+
+ ressource.addListener(new TreeNodeListenerAdapter() {
+
+ public void onContextMenu(Node node, EventObject e) {
+
+ e.stopEvent();
+ showMenu(ressource, e);
+
+ }
+
+ @Override
+ public void onDblClick(Node node, EventObject e) {
+ // Tools.redirect(baseURL + resourceFile.getPath());
+ String url = "";
+
+ String path = Tools.encodePath(rDef.getPath());
+
+ url = Tools.getRessourceAccessURL() + "?pluginKey="
+ + rDef.getPluginKey() + "&ressourceKey="
+ + path + "&mode=";
+
+ Tools.displayRessourceUrl(url, path, rDef.getName(), getIconCls());
+ }
+
+ });
+
+
+ activity.appendChild(ressource);
+
+ String imgIcon = "";
+
+ if(rDef.getType().equals("RessourceFile"))
+ {
+ imgIcon = Tools.getBaseURL() + "images/leaf16.gif";
+ }
+
+ if (rDef.getType().equals("RessourceMoodleCourse"))
+ {
+ imgIcon = Tools.getBaseURL() + "images/moodle20.png";
+ }
+
+ ressource.setIcon(imgIcon);
+
+ }
+ }
+ if(activity.getChildNodes() == null || activity.getChildNodes().length == 0)
+ {
+ activity.setIconCls("my-tree-node-collapsed");
+ }
+
+ groupeCompetences.appendChild(activity);
+ }
+
+
+ rootNode.appendChild(groupeCompetences);
+
+ }
+
+
+ /*TreeSortFunction tsf = new TreeSortFunction(){
+
+ public String sortValue(TreeNode node) {
+
+ String v = node.getAttribute("type");
+ if (v != null){
+ if (v.equals(ScenarioDefinition.EVALUATION)){
+ return "0" + node.getText();
+ }else if (v.equals(ScenarioDefinitionValidation.VALIDATION)){
+ return "1" + node.getText();
+ }else if (v.equals(ScenarioDefCert.CERTIFICATION)){
+ return "2" + node.getText();
+ }
+ }
+
+ return node.getText();
+
+
+
+
+ }
+ };*/
+
+ TreeSorterConfig tsc = new TreeSorterConfig();
+ //tsc.setSortType(tsf);
+
+
+ new TreeSorter(this, tsc);
+
+
+ }
+
+ public boolean isTrashable(Object ...objects) {
+
+ for (Object o : objects){
+ TreeDragData myData = (TreeDragData) o;
+ TreeNode node = myData.getTreeNode();
+ try{
+ if (!node.getAttribute("nodeType").equals("rDefNode")) {
+
+ return false;
+ }
+ }catch (Exception exc){};
+
+
+ }
+
+ return true;
+ }
+
+ public void trashedElements(Object ...objects) {
+
+ for (Object o : objects){
+ TreeDragData myData = (TreeDragData) o;
+ TreeNode node = myData.getTreeNode();
+ try{
+ if (node.getAttribute("nodeType").equals("rDefNode")) {
+
+ this.deleteRDefNode(node);
+
+ }
+ }catch (Exception exc){}
+
+ }
+
+
+ }
+
+ private void deleteRDefNode(TreeNode node){
+
+
+ if(node.getAttribute("nodeType").equals("rDefNode"))
+ {
+ TreeNode parentNode = (TreeNode)node.getParentNode();
+
+ TreeNode esNode = (TreeNode)parentNode.getParentNode();
+
+ EvaluationSubject es = (EvaluationSubject)esNode.getAttributeAsObject("es");
+
+ deleteRDef(node , es);
+
+
+ node.remove();
+
+ if(parentNode.getChildNodes() == null || parentNode.getChildNodes().length == 0){
+ parentNode.setIconCls("my-tree-node-collapsed");
+ }
+ }
+
+ }
+
+ private void deleteRDef (TreeNode node, final EvaluationSubject es){
+ final Set<RessourceDefinition> srd = new HashSet<RessourceDefinition>();
+
+ final RessourceDefinition rDef = (RessourceDefinition)node.getAttributeAsObject("ressource");
+ srd.add(rDef);
+ AsyncCallback callback = new AsyncCallback()
+ {
+
+ public void onFailure(Throwable caught)
+ {
+ System.out.println("Probleme dans la suppression des ressources");
+
+ }
+
+ public void onSuccess(Object result)
+ {
+ es.getRessourceDefinitions().removeAll(srd);
+
+ }
+
+ };
+
+ ConnexionServer.definitionService.removeRessourceDefinitions(srd,
+ callback);
+ }
+
+
+ private void showMenu(final TreeNode node, EventObject e) {
+ //if (menuNode == null){
+ menuNode = new Menu();
+
+ final Item viewItem = new Item(
+ Messages.getString("Visualiser"), new BaseItemListenerAdapter() {
+ @Override
+ public void onClick(BaseItem item, EventObject e) {
+ String url = "";
+ RessourceDefinition rd = (RessourceDefinition)node.getAttributeAsObject("ressource");
+
+ String path = Tools.encodePath(rd.getPath());
+
+ url = Tools.getRessourceAccessURL() + "?pluginKey="
+ + rd.getPluginKey() + "&ressourceKey="
+ + path + "&mode=";
+
+ Tools.displayRessourceUrl(url, path, rd.getName(), getIconCls());
+ }
+ });
+
+ menuNode.addItem(viewItem);
+
+ final Item addItemOrEdit = new Item(Messages.getString("Edit_ressource"), new BaseItemListenerAdapter() {
+
+
+ @Override
+ public void onClick(BaseItem item, EventObject e) {
+
+
+
+ RessourceDefinition rd = (RessourceDefinition)node.getAttributeAsObject("ressource");
+
+ Page.callDocumentEditor(rd);
+
+ }
+ });
+ addItemOrEdit.setIconCls("menu-rename");
+
+ menuNode.addItem(addItemOrEdit);
+
+
+ final Item delItem = new Item(
+ Messages.getString("Supprimer"), new BaseItemListenerAdapter() {
+ @Override
+ public void onClick(BaseItem item, EventObject e) {
+
+ if(node.getParentNode() != null)
+ { TreeNode parentNode = (TreeNode)node.getParentNode();
+ TreeNode esNode = (TreeNode)parentNode.getParentNode();
+
+ EvaluationSubject es = (EvaluationSubject)esNode.getAttributeAsObject("es");
+
+ deleteRDef(node, es);
+ node.remove();
+ if(parentNode.getChildNodes() == null || parentNode.getChildNodes().length == 0){
+ parentNode.setIconCls("my-tree-node-collapsed");
+ }
+ }
+
+ }
+ });
+
+ delItem.setIconCls("menu-delete");
+ menuNode.addItem(delItem);
+
+ menuNode.addListener(new MenuListenerAdapter() {
+ public void doBeforeShow(Menu menu) {
+ String filename = "";
+ RessourceDefinition rd = (RessourceDefinition)node.getAttributeAsObject("ressource");
+
+ filename = rd.getName();
+ if (filename.endsWith(".html") || filename.endsWith(".htm")){
+ addItemOrEdit.show();
+
+ }else{
+ addItemOrEdit.hide();
+ }
+ // Can only rename ES container not inside items
+ if ("rDefNode".equals(ctxNode.getAttribute("nodeType"))) {
+ viewItem.show();
+ delItem.show();
+
+ } else {
+ viewItem.hide();
+ delItem.hide();
+ }
+ }
+ });
+
+ //}
+
+
+ if (ctxNode != null) {
+ ctxNode = null;
+ }
+ ctxNode = node;
+ menuNode.showAt(e.getXY());
+ }
+
+
+
+ private void showMenuAdd(final TreeNode node, EventObject e) {
+
+ menuNodeAdd = new Menu();
+
+
+ Item addItemOrEdit = new Item(Messages.getString("Add_ressource"), new BaseItemListenerAdapter() {
+ @Override
+ public void onClick(BaseItem item, EventObject e) {
+ if (node.getAttribute("nodeType").equals("activityNode"))
+ {
+ final EvaluationSubject es = (EvaluationSubject)node.getParentNode().getAttributeAsObject("es");
+
+ final Activity activity = (Activity)node.getAttributeAsObject("activity");
+ MessageBox.prompt(Messages.getString("filename"), Messages.getString("filename_please"),
+ new MessageBox.PromptCallback() {
+
+ public void execute(String btnID, String text) {
+ if (btnID.equals("ok") && !text.equals("")){
+ if ((!text.endsWith(".html")) && (!text.endsWith(".htm")) ){
+ text = text + ".html";
+ }
+ AsyncCallback callback = new AsyncCallback(){
+
+ public void onFailure(Throwable caught) {
+
+ }
+
+ public void onSuccess(Object result) {
+ Set<RessourceDefinition> rDefs = (HashSet<RessourceDefinition>) result;
+
+ Set<RessourceDefinition> allRDefs = es.getRessourceDefinitions();
+
+ for (int i = 0; i < rDefs.size(); i++) {
+ final RessourceDefinition rd = (RessourceDefinition) rDefs
+ .toArray()[i];
+ // on conserve les nouveaux rdef dans le ES
+
+ final TreeNode newRessource = new TreeNode(rd.getName());
+ newRessource.setAttribute("nodeType", "rDefNode");
+ newRessource.setAttribute("ressource", rd);
+ newRessource.addListener(new TreeNodeListenerAdapter() {
+
+ public void onContextMenu(Node node, EventObject e) {
+
+ e.stopEvent();
+ showMenu(newRessource, e);
+
+ }
+
+ @Override
+ public void onDblClick(Node node, EventObject e) {
+ // Tools.redirect(baseURL + resourceFile.getPath());
+ String url = "";
+
+ String path = Tools.encodePath(rd.getPath());
+
+ url = Tools.getRessourceAccessURL() + "?pluginKey="
+ + rd.getPluginKey() + "&ressourceKey="
+ + path+ "&mode=";
+
+ Tools.displayRessourceUrl(url, path, rd.getName(), getIconCls());
+ }
+
+ });
+
+ node.appendChild(newRessource);
+ node.expand();
+
+ allRDefs.add(rd);
+
+ }
+
+ es.setRessourceDefinitions(allRDefs);
+
+
+ RessourceDefinition RD = rDefs.iterator().next();
+
+
+
+
+ Page.callDocumentEditor(RD);
+
+
+ Tools.hideLoadingRegion(getId());
+
+
+ }};
+
+ RessourceDefinition myRDef = new RessourceDefinition();
+ myRDef.setActivityId(activity.getId());
+ // myRDef.setEvaluationSubject(es);
+ myRDef.setPluginKey("JACKRABBIT");
+ //myRDef.setType(ri.getType());
+ myRDef.setName(text);
+ myRDef.setPath("");
+
+
+
+ Tools
+ .showLoadingRegion(getId(),
+ Messages.getString("MAJ"), Messages.getString("Chargement"));
+
+ Set<RessourceDefinition> myResSet = new HashSet<RessourceDefinition>();
+ myResSet.add(myRDef);
+ ConnexionServer.definitionService
+ .addRessourceDefinitionsToES(myResSet, es.getId(),
+ callback);
+
+
+ }}});
+ }}});
+
+ menuNodeAdd.addItem(addItemOrEdit);
+
+
+ if (ctxNodeAdd != null) {
+ ctxNodeAdd = null;
+ }
+ ctxNodeAdd = node;
+ menuNodeAdd.showAt(e.getXY());
+ }
+
+
+
+
+
+ public EvaluationDefinition getCurrentEvalShown(){
+ return currentEvalShown;
+ }
+
+ public void setCurrentEvalShown(EvaluationDefinition currentEvalShown){
+ this.currentEvalShown = currentEvalShown;
+ }
+}