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: /**
018: * @author Alexander T. Simbirtsev
019: * @version $Revision$
020: */package javax.swing.tree;
021:
022: import java.io.Serializable;
023: import java.util.EventListener;
024:
025: import javax.swing.event.EventListenerList;
026: import javax.swing.event.TreeModelEvent;
027: import javax.swing.event.TreeModelListener;
028:
029: import org.apache.harmony.x.swing.TreeCommons;
030: import org.apache.harmony.x.swing.Utilities;
031:
032: import org.apache.harmony.x.swing.internal.nls.Messages;
033:
034: public class DefaultTreeModel implements TreeModel, Serializable {
035:
036: protected boolean asksAllowsChildren;
037: protected EventListenerList listenerList = new EventListenerList();
038: protected TreeNode root;
039:
040: public DefaultTreeModel(final TreeNode root) {
041: this .root = root;
042: }
043:
044: public DefaultTreeModel(final TreeNode root,
045: final boolean asksAllowsChildren) {
046: this .root = root;
047: this .asksAllowsChildren = asksAllowsChildren;
048: }
049:
050: public void setAsksAllowsChildren(final boolean asksAllowsChildren) {
051: this .asksAllowsChildren = asksAllowsChildren;
052: }
053:
054: public boolean asksAllowsChildren() {
055: return asksAllowsChildren;
056: }
057:
058: public void addTreeModelListener(final TreeModelListener l) {
059: listenerList.add(TreeModelListener.class, l);
060: }
061:
062: public void removeTreeModelListener(final TreeModelListener l) {
063: listenerList.remove(TreeModelListener.class, l);
064: }
065:
066: public TreeModelListener[] getTreeModelListeners() {
067: return (TreeModelListener[]) listenerList
068: .getListeners(TreeModelListener.class);
069: }
070:
071: public <T extends EventListener> T[] getListeners(
072: final Class<T> listenerType) {
073: return listenerList.getListeners(listenerType);
074: }
075:
076: public void setRoot(final TreeNode root) {
077: this .root = root;
078: if (root != null) {
079: nodeStructureChanged(root);
080: } else {
081: fireRootChangedToNull(this );
082: }
083: }
084:
085: public Object getRoot() {
086: return root;
087: }
088:
089: public Object getChild(final Object parent, final int i) {
090: return ((TreeNode) parent).getChildAt(i);
091: }
092:
093: public int getChildCount(final Object node) {
094: return ((TreeNode) node).getChildCount();
095: }
096:
097: public int getIndexOfChild(final Object parent, final Object child) {
098: if (parent == null || child == null) {
099: return -1;
100: }
101:
102: TreeNode parentNode = (TreeNode) parent;
103: int numChildren = parentNode.getChildCount();
104: for (int i = 0; i < numChildren; i++) {
105: if (child.equals(parentNode.getChildAt(i))) {
106: return i;
107: }
108: }
109:
110: return -1;
111: }
112:
113: public boolean isLeaf(final Object node) {
114: return asksAllowsChildren ? !((TreeNode) node)
115: .getAllowsChildren() : (((TreeNode) node)
116: .getChildCount() == 0);
117: }
118:
119: public void valueForPathChanged(final TreePath path,
120: final Object value) {
121: MutableTreeNode node = (MutableTreeNode) path
122: .getLastPathComponent();
123: node.setUserObject(value);
124: nodeChanged(node);
125: }
126:
127: public void reload() {
128: reload(root);
129: }
130:
131: public void reload(final TreeNode node) {
132: nodeStructureChanged(node);
133: }
134:
135: public void insertNodeInto(final MutableTreeNode newChild,
136: final MutableTreeNode parent, final int index) {
137:
138: if (newChild == null) {
139: throw new IllegalArgumentException(Messages.getString(
140: "swing.03", "new child")); //$NON-NLS-1$ //$NON-NLS-2$
141: }
142: parent.insert(newChild, index);
143: nodesWereInserted(parent, new int[] { index });
144: }
145:
146: public void removeNodeFromParent(final MutableTreeNode node) {
147: MutableTreeNode parent = (MutableTreeNode) node.getParent();
148: if (parent == null) {
149: throw new IllegalArgumentException(Messages
150: .getString("swing.AF")); //$NON-NLS-1$
151: }
152: int index = parent.getIndex(node);
153: parent.remove(node);
154: nodesWereRemoved(parent, new int[] { index },
155: new Object[] { node });
156: }
157:
158: public void nodeChanged(final TreeNode node) {
159: if (node == root) {
160: nodesChanged(node, null);
161: return;
162: }
163: if (node == null) {
164: return;
165: }
166: final TreeNode parent = node.getParent();
167: if (parent == null) {
168: return;
169: }
170: nodesChanged(parent,
171: new int[] { getIndexOfChild(parent, node) });
172: }
173:
174: public void nodesChanged(final TreeNode node,
175: final int[] childIndices) {
176: if (node == null || node != root
177: && Utilities.isEmptyArray(childIndices)) {
178: return;
179: }
180:
181: fireTreeNodesChanged(this , getPathToRoot(node), childIndices,
182: getNodeChildren(node, childIndices));
183: }
184:
185: public void nodesWereInserted(final TreeNode node,
186: final int[] childIndices) {
187: if (node == null || Utilities.isEmptyArray(childIndices)) {
188: return;
189: }
190:
191: fireTreeNodesInserted(this , getPathToRoot(node), childIndices,
192: getNodeChildren(node, childIndices));
193: }
194:
195: public void nodesWereRemoved(final TreeNode node,
196: final int[] childIndices, final Object[] removedChildren) {
197: if (node == null || Utilities.isEmptyArray(childIndices)) {
198: return;
199: }
200: fireTreeNodesRemoved(this , getPathToRoot(node), childIndices,
201: removedChildren);
202: }
203:
204: public void nodeStructureChanged(final TreeNode node) {
205: if (node == null) {
206: return;
207: }
208: fireTreeStructureChanged(this , getPathToRoot(node), null, null);
209: }
210:
211: public TreeNode[] getPathToRoot(final TreeNode aNode) {
212: if (aNode == null) {
213: return new TreeNode[0];
214: }
215:
216: return getPathToRoot(aNode, 0);
217: }
218:
219: protected TreeNode[] getPathToRoot(final TreeNode aNode,
220: final int depth) {
221: return TreeCommons.getPathToAncestor(aNode, root, depth);
222: }
223:
224: protected void fireTreeNodesChanged(final Object source,
225: final Object[] path, final int[] childIndices,
226: final Object[] children) {
227: TreeModelListener[] listeners = getTreeModelListeners();
228: if (Utilities.isEmptyArray(listeners)) {
229: return;
230: }
231:
232: TreeModelEvent event = new TreeModelEvent(source, path,
233: childIndices, children);
234: for (int i = 0; i < listeners.length; i++) {
235: listeners[i].treeNodesChanged(event);
236: }
237: }
238:
239: protected void fireTreeNodesInserted(final Object source,
240: final Object[] path, final int[] childIndices,
241: final Object[] children) {
242: TreeModelListener[] listeners = getTreeModelListeners();
243: if (Utilities.isEmptyArray(listeners)) {
244: return;
245: }
246:
247: TreeModelEvent event = new TreeModelEvent(source, path,
248: childIndices, children);
249: for (int i = 0; i < listeners.length; i++) {
250: listeners[i].treeNodesInserted(event);
251: }
252: }
253:
254: protected void fireTreeNodesRemoved(final Object source,
255: final Object[] path, final int[] childIndices,
256: final Object[] children) {
257:
258: TreeModelListener[] listeners = getTreeModelListeners();
259: if (Utilities.isEmptyArray(listeners)) {
260: return;
261: }
262:
263: TreeModelEvent event = new TreeModelEvent(source, path,
264: childIndices, children);
265: for (int i = 0; i < listeners.length; i++) {
266: listeners[i].treeNodesRemoved(event);
267: }
268: }
269:
270: protected void fireTreeStructureChanged(final Object source,
271: final Object[] path, final int[] childIndices,
272: final Object[] children) {
273: TreeModelListener[] listeners = getTreeModelListeners();
274: if (Utilities.isEmptyArray(listeners)) {
275: return;
276: }
277:
278: TreeModelEvent event = new TreeModelEvent(source, path,
279: childIndices, children);
280: for (int i = 0; i < listeners.length; i++) {
281: listeners[i].treeStructureChanged(event);
282: }
283: }
284:
285: private void fireRootChangedToNull(final Object source) {
286: TreeModelListener[] listeners = getTreeModelListeners();
287: if (Utilities.isEmptyArray(listeners)) {
288: return;
289: }
290:
291: TreeModelEvent event = new TreeModelEvent(source,
292: (TreePath) null);
293: for (int i = 0; i < listeners.length; i++) {
294: listeners[i].treeStructureChanged(event);
295: }
296: }
297:
298: private Object[] getNodeChildren(final TreeNode node,
299: final int[] childIndices) {
300: if (childIndices == null) {
301: return null;
302: }
303:
304: Object[] result = new Object[childIndices.length];
305: for (int i = 0; i < result.length; i++) {
306: result[i] = node.getChildAt(childIndices[i]);
307: }
308: return result;
309: }
310:
311: }
|