KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > formmodel > tree > TreeModelHelper


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.forms.formmodel.tree;
17
18 import java.util.EventListener JavaDoc;
19
20 import org.apache.cocoon.forms.event.WidgetEventMulticaster;
21
22 /**
23  * A helper class to ease the implementation of {@link TreeModel}s
24  *
25  * @version $Id: TreeModelHelper.java 190962 2005-06-16 17:17:00Z sylvain $
26  */

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 JavaDoc getNode(TreePath path) {
38         if (path == TreePath.ROOT_PATH) {
39             return model.getRoot();
40         }
41         
42         Object JavaDoc 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 JavaDoc a, EventListener JavaDoc b) {
72             super(a, b);
73         }
74
75         protected static EventListener JavaDoc addInternal(EventListener JavaDoc a, EventListener JavaDoc 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