View Javadoc

1   /*
2    * ProportionLinePart.java
3    *
4    * Created on 4 december 2002, 17:58
5    */
6   
7   package net.sourceforge.simplegamenet.util.proportionlayout;
8   
9   import java.awt.*;
10  import java.util.ArrayList;
11  
12  /***
13   * @author Geoffrey and Jeroen
14   */
15  class ProportionLinePart implements java.io.Serializable, Comparable {
16  
17      /***
18       *
19       */
20      static final int COLUMN = 0;
21  
22      /***
23       *
24       */
25      static final int ROW = 1;
26  
27      /***
28       * Do not resize the component.
29       */
30      static final int NONE = 0;
31  
32      /***
33       * Resize the component both horizontally and vertically.
34       */
35      static final int FILL = 1;
36  
37      /***
38       * Put the component in the center of its display area.
39       */
40      static final int CENTER = 10;
41  
42      /***
43       * Put the component at the left of its display area, centered horizontally.
44       */
45      static final int LEFT = 11;
46  
47      /***
48       * Put the component at right of its display area.
49       */
50      static final int RIGHT = 12;
51  
52      private int lineType;
53      Component component;
54      private int minimumWidth;
55  
56      int gridX;
57      int gridWidth;
58      int extendGridX;
59  
60      private int anchor;
61      private int fill;
62      private int insetLeft;
63      private int insetRight;
64      private int iPadX;
65  
66      /***
67       * Creates a new instance of ProportionLinePart
68       */
69      ProportionLinePart(int lineType, Component component, int minimumWidth,
70                         int gridX, int gridWidth, int extendGridX, int anchor, int fill,
71                         int insetLeft, int insetRight) {
72          this.lineType = lineType;
73          this.component = component;
74          this.minimumWidth = minimumWidth;
75          this.gridX = gridX;
76          this.gridWidth = gridWidth;
77          this.extendGridX = extendGridX;
78          this.anchor = anchor;
79          this.fill = fill;
80          this.insetLeft = insetLeft;
81          this.insetRight = insetRight;
82      }
83  
84      int getMinimumWidth() {
85          int componentWidth = 0;
86          switch (lineType) {
87              case COLUMN:
88                  componentWidth = component.getMinimumSize().width;
89                  break;
90              case ROW:
91                  componentWidth = component.getMinimumSize().height;
92                  break;
93          }
94          componentWidth += insetLeft + insetRight;
95          if (componentWidth < minimumWidth) {
96              return minimumWidth;
97          } else {
98              return componentWidth;
99          }
100     }
101 
102     int getPreferredWidth() {
103         int componentWidth = 0;
104         switch (lineType) {
105             case COLUMN:
106                 componentWidth = component.getPreferredSize().width;
107                 break;
108             case ROW:
109                 componentWidth = component.getPreferredSize().height;
110                 break;
111         }
112         componentWidth += insetLeft + insetRight;
113         if (componentWidth < minimumWidth) {
114             return minimumWidth;
115         } else {
116             return componentWidth;
117         }
118     }
119 
120     int getX(ArrayList lines, int componentWidth) {
121         int x = ((ProportionLine) lines.get(gridX)).x;
122         if (fill == FILL) {
123             return x + insetLeft;
124         } else {
125             int width = 0;
126             for (int i = gridX; i < gridX + gridWidth; i++) {
127                 width += ((ProportionLine) lines.get(i)).width;
128             }
129             if (componentWidth < minimumWidth) {
130                 componentWidth = minimumWidth;
131             }
132             switch (anchor) {
133                 case LEFT:
134                     return x + insetLeft;
135                 case CENTER:
136                     return x + (width + insetLeft - insetRight - componentWidth) / 2;
137                 case RIGHT:
138                     return x + width - insetRight - componentWidth;
139             }
140             return 0;
141         }
142     }
143 
144     int getWidth(ArrayList lines, int componentWidth) {
145         if (fill == NONE) {
146             if (componentWidth < minimumWidth) {
147                 return minimumWidth;
148             } else {
149                 return componentWidth;
150             }
151         } else {
152             int width = 0;
153             for (int i = gridX; i < gridX + gridWidth; i++) {
154                 width += ((ProportionLine) lines.get(i)).width;
155             }
156             return width;
157         }
158     }
159 
160     public boolean equals(Object object) {
161         try {
162             ProportionLinePart linePart = (ProportionLinePart) object;
163             if (gridX == linePart.gridX && gridWidth == linePart.gridWidth
164                     && component == linePart.component) {
165                 return true;
166             } else {
167                 return false;
168             }
169         } catch (ClassCastException exception) {
170             return false;
171         }
172     }
173 
174     public int compareTo(Object object) {
175         try {
176             ProportionLinePart linePart = (ProportionLinePart) object;
177             if (gridWidth < linePart.gridWidth) {
178                 return -1;
179             } else if (gridWidth > linePart.gridWidth) {
180                 return 1;
181             } else {
182                 if (gridX < linePart.gridX) {
183                     return -1;
184                 } else if (gridX > linePart.gridX) {
185                     return 1;
186                 } else {
187                     if (component == linePart.component) {
188                         return 0;
189                     } else {
190                         return -1;
191                     }
192                 }
193             }
194         } catch (ClassCastException exception) {
195             return -1;
196         }
197     }
198 
199 }