From b6bfb56f4779ba0ed8271a899e4d81be75fb2ed9 Mon Sep 17 00:00:00 2001 From: Diego Pino Garcia Date: Mon, 31 Aug 2009 12:48:08 +0200 Subject: [PATCH] ItEr23S10CUAsignacionGrupoRecursosAPlanificacionItEr22S10: [Refactoring] Adds SpecificResourceAllocation form Select Resource and add to SpecificResourceAllocation list Delete SpecificResourceAllocation from list --- .../web/planner/IResourceAllocationModel.java | 7 + .../planner/ResourceAllocationController.java | 185 +++++++++++------- .../web/planner/ResourceAllocationModel.java | 10 + .../src/main/webapp/planner/order.zul | 67 ++++--- 4 files changed, 171 insertions(+), 98 deletions(-) diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/IResourceAllocationModel.java b/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/IResourceAllocationModel.java index 176033999..384a35a60 100644 --- a/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/IResourceAllocationModel.java +++ b/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/IResourceAllocationModel.java @@ -120,4 +120,11 @@ public interface IResourceAllocationModel { */ void updateGanttTaskDuration(); + /** + * Adds {@link SpecificResourceAllocation} to {@link Task} + * + * @param worker + */ + void addSpecificResourceAllocation(Worker worker); + } diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/ResourceAllocationController.java b/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/ResourceAllocationController.java index 172136e19..382cb2b3c 100644 --- a/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/ResourceAllocationController.java +++ b/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/ResourceAllocationController.java @@ -1,6 +1,9 @@ package org.navalplanner.web.planner; +import static org.navalplanner.web.I18nHelper._; + import java.math.BigDecimal; +import java.util.List; import java.util.Set; import org.navalplanner.business.planner.entities.ResourceAllocation; @@ -9,6 +12,7 @@ import org.navalplanner.business.planner.entities.Task; import org.navalplanner.business.resources.entities.Criterion; import org.navalplanner.business.resources.entities.Worker; import org.navalplanner.web.common.Util; +import org.navalplanner.web.common.components.WorkerSearch; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.context.annotation.Scope; import org.zkoss.zk.ui.Component; @@ -16,20 +20,17 @@ import org.zkoss.zk.ui.SuspendNotAllowedException; import org.zkoss.zk.ui.WrongValueException; import org.zkoss.zk.ui.event.Event; import org.zkoss.zk.ui.event.EventListener; -import org.zkoss.zk.ui.event.Events; import org.zkoss.zk.ui.util.Clients; import org.zkoss.zk.ui.util.GenericForwardComposer; +import org.zkoss.zul.Button; import org.zkoss.zul.Decimalbox; import org.zkoss.zul.Label; import org.zkoss.zul.Listbox; import org.zkoss.zul.Listcell; import org.zkoss.zul.Listitem; import org.zkoss.zul.ListitemRenderer; -import org.zkoss.zul.Textbox; import org.zkoss.zul.api.Window; -import static org.navalplanner.web.I18nHelper._; - /** * Controller for {@link ResourceAllocation} view. * @@ -41,7 +42,7 @@ public class ResourceAllocationController extends GenericForwardComposer { private IResourceAllocationModel resourceAllocationModel; - private ResourceAllocationListitemRender resourceAllocationRenderer = new ResourceAllocationListitemRender(); + private ResourceAllocationRenderer resourceAllocationRenderer = new ResourceAllocationRenderer(); private Listbox resourcesList; @@ -64,7 +65,7 @@ public class ResourceAllocationController extends GenericForwardComposer { return resourceAllocationModel.getResourceAllocations(); } - public ResourceAllocationListitemRender getResourceAllocationRenderer() { + public ResourceAllocationRenderer getResourceAllocationRenderer() { return resourceAllocationRenderer; } @@ -126,92 +127,136 @@ public class ResourceAllocationController extends GenericForwardComposer { window.setVisible(false); } + public void showSearchResources(Event e) { + WorkerSearch workerSearch = new WorkerSearch(); + workerSearch.setParent(self.getParent()); + workerSearch.afterCompose(); + + Window window = workerSearch.getWindow(); + try { + window.doModal(); + } catch (SuspendNotAllowedException e1) { + e1.printStackTrace(); + return; + } catch (InterruptedException e1) { + e1.printStackTrace(); + return; + } + + // Get selected workers and add specificResourceAllocations + List workers = workerSearch.getWorkers(); + for (Worker worker : workers) { + resourceAllocationModel.addSpecificResourceAllocation(worker); + } + + Util.reloadBindings(resourcesList); + } + /** - * Renders every {@link ResourceAllocation} showing a form to modify its - * information. * - * @author Manuel Rego Casasnovas + * Renders a {@link SpecificResourceAllocation} item + * + * @author Diego Pino Garcia + * */ - public class ResourceAllocationListitemRender implements ListitemRenderer { + private class ResourceAllocationRenderer implements ListitemRenderer { @Override public void render(Listitem item, Object data) throws Exception { - final ResourceAllocation resourceAllocation = (ResourceAllocation) data; + final SpecificResourceAllocation resourceAllocation = (SpecificResourceAllocation) data; + item.setValue(resourceAllocation); - resourceAllocationModel.setResourceAllocation(resourceAllocation); - - final Worker worker = resourceAllocationModel.getWorker(); - - Listcell cellResource = new Listcell(); - final Textbox resourceTextbox = new Textbox(); - Util.bind( - resourceTextbox, new Util.Getter() { - - @Override - public String get() { - if (worker == null) { - return ""; - } - return worker.getNif(); - } - }, new Util.Setter() { - - @Override - public void set(String value) { - Worker worker = resourceAllocationModel - .findWorkerByNif(value); - if (worker == null) { - throw new WrongValueException(resourceTextbox, - _("Worker not found")); - } else { - resourceAllocationModel - .setWorker( - (SpecificResourceAllocation) resourceAllocation, - worker); - } - } - }); - resourceTextbox.addEventListener(Events.ON_CHANGE, + // Label fields are fixed, can only be viewed + appendLabel(item, resourceAllocation.getWorker().getName()); + appendLabel(item, resourceAllocation.getWorker().getNif()); + // Pecentage field is editable + bindPercentage(appendDecimalbox(item), resourceAllocation); + // On click delete button + appendButton(item, _("Delete")).addEventListener("onClick", new EventListener() { @Override public void onEvent(Event event) throws Exception { + resourceAllocationModel + .removeResourceAllocation(resourceAllocation); Util.reloadBindings(resourcesList); } }); - cellResource.appendChild(resourceTextbox); - cellResource.setParent(item); + } - Listcell cellPercentage = new Listcell(); - cellPercentage.appendChild(Util.bind( - new Decimalbox(), - new Util.Getter() { + /** + * Appends {@link Label} to {@link Listitem} + * + * @param listitem + * @param name value for {@link Label} + */ + private void appendLabel(Listitem listitem, String name) { + Label label = new Label(name); - @Override - public BigDecimal get() { - return resourceAllocation.getPercentage().scaleByPowerOfTen(2); - } - }, new Util.Setter() { + Listcell listCell = new Listcell(); + listCell.appendChild(label); + listitem.appendChild(listCell); + } - @Override - public void set(BigDecimal value) { - resourceAllocation.setPercentage(value.setScale(2).divide(new BigDecimal(100),BigDecimal.ROUND_DOWN)); - } - })); - cellPercentage.setParent(item); + /** + * Appends {@link Button} to {@link Listitem} + * + * @param listitem + * @param label value for {@link Button} + * @return + */ + private Button appendButton(Listitem listitem, String label) { + Button button = new Button(label); - Listcell cellMessage = new Listcell(); - String message = ""; + Listcell listCell = new Listcell(); + listCell.appendChild(button); + listitem.appendChild(listCell); - if (worker != null) { - if (!resourceAllocationModel.workerSatisfiesCriterions()) { - message = _("The worker does not satisfy the criterions"); + return button; + } + + /** + * Append a Textbox @{link Percentage} to listItem + * + * @param listItem + */ + private Decimalbox appendDecimalbox(Listitem item) { + Decimalbox decimalbox = new Decimalbox(); + + // Insert textbox in listcell and append to listItem + Listcell listCell = new Listcell(); + listCell.appendChild(decimalbox); + item.appendChild(listCell); + + return decimalbox; + } + + /** + * Binds Textbox @{link Percentage} to a {@link ResourceAllocation} + * {@link Percentage} + * + * @param txtPercentage + * @param resourceAllocation + */ + private void bindPercentage(final Decimalbox decimalbox, + final ResourceAllocation resourceAllocation) { + Util.bind(decimalbox, new Util.Getter() { + + @Override + public BigDecimal get() { + return resourceAllocation.getPercentage().scaleByPowerOfTen(2); } - } - cellMessage.appendChild(new Label(message)); - cellMessage.setParent(item); + }, new Util.Setter() { + + @Override + public void set(BigDecimal value) { + resourceAllocation + .setPercentage(value.setScale(2).divide( + new BigDecimal(100), BigDecimal.ROUND_DOWN)); + } + }); } } } diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/ResourceAllocationModel.java b/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/ResourceAllocationModel.java index 61468d37a..55a486d46 100644 --- a/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/ResourceAllocationModel.java +++ b/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/ResourceAllocationModel.java @@ -1,5 +1,6 @@ package org.navalplanner.web.planner; +import java.math.BigDecimal; import java.util.ArrayList; import java.util.HashSet; import java.util.Set; @@ -186,4 +187,13 @@ public class ResourceAllocationModel implements IResourceAllocationModel { ganttTask.setEndDate(task.getEndDate()); } + @Override + public void addSpecificResourceAllocation(Worker worker) { + SpecificResourceAllocation resourceAllocation = SpecificResourceAllocation + .create(task); + resourceAllocation.setWorker(worker); + resourceAllocation.setPercentage(new BigDecimal(1)); + task.addResourceAllocation(resourceAllocation); + } + } \ No newline at end of file diff --git a/navalplanner-webapp/src/main/webapp/planner/order.zul b/navalplanner-webapp/src/main/webapp/planner/order.zul index 14518d57f..5da0a1ddd 100644 --- a/navalplanner-webapp/src/main/webapp/planner/order.zul +++ b/navalplanner-webapp/src/main/webapp/planner/order.zul @@ -6,6 +6,11 @@ + + + - - -