ItEr42S17CUGravacionModelosUnidadesTraballoItEr41S20: Extracting generic superclass for OrderElementTreeModel
This commit is contained in:
parent
b9ea81ca6f
commit
ac11bc3f8d
3 changed files with 265 additions and 220 deletions
|
|
@ -211,9 +211,9 @@ public class OrderElementTreeController extends GenericForwardComposer {
|
|||
snapshotOfOpenedNodes = TreeViewStateSnapshot.snapshotOpened(tree);
|
||||
try {
|
||||
if (tree.getSelectedCount() == 1) {
|
||||
getModel().addOrderElementAt(getSelectedNode());
|
||||
getModel().addElementAt(getSelectedNode());
|
||||
} else {
|
||||
getModel().addOrderElement();
|
||||
getModel().addElement();
|
||||
}
|
||||
filterByPredicateIfAny();
|
||||
} catch (IllegalStateException e) {
|
||||
|
|
|
|||
|
|
@ -20,116 +20,31 @@
|
|||
|
||||
package org.navalplanner.web.orders;
|
||||
|
||||
import static org.navalplanner.web.I18nHelper._;
|
||||
import static org.navalplanner.business.i18n.I18nHelper._;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.navalplanner.business.orders.entities.Order;
|
||||
import org.navalplanner.business.orders.entities.OrderElement;
|
||||
import org.navalplanner.business.orders.entities.OrderLine;
|
||||
import org.navalplanner.business.orders.entities.OrderLineGroup;
|
||||
import org.zkoss.ganttz.util.MutableTreeModel;
|
||||
import org.zkoss.zul.TreeModel;
|
||||
import org.navalplanner.business.trees.ITreeNode;
|
||||
import org.navalplanner.business.trees.ITreeParentNode;
|
||||
import org.navalplanner.web.orders.components.EntitiesTree;
|
||||
|
||||
/**
|
||||
* Model for a the {@link OrderElement} tree for a {@link Order} <br />
|
||||
* @author Lorenzo Tilve Álvaro <ltilve@igalia.com>
|
||||
* @author Diego Pino García <dpino@igalia.com>
|
||||
*/
|
||||
public class OrderElementTreeModel {
|
||||
public class OrderElementTreeModel extends EntitiesTree<OrderElement> {
|
||||
|
||||
private static MutableTreeModel<OrderElement> createTreeFrom(Order order) {
|
||||
MutableTreeModel<OrderElement> treeModel = MutableTreeModel
|
||||
.create(
|
||||
OrderElement.class, order);
|
||||
OrderElement parent = treeModel.getRoot();
|
||||
List<OrderElement> orderElements = order.getOrderElements();
|
||||
treeModel.add(parent, orderElements);
|
||||
addChildren(treeModel, orderElements);
|
||||
return treeModel;
|
||||
public OrderElementTreeModel(OrderElement root,
|
||||
List<OrderElement> rootChildren) {
|
||||
super(OrderElement.class, root, rootChildren);
|
||||
}
|
||||
|
||||
private static MutableTreeModel<OrderElement> createTreeFrom(Order order,
|
||||
List<OrderElement> orderElements) {
|
||||
MutableTreeModel<OrderElement> treeModel = MutableTreeModel.create(
|
||||
OrderElement.class, order);
|
||||
OrderElement parent = treeModel.getRoot();
|
||||
treeModel.add(parent, orderElements);
|
||||
addChildren(treeModel, orderElements);
|
||||
return treeModel;
|
||||
}
|
||||
|
||||
private static void addChildren(MutableTreeModel<OrderElement> treeModel,
|
||||
List<OrderElement> orderElements) {
|
||||
for (OrderElement orderElement : orderElements) {
|
||||
treeModel.add(orderElement, orderElement.getChildren());
|
||||
addChildren(treeModel, orderElement.getChildren());
|
||||
}
|
||||
}
|
||||
|
||||
private MutableTreeModel<OrderElement> tree;
|
||||
|
||||
public OrderElementTreeModel(Order order) {
|
||||
tree = createTreeFrom(order);
|
||||
}
|
||||
|
||||
public OrderElementTreeModel(Order order, List<OrderElement> orderElements) {
|
||||
tree = createTreeFrom(order, orderElements);
|
||||
}
|
||||
|
||||
public TreeModel asTree() {
|
||||
return tree;
|
||||
}
|
||||
|
||||
public void addOrderElement() {
|
||||
addOrderElementAtImpl(tree.getRoot());
|
||||
}
|
||||
|
||||
private OrderElement createNewOrderElement() {
|
||||
OrderElement newOrderElement = OrderLine
|
||||
.createOrderLineWithUnfixedPercentage(0);
|
||||
newOrderElement.setName(_("New order element"));
|
||||
return newOrderElement;
|
||||
}
|
||||
|
||||
public void addOrderElementAt(OrderElement node) {
|
||||
addOrderElementAtImpl(node);
|
||||
}
|
||||
|
||||
private void addOrderElementAtImpl(OrderElement parent) {
|
||||
addOrderElementAt(parent, createNewOrderElement());
|
||||
|
||||
}
|
||||
|
||||
private void addToTree(OrderElement parentNode, OrderElement elementToAdd) {
|
||||
tree.add(parentNode, elementToAdd);
|
||||
addChildren(tree, Arrays.asList(elementToAdd));
|
||||
}
|
||||
|
||||
private void addToTree(OrderElement parentNode, int position,
|
||||
OrderElement elementToAdd) {
|
||||
tree.add(parentNode, position, Arrays.asList(elementToAdd));
|
||||
addChildren(tree, Arrays.asList(elementToAdd));
|
||||
}
|
||||
|
||||
private void addOrderElementAt(OrderElement parent,
|
||||
OrderElement orderElement) {
|
||||
OrderLineGroup container = turnIntoContainerIfNeeded(parent);
|
||||
container.add(orderElement);
|
||||
addToTree(toNode(container), orderElement);
|
||||
updateCriterionRequirementsInHierarchy(parent, orderElement,
|
||||
(OrderElement) container);
|
||||
}
|
||||
|
||||
private void addOrderElementAt(OrderElement destinationNode,
|
||||
OrderElement elementToAdd, int position) {
|
||||
OrderLineGroup container = turnIntoContainerIfNeeded(destinationNode);
|
||||
container.add(position, elementToAdd);
|
||||
addToTree(toNode(container), position, elementToAdd);
|
||||
updateCriterionRequirementsInHierarchy(destinationNode, elementToAdd,
|
||||
(OrderElement) container);
|
||||
public OrderElementTreeModel(OrderElement root) {
|
||||
super(OrderElement.class, root);
|
||||
}
|
||||
|
||||
private void updateCriterionRequirementsInHierarchy(
|
||||
|
|
@ -142,130 +57,19 @@ public class OrderElementTreeModel {
|
|||
}
|
||||
}
|
||||
|
||||
private OrderElement toNode(OrderLineGroup container) {
|
||||
return container;
|
||||
@Override
|
||||
protected void added(ITreeNode<OrderElement> destination,
|
||||
ITreeNode<OrderElement> added,
|
||||
ITreeParentNode<OrderElement> turnedIntoContainer) {
|
||||
updateCriterionRequirementsInHierarchy(destination.getThis(), added
|
||||
.getThis(), turnedIntoContainer.getThis());
|
||||
}
|
||||
|
||||
private OrderLineGroup turnIntoContainerIfNeeded(
|
||||
OrderElement selectedForTurningIntoContainer) {
|
||||
if (selectedForTurningIntoContainer instanceof OrderLineGroup) {
|
||||
return (OrderLineGroup) selectedForTurningIntoContainer;
|
||||
}
|
||||
OrderLineGroup parentContainer = asOrderLineGroup(getParent(selectedForTurningIntoContainer));
|
||||
OrderLineGroup asContainer = selectedForTurningIntoContainer
|
||||
.toContainer();
|
||||
parentContainer.replace(selectedForTurningIntoContainer, asContainer);
|
||||
tree.replace(selectedForTurningIntoContainer, asContainer);
|
||||
addChildren(tree, Arrays.asList((OrderElement) asContainer));
|
||||
return asContainer;
|
||||
}
|
||||
|
||||
private OrderElement getParent(OrderElement node) {
|
||||
return tree.getParent(node);
|
||||
}
|
||||
|
||||
public List<OrderElement> getParents(OrderElement node) {
|
||||
return tree.getParents(node);
|
||||
}
|
||||
|
||||
public void indent(OrderElement nodeToIndent) {
|
||||
OrderElement parentOfSelected = tree.getParent(nodeToIndent);
|
||||
int position = getChildren(parentOfSelected).indexOf(nodeToIndent);
|
||||
if (position == 0) {
|
||||
return;
|
||||
}
|
||||
OrderElement destination = (OrderElement) getChildren(parentOfSelected)
|
||||
.get(position - 1);
|
||||
move(nodeToIndent, destination, getChildren(destination).size());
|
||||
}
|
||||
|
||||
private List<OrderElement> getChildren(OrderElement node) {
|
||||
List<OrderElement> result = new ArrayList<OrderElement>();
|
||||
final int childCount = tree.getChildCount(node);
|
||||
for (int i = 0; i < childCount; i++) {
|
||||
result.add(tree.getChild(node, i));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public void unindent(OrderElement nodeToUnindent) {
|
||||
OrderElement parent = tree.getParent(nodeToUnindent);
|
||||
if (tree.isRoot(parent)) {
|
||||
return;
|
||||
}
|
||||
OrderElement destination = tree.getParent(parent);
|
||||
move(nodeToUnindent, destination, getChildren(destination).indexOf(
|
||||
parent) + 1);
|
||||
}
|
||||
|
||||
public void move(OrderElement toBeMoved, OrderElement destination) {
|
||||
move(toBeMoved, destination, getChildren(destination).size());
|
||||
}
|
||||
|
||||
public void moveToRoot(OrderElement toBeMoved) {
|
||||
move(toBeMoved, tree.getRoot(), 0);
|
||||
}
|
||||
|
||||
private void move(OrderElement toBeMoved, OrderElement destination,
|
||||
int position) {
|
||||
if (getChildren(destination).contains(toBeMoved)) {
|
||||
return;// it's already moved
|
||||
}
|
||||
if (isGreatInHierarchy(toBeMoved, destination)) {
|
||||
return;
|
||||
}
|
||||
removeNode(toBeMoved);
|
||||
addOrderElementAt(destination, toBeMoved, position);
|
||||
}
|
||||
|
||||
private boolean isGreatInHierarchy(OrderElement parent, OrderElement child) {
|
||||
return find(child, getChildren(parent));
|
||||
}
|
||||
|
||||
private boolean find(OrderElement child, List<OrderElement> children) {
|
||||
if (children.indexOf(child) >= 0) {
|
||||
return true;
|
||||
}
|
||||
for (OrderElement criterionDTO : children) {
|
||||
return find(child, getChildren(criterionDTO));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void up(OrderElement node) {
|
||||
OrderLineGroup orderLineGroup = asOrderLineGroup(tree.getParent(node));
|
||||
orderLineGroup.up(node);
|
||||
tree.up(node);
|
||||
}
|
||||
|
||||
public void down(OrderElement node) {
|
||||
OrderLineGroup orderLineGroup = asOrderLineGroup(tree.getParent(node));
|
||||
orderLineGroup.down(node);
|
||||
tree.down(node);
|
||||
}
|
||||
|
||||
private OrderLineGroup asOrderLineGroup(OrderElement node) {
|
||||
return (OrderLineGroup) node;
|
||||
}
|
||||
|
||||
public void removeNode(OrderElement orderElement) {
|
||||
if (orderElement == tree.getRoot()) {
|
||||
return;
|
||||
}
|
||||
OrderLineGroup parent = asOrderLineGroup(tree.getParent(orderElement));
|
||||
parent.remove(orderElement);
|
||||
tree.remove(orderElement);
|
||||
// If removed node was the last one and its parent is not the root node
|
||||
if (!tree.isRoot(parent) && tree.getChildCount(parent) == 0) {
|
||||
OrderElement asLeaf = ((OrderElement) parent).toLeaf();
|
||||
OrderElement parentContainer = getParent(toNode(parent));
|
||||
asOrderLineGroup(parentContainer).replace((OrderElement) parent,
|
||||
asLeaf);
|
||||
tree.replace((OrderElement) parent, asLeaf);
|
||||
}
|
||||
}
|
||||
|
||||
public int[] getPath(OrderElement orderElement) {
|
||||
return tree.getPath(tree.getRoot(), orderElement);
|
||||
@Override
|
||||
protected OrderElement createNewElement() {
|
||||
OrderElement newOrderElement = OrderLine
|
||||
.createOrderLineWithUnfixedPercentage(0);
|
||||
newOrderElement.setName(_("New order element"));
|
||||
return newOrderElement;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,241 @@
|
|||
/*
|
||||
* This file is part of ###PROJECT_NAME###
|
||||
*
|
||||
* Copyright (C) 2009 Fundación para o Fomento da Calidade Industrial e
|
||||
* Desenvolvemento Tecnolóxico de Galicia
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.navalplanner.web.orders.components;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.navalplanner.business.orders.entities.OrderElement;
|
||||
import org.navalplanner.business.trees.ITreeNode;
|
||||
import org.navalplanner.business.trees.ITreeParentNode;
|
||||
import org.zkoss.ganttz.util.MutableTreeModel;
|
||||
import org.zkoss.zul.TreeModel;
|
||||
|
||||
/**
|
||||
* @author Óscar González Fernández <ogonzalez@igalia.com>
|
||||
*
|
||||
*/
|
||||
public abstract class EntitiesTree<T extends ITreeNode<T>> {
|
||||
|
||||
protected static <T extends ITreeNode<T>> MutableTreeModel<T> createTreeFrom(
|
||||
Class<T> type, T tree) {
|
||||
List<T> children = tree.getChildren();
|
||||
return createTreeFrom(type, tree, children);
|
||||
}
|
||||
|
||||
protected static <T extends ITreeNode<T>> MutableTreeModel<T> createTreeFrom(
|
||||
Class<T> type, T tree, List<T> children) {
|
||||
MutableTreeModel<T> treeModel = MutableTreeModel.create(type, tree);
|
||||
T parent = treeModel.getRoot();
|
||||
treeModel.add(parent, children);
|
||||
addChildren(treeModel, children);
|
||||
return treeModel;
|
||||
}
|
||||
|
||||
private static <T extends ITreeNode<T>> void addChildren(
|
||||
MutableTreeModel<T> treeModel, List<T> children) {
|
||||
for (T each : children) {
|
||||
treeModel.add(each, each.getChildren());
|
||||
addChildren(treeModel, each.getChildren());
|
||||
}
|
||||
}
|
||||
|
||||
private MutableTreeModel<T> tree;
|
||||
|
||||
protected EntitiesTree(Class<T> type, T root) {
|
||||
tree = createTreeFrom(type, root);
|
||||
}
|
||||
|
||||
protected EntitiesTree(Class<T> type, T root, List<T> rootChildren) {
|
||||
tree = createTreeFrom(type, root, rootChildren);
|
||||
}
|
||||
|
||||
public TreeModel asTree() {
|
||||
return tree;
|
||||
}
|
||||
|
||||
public void addElement() {
|
||||
addElementAtImpl(tree.getRoot());
|
||||
}
|
||||
|
||||
protected abstract T createNewElement();
|
||||
|
||||
public void addElementAt(T node) {
|
||||
addElementAtImpl(node);
|
||||
}
|
||||
|
||||
private void addElementAtImpl(T parent) {
|
||||
addOrderElementAt(parent, createNewElement());
|
||||
|
||||
}
|
||||
|
||||
private void addToTree(ITreeNode<T> parentNode, ITreeNode<T> elementToAdd) {
|
||||
tree.add(parentNode.getThis(), elementToAdd.getThis());
|
||||
addChildren(tree, Collections.singletonList(elementToAdd.getThis()));
|
||||
}
|
||||
|
||||
private void addToTree(ITreeNode<T> parentNode, int position,
|
||||
ITreeNode<T> elementToAdd) {
|
||||
List<T> children = Collections
|
||||
.singletonList(elementToAdd.getThis());
|
||||
tree.add(parentNode.getThis(), position, children);
|
||||
addChildren(tree, children);
|
||||
}
|
||||
|
||||
private void addOrderElementAt(ITreeNode<T> parent, ITreeNode<T> element) {
|
||||
ITreeParentNode<T> container = turnIntoContainerIfNeeded(parent);
|
||||
container.add(element.getThis());
|
||||
addToTree(container.getThis(), element);
|
||||
added(parent, element, container);
|
||||
}
|
||||
|
||||
private void addOrderElementAt(ITreeNode<T> destinationNode,
|
||||
ITreeNode<T> elementToAdd, int position) {
|
||||
ITreeParentNode<T> container = turnIntoContainerIfNeeded(destinationNode);
|
||||
container.add(position, elementToAdd.getThis());
|
||||
addToTree(container, position, elementToAdd);
|
||||
added(destinationNode, elementToAdd, container);
|
||||
}
|
||||
|
||||
protected abstract void added(ITreeNode<T> destination, ITreeNode<T> added,
|
||||
ITreeParentNode<T> turnedIntoContainer);
|
||||
|
||||
private ITreeParentNode<T> turnIntoContainerIfNeeded(
|
||||
ITreeNode<T> selectedForTurningIntoContainer) {
|
||||
if (selectedForTurningIntoContainer instanceof ITreeParentNode) {
|
||||
return (ITreeParentNode<T>) selectedForTurningIntoContainer;
|
||||
}
|
||||
ITreeParentNode<T> parentContainer = getParent(selectedForTurningIntoContainer);
|
||||
ITreeParentNode<T> asContainer = selectedForTurningIntoContainer
|
||||
.toContainer();
|
||||
parentContainer.replace(selectedForTurningIntoContainer.getThis(),
|
||||
asContainer.getThis());
|
||||
tree.replace(selectedForTurningIntoContainer.getThis(), asContainer
|
||||
.getThis());
|
||||
addChildren(tree, Collections.singletonList(asContainer.getThis()));
|
||||
return asContainer;
|
||||
}
|
||||
|
||||
private ITreeParentNode<T> getParent(ITreeNode<T> node) {
|
||||
return (ITreeParentNode<T>) tree.getParent(node.getThis());
|
||||
}
|
||||
|
||||
public List<T> getParents(T node) {
|
||||
return tree.getParents(node);
|
||||
}
|
||||
|
||||
public void indent(T nodeToIndent) {
|
||||
T parentOfSelected = tree.getParent(nodeToIndent);
|
||||
int position = getChildren(parentOfSelected).indexOf(nodeToIndent);
|
||||
if (position == 0) {
|
||||
return;
|
||||
}
|
||||
T destination = getChildren(parentOfSelected)
|
||||
.get(position - 1);
|
||||
move(nodeToIndent, destination, getChildren(destination).size());
|
||||
}
|
||||
|
||||
private List<T> getChildren(T node) {
|
||||
List<T> result = new ArrayList<T>();
|
||||
final int childCount = tree.getChildCount(node);
|
||||
for (int i = 0; i < childCount; i++) {
|
||||
result.add(tree.getChild(node, i));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public void unindent(T nodeToUnindent) {
|
||||
T parent = tree.getParent(nodeToUnindent);
|
||||
if (tree.isRoot(parent)) {
|
||||
return;
|
||||
}
|
||||
T destination = tree.getParent(parent);
|
||||
move(nodeToUnindent, destination, getChildren(destination).indexOf(
|
||||
parent) + 1);
|
||||
}
|
||||
|
||||
public void move(T toBeMoved, T destination) {
|
||||
move(toBeMoved, destination, getChildren(destination).size());
|
||||
}
|
||||
|
||||
public void moveToRoot(T toBeMoved) {
|
||||
move(toBeMoved, tree.getRoot(), 0);
|
||||
}
|
||||
|
||||
private void move(T toBeMoved, T destination,
|
||||
int position) {
|
||||
if (getChildren(destination).contains(toBeMoved)) {
|
||||
return;// it's already moved
|
||||
}
|
||||
if (isGreatInHierarchy(toBeMoved, destination)) {
|
||||
return;
|
||||
}
|
||||
removeNode(toBeMoved);
|
||||
addOrderElementAt(destination, toBeMoved, position);
|
||||
}
|
||||
|
||||
private boolean isGreatInHierarchy(T parent, T child) {
|
||||
return find(child, getChildren(parent));
|
||||
}
|
||||
|
||||
private boolean find(T child, List<T> children) {
|
||||
if (children.indexOf(child) >= 0) {
|
||||
return true;
|
||||
}
|
||||
for (T each : children) {
|
||||
return find(child, getChildren(each));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void up(T node) {
|
||||
ITreeParentNode<T> parent = getParent(node);
|
||||
parent.up(node);
|
||||
tree.up(node);
|
||||
}
|
||||
|
||||
public void down(T node) {
|
||||
ITreeParentNode<T> parent = getParent(node);
|
||||
parent.down(node);
|
||||
tree.down(node);
|
||||
}
|
||||
|
||||
public void removeNode(T element) {
|
||||
if (element == tree.getRoot()) {
|
||||
return;
|
||||
}
|
||||
ITreeParentNode<T> parent = getParent(element);
|
||||
parent.remove(element);
|
||||
tree.remove(element);
|
||||
// If removed node was the last one and its parent is not the root node
|
||||
if (!tree.isRoot(parent.getThis()) && tree.getChildCount(parent) == 0) {
|
||||
T asLeaf = parent.toLeaf();
|
||||
ITreeParentNode<T> parentContainer = getParent(parent.getThis());
|
||||
parentContainer.replace(parent.getThis(), asLeaf);
|
||||
tree.replace(parent.getThis(), asLeaf);
|
||||
}
|
||||
}
|
||||
|
||||
public int[] getPath(OrderElement orderElement) {
|
||||
return tree.getPath(tree.getRoot(), orderElement);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue