1 /* |
|
2 * CDDL HEADER START |
|
3 * |
|
4 * The contents of this file are subject to the terms of the |
|
5 * Common Development and Distribution License (the "License"). |
|
6 * You may not use this file except in compliance with the License. |
|
7 * |
|
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
9 * or http://www.opensolaris.org/os/licensing. |
|
10 * See the License for the specific language governing permissions |
|
11 * and limitations under the License. |
|
12 * |
|
13 * When distributing Covered Code, include this CDDL HEADER in each |
|
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
15 * If applicable, add the following below this CDDL HEADER, with the |
|
16 * fields enclosed by brackets "[]" replaced with your own identifying |
|
17 * information: Portions Copyright [yyyy] [name of copyright owner] |
|
18 * |
|
19 * CDDL HEADER END |
|
20 */ |
|
21 |
|
22 /* |
|
23 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. |
|
24 */ |
|
25 |
|
26 package com.oracle.solaris.vp.util.swing.tree; |
|
27 |
|
28 import java.util.*; |
|
29 import javax.swing.event.*; |
|
30 import javax.swing.tree.*; |
|
31 import com.oracle.solaris.vp.util.swing.event.TreeModelListeners; |
|
32 |
|
33 /** |
|
34 * The {@code NotifyingTreeNode} class propagates insertions/deletions to the |
|
35 * {@code TreeNode} to registered listeners. The {@code NotifyingTreeNode} adds |
|
36 * itself as a {@code TreeModelListener} to each of its children. |
|
37 */ |
|
38 @SuppressWarnings({"serial"}) |
|
39 public class NotifyingTreeNode<U> extends TypedTreeNode<U> |
|
40 implements TreeModelListener { |
|
41 |
|
42 // |
|
43 // Instance data |
|
44 // |
|
45 |
|
46 private TreeModelListeners listeners = new TreeModelListeners(); |
|
47 |
|
48 // |
|
49 // Constructors |
|
50 // |
|
51 |
|
52 public NotifyingTreeNode() { |
|
53 } |
|
54 |
|
55 public NotifyingTreeNode(Object userObject) { |
|
56 super(userObject); |
|
57 } |
|
58 |
|
59 public NotifyingTreeNode(Object userObject, boolean allowsChildren) { |
|
60 super(userObject, allowsChildren); |
|
61 } |
|
62 |
|
63 // |
|
64 // TreeModelListener methods |
|
65 // |
|
66 |
|
67 /** |
|
68 * Propagates the given event to registered listeners. |
|
69 */ |
|
70 @Override |
|
71 public void treeNodesChanged(TreeModelEvent e) { |
|
72 listeners.treeNodesChanged(e); |
|
73 } |
|
74 |
|
75 /** |
|
76 * Propagates the given event to registered listeners. |
|
77 */ |
|
78 @Override |
|
79 public void treeNodesInserted(TreeModelEvent e) { |
|
80 listeners.treeNodesInserted(e); |
|
81 } |
|
82 |
|
83 /** |
|
84 * Propagates the given event to registered listeners. |
|
85 */ |
|
86 @Override |
|
87 public void treeNodesRemoved(TreeModelEvent e) { |
|
88 listeners.treeNodesRemoved(e); |
|
89 } |
|
90 |
|
91 /** |
|
92 * Propagates the given event to registered listeners. |
|
93 */ |
|
94 @Override |
|
95 public void treeStructureChanged(TreeModelEvent e) { |
|
96 listeners.treeStructureChanged(e); |
|
97 } |
|
98 |
|
99 // |
|
100 // DefaultMutableTreeNode methods |
|
101 // |
|
102 |
|
103 @Override |
|
104 public void insert(MutableTreeNode child, int index) { |
|
105 super.insert(child, index); |
|
106 |
|
107 if (child instanceof TreeModelListener) { |
|
108 ((NotifyingTreeNode)child).addTreeModelListener(this); |
|
109 } |
|
110 } |
|
111 |
|
112 @Override |
|
113 public void remove(int index) { |
|
114 TreeNode child = getChildAt(index); |
|
115 if (child instanceof TreeModelListener) { |
|
116 ((NotifyingTreeNode)child).removeTreeModelListener(this); |
|
117 } |
|
118 |
|
119 super.remove(index); |
|
120 } |
|
121 |
|
122 // |
|
123 // NotifyingTreeNode methods |
|
124 // |
|
125 |
|
126 public void addTreeModelListener(TreeModelListener listener) { |
|
127 listeners.add(listener); |
|
128 } |
|
129 |
|
130 /** |
|
131 * Called when this {@code TreeNode} has changed. |
|
132 */ |
|
133 public void fireTreeNodeChanged() { |
|
134 TreePath path = null; |
|
135 int index = 0; |
|
136 |
|
137 TreeNode parent = getParent(); |
|
138 if (parent != null) { |
|
139 index = parent.getIndex(this); |
|
140 } |
|
141 |
|
142 TreeModelEvent event = new TreeModelEvent( |
|
143 this, path, new int[] {index}, new TreeNode[] {this}); |
|
144 |
|
145 treeNodesChanged(event); |
|
146 } |
|
147 |
|
148 /** |
|
149 * Called when child {@code TreeNode}s of this {@code TreeNode} have |
|
150 * changed. |
|
151 */ |
|
152 public void fireTreeNodesChanged(TreeNode[] nodes, int[] indexes) { |
|
153 TreeModelEvent event = |
|
154 new TreeModelEvent(this, getTreePath(), indexes, nodes); |
|
155 |
|
156 treeNodesChanged(event); |
|
157 } |
|
158 |
|
159 public void fireTreeNodesInserted(TreeNode[] nodes, int[] indexes) { |
|
160 TreeModelEvent event = |
|
161 new TreeModelEvent(this, getTreePath(), indexes, nodes); |
|
162 |
|
163 treeNodesInserted(event); |
|
164 } |
|
165 |
|
166 public void fireTreeNodesRemoved(TreeNode[] nodes, int[] indexes) { |
|
167 TreeModelEvent event = |
|
168 new TreeModelEvent(this, getTreePath(), indexes, nodes); |
|
169 |
|
170 treeNodesRemoved(event); |
|
171 } |
|
172 |
|
173 public void fireTreeStructureChanged() { |
|
174 TreeModelEvent event = new TreeModelEvent(this, getTreePath()); |
|
175 treeStructureChanged(event); |
|
176 } |
|
177 |
|
178 public TreePath getTreePath() { |
|
179 List<Object> path = new ArrayList<Object>(); |
|
180 path.add(this); |
|
181 |
|
182 TreeNode parent = getParent(); |
|
183 while (parent != null) { |
|
184 path.add(0, parent); |
|
185 parent = parent.getParent(); |
|
186 } |
|
187 |
|
188 Object[] array = path.toArray(new Object[path.size()]); |
|
189 return new TreePath(array); |
|
190 } |
|
191 |
|
192 public void removeTreeModelListener(TreeModelListener listener) { |
|
193 listeners.remove(listener); |
|
194 } |
|
195 } |
|