001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.forms.formmodel.tree;
018:
019: import java.util.EventListener;
020:
021: import org.apache.cocoon.forms.event.WidgetEventMulticaster;
022:
023: /**
024: * A helper class to ease the implementation of {@link TreeModel}s
025: *
026: * @version $Id: TreeModelHelper.java 518026 2007-03-14 05:25:54Z antonio $
027: */
028: public class TreeModelHelper {
029:
030: private TreeModel model;
031:
032: private TreeModelListener listener;
033:
034: public TreeModelHelper(TreeModel model) {
035: this .model = model;
036: }
037:
038: public Object getNode(TreePath path) {
039: if (path == TreePath.ROOT_PATH) {
040: return model.getRoot();
041: }
042:
043: Object parent = getNode(path.getParentPath());
044: if (parent == null) {
045: return null;
046: }
047:
048: return model.getChild(parent, path.getLastKey());
049: }
050:
051: public void addTreeModelListener(TreeModelListener listener) {
052: this .listener = EventMulticaster.add(this .listener, listener);
053: }
054:
055: public void removeTreeModelListener(TreeModelListener listener) {
056: this .listener = EventMulticaster
057: .remove(this .listener, listener);
058: }
059:
060: boolean hasListeners() {
061: return this .listener != null;
062: }
063:
064: public void fireTreeStructureChanged(TreePath path) {
065: if (hasListeners()) {
066: TreeModelEvent event = new TreeModelEvent(model, path);
067: this .listener.treeStructureChanged(event);
068: }
069: }
070:
071: private static class EventMulticaster extends
072: WidgetEventMulticaster implements TreeModelListener {
073:
074: protected EventMulticaster(EventListener a, EventListener b) {
075: super (a, b);
076: }
077:
078: protected static EventListener addInternal(EventListener a,
079: EventListener b) {
080: if (a == null)
081: return b;
082: if (b == null)
083: return a;
084: return new EventMulticaster(a, b);
085: }
086:
087: public static TreeModelListener add(TreeModelListener a,
088: TreeModelListener b) {
089: return (TreeModelListener) addInternal(a, b);
090: }
091:
092: public static TreeModelListener remove(TreeModelListener l,
093: TreeModelListener oldl) {
094: return (TreeModelListener) removeInternal(l, oldl);
095: }
096:
097: protected EventListener remove(EventListener oldl) {
098: if (oldl == a)
099: return b;
100: if (oldl == b)
101: return a;
102: EventListener a2 = removeInternal(a, oldl);
103: EventListener b2 = removeInternal(b, oldl);
104: if (a2 == a && b2 == b) {
105: return this ; // it's not here
106: }
107: return addInternal(a2, b2);
108: }
109:
110: public void treeStructureChanged(TreeModelEvent event) {
111: ((TreeModelListener) a).treeStructureChanged(event);
112: ((TreeModelListener) b).treeStructureChanged(event);
113: }
114: }
115: }
|