Source Code Cross Referenced for DefaultTreeModel.java in  » Apache-Harmony-Java-SE » javax-package » javax » swing » tree » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Apache Harmony Java SE » javax package » javax.swing.tree 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.