1 16 package org.apache.cocoon.forms.formmodel.tree; 17 18 import java.util.EventListener ; 19 20 import org.apache.cocoon.forms.event.WidgetEventMulticaster; 21 22 27 public class TreeModelHelper { 28 29 private TreeModel model; 30 31 private TreeModelListener listener; 32 33 public TreeModelHelper(TreeModel model) { 34 this.model = model; 35 } 36 37 public Object getNode(TreePath path) { 38 if (path == TreePath.ROOT_PATH) { 39 return model.getRoot(); 40 } 41 42 Object parent = getNode(path.getParentPath()); 43 if (parent == null) { 44 return null; 45 } 46 47 return model.getChild(parent, path.getLastKey()); 48 } 49 50 public void addTreeModelListener(TreeModelListener listener) { 51 this.listener = EventMulticaster.add(this.listener, listener); 52 } 53 54 public void removeTreeModelListener(TreeModelListener listener) { 55 this.listener = EventMulticaster.remove(this.listener, listener); 56 } 57 58 boolean hasListeners() { 59 return this.listener != null; 60 } 61 62 public void fireTreeStructureChanged(TreePath path) { 63 if (hasListeners()) { 64 TreeModelEvent event = new TreeModelEvent(model, path); 65 this.listener.treeStructureChanged(event); 66 } 67 } 68 69 private static class EventMulticaster extends WidgetEventMulticaster implements TreeModelListener { 70 71 protected EventMulticaster(EventListener a, EventListener b) { 72 super(a, b); 73 } 74 75 protected static EventListener addInternal(EventListener a, EventListener b) { 76 if (a == null) return b; 77 if (b == null) return a; 78 return new EventMulticaster(a, b); 79 } 80 81 public static TreeModelListener add(TreeModelListener a, TreeModelListener b) { 82 return (TreeModelListener)addInternal(a, b); 83 } 84 85 public static TreeModelListener remove(TreeModelListener l, TreeModelListener oldl) { 86 return (TreeModelListener)removeInternal(l, oldl); 87 } 88 89 public void treeStructureChanged(TreeModelEvent event) { 90 ((TreeModelListener)a).treeStructureChanged(event); 91 ((TreeModelListener)b).treeStructureChanged(event); 92 } 93 } 94 } 95 | Popular Tags |