ItEr19S08CUCreacionProxectoPlanificacionItEr18S08: Fixing problem in left tree. When adding a task that contained other children, only top node was added.

This commit is contained in:
Óscar González Fernández 2009-07-24 17:48:46 +02:00 committed by Javier Moran Rua
parent 2202766d6b
commit 57cceb716d

View file

@ -1,6 +1,8 @@
package org.zkoss.ganttz;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
@ -52,6 +54,7 @@ public class LeftTasksTree extends HtmlMacroComponent {
cell.setSclass(cssClass);
}
detailsForBeans.put(task, leftTasksTreeRow);
deferredFiller.isBeingRendered(task, item);
}
private void expandWhenOpened(final TaskContainer taskBean,
@ -142,8 +145,7 @@ public class LeftTasksTree extends HtmlMacroComponent {
return null;
}
public List<ChildAndParent> group(Task origin,
List<Task> parents) {
public List<ChildAndParent> group(Task origin, List<Task> parents) {
ArrayList<ChildAndParent> result = new ArrayList<ChildAndParent>();
Task child = origin;
Task parent;
@ -207,8 +209,62 @@ public class LeftTasksTree extends HtmlMacroComponent {
}
/**
* This class is a workaround for an issue with zk {@link Tree}. Once the
* tree is created, a node with more children can't be added. Only the top
* element is added to the tree, although the element has children. The Tree
* discards the adding event for the children because the parent says it's
* not loaded. This is the condition that is not satisfied:<br />
* <code>if(parent != null &&
(!(parent instanceof Treeitem) || ((Treeitem)parent).isLoaded())){</code><br />
* This problem is present in zk 3.6.1 at least.
* @author Óscar González Fernández <ogonzalez@igalia.com>
* @see Tree#onTreeDataChange
*/
private class DeferredFiller {
private Set<Task> pendingToAddChildren = new HashSet<Task>();
public void addParentOfPendingToAdd(Task parent) {
pendingToAddChildren.add(parent);
}
public void isBeingRendered(final Task parent, final Treeitem item) {
if (!pendingToAddChildren.contains(parent))
return;
markLoaded(item);
fillModel(parent, parent.getTasks(), false);
pendingToAddChildren.remove(parent);
}
private void markLoaded(Treeitem item) {
try {
Method method = getSetLoadedMethod();
method.invoke(item, true);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private Method setLoadedMethod = null;
private Method getSetLoadedMethod() {
if (setLoadedMethod != null)
return setLoadedMethod;
try {
Method method = Treeitem.class.getDeclaredMethod("setLoaded",
Boolean.TYPE);
method.setAccessible(true);
return setLoadedMethod = method;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
private static Log LOG = LogFactory.getLog(LeftTasksTree.class);
private final DeferredFiller deferredFiller = new DeferredFiller();
private final List<Task> tasks;
private MutableTreeModel<Task> tasksTreeModel;
@ -221,19 +277,25 @@ public class LeftTasksTree extends HtmlMacroComponent {
this.tasks = tasks;
}
private static void fillModel(MutableTreeModel<Task> treeModel,
List<Task> tasks) {
for (Task task : tasks) {
fillModel(treeModel, treeModel.getRoot(), task);
}
private void fillModel(List<Task> tasks, boolean firstTime) {
fillModel(this.tasksTreeModel.getRoot(), tasks, firstTime);
}
private static void fillModel(MutableTreeModel<Task> treeModel,
Task parent, Task node) {
treeModel.add(parent, node);
if (node.isContainer()) {
for (Task child : node.getTasks()) {
fillModel(treeModel, node, child);
private void fillModel(Task parent, List<Task> children,
final boolean firstTime) {
for (Task node : children) {
if (firstTime) {
this.tasksTreeModel.add(parent, node);
if (node.isContainer()) {
fillModel(node, node.getTasks(), firstTime);
}
} else {
if (node.isContainer()) {
this.deferredFiller.addParentOfPendingToAdd(node);
}
// the node must be added after, so the multistepTreeFiller is
// ready
this.tasksTreeModel.add(parent, node);
}
}
}
@ -259,14 +321,14 @@ public class LeftTasksTree extends HtmlMacroComponent {
super.afterCompose();
tasksTree = (Tree) getFellow("tasksTree");
tasksTreeModel = MutableTreeModel.create(Task.class);
fillModel(tasksTreeModel, tasks);
fillModel(tasks, true);
tasksTree.setModel(tasksTreeModel);
tasksTree.setTreeitemRenderer(new TaskBeanRenderer());
}
void addTask(Task task) {
fillModel(Arrays.asList(task), false);
detailsForBeans.requestFocusFor(task);
tasksTreeModel.add(tasksTreeModel.getRoot(), task);
}
public CommandContextualized<?> getGoingDownInLastArrowCommand() {