summaryrefslogtreecommitdiff
path: root/NET/worlds/console/QuantizedStackedLayout.java
blob: 98f12f08b5ca17140c1dbea26b5c3386a271d761 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package NET.worlds.console;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;

class QuantizedStackedLayout extends StackedLayout {
   ColorFiller m_filler;

   public QuantizedStackedLayout(ColorFiller filler) {
      this.m_filler = filler;
   }

   @Override
   public void layoutContainer(Container target) {
      int count = target.getComponentCount();
      Insets insets = target.getInsets();
      Dimension size = target.getSize();
      int width = size.width - insets.left - insets.right;
      int height = size.height - insets.top - insets.bottom;
      int fillerHeight = 0;

      for (int i = 0; i < count; i++) {
         Component c = target.getComponent(i);
         if (c != this.m_filler) {
            int h = c.getPreferredSize().height;
            if (i == count - 1) {
               h = Math.max(h, height);
            } else {
               h = Math.min(h, height);
            }

            h = Math.max(0, h);
            height -= h;
            if (c instanceof QuantizedCanvas) {
               QuantizedCanvas qc = (QuantizedCanvas)c;
               fillerHeight += qc.getRemainder(h);
            }
         }
      }

      this.m_filler.setHeight(fillerHeight);
      int x = insets.left;
      int y = insets.top;
      height = size.height - insets.top - insets.bottom;

      for (int ix = 0; ix < count; ix++) {
         Component c = target.getComponent(ix);
         int hx = c.getPreferredSize().height;
         if (ix == count - 1) {
            hx = Math.max(hx, height);
         } else {
            hx = Math.min(hx, height);
         }

         hx = Math.max(0, hx);
         c.setBounds(x, y, width, hx);
         height -= hx;
         y += hx;
      }
   }
}