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