KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > analyzer > layout > ModeLayout


1 /*
2  * ALayout.java
3  *
4  * Created on April 23, 2005, 6:57 PM
5  */

6
7 package analyzer.layout;
8
9 import java.util.*;
10
11 /**
12  *
13  * @author loicsegapelli
14  */

15 public class ModeLayout implements Comparator{
16     
17     protected double verticalWeight,horizontalWeight;
18     public String JavaDoc name,wstcref;
19     public ArrayList paths;
20     
21     /** Creates a new instance of ALayout */
22     public ModeLayout(){
23         paths=new ArrayList();
24         verticalWeight=horizontalWeight=1.0;
25     }
26     
27     public double getLevelWeight(int level){
28         return ((Path)paths.get(level)).weight;
29     }
30     
31     public int getLevelNumber(int level){
32         return ((Path)paths.get(level)).number;
33     }
34     
35     public String JavaDoc getLevelOrientation(int level){
36         return ((Path)paths.get(level)).orientation;
37     }
38     
39     public void addPath(int number,double weight,String JavaDoc orientation){
40         paths.add(new Path(number,weight,orientation));
41     }
42     
43     public int pathSize(){
44         return paths.size();
45     }
46     
47     public int compare(Object JavaDoc o1, Object JavaDoc o2) {
48         ModeLayout l1=(ModeLayout)o1;
49         ModeLayout l2=(ModeLayout)o2;
50         int size=(l1.pathSize()>l2.pathSize()?l2.pathSize():l1.pathSize());
51         int out=0,s1,s2;
52         for(int i=0;i<size;i++){
53             s1=l1.getLevelNumber(i);
54             s2=l2.getLevelNumber(i);
55             s1=s1-s2;
56             if(s1!=0){
57                 out=s1;
58                 return out;
59             }
60         }
61         if(l1.pathSize()>l2.pathSize()){
62             return -l1.getLevelNumber(l2.pathSize());
63         } else if(l1.pathSize()<l2.pathSize()){
64             return l2.getLevelNumber(l1.pathSize());
65         } else{
66             return 0;
67         }
68     }
69     
70     public boolean equals(Object JavaDoc obj) {
71         if(obj.getClass()==ModeLayout.class)return true;
72         else return false;
73     }
74     
75     public String JavaDoc toString(){
76         String JavaDoc out=new String JavaDoc(name).toUpperCase();
77         return wstcref==null?out:out+wstcref;
78     }
79     
80     public void print(){
81         System.out.println(name.toUpperCase());
82         for(int i=0;i<paths.size();i++) System.out.print(":"+((Path)paths.get(i)).toString());
83         System.out.println();
84         System.out.println("vertical w:"+verticalWeight+" horizontal w:"+horizontalWeight);
85     }
86     
87     protected void updateVerticalWeight(double weight){
88         verticalWeight*=weight;
89     }
90     
91     protected void updateHorizontalWeight(double weight){
92         horizontalWeight*=weight;
93     }
94     
95     private class Path{
96         public double weight;
97         public int number;
98         public String JavaDoc orientation;
99         public Path(int myNumber,double myWeight,String JavaDoc myOrientation){
100             orientation=myOrientation;
101             number=myNumber;
102             weight=myWeight;
103             if(orientation.equals("horizontal")) updateHorizontalWeight(myWeight);
104             else updateVerticalWeight(myWeight);
105         }
106         
107         public String JavaDoc toString(){
108             return number+":"+weight+":"+orientation;
109         }
110     }
111 }
112
Popular Tags