1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.cosylab.gui.components.wheelswitch;
21
22 import java.awt.Component;
23 import java.awt.Container;
24 import java.awt.Dimension;
25 import java.awt.LayoutManager;
26
27 import com.cosylab.gui.components.Wheelswitch;
28
29
30
31
32
33
34
35
36 public class WheelswitchLayout implements LayoutManager {
37
38
39
40
41 public WheelswitchLayout() {
42 super();
43 }
44
45
46
47
48
49
50 public void addLayoutComponent(String name, Component comp) {
51 }
52
53
54
55
56
57
58 public void removeLayoutComponent(Component comp) {
59 }
60
61
62
63
64 public Dimension preferredLayoutSize(Container parent) {
65 return parent.getPreferredSize();
66 }
67
68
69
70
71 public Dimension minimumLayoutSize(Container parent) {
72 return parent.getMinimumSize();
73 }
74
75
76
77
78 public void layoutContainer(Container parent) {
79 Wheelswitch wswitch = (Wheelswitch)parent;
80
81 int numberOfComponents = wswitch.getComponentCount();
82 if (numberOfComponents<2) return;
83
84 int height = wswitch.getHeight();
85 int width = wswitch.getWidth();
86
87 if (width==0 || height==0) return;
88
89 int preferredWidth = wswitch.getPreferredSize().width;
90 int preferredHeight = wswitch.getPreferredSize().height;
91
92 double widthRatio = 1.*width/preferredWidth;
93 double heightRatio = 1.*height/preferredHeight;
94
95 double ratio = Math.min(widthRatio,heightRatio);
96
97 int x = (int)Math.ceil((width-preferredWidth*ratio)/2.);
98 int y = 0;
99 int compWidth = 0;
100 int compHeight = 0;
101 Component comp = null;
102 for (int i=numberOfComponents-1; i>=0; i--) {
103 comp = wswitch.getComponent(i);
104 compHeight = (int)(comp.getPreferredSize().height*ratio);
105 compWidth = (int)(comp.getPreferredSize().width*ratio);
106 y = (int)((height-compHeight)/2.);
107 x += compWidth;
108 comp.setBounds(width-x,y,compWidth,compHeight);
109 }
110 }
111 }