VisClient/org/hfbk/vis/visnode/VisRoot.java

Go to the documentation of this file.
00001 /*
00002  * Created on 19.04.2008
00003  *
00004  */
00005 package org.hfbk.vis.visnode;
00006 
00007 import java.io.UnsupportedEncodingException;
00008 import java.net.URLEncoder;
00009 import java.util.HashMap;
00010 import java.util.HashSet;
00011 import java.util.List;
00012 import java.util.concurrent.CopyOnWriteArrayList;
00013 
00014 import org.dronus.graph.Node;
00015 import org.hfbk.vis.Logger;
00016 import org.hfbk.vis.Prefs;
00017 import org.hfbk.vis.VisClient;
00018 import org.hfbk.vis.VisNodeFactory;
00019 import org.hfbk.vis.source.Source;
00020 import org.lwjgl.util.vector.Vector3f;
00021 
00036 public class VisRoot extends VisNode {
00037 
00038         
00040         //public Vector3f epicenter=new Vector3f(20,0,-15);
00041     public Vector3f epicenter=new Vector3f(200,0,-150);
00042         
00043         
00045         List<Source> fetchers = new CopyOnWriteArrayList<Source>();
00046 
00048         public VisClient client;
00049         
00054         HashMap<String,VisNode>sourceNodesBySource=new HashMap<String,VisNode>();
00055         
00060         HashMap<String,VisNode>nodesBySource=new HashMap<String,VisNode>();
00061         
00065         public VisNode lastVisNode;
00066         
00067         public VisRoot(VisClient client) {
00068                 super(null, new Vector3f(0,0,0));
00069                 this.client=client;
00070         }
00071 
00072         void renderSelf() {
00073                 // add incoming nodes, if any.
00074                 for (Source fetcher : fetchers)
00075                         if(fetcher.finished){
00076                                 Node root=fetcher.graph.getRoot();
00077                                 if (root!=null){
00078                                         String url=fetcher.url.toString();
00079                                         VisNode sourceVisNode=sourceNodesBySource.get(url);
00080                                         
00081                                         visualiseTree(root, sourceVisNode);
00082                                         
00083                                         nodesBySource.put(url, lastVisNode);
00084                                         
00085                                         if(lastVisNode!=null){ //should be. maybe remove this one. 
00086                                                 lastVisNode.url=url; //root gets used vis/server url, eg. for additional fetches.
00087                                                 client.setLookAt(lastVisNode.parent.traverse(this, lastVisNode.position));
00088                                         }
00089                                 }
00090                                 fetchers.remove(fetcher);
00091                         }               
00092         }
00093         
00094         
00101         public VisNode create(String type, String keyword) {
00102                 visualiseTree(new Node("0",keyword,type), null);
00103                 client.setViewpoint(lastVisNode.position,1);
00104                 return lastVisNode;
00105         }
00106         
00118         public void fetch(String source, String keyword, VisNode sourceNode) {
00119                 String url=buildQuery(source, keyword);
00120                 Logger.log(Prefs.current.user, "[top, get, " + source + ", " + keyword + ", 0, 0, 0, 0]");                                              
00121                 fetch(url, sourceNode);
00122         }
00123         
00124         void fetch(String url, VisNode sourceNode){
00125                 sourceNodesBySource.put(url, sourceNode);               
00126                 
00127                 VisNode oldNode= nodesBySource.get(url);
00128                 
00129                 if (oldNode!=null)
00130                         client.setViewpoint(oldNode.position,1);
00131                 else{
00132                         Source fetcher=Source.getSource(url);   
00133                         
00134                         fetchers.add(fetcher);
00135                 }
00136         }
00137         
00138         String buildQuery(String source, String keyword){
00139                 try {
00140                         return 
00141                                 Prefs.current.baseURL +
00142                                 "graph.php?action=getgraph&source="+source +
00143                                 "&keyword="+URLEncoder.encode(keyword,"utf8")+                          
00144                                 "&URL="+URLEncoder.encode(keyword,"utf8");
00145                 } catch (UnsupportedEncodingException e) {      throw new RuntimeException(e);  }
00146         } 
00147         
00148         
00166         void visualiseTree(Node root, VisNode sourceVisNode) {
00167                 String t = root.type;
00168                 
00169                 VisNode visRoot=this;           
00170                 Vector3f position;
00171                 if (t.equals("keycloud")||t.equals("imagefield")||t.equals("obj")||t.equals("ical")){
00172                         if (sourceVisNode != null){
00173                                 //this nodes are parented to their source nodes!
00174                                 visRoot= sourceVisNode;
00175                                 visRoot.layoutLocked=true;
00176                                 
00177                                 if (visRoot instanceof VisKeyword) //place inside loose keyword field 
00178                                          position=new Vector3f(visRoot.radius+5,0,0); 
00179                                 else    //place right to whole object 
00180                                         position=new Vector3f(visRoot.getExtends()+5,0,0); 
00181                                 
00182                         }else{
00183                                 //just place inside view
00184                                 position=new Vector3f(client.mouseViewpoint);
00185                                 position.z-=50;
00186                         }       
00187                 }else{ //place on a new ground level
00188                         epicenter.y+=45;
00189                         position=epicenter;
00190                 }
00191                 
00192                 lastVisNode=null;
00193                 visualiseSubtree(root, 1, visRoot, 20,  new HashSet<Node>()) ;
00194                 
00195                 if (lastVisNode!=null) 
00196                         lastVisNode.position.set(position); 
00197         }
00198         
00199         // recurses a graph tree seeking for visualisable nodes
00200         void visualiseSubtree(Node n, int siblings, VisNode parentVisNode, float spread,
00201                         HashSet<Node> nodestack) {
00202                 
00203                 // check & remember node to not do an infinite loop             
00204                 if (nodestack.contains(n))      return;
00205                 nodestack.add(n);
00206                 
00207                 if (n.type.equals("URL"))
00208                         parentVisNode.url=n.text;
00209                 
00210                 // spread around parent. to get a good z order for transparency, we do z by child order.
00211                 int index = parentVisNode.children.size(); // number of this child
00212                 float z = (2 * (index / (float) siblings) - 1) * spread;
00213                 Vector3f position = new Vector3f((float) (Math.random() - .5) * spread,
00214                                 (float) (Math.random() - .5) * spread * .15f, z);
00215         
00216                 //      try to build a visual for this Node. not all Nodes need to be visualisable.
00217                 VisNode vn = VisNodeFactory.create(n, position);
00218                 
00219                 if (vn != null) // we sucessfully build some visual appearance
00220                         parentVisNode.add(vn); // add to parent node            
00221                         
00222                 // recurse childs by edges..
00223                 for (Node to : n.getFromNodes()) 
00224                         visualiseSubtree(to, n.edgeCount(), (vn != null ? vn : parentVisNode), spread/4, nodestack);
00225                 
00226                 if (vn!=null) lastVisNode=vn;
00227                 
00228                 nodestack.remove(n); // allow further instances of the same node if not looped
00229         }
00230 
00231         public void add(VisNode node) {
00232                 children.add(0, node);
00233                 node.parent=this;
00234         }
00235         
00236         void closeSelf() {
00237                 fetchers.clear();
00238                 super.closeSelf();
00239         }
00240 }

Generated on Tue Apr 7 17:57:22 2009 for visclient by  doxygen 1.5.1