View Javadoc

1   package org.catacomb.druid.gui.edit;
2   
3   
4   import org.catacomb.be.StringIdentifiable;
5   import org.catacomb.druid.event.LabelActor;
6   import org.catacomb.druid.gui.base.DummyTree;
7   import org.catacomb.druid.swing.DScrollPane;
8   import org.catacomb.druid.swing.DTree;
9   import org.catacomb.druid.util.tree.RelationNode;
10  import org.catacomb.interlish.structure.*;
11  import org.catacomb.report.E;
12  
13  
14  import java.awt.Color;
15  import java.util.ArrayList;
16  
17  
18  public class DruTreePanel extends DruGCPanel implements TreeDisplay,
19      TreeChangeReporter, LabelActor, SelectionActor,
20      TreeExplorer, SelectionSource {
21  
22      static final long serialVersionUID = 1001;
23  
24      DTree dTree;
25      DruChoice pivotChoice;
26      DScrollPane scrollPane;
27  
28      Tree tree;
29  
30      SelectionActor selectionActor;
31      boolean dropEvents;
32  
33      private TreeProvider treeProvider;
34  
35      private String selectionType;
36  
37      private String pathToOpen;
38      private boolean pathRequired;
39  
40  
41      private String lastSelection;
42  
43  
44      private boolean defaultRootVisibility;
45  
46  
47      public DruTreePanel() {
48          super();
49  
50          dTree = new DTree();
51          scrollPane = new DScrollPane(dTree);
52          setSingle();
53          addDComponent(scrollPane);
54          scrollPane.setScrollSize(180, 240);
55          selectionType = SpecialStrings.NONE_STRING;
56          dTree.setSelectionActor(this);
57          setTree(new DummyTree());
58      }
59  
60  
61      public void setRootVisibility(boolean b) {
62          defaultRootVisibility = b;
63      }
64  
65      public void setBg(Color c) {
66          dTree.setBg(c);
67          super.setBg(c);
68      }
69  
70  
71      public void clear() {
72          tree = null;
73          dTree.clear();
74      }
75  
76  
77      public void setTree(Tree tr) {
78          if (tr != null) {
79              tree = tr;
80              tree.setTreeChangeReporter(this);
81              dTree.setTree(tree);
82  
83              if (pivotChoice != null && tree instanceof PivotedTree) {
84                  pivotChoice.setOptions(((PivotedTree)tree).getPivotNames());
85              }
86  
87              int ipol = tree.getRootPolicy();
88              if (ipol == Tree.HIDE_ROOT) {
89                  dTree.setRootVisible(false);
90  
91              } else if (ipol == Tree.SHOW_ROOT) {
92                  dTree.setRootVisible(true);
93  
94              } else {
95                  dTree.setRootVisible(defaultRootVisibility);
96              }
97          }
98  
99          if (pathToOpen != null) {
100             ensureVisible(pathToOpen, pathRequired);
101             pathToOpen = null;
102         }
103     }
104 
105 
106     public void setPivotChoice(DruChoice pc) {
107         pivotChoice = pc;
108 
109         E.missing("should action connect pivot choice");
110 //      pivotChoice.setLabelActor(this);
111         E.missing("cant add to toolbar...");
112         // addToToolbar(pivotChoice);
113     }
114 
115 
116     public void labelAction(String s, boolean b) {
117         // for the repivot actions;
118         if (tree instanceof PivotedTree) {
119             ((PivotedTree)tree).repivot(s);
120             treeModified();
121         }
122     }
123 
124 
125     public void setSelected(String s) {
126         //  ensureVisible(s);
127         checkIsSelected(s);
128     }
129 
130     public void selectionAction(Object oselin, String sidin) {
131         Object osel = oselin;
132         String sid = sidin;
133         if (osel == null) {
134             return; // POSERR;
135         }
136 
137         if (osel instanceof TreeNode) {
138             if (((TreeNode)osel).isLeaf()) {
139                 selectionType = "leaf";
140             } else {
141                 selectionType = "branch";
142             }
143         } else {
144             E.warning("non tree node in tree selection " + osel);
145             selectionType = SpecialStrings.NONE_STRING;
146         }
147 
148         if (dropEvents) {
149 
150         } else {
151             if (osel instanceof RelationNode) {
152                 RelationNode rnode = (RelationNode)osel;
153                 osel = rnode.getPeer();
154             }
155             if (selectionActor == null) {
156                 if (osel instanceof StringIdentifiable) {
157                     sid = ((StringIdentifiable)osel).getStringIdentifier();
158 
159                 } else if (osel instanceof TreeNode) {
160                     sid = getSlashPath((TreeNode)osel);
161                 }
162 
163                 lastSelection = sid;
164 
165                 valueChange(sid);
166             } else {
167                 selectionActor.selectionAction(osel, sid);
168             }
169         }
170     }
171 
172 
173 
174 
175     private String getSlashPath(TreeNode tn) {
176         String ret = tn.toString();
177         Object p = tn.getParent();
178         if (p instanceof TreeNode) {
179             ret = getSlashPath((TreeNode)p) + "/" + ret;
180         }
181         return ret;
182     }
183 
184 
185 
186     public void setSelectionActor(SelectionActor sact) {
187         selectionActor = sact;
188     }
189 
190 
191     public void treeModified() {
192         dropEvents = true;
193         dTree.treeModified();
194         //dTree.setTree(tree);
195         dropEvents = false;
196     }
197 
198 
199 
200     public void showNewItem(Object[] pathToChild) {
201         dropEvents = true;
202         dTree.treeModified();
203         //dTree.setTree(tree);
204 
205         // dumpptc(pathToChild);
206 
207         dTree.dTreeExpandPath(pathToChild);
208         dropEvents = false;
209     }
210 
211 
212     @SuppressWarnings("unused")
213     private void dumpptc(Object[] oa) {
214         for (int i = 0; i < oa.length; i++) {
215             Object obj = oa[i];
216             E.info("item " + i + "   " + obj + " " + obj.getClass().getName());
217         }
218     }
219 
220 
221 
222     public void setTreeProvider(TreeProvider provider) {
223         treeProvider = provider;
224         treeProvider.setTreeExplorer(this);
225 
226 
227     }
228 
229 
230 
231     public void setMenu(DruMenu drum) {
232         dTree.setMenu(drum.getGUIPeer());
233 
234         // druMenu = drum;
235     }
236 
237 
238 
239     public String getSelectionType() {
240         return selectionType;
241     }
242 
243     public void enableDrag() {
244         dTree.enableDrag();
245 
246     }
247     private Object[] pathTo(TreeNode tn) {
248         ArrayList<Object> al = new ArrayList<Object>();
249 
250         al.add(tn);
251 
252         Object obj = tn.getParent();
253         while (obj != null) {
254             al.add(0, obj);
255             if (obj instanceof TreeNode) {
256                 obj = ((TreeNode)obj).getParent();
257             } else {
258                 obj = null;
259             }
260         }
261         return al.toArray();
262     }
263 
264 
265     public void nodeAddedUnder(TreeNode parent, TreeNode child) {
266         //    E.info("dru tree should be expanding path to " + parent);
267         showNewItem(pathTo(parent));
268     }
269 
270 
271 
272     public void nodeRemoved(TreeNode parent, TreeNode child) {
273         showNewItem(pathTo(parent));
274     }
275 
276     public void ensureVisible(String sfc) {
277         ensureVisible(sfc, true);
278     }
279 
280 
281     public void expandPath(Object[] oa) {
282         dTree.dTreeExpandPath(oa);
283     }
284 
285 
286     public void ensureVisible(String sfc, boolean breq) {
287         Object[] oa = tree.getObjectPath(sfc, breq);
288         if (oa != null) {
289             Object[] oap = new Object[oa.length-1];
290             for (int i = 0; i < oap.length; i++) {
291                 oap[i] = oa[i];
292             }
293             dTree.dTreeExpandPath(oap);
294 
295         } else {
296             if (breq) {
297                 E.shortWarning("cant match " + sfc + " to a tree path in " + tree);
298             }
299         }
300     }
301 
302     public void setPathToOpen(String sfc) {
303         setPathToOpen(sfc, true);
304     }
305 
306     public void setPathToOpen(String sfc, boolean breq) {
307         pathToOpen = sfc;
308         pathRequired = breq;
309     }
310 
311 
312     public void checkIsSelected(String pth) {
313         dropEvents = true;
314 
315         if (pth != null && !(pth.equals(lastSelection))) {
316             //    E.info("Time to update selection in tree from " + lastSelection + " to " + pth);
317             lastSelection = pth;
318             Object[] oa = tree.getObjectPath(pth, true);
319             if (oa == null) {
320                 E.warning("null object path for " + pth);
321             } else {
322                 dTree.setSelected(oa);
323             }
324         }
325 
326         dropEvents = false;
327     }
328 
329 
330     public String getSelection() {
331         return lastSelection;
332     }
333 
334 
335 }