summaryrefslogtreecommitdiff
path: root/NET/worlds/console/StackedLayout.java
blob: 2cbc840821f8c4fe074bd98fcb369f990f923a72 (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
64
65
66
67
68
69
package NET.worlds.console;

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

class StackedLayout implements LayoutManager {
   public StackedLayout() {
   }

   @Override
   public void addLayoutComponent(String name, Component c) {
   }

   @Override
   public void removeLayoutComponent(Component c) {
   }

   @Override
   public Dimension preferredLayoutSize(Container target) {
      Dimension size = new Dimension(0, 0);
      int count = target.getComponentCount();

      for (int i = 0; i < count; i++) {
         Component m = target.getComponent(i);
         Dimension d = m.getPreferredSize();
         size.width = Math.max(size.width, d.width);
         size.height = size.height + d.height;
      }

      Insets insets = target.getInsets();
      size.width = size.width + insets.left + insets.right;
      size.height = size.height + insets.top + insets.bottom;
      return size;
   }

   @Override
   public Dimension minimumLayoutSize(Container target) {
      return this.preferredLayoutSize(target);
   }

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

      for (int i = 0; i < count; i++) {
         Component c = target.getComponent(i);
         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);
         c.setBounds(x, y, width, h);
         height -= h;
         y += h;
      }
   }
}