1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.gui.table;
21
22 import javax.swing.SwingUtilities;
23
24 import org.xnap.event.ListEvent;
25 import org.xnap.event.ListListener;
26 import org.xnap.event.StateEvent;
27 import org.xnap.event.StateListener;
28 import org.xnap.transfer.Transfer;
29 import org.xnap.transfer.TransferContainer;
30
31 /***
32 *
33 */
34 public class TransferContainerTreeTableNode extends DefaultTreeTableNode
35 implements StateListener, ListListener
36 {
37
38
39
40
41
42
43
44 /***
45 * Constructs a <code>TransferContainerTreeTableNode</code>.
46 *
47 * @param model the table model
48 * @param transfer must implement {@link TransferContainer}
49 */
50 public TransferContainerTreeTableNode(TransferTableModel model,
51 Transfer transfer)
52 {
53 super(model, transfer);
54
55 transfer.addStateListener(this);
56 ((TransferContainer)transfer).addListListener(this);
57 }
58
59
60
61 /***
62 * Creates a {@link LeafTreeTableNode}, if <code>data</code> is
63 * not already contained in children.
64 *
65 * @return true, if data was added; false, otherwise
66 */
67 public boolean add(Transfer transfer)
68 {
69
70 int index = getIndexOfChildByData(transfer);
71 if (index == -1) {
72 transfer.addStateListener(this);
73 super.add(new LeafTreeTableNode(transfer));
74 return true;
75 }
76 return false;
77 }
78
79 public void addExistingChildren()
80 {
81 Transfer[] children = ((TransferContainer)getData()).getChildren();
82 for (int i = 0; i < children.length; i++) {
83 add(children[i]);
84 }
85 }
86
87 public void remove(Transfer transfer)
88 {
89 int index = getIndexOfChildByData(transfer);
90 if (index != -1) {
91 transfer.removeStateListener(this);
92 removeChildAt(index);
93 }
94 }
95
96 public void itemAdded(final ListEvent event)
97 {
98 Runnable runner = new Runnable()
99 {
100 public void run()
101 {
102 add((Transfer)event.getItem());
103 }
104 };
105 SwingUtilities.invokeLater(runner);
106 }
107
108 public void itemRemoved(final ListEvent event)
109 {
110 Runnable runner = new Runnable()
111 {
112 public void run()
113 {
114 remove((Transfer)event.getItem());
115 }
116 };
117 SwingUtilities.invokeLater(runner);
118 }
119
120 public void stateChanged(final StateEvent event)
121 {
122 Runnable runner = new Runnable()
123 {
124 public void run()
125 {
126 if (event.getSource() == getData()) {
127 changed();
128 }
129 else {
130 int index = getIndexOfChildByData(event.getSource());
131 if (index != -1) {
132 changedChildAt(index);
133 }
134 }
135 }
136 };
137 SwingUtilities.invokeLater(runner);
138 }
139
140 }