Source code: org/modama/tools/ParameterLayout.java
1 /*
2 * Created by IntelliJ IDEA.
3 * User: Administrator
4 * Date: 16.08.2002
5 * Time: 00:25:42
6 * To change template for new class use
7 * Code Style | Class Templates options (Tools | IDE Options).
8 */
9 package org.modama.tools;
10
11 import javax.swing.*;
12 import java.awt.*;
13
14 /**
15 * Einfacher Layoutmanager, stellt Components untereinander dar,
16 * zu jedem Field sollte ein String übergeben werden, welcher als
17 * Label vor dem Field angezeigt wird
18 *
19 * die groese der rechten und linken seite kann angegeben werden, oder
20 * automatisch bestimmt werden (0 oder keine parameter uebergeben)
21 *
22 */
23 public class ParameterLayout implements LayoutManager2
24 {
25
26 private int left; // breite der labels
27 private int right; // breite der textfields
28 private float ratio; // left / (left + right)
29 // private int textfieldheight; // höhe eines textfields
30 private Container parent;
31 private boolean autosizeright; // wenn true, automatisch groesse einstellen
32 private boolean autosizeleft; // wenn true, automatisch groesse einstellen
33 /** the height of all the components */
34 private int heightofallcomps;
35
36 public ParameterLayout( Container parent ) {
37 this( parent, 0, 0 );
38 }
39
40 public ParameterLayout( Container parent, int left, int right) {
41 if( left != 0 ) autosizeleft = false;
42 else autosizeleft = true;
43 if( right != 0 ) autosizeright = false;
44 else{
45 autosizeright = true;
46 right = 150;
47 }
48 this.left = left;
49 this.right = right;
50 this.parent = parent;
51 ratio = (float)left / (left + right);
52 heightofallcomps = 0;
53 // textfieldheight = 20;
54 }
55
56 public void addLayoutComponent(String name, Component comp) {
57 }
58
59 public void removeLayoutComponent(Component comp) {
60 }
61
62 public Dimension preferredLayoutSize(Container parent) {
63 return getLayoutSize( parent );
64 }
65
66 public Dimension minimumLayoutSize(Container parent) {
67 return getLayoutSize( parent );
68 }
69
70 protected Dimension getLayoutSize( Container parent ) {
71 Insets insets = parent.getInsets();
72 return new Dimension( left + right + insets.left + insets.right, insets.top + insets.bottom + heightofallcomps );
73 }
74
75 public void layoutContainer(Container parent) {
76 synchronized (parent.getTreeLock()) {
77 Insets insets = parent.getInsets();
78 int ncomponents = parent.getComponentCount();
79
80 if (ncomponents == 0) {
81 return;
82 }
83
84 // Total parent dimensions
85 Dimension size = parent.getSize();
86 int totalW = size.width - (insets.left + insets.right);
87 int totalH = size.height - (insets.top + insets.bottom);
88
89 // tatsächliche aufteilung
90 //int left = (int)(ratio * size.width);
91 //int right = size.width - left;
92 int left = this.left;
93 int right = totalW - left;
94
95 int act_y_pos = 0;
96 int act_height = 20;
97 for ( int i = 0; i < ncomponents; i++ )
98 {
99 Component c = parent.getComponent(i);
100 int height = c.getPreferredSize().height;
101 if( height > act_height ) act_height = height;
102 //if( c instanceof JLabel ) {
103 if( (i & 1) == 1 )
104 {
105 c.setBounds( insets.left, insets.top + act_y_pos, left, act_height );
106 act_y_pos += act_height;
107 act_height = 20;
108 }
109 else
110 {
111 //if( c instanceof JTextField ) {
112 c.setBounds( insets.left + left, insets.top + act_y_pos, right, act_height );
113 }
114 }
115 // update this value
116 heightofallcomps = act_y_pos;
117 }
118 }
119
120 public void addLayoutComponent(Component comp, Object constraints) {
121 if (constraints instanceof String) {
122 // label erzeugen
123 JLabel label = new JLabel( " "+(String)constraints );
124 Dimension label_preferredSize = label.getPreferredSize();
125 Dimension comp_preferredSize = comp.getPreferredSize();
126 // add the height
127 heightofallcomps += Math.max( label_preferredSize.height, comp_preferredSize.height );
128 if( autosizeleft ){
129 // testen ob linker teil gross genug ist, das label ganz reinpasst
130 int labelsize = (int)label_preferredSize.getWidth();
131 // wenn zu klein, vergroesern
132 if( labelsize > left ) left = labelsize;
133 }
134 if( autosizeright ){
135 // testen ob linker teil gross genug ist, das label ganz reinpasst
136 int size = (int)comp.getPreferredSize().getWidth();
137 // wenn zu klein, vergroesern
138 if( size > right ) right = size;
139 }
140 parent.add( label );
141 } else if (constraints != null) {
142 throw new IllegalArgumentException(
143 "cannot add to layout: constraint must be a String");
144 }
145 }
146
147 /**
148 * Returns the maximum size of this component.
149 * @see java.awt.Component#getMinimumSize()
150 * @see java.awt.Component#getPreferredSize()
151 * @see LayoutManager
152 */
153 public Dimension maximumLayoutSize(Container target) {
154 return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
155 }
156
157 /**
158 * Returns the alignment along the x axis. This specifies how
159 * the component would like to be aligned relative to other
160 * components. The value should be a number between 0 and 1
161 * where 0 represents alignment along the origin, 1 is aligned
162 * the furthest away from the origin, 0.5 is centered, etc.
163 */
164 public float getLayoutAlignmentX(Container target) {
165 return 0.5f;
166 }
167
168 /**
169 * Returns the alignment along the y axis. This specifies how
170 * the component would like to be aligned relative to other
171 * components. The value should be a number between 0 and 1
172 * where 0 represents alignment along the origin, 1 is aligned
173 * the furthest away from the origin, 0.5 is centered, etc.
174 */
175 public float getLayoutAlignmentY(Container target) {
176 return 0.5f;
177 }
178
179 /**
180 * Invalidates the layout, indicating that if the layout manager
181 * has cached information it should be discarded.
182 */
183 public void invalidateLayout(Container target) {
184 // Do nothing
185 }
186
187 }