001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019: package org.netbeans.modules.xslt.mapper.model;
020:
021: import java.util.ArrayList;
022: import java.util.List;
023: import javax.swing.event.TreeModelEvent;
024: import javax.swing.event.TreeModelListener;
025: import javax.swing.tree.TreeModel;
026: import javax.swing.tree.TreePath;
027: import org.netbeans.modules.xslt.mapper.model.nodes.TreeNode;
028: import org.netbeans.modules.xslt.mapper.view.XsltMapper;
029:
030: /**
031: *
032: * @author nk160297
033: */
034: public abstract class XsltNodesTreeModel implements TreeModel {
035:
036: private TreeNode myRootNode;
037: private List<TreeModelListener> listeners = new ArrayList<TreeModelListener>();
038: private XsltMapper mapper;
039:
040: public XsltNodesTreeModel(XsltMapper mapper) {
041: this .mapper = mapper;
042: resetRoot();
043: }
044:
045: public abstract TreeNode loadRoot();
046:
047: public XsltMapper getMapper() {
048: return this .mapper;
049: }
050:
051: public Object getRoot() {
052: return myRootNode;
053: }
054:
055: public void resetRoot() {
056: TreeNode newRootNode = loadRoot();
057: if (newRootNode != null
058: && myRootNode != null
059: && newRootNode.getDataObject() == myRootNode
060: .getDataObject()) {
061: return;
062: }
063:
064: if (myRootNode != null) {
065: myRootNode.removeFromTree();
066: }
067:
068: myRootNode = newRootNode;
069: }
070:
071: public int getIndexOfChild(Object parent, Object requiredChild) {
072: assert parent instanceof TreeNode;
073: assert requiredChild instanceof TreeNode;
074: //
075: List<TreeNode> children = ((TreeNode) parent).getChildren();
076:
077: return children.indexOf(requiredChild);
078:
079: }
080:
081: public Object getChild(Object parent, int index) {
082: assert parent instanceof TreeNode;
083:
084: //
085: List<TreeNode> children = ((TreeNode) parent).getChildren();
086: return children.get(index);
087: }
088:
089: public boolean isLeaf(Object node) {
090: assert node instanceof TreeNode;
091: //
092: return ((TreeNode) node).getChildren().isEmpty();
093: }
094:
095: public int getChildCount(Object parent) {
096: assert parent instanceof TreeNode;
097: //
098: return ((TreeNode) parent).getChildren().size();
099: }
100:
101: public void removeTreeModelListener(TreeModelListener l) {
102: listeners.remove(l);
103: }
104:
105: public void addTreeModelListener(TreeModelListener l) {
106: listeners.add(l);
107: }
108:
109: public void valueForPathChanged(TreePath path, Object newValue) {
110: // do nothing for a while
111: }
112:
113: public void fireTreeChanged(TreePath tp) {
114: TreeModelEvent event = new TreeModelEvent(this , tp);
115: for (TreeModelListener listener : listeners) {
116: listener.treeStructureChanged(event);
117: }
118: }
119:
120: }
|