--- /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.plan.widget;
+
+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.core.Ext;
+import com.gwtext.client.core.SortDir;
+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.SortState;
+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.GridDragData;
+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.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.domain.Entity;
+import com.pentila.evalcomp.domain.User;
+import com.pentila.evalcomp.domain.plan.PlanComponent;
+
+// TODO: Auto-generated Javadoc
+/**
+ * The Class RoleDefWidget.
+ */
+public class PlanRoleWidget extends GridPanelDropable {
+
+ /** The cfg column model. */
+ private ColumnModel cfgColumnModelRolePlan;
+
+ /** The cfg columns role. */
+ private ColumnConfig[] cfgColumnsRolePlan;
+
+ /** The plan. */
+ private PlanComponent currentPlan = null;
+
+ /** The role string. */
+ private String currentRole = null;
+
+ /** The menu. */
+ private Menu menuEntity;
+
+ /** The readOnly flag. */
+ private Boolean readOnly = true;
+
+ /** The single. */
+ private Boolean single = false;
+
+ /** The render. */
+ private Renderer renderRolePlan = new Renderer() {
+ public String render(Object value, CellMetadata cellMetadata,
+ Record record, int rowIndex, int colNum, Store store) {
+ String icon = "<img src=\"" + Tools.getBaseURL()
+ + "images/user16.png\"> {0}";
+ return Format.format(icon, new String[] { record
+ .getAsString("entityName"), });
+ }
+ };
+
+ /** The record def. */
+ RecordDef recordDef = new RecordDef(new FieldDef[] {
+ new StringFieldDef("entityID"),
+ new StringFieldDef("entityName"),
+ new ObjectFieldDef("entityObject") });
+
+ /** The store. */
+ private Store storeEntity;
+
+ /**
+ * Instantiates a new role widget.
+ *
+ * @param plan
+ * the plan
+ * @param role
+ * the current role
+ * @param ro
+ * the ro
+ */
+ public PlanRoleWidget(PlanComponent plan, String role, Boolean ro) {
+ super();
+ // TODO Auto-generated constructor stub
+ this.currentPlan = plan;
+ this.currentRole = role;
+ this.readOnly = ro;
+
+
+
+ cfgColumnsRolePlan = new ColumnConfig[] { new ColumnConfig(
+ Messages.getString("Utilisateurs"), "entityName", 169, true, renderRolePlan,
+ "entityName") };
+ cfgColumnModelRolePlan = new ColumnModel(cfgColumnsRolePlan);
+
+ initializeInterface();
+
+ this.setDragDropText(Messages.getString("x_utilisateurx_selectionnex"));
+ }
+
+
+ /**
+ * Instantiates a new plan role widget.
+ *
+ * @param plan the plan
+ * @param role the role
+ * @param ro the ro
+ * @param single the single
+ */
+ public PlanRoleWidget(PlanComponent plan, String role, Boolean ro, Boolean single) {
+ this(plan, role, ro);
+ this.single = single;
+
+ }
+ /**
+ * Delete grp users.
+ */
+ private void deleteEntities() {
+ Record[] rows = this.getSelectionModel().getSelections();
+ new HashSet<Long>();
+
+ final Set<Entity> entities = new HashSet<Entity>();
+
+ for (Record row : rows) {
+ this.getStore().remove(row);
+ entities.add((Entity) row.getAsObject("entityObject")); //$NON-NLS-1$
+ }
+ AsyncCallback callback = new AsyncCallback() {
+
+ public void onFailure(Throwable caught) {
+ System.out.println("Probleme dans Suppression des set enities"); //$NON-NLS-1$
+ }
+
+ public void onSuccess(Object result) {
+ // on supprime les entities du plan
+ if (currentPlan.getMapRoleEntities().containsKey(currentRole) && currentPlan.getMapRoleEntities().get(currentRole) != null){
+ currentPlan.getMapRoleEntities().get(currentRole)
+ .removeAll(entities);
+ }
+ }
+ };
+ ConnexionServer.planService.removeEntitiesFromPlanComponent(currentPlan
+ .getId(), entities, currentRole, callback);
+ }
+
+ /**
+ * Initialize interface.
+ */
+ private void initializeInterface() {
+
+ this.setTitle(Messages.getString(currentRole), "iconGroup32");
+
+
+
+ DataProxy proxy = new MemoryProxy(new Object[][]{});
+ Reader reader = new ArrayReader(recordDef);
+ storeEntity = new Store(proxy, reader);
+ storeEntity.setSortInfo(new SortState("entityName", SortDir.ASC));
+ storeEntity.load();
+
+ this.setStore(storeEntity);
+ this.setColumnModel(cfgColumnModelRolePlan);
+
+ this.setWidth(175);
+ this.setHeight(250);
+
+ if (readOnly) {
+ this.setEnableDragDrop(false);
+ } else {
+ this.setEnableDragDrop(true);
+
+ this.setDdGroup("evalCompDDGroup");
+
+ this.addGridRowListener(new GridRowListenerAdapter() {
+ @Override
+ public void onRowContextMenu(GridPanel grid, int rowIndex,
+ EventObject e) {
+ e.stopEvent();
+ showDelEntityContextMenu(e);
+ }
+
+ });
+
+ DropTargetConfig cfg = new DropTargetConfig();
+ cfg.setdDdGroup("evalCompDDGroup");
+
+ new DropTarget(this, cfg) {
+ @Override
+ public boolean notifyDrop(DragSource source, EventObject e,
+ DragData data) {
+
+ try {
+
+
+ if (single && getStore().getCount() != 0){
+ return false;
+ }
+
+ final Set<Entity> entityTMP = new HashSet<Entity>();
+ GridDragData myData = (GridDragData) data;
+ GridPanel myGrid = (GridPanel) Ext.getCmp(myData
+ .getGrid().getId());
+ Record[] rows = myGrid.getSelectionModel()
+ .getSelections();
+ for (Record row : rows) {
+ User u = (User) row.getAsObject("userObject");
+ if (Tools.storeFinder(getStore(), "entityID",
+ ((Entity) u).getId().toString()) == null) {
+ entityTMP.add(u);
+ }
+ }
+
+ final AsyncCallback callback = new AsyncCallback() {
+
+ public void onFailure(Throwable caught) {
+ System.out
+ .println("Probleme dans ajout des enities");
+ }
+
+ public void onSuccess(Object result) {
+
+ Set<Entity> entities = (Set<Entity>) result;
+
+ Object[][] objList = new Object[entities.size()][];
+ for (int i = 0; i < entities.size(); i++) {
+ Entity e = (Entity) entities.toArray()[i];
+ Object[] monObjet = new Object[] {
+ e.getId().toString(),
+ ((User) e).getLastname() + " "
+ + ((User) e).getFirstname(),
+ e };
+ objList[i] = monObjet;
+ }
+
+ RecordDef recordDef = new RecordDef(
+ new FieldDef[] {
+ new StringFieldDef("entityID"),
+ new StringFieldDef("entityName"),
+ new ObjectFieldDef(
+ "entityObject") });
+ 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);
+ }
+
+
+
+
+ if (!currentPlan.getMapRoleEntities()
+ .containsKey(currentRole) || currentPlan.getMapRoleEntities().get(currentRole) == null) {
+ currentPlan.getMapRoleEntities().put(
+ currentRole, new HashSet<Entity>());
+
+
+ }
+
+
+
+ currentPlan.getMapRoleEntities().get(
+ currentRole).addAll(entities);
+ }
+ };
+ if (entityTMP.size() != 0) {
+
+ ConnexionServer.planService
+ .addEntitiesToPlanComponent(currentPlan
+ .getId(), entityTMP, currentRole,
+ callback);
+ }
+ return true;
+ } catch (Exception ex) {
+ }
+ return false;
+ }
+
+ @Override
+ public String notifyOver(DragSource source, EventObject e,
+ DragData data) {
+ try {
+ GridDragData myData = (GridDragData) data;
+ GridPanel myGrid = (GridPanel) Ext.getCmp(myData
+ .getGrid().getId());
+ if (myGrid.getId().startsWith("usersGridPalette") && (!single || getStore().getCount() == 0)) { //$NON-NLS-1$
+ return "x-dd-drop-ok"; //$NON-NLS-1$
+ }
+ } catch (Exception ex) {
+ }
+ return ""; //$NON-NLS-1$
+ }
+ };
+
+ }
+ }
+
+ /**
+ * Show del user context menu.
+ *
+ * @param e
+ * the e
+ */
+ private void showDelEntityContextMenu(EventObject e) {
+ if (menuEntity == null) {
+ menuEntity = new Menu();
+
+ Item deleteItem = new Item(Messages
+ .getString("Supprimer_cette_entree"), //$NON-NLS-1$
+ new BaseItemListenerAdapter() {
+ @Override
+ public void onClick(BaseItem item, EventObject e) {
+ deleteEntities();
+ }
+ });
+
+
+ deleteItem.setIconCls("menu-delete"); //$NON-NLS-1$
+ menuEntity.addItem(deleteItem);
+ }
+ menuEntity.showAt(e.getXY());
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.client.utilities.GridPanelDropable#trashedElements()
+ */
+ @Override
+ public void trashedElements(Object...objects) {
+ deleteEntities();
+ }
+
+ /**
+ * Update role def grid.
+ */
+ public void updateRolePlanGrid() {
+ // create source group grid
+ this.getStore().removeAll();
+ if (currentPlan != null && currentRole != null) {
+
+ Set<Entity> entities = currentPlan.getEntitiesByRole(currentRole);
+
+ if (entities != null && !entities.isEmpty()) {
+
+ Object[][] objList = new Object[entities.size()][];
+ for (int i = 0; i < entities.size(); i++) {
+ Entity e = (Entity) entities.toArray()[i];
+ Object[] monObjet = new Object[] {
+ e.getId().toString(),
+ ((User) e).getLastname() + " "
+ + ((User) e).getFirstname(), e };
+ objList[i] = monObjet;
+ }
+ RecordDef recordDef = new RecordDef(new FieldDef[] {
+ new StringFieldDef("entityID"), //$NON-NLS-1$
+ new StringFieldDef("entityName"), //$NON-NLS-1$
+ new ObjectFieldDef("entityObject") }); //$NON-NLS-1$
+ DataProxy proxy = new MemoryProxy(objList);
+ Reader reader = new ArrayReader(recordDef);
+ Store tmpStore = new Store(proxy, reader);
+ tmpStore.setSortInfo(new SortState("entityName", SortDir.ASC));
+ tmpStore.load();
+ tmpStore.sort("entityName"); //$NON-NLS-1$
+ for (Record rec : tmpStore.getRecords()) {
+ getStore().addSorted(rec);
+ }
+
+ }
+
+ }
+ }
+
+}