View Javadoc

1   /*
2    * ProportionLine.java
3    *
4    * Created on 2 december 2002, 16:47
5    */
6   
7   package net.sourceforge.simplegamenet.util.proportionlayout;
8   
9   import java.util.ArrayList;
10  import java.util.Iterator;
11  import java.util.TreeSet;
12  
13  /***
14   * @author Geoffrey and Jeroen
15   */
16  class ProportionLine implements java.io.Serializable {
17  
18      static int arrangeLines(ArrayList lines, TreeSet lineParts, int sizeType) {
19          // Reset all lines
20          for (Iterator iterator = lines.iterator(); iterator.hasNext();) {
21              ProportionLine line = (ProportionLine) iterator.next();
22              switch (sizeType) {
23                  case ProportionLayout.MINIMUM_SIZE:
24                      line.minimumWidth = line.absoluteMinimumWidth;
25                      break;
26                  case ProportionLayout.PREFERRED_SIZE:
27                      line.preferredWidth = line.absoluteMinimumWidth;
28                      break;
29              }
30          }
31          // Adjust all lines for each component
32          for (Iterator iterator = lineParts.iterator(); iterator.hasNext();) {
33              ProportionLinePart part = (ProportionLinePart) iterator.next();
34              int restWidth = 0;
35              switch (sizeType) {
36                  case ProportionLayout.MINIMUM_SIZE:
37                      restWidth = part.getMinimumWidth();
38                      break;
39                  case ProportionLayout.PREFERRED_SIZE:
40                      restWidth = part.getPreferredWidth();
41                      break;
42              }
43              for (int i = part.gridX; i < part.gridX + part.gridWidth; i++) {
44                  ProportionLine line = (ProportionLine) lines.get(i);
45                  switch (sizeType) {
46                      case ProportionLayout.MINIMUM_SIZE:
47                          restWidth -= line.minimumWidth;
48                          break;
49                      case ProportionLayout.PREFERRED_SIZE:
50                          restWidth -= line.preferredWidth;
51                          break;
52                  }
53              }
54              if (restWidth > 0) {
55                  ProportionLine line = (ProportionLine) lines.get(part.extendGridX);
56                  switch (sizeType) {
57                      case ProportionLayout.MINIMUM_SIZE:
58                          line.minimumWidth += restWidth;
59                          break;
60                      case ProportionLayout.PREFERRED_SIZE:
61                          line.preferredWidth += restWidth;
62                          break;
63                  }
64                  if (line.proportion != ProportionLayout.NO_PROPORTION) {
65                      for (Iterator iterator2 = lines.iterator(); iterator2.hasNext();) {
66                          ProportionLine line2 = (ProportionLine) iterator2.next();
67                          if (line != line2
68                                  && line2.proportion != ProportionLayout.NO_PROPORTION) {
69                              switch (sizeType) {
70                                  case ProportionLayout.MINIMUM_SIZE:
71                                      line2.minimumWidth = (int) (line.minimumWidth
72                                              * line2.proportion
73                                              / line.proportion);
74                                      break;
75                                  case ProportionLayout.PREFERRED_SIZE:
76                                      line2.preferredWidth = (int) (line.preferredWidth
77                                              * line2.proportion
78                                              / line.proportion);
79                                      break;
80                              }
81                          }
82                      }
83                  }
84              }
85          }
86          int returnWidth = 0;
87          // Add the width of all lines
88          for (Iterator iterator = lines.iterator(); iterator.hasNext();) {
89              switch (sizeType) {
90                  case ProportionLayout.MINIMUM_SIZE:
91                      returnWidth += ((ProportionLine) iterator.next()).minimumWidth;
92                      break;
93                  case ProportionLayout.PREFERRED_SIZE:
94                      returnWidth += ((ProportionLine) iterator.next()).preferredWidth;
95                      break;
96              }
97          }
98          return returnWidth;
99      }
100 
101     static void layoutLines(ArrayList lines, int lineX, int extraWidth, int typeGap,
102                             double lineProportionTotal, int sizeType) {
103         for (Iterator iterator = lines.iterator(); iterator.hasNext();) {
104             ProportionLine line = (ProportionLine) iterator.next();
105             line.x = lineX;
106             int lineWidth = 0;
107             switch (sizeType) {
108                 case ProportionLayout.MINIMUM_SIZE:
109                     lineWidth = line.minimumWidth;
110                     if (extraWidth > 0) {
111                         lineWidth += (int) ((double) (line.preferredWidth - line.minimumWidth) *
112                                 (double) extraWidth / (double) typeGap);
113                     }
114                     break;
115                 case ProportionLayout.PREFERRED_SIZE:
116                     lineWidth = line.preferredWidth;
117                     if (line.proportion != ProportionLayout.NO_PROPORTION) {
118                         lineWidth += (int) (extraWidth * line.proportion / lineProportionTotal);
119                     }
120                     break;
121             }
122             line.width = lineWidth;
123             lineX += lineWidth;
124         }
125     }
126 
127     int absoluteMinimumWidth;
128     double proportion;
129 
130     int minimumWidth = 0;
131     int preferredWidth = 0;
132 
133     int x = 0;
134     int width = 0;
135 
136     /***
137      * Creates a new instance of ProportionLine
138      */
139     ProportionLine(int absoluteMinimumWidth, double proportion)
140             throws IllegalArgumentException {
141         if (absoluteMinimumWidth < 0) {
142             throw new IllegalArgumentException("absoluteMinimumWidth should be postive");
143         }
144         this.absoluteMinimumWidth = absoluteMinimumWidth;
145         this.proportion = proportion;
146     }
147 
148 }