1 package edu.jiangxin.apktoolbox.swing.treetable;
2
3 import javax.swing.event.EventListenerList;
4 import javax.swing.event.TreeModelEvent;
5 import javax.swing.event.TreeModelListener;
6 import javax.swing.tree.TreePath;
7
8 public abstract class MyAbstractTreeTableModel implements MyTreeTableModel {
9 protected Object root;
10 protected EventListenerList listenerList = new EventListenerList();
11
12 private static final int CHANGED = 0;
13 private static final int INSERTED = 1;
14 private static final int REMOVED = 2;
15 private static final int STRUCTURE_CHANGED = 3;
16
17 public MyAbstractTreeTableModel(Object root) {
18 this.root = root;
19 }
20
21 public Object getRoot() {
22 return root;
23 }
24
25 public boolean isLeaf(Object node) {
26 return getChildCount(node) == 0;
27 }
28
29 public void valueForPathChanged(TreePath path, Object newValue) {
30 }
31
32
33
34
35 public int getIndexOfChild(Object parent, Object child) {
36 return 0;
37 }
38
39 @Override
40 public void addTreeModelListener(TreeModelListener treeModelListener) {
41 listenerList.add(TreeModelListener.class, treeModelListener);
42 }
43
44 @Override
45 public void removeTreeModelListener(TreeModelListener treeModelListener) {
46 listenerList.remove(TreeModelListener.class, treeModelListener);
47 }
48
49 private void fireTreeNode(int changeType, Object source, Object[] path, int[] childIndices, Object[] children) {
50 Object[] listeners = listenerList.getListenerList();
51 TreeModelEvent e = new TreeModelEvent(source, path, childIndices, children);
52 for (int i = listeners.length - 2; i >= 0; i -= 2) {
53 if (listeners[i] == TreeModelListener.class) {
54
55 switch (changeType) {
56 case CHANGED:
57 ((TreeModelListener) listeners[i + 1]).treeNodesChanged(e);
58 break;
59 case INSERTED:
60 ((TreeModelListener) listeners[i + 1]).treeNodesInserted(e);
61 break;
62 case REMOVED:
63 ((TreeModelListener) listeners[i + 1]).treeNodesRemoved(e);
64 break;
65 case STRUCTURE_CHANGED:
66 ((TreeModelListener) listeners[i + 1]).treeStructureChanged(e);
67 break;
68 default:
69 break;
70 }
71
72 }
73 }
74 }
75
76 protected void fireTreeNodesChanged(Object source, Object[] path, int[] childIndices, Object[] children) {
77 fireTreeNode(CHANGED, source, path, childIndices, children);
78 }
79
80 protected void fireTreeNodesInserted(Object source, Object[] path, int[] childIndices, Object[] children) {
81 fireTreeNode(INSERTED, source, path, childIndices, children);
82 }
83
84 protected void fireTreeNodesRemoved(Object source, Object[] path, int[] childIndices, Object[] children) {
85 fireTreeNode(REMOVED, source, path, childIndices, children);
86 }
87
88 protected void fireTreeStructureChanged(Object source, Object[] path, int[] childIndices, Object[] children) {
89 fireTreeNode(STRUCTURE_CHANGED, source, path, childIndices, children);
90 }
91
92 }