--- /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.SimpleStore;
+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.definition.ScenarioDefinition;
+import com.pentila.evalcomp.domain.plan.Plan;
+import com.pentila.evalcomp.domain.plan.ScenarioDefinitionPlan;
+import com.pentila.evalcomp.domain.transit.TransitProcessDefinition;
+
+// TODO: Auto-generated Javadoc
+/**
+ * The Class RoleDefWidget.
+ */
+public class PlanScenarioWidget extends GridPanelDropable {
+
+ /** The cfg column model PlanSc. */
+ private ColumnModel cfgColumnModelPlanSc;
+
+ /** The cfg columns PlanSc. */
+ private ColumnConfig[] cfgColumnsPlanSc;
+
+ /** The plan. */
+ private Plan currentPlan = null;
+
+ /** The menu PlanSc. */
+ private Menu menuPlanSc;
+
+ /** The readOnly flag. */
+ private Boolean readOnly = true;
+
+ /** The render PlanSc. */
+ private Renderer renderPlanSc = new Renderer() {
+ public String render(Object value, CellMetadata cellMetadata,
+ Record record, int rowIndex, int colNum, Store store) {
+ String icon = "<img src=\"" + Tools.getBaseURL() //$NON-NLS-1$
+ + "images/scenario16.png\"> {0}"; //$NON-NLS-1$
+ return Format.format(icon,
+ new String[] { record.getAsString("planScName"), }); //$NON-NLS-1$
+ }
+ };
+
+ /** The store group users. */
+ private Store storePlanSc;
+
+ /**
+ * Instantiates a new PlanSc widget.
+ *
+ * @param plan
+ * the plan
+ * @param ro
+ * the ro
+ */
+ public PlanScenarioWidget(Plan plan, Boolean ro) {
+ super();
+ // TODO Auto-generated constructor stub
+ this.currentPlan = plan;
+ this.readOnly = ro;
+
+ this.setIconCls("iconScenario32"); //$NON-NLS-1$
+
+ cfgColumnsPlanSc = new ColumnConfig[] { new ColumnConfig(
+ Messages.getString("Nom"), "planScName", 179, true, renderPlanSc, "planScName") }; //$NON-NLS-1$
+ cfgColumnModelPlanSc = new ColumnModel(cfgColumnsPlanSc);
+
+ initializeInterface();
+
+ this.setDragDropText(Messages.getString("x_scenariox_selectionnex")); //$NON-NLS-1$
+ }
+
+ /**
+ * Delete PlanSc.
+ */
+ private void deletePlanSc() {
+ Record[] rows = this.getSelectionModel().getSelections();
+
+ final Set<ScenarioDefinitionPlan> planScs = new HashSet<ScenarioDefinitionPlan>();
+
+ for (Record row : rows) {
+
+ planScs.add((ScenarioDefinitionPlan) row
+ .getAsObject("planScObject")); //$NON-NLS-1$
+ }
+
+ AsyncCallback callback = new AsyncCallback() {
+
+ public void onFailure(Throwable caught) {
+ System.out
+ .println("Probleme dans Suppression des plan scenario"); //$NON-NLS-1$
+ }
+
+ public void onSuccess(Object result) {
+ Record[] rows = getSelectionModel().getSelections();
+
+ for (Record row : rows) {
+ getStore().remove(row);
+
+ }
+ currentPlan.getScenarioDefinitionPlans().removeAll(planScs);
+ }
+ };
+ ConnexionServer.planService.removeScenarioDefinitionPlans(planScs,
+ callback);
+ }
+
+ /**
+ * Initialize interface.
+ */
+ private void initializeInterface() {
+ this.setTitle(Messages.getString("Scenarios"));
+
+ storePlanSc = new SimpleStore(0, new FieldDef[] {
+ new StringFieldDef("planScID"),
+ new StringFieldDef("planScName"), //$NON-NLS-1$
+ new ObjectFieldDef("planScObject") }, new Object[][] {}); //$NON-NLS-1$
+
+ storePlanSc.setSortInfo(new SortState("planScName", SortDir.ASC));
+ storePlanSc.load();
+
+ this.setStore(storePlanSc);
+ this.setColumnModel(cfgColumnModelPlanSc);
+ this.setWidth(185);
+ this.setHeight(200);
+
+ if (readOnly) {
+ this.setEnableDragDrop(false);
+ } else {
+ this.setEnableDragDrop(true);
+
+ this.setDdGroup("evalCompDDGroup"); //$NON-NLS-1$
+
+ this.addGridRowListener(new GridRowListenerAdapter() {
+ @Override
+ public void onRowContextMenu(GridPanel grid, int rowIndex,
+ EventObject e) {
+ e.stopEvent();
+ showDelPlanScContextMenu(e);
+ }
+
+ });
+
+ DropTargetConfig cfg = new DropTargetConfig();
+ cfg.setdDdGroup("evalCompDDGroup"); //$NON-NLS-1$
+
+ new DropTarget(this, cfg) {
+ @Override
+ public boolean notifyDrop(DragSource source, EventObject e,
+ DragData data) {
+ try {
+
+ final Set<ScenarioDefinitionPlan> planScTMPset = new HashSet<ScenarioDefinitionPlan>();
+ GridDragData myData = (GridDragData) data;
+ GridPanel myGrid = (GridPanel) Ext.getCmp(myData
+ .getGrid().getId());
+ Record[] rows = myGrid.getSelectionModel()
+ .getSelections();
+
+ boolean ok = true;
+ for (Record r : rows) {
+ String t = r.getAsString("type");
+
+ if (t == null
+ || !t.startsWith(ScenarioDefinition.EVALUATION)) {
+ ok = false;
+ break;
+ }
+ }
+ if (myGrid.getId().startsWith("scenarioGridPalette") && ok) {
+
+ Set<String> existantPDIds = new HashSet<String>();
+ for (ScenarioDefinitionPlan sdp : currentPlan
+ .getScenarioDefinitionPlans()) {
+ existantPDIds.add(sdp.getProcessDefinitionId());
+ }
+ for (Record row : rows) {
+ TransitProcessDefinition tpd = (TransitProcessDefinition) row
+ .getAsObject("scenarioTransObject");
+ if (!existantPDIds.contains(tpd.getId())) {
+ ScenarioDefinitionPlan newPlanSc = new ScenarioDefinitionPlan();
+ newPlanSc.setProcessDefinitionId(tpd
+ .getId());
+ newPlanSc.setName(tpd.getName());
+ newPlanSc.setDescription(tpd
+ .getDescription());
+
+ planScTMPset.add(newPlanSc);
+ }
+
+ }
+
+ final AsyncCallback callback = new AsyncCallback() {
+
+ public void onFailure(Throwable caught) {
+ System.out
+ .println("Probleme dans ajout des scenario definition plan"); //$NON-NLS-1$
+ }
+
+ public void onSuccess(Object result) {
+ Set<ScenarioDefinitionPlan> planScAdded = (Set<ScenarioDefinitionPlan>) result;
+
+ Object[][] objList = new Object[planScAdded
+ .size()][];
+ for (int i = 0; i < planScAdded.size(); i++) {
+ ScenarioDefinitionPlan pSc = (ScenarioDefinitionPlan) planScAdded
+ .toArray()[i];
+ Object[] monObjet = new Object[] {
+ pSc.getId().toString(),
+ pSc.getName(), pSc };
+ objList[i] = monObjet;
+ }
+
+ RecordDef recordDef = new RecordDef(
+ new FieldDef[] {
+ new StringFieldDef(
+ "planScID"), //$NON-NLS-1$
+ new StringFieldDef(
+ "planScName"), //$NON-NLS-1$
+ new ObjectFieldDef(
+ "planScObject") }); //$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);
+ }
+
+ currentPlan.getScenarioDefinitionPlans()
+ .addAll(planScAdded);
+ }
+ };
+ if (!planScTMPset.isEmpty()) {
+ ConnexionServer.planService
+ .addScenarioDefinitionPlansToPlan(
+ currentPlan.getId(),
+ planScTMPset, 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());
+ Record[] rc = myGrid.getSelectionModel()
+ .getSelections();
+ boolean ok = true;
+ for (Record r : rc) {
+ String t = r.getAsString("type");
+
+ if (t == null
+ || !t.startsWith(ScenarioDefinition.EVALUATION)) {
+ ok = false;
+ break;
+ }
+ }
+
+ if (myGrid.getId().startsWith("scenarioGridPalette") && ok) { //$NON-NLS-1$
+ return "x-dd-drop-ok"; //$NON-NLS-1$
+ }
+ return ""; //$NON-NLS-1$
+ } catch (Exception ex) {
+ return ""; //$NON-NLS-1$
+ }
+ }
+ };
+ }
+ }
+
+ /**
+ * Show del user context menu.
+ *
+ * @param e
+ * the e
+ */
+ private void showDelPlanScContextMenu(EventObject e) {
+ if (menuPlanSc == null) {
+ menuPlanSc = new Menu();
+
+ Item deleteItem = new Item(
+ Messages.getString("Supprimer_cette_entree"),
+ new BaseItemListenerAdapter() {
+ @Override
+ public void onClick(BaseItem item, EventObject e) {
+ deletePlanSc();
+ }
+ });
+
+
+ deleteItem.setIconCls("menu-delete");
+ menuPlanSc.addItem(deleteItem);
+ }
+ menuPlanSc.showAt(e.getXY());
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.client.utilities.GridPanelDropable#trashedElements()
+ */
+ @Override
+ public void trashedElements(Object...objects) {
+ deletePlanSc();
+ }
+
+ /**
+ * Update role def grid.
+ *
+ * @param plan
+ * the plan
+ */
+ public void updatePlanScGrid(Plan plan) {
+ this.currentPlan = plan;
+ this.getStore().removeAll();
+ // create source group grid
+ if (plan != null) {
+
+ Set<ScenarioDefinitionPlan> planScs = plan
+ .getScenarioDefinitionPlans();
+ if (!planScs.isEmpty()) {
+ Object[][] objList = new Object[planScs.size()][];
+ for (int i = 0; i < planScs.size(); i++) {
+ ScenarioDefinitionPlan p = (ScenarioDefinitionPlan) planScs
+ .toArray()[i];
+ Object[] monObjet = new Object[] { p.getId().toString(),
+ p.getName(), p };
+ objList[i] = monObjet;
+ }
+
+ /*
+ * RecordDef recordDef = new RecordDef(new FieldDef[] { new
+ * StringFieldDef("planScID"), //$NON-NLS-1$ new
+ * StringFieldDef("planScName"), //$NON-NLS-1$ new
+ * ObjectFieldDef("planScObject") }); //$NON-NLS-1$
+ */
+ DataProxy proxy = new MemoryProxy(objList);
+ // Reader reader = new ArrayReader(recordDef);
+ // Store tmpStore = new Store(proxy, reader);
+ getStore().setDataProxy(proxy);
+ getStore()
+ .setSortInfo(new SortState("planScName", SortDir.ASC));
+
+ getStore().reload();
+ //tmpStore.sort("planScName"); //$NON-NLS-1$
+
+ // this.reconfigure(tmpStore, cfgColumnModelPlanSc);
+ }
+ }
+ }
+
+}