KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > piagetproject > layout > Layout


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

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

15 public class Layout 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 Layout(){
23         paths = new ArrayList();
24         verticalWeight = horizontalWeight = 1.0;
25     }
26     
27     public double getLevelWeight(int level){
28         if(level >= paths.size()) {
29             System.out.println(name+":impossible to satisfy request level:"+level);
30             return 0;
31         }
32         return ((Path)paths.get(level)).weight;
33     }
34     
35     public int getLevelNumber(int level){
36         if(level >= paths.size()) {
37             System.out.println(name+":impossible to satisfy request level:"+level);
38             return 0;
39         }
40         return ((Path)paths.get(level)).number;
41     }
42     
43     public String JavaDoc getLevelOrientation(int level){
44         if(level >= paths.size()) {
45             System.out.println(name+":impossible to satisfy request level:"+level);
46             return Path.HORIZONTAL;
47         }
48         return ((Path)paths.get(level)).orientation;
49     }
50     
51     public void addPath(int number,double weight,String JavaDoc orientation){
52         paths.add(new Path(number,weight,orientation));
53     }
54     
55     public int pathSize(){
56         return paths.size();
57     }
58     
59     public int compare(Object JavaDoc o1, Object JavaDoc o2) {
60         Layout l1=(Layout)o1;
61         Layout l2=(Layout)o2;
62         int size=(l1.pathSize()>l2.pathSize()?l2.pathSize():l1.pathSize());
63         int out=0,s1,s2;
64         for(int i=0;i<size;i++){
65             s1=l1.getLevelNumber(i);
66             s2=l2.getLevelNumber(i);
67             s1=s1-s2;
68             if(s1!=0){
69                 out=s1;
70                 return out;
71             }
72         }
73         if(l1.pathSize()>l2.pathSize()){
74             return -l1.getLevelNumber(l2.pathSize());
75         } else if(l1.pathSize()<l2.pathSize()){
76             return l2.getLevelNumber(l1.pathSize());
77         } else{
78             return 0;
79         }
80     }
81     
82     public boolean equals(Object JavaDoc obj) {
83         if(obj.getClass()==Layout.class)return true;
84         else return false;
85     }
86     
87     public String JavaDoc toString(){
88         String JavaDoc out=new String JavaDoc(name).toUpperCase();
89         return wstcref==null?out:out+wstcref;
90     }
91     
92     public void print(){
93         System.out.println(name.toUpperCase());
94         for(int i=0;i<paths.size();i++) System.out.print(":"+((Path)paths.get(i)).toString());
95         System.out.println("\nvertical w:"+verticalWeight+" horizontal w:"+horizontalWeight);
96     }
97     
98     protected void updateVerticalWeight(double weight){
99         verticalWeight*=weight;
100     }
101     
102     protected void updateHorizontalWeight(double weight){
103         horizontalWeight*=weight;
104     }
105     
106     private class Path{
107         public final static String JavaDoc HORIZONTAL = "horizontal";
108         public double weight;
109         public int number;
110         public String JavaDoc orientation;
111         public Path(int myNumber, double myWeight, String JavaDoc myOrientation){
112             orientation = myOrientation;
113             number = myNumber;
114             weight = myWeight;
115             if(orientation.equals(HORIZONTAL)) updateHorizontalWeight(myWeight);
116             else updateVerticalWeight(myWeight);
117         }
118         
119         public String JavaDoc toString(){
120             return number+":"+weight+":"+orientation;
121         }
122     }
123 }
124
Popular Tags