KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > admin > dso > FieldTreeNode


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.admin.dso;
5
6 import com.tc.admin.AdminClient;
7 import com.tc.admin.AdminClientContext;
8 import com.tc.admin.ConnectionContext;
9 import com.tc.admin.common.XAbstractAction;
10 import com.tc.admin.common.XTreeModel;
11 import com.tc.admin.common.XTreeNode;
12
13 import java.awt.event.ActionEvent JavaDoc;
14 import java.awt.event.KeyEvent JavaDoc;
15 import java.awt.event.MouseEvent JavaDoc;
16 import java.util.Vector JavaDoc;
17
18 import javax.swing.Icon JavaDoc;
19 import javax.swing.JPopupMenu JavaDoc;
20 import javax.swing.JTree JavaDoc;
21 import javax.swing.KeyStroke JavaDoc;
22 import javax.swing.SwingUtilities JavaDoc;
23 import javax.swing.event.TreeSelectionEvent JavaDoc;
24 import javax.swing.tree.TreeNode JavaDoc;
25 import javax.swing.tree.TreePath JavaDoc;
26
27 public class FieldTreeNode extends XTreeNode implements DSOObjectTreeNode {
28   protected ConnectionContext m_cc;
29   protected DSOField m_field;
30   private MoreAction m_moreAction;
31   private LessAction m_lessAction;
32   private JPopupMenu JavaDoc m_popupMenu;
33   private int m_batchSize;
34   private RefreshAction m_refreshAction;
35
36   private static final String JavaDoc REFRESH_ACTION = "RefreshAction";
37   
38   public FieldTreeNode(ConnectionContext cc, DSOField field) {
39     super(field);
40
41     m_cc = cc;
42     m_field = field;
43     m_batchSize = ConnectionContext.DSO_SMALL_BATCH_SIZE;
44
45     if(!field.isPrimitive()) {
46       init();
47       initMenu();
48     }
49   }
50
51   public DSOObject getDSOObject() {
52     return getField();
53   }
54   
55   protected void init() {
56     int count = m_field.getFieldCount();
57
58     if(children == null) {
59       children = new Vector JavaDoc();
60     }
61     children.setSize(count);
62   }
63   
64   private void initMenu() {
65     m_refreshAction = new RefreshAction();
66
67     m_popupMenu = new JPopupMenu JavaDoc("Root Actions");
68     m_popupMenu.add(m_refreshAction);
69     
70     if(m_field.isArray() || m_field.isCollection()) {
71       m_popupMenu.add(m_moreAction = new MoreAction());
72       m_popupMenu.add(m_lessAction = new LessAction());
73     }
74
75     addActionBinding(REFRESH_ACTION, m_refreshAction);
76   }
77
78   public JPopupMenu JavaDoc getPopupMenu() {
79     return m_popupMenu;
80   }
81
82   public DSOField getField() {
83     return m_field;
84   }
85
86   private void fillInChildren() {
87     int childCount = getChildCount();
88     boolean nso = false;
89
90     for(int i = 0; i < childCount; i++) {
91       if(children.elementAt(i) == null) {
92         DSOObject field = m_field.getField(i);
93         XTreeNode child = createFieldNode(field);
94
95         children.setElementAt(child, i);
96         child.setParent(FieldTreeNode.this);
97
98         if(field == null) {
99           nso = true;
100         }
101       }
102     }
103
104     if(nso) {
105       SwingUtilities.invokeLater(new AncestorReaper());
106     }
107   }
108
109   class AncestorReaper implements Runnable JavaDoc {
110     public void run() {
111       XTreeNode node = FieldTreeNode.this;
112
113       while(node != null) {
114         if(node instanceof FieldTreeNode) {
115           FieldTreeNode ftn = (FieldTreeNode)node;
116
117           if(ftn.getField().isValid()) {
118             ftn.refreshChildren();
119             return;
120           }
121         }
122         else if(node instanceof RootTreeNode) {
123           ((RootTreeNode)node).refresh();
124           return;
125         }
126
127         node = (XTreeNode)node.getParent();
128       }
129     }
130   }
131
132   public TreeNode getChildAt(int index) {
133     if(children != null && children.elementAt(index) == null) {
134       AdminClientContext acc = AdminClient.getContext();
135
136       acc.controller.block();
137       fillInChildren();
138       acc.controller.unblock();
139     }
140
141     return super.getChildAt(index);
142   }
143
144   private XTreeNode createFieldNode(DSOObject field) {
145     return RootsHelper.getHelper().createFieldNode(m_cc, field);
146   }
147
148   public int getChildCount() {
149     return m_field != null ? m_field.getFieldCount() : 0;
150   }
151
152   public Icon JavaDoc getIcon() {
153     RootsHelper helper = RootsHelper.getHelper();
154     
155     return m_field.isCycle() ? helper.getCycleIcon() : helper.getFieldIcon();
156   }
157
158   public void nodeSelected(TreeSelectionEvent JavaDoc e) {
159     if(m_field.isCycle()) {
160       DSOObject cycleRoot = m_field.getCycleRoot();
161       XTreeNode parentNode = (XTreeNode)getParent();
162
163       while(parentNode != null) {
164         if(parentNode instanceof DSOObjectTreeNode) {
165           if(((DSOObjectTreeNode)parentNode).getDSOObject() == cycleRoot) {
166             JTree JavaDoc tree = (JTree JavaDoc)e.getSource();
167             TreePath JavaDoc path = new TreePath JavaDoc(parentNode.getPath());
168             
169             tree.getSelectionModel().addSelectionPath(path);
170             return;
171           }
172         }
173         
174         parentNode = (XTreeNode)parentNode.getParent();
175       }
176     }
177   }
178
179   public void refreshChildren() {
180     tearDownChildren();
181
182     if(m_field != null) {
183       m_field.initFields();
184       children.setSize(getChildCount());
185       fillInChildren();
186     }
187
188     getModel().nodeStructureChanged(this);
189   }
190
191   public void refresh() {
192     AdminClientContext acc = AdminClient.getContext();
193     boolean expanded = acc.controller.isExpanded(this);
194     XTreeModel model = getModel();
195     XTreeNode node;
196
197     for(int i = getChildCount()-1; i >= 0; i--) {
198       node = (XTreeNode)getChildAt(i);
199       node.tearDown();
200       model.removeNodeFromParent(node);
201     }
202
203     try {
204       m_field.refresh();
205     } catch(Exception JavaDoc e) {
206       // TODO: ask parent to teardown
207
e.printStackTrace();
208     }
209
210     init();
211
212     model.nodeStructureChanged(FieldTreeNode.this);
213     if(expanded) {
214       acc.controller.expand(this);
215     }
216   }
217   
218   private class RefreshAction extends XAbstractAction {
219     private RefreshAction() {
220       super("Refresh", RootsHelper.getHelper().getRefreshIcon());
221       setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0, true));
222     }
223
224     public void actionPerformed(ActionEvent JavaDoc ae) {
225       AdminClientContext acc = AdminClient.getContext();
226       String JavaDoc name = m_field.getName();
227
228       acc.controller.setStatus("Refreshing field " + name + "...");
229       acc.controller.block();
230
231       refresh();
232
233       acc.controller.clearStatus();
234       acc.controller.unblock();
235     }
236   }
237
238   public void nodeClicked(MouseEvent JavaDoc me) {
239     if(m_refreshAction != null) {
240       m_refreshAction.actionPerformed(null);
241     }
242   }
243
244   private class MoreAction extends XAbstractAction {
245     private MoreAction() {
246       super("More");
247     }
248
249     public void actionPerformed(ActionEvent JavaDoc ae) {
250       AdminClientContext acc = AdminClient.getContext();
251       String JavaDoc name = m_field.getName();
252
253       if(incrementDSOBatchSize() == ConnectionContext.DSO_MAX_BATCH_SIZE) {
254         setEnabled(false);
255       }
256       m_lessAction.setEnabled(true);
257       m_field.setBatchSize(m_batchSize);
258       
259       acc.controller.setStatus("Refreshing root " + name + "...");
260       acc.controller.block();
261
262       refresh();
263
264       acc.controller.clearStatus();
265       acc.controller.unblock();
266     }
267   }
268   
269   private class LessAction extends XAbstractAction {
270     private LessAction() {
271       super("Less");
272       setEnabled(false);
273     }
274
275     public void actionPerformed(ActionEvent JavaDoc ae) {
276       AdminClientContext acc = AdminClient.getContext();
277       String JavaDoc name = m_field.getName();
278
279       if(decrementDSOBatchSize() == ConnectionContext.DSO_SMALL_BATCH_SIZE) {
280         setEnabled(false);
281       }
282       m_moreAction.setEnabled(true);
283       m_field.setBatchSize(m_batchSize);
284       
285       acc.controller.setStatus("Refreshing field " + name + "...");
286       acc.controller.block();
287
288       refresh();
289
290       acc.controller.clearStatus();
291       acc.controller.unblock();
292     }
293   }
294   
295   int incrementDSOBatchSize() {
296     switch(m_batchSize) {
297       case ConnectionContext.DSO_SMALL_BATCH_SIZE:
298         m_batchSize = ConnectionContext.DSO_MEDIUM_BATCH_SIZE;
299         break;
300       case ConnectionContext.DSO_MEDIUM_BATCH_SIZE:
301         m_batchSize = ConnectionContext.DSO_LARGE_BATCH_SIZE;
302         break;
303       case ConnectionContext.DSO_LARGE_BATCH_SIZE:
304         m_batchSize = ConnectionContext.DSO_MAX_BATCH_SIZE;
305         break;
306     }
307
308     return m_batchSize;
309   }
310   
311   int decrementDSOBatchSize() {
312     switch(m_batchSize) {
313       case ConnectionContext.DSO_MEDIUM_BATCH_SIZE:
314         m_batchSize = ConnectionContext.DSO_SMALL_BATCH_SIZE;
315         break;
316       case ConnectionContext.DSO_LARGE_BATCH_SIZE:
317         m_batchSize = ConnectionContext.DSO_MEDIUM_BATCH_SIZE;
318         break;
319       case ConnectionContext.DSO_MAX_BATCH_SIZE:
320         m_batchSize = ConnectionContext.DSO_LARGE_BATCH_SIZE;
321         break;
322     }
323
324     return m_batchSize;
325   }
326   
327   public int resetDSOBatchSize() {
328     return m_batchSize = ConnectionContext.DSO_SMALL_BATCH_SIZE;
329   }
330
331   public void tearDown() {
332     super.tearDown();
333
334     m_cc = null;
335     m_field = null;
336   }
337 }
338
Popular Tags