1 6 7 package analyzer.layout; 8 9 import java.util.*; 10 import org.netbeans.swing.gridsplit.*; 11 import java.awt.Component ; 12 13 17 public class LayoutTree { 18 19 LayoutTree[] sons; 20 static TreeSet treeSet; 21 int level; 22 ModeLayout leafLayout; 23 String orientation; 24 public double splitValue; 25 private static Test t; 26 27 28 public LayoutTree(ArrayList layoutList,Test test) { 29 t=test; 30 ArrayList sorted=sort(layoutList); 31 process(sorted,0,-1); 32 } 33 34 private LayoutTree(ArrayList layoutList,int myLevel,double split){ 35 process(layoutList,myLevel,split); 36 } 37 38 private void process(ArrayList layoutList,int myLevel,double mySplitValue){ 39 splitValue=mySplitValue; 41 level=myLevel; 42 if(layoutList.size()==1){ 43 leafLayout=(ModeLayout)layoutList.get(0); 44 return; 46 } 47 orientation=((ModeLayout)layoutList.get(0)).getLevelOrientation(level); 48 ArrayList[] arranged=arrange(layoutList,level); 49 sons=new LayoutTree[arranged.length]; 50 double w; 51 for(int i=0;i<sons.length;i++) { 52 w=((ModeLayout)arranged[i].get(0)).getLevelWeight(level); 53 sons[i]=new LayoutTree(arranged[i],level+1,w); 54 } 55 } 56 57 public void splitComponent(Component c){ 58 if(leafLayout!=null){ 59 ((TestComponent)c).setID(leafLayout.toString()); 60 return; 61 } 62 for(int i=0;i<sons.length;i++){ 63 Component comp; 64 if(i==sons.length-1) comp=c; 65 else comp=t.addComponent(c,orientation,sons[i].splitValue); 66 sons[i].splitComponent(comp); 67 } 68 } 69 70 static public void printTree(){ 71 if(treeSet==null){ 72 System.out.println("no tree"); 73 return; 74 } 75 Iterator it=treeSet.iterator(); 76 while(it.hasNext()) ((ModeLayout)it.next()).print(); 77 } 78 79 83 84 static private ArrayList sort(ArrayList layoutList){ 85 treeSet=new TreeSet((Comparator)layoutList.get(0)); 86 for(int i=0;i<layoutList.size();i++){ 87 treeSet.add(layoutList.get(i)); 88 } 89 Iterator it=treeSet.iterator(); 90 ArrayList sorted=new ArrayList(); 91 while(it.hasNext()) sorted.add(it.next()); 92 return sorted; 93 } 94 95 static private ArrayList[] arrange(ArrayList sortedList,int level){ 96 ModeLayout l; 97 ArrayList[] tempList=new ArrayList[sortedList.size()]; 98 int past=-1,current=-1,index=-1; 99 for(int i=0;i<tempList.length;i++){ 100 l=(ModeLayout)sortedList.get(i); 101 current=l.getLevelNumber(level); 102 if(current!=past){ 103 index++; 104 tempList[index]=new ArrayList(); 105 } 106 tempList[index].add((Object )l); 107 past=current; 108 } 109 ArrayList[] out=new ArrayList[index+1]; 110 for(int i=0;i<index+1;i++) out[i]=tempList[i]; 111 return out; 112 } 113 114 } 115 | Popular Tags |