OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 17 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
17 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
182 ilm 4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
17 ilm 5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 /*
15
 * Created on 31 oct. 2004
16
 *
17
 * To change the template for this generated file go to
18
 * Window>Preferences>Java>Code Generation>Code and Comments
19
 */
20
package org.openconcerto.ui;
21
import java.awt.Component;
22
import java.awt.Container;
23
import java.awt.Dimension;
24
import java.awt.FlowLayout;
25
import java.awt.Insets;
26
 
27
/**
28
 * VFlowLayout is similair to FlowLayout except it lays out components
29
 * vertically. Extends FlowLayout because it mimics much of the
30
 * behavior of the FlowLayout class, except vertically. An additional
31
 * feature is that you can specify a fill to edge flag, which causes
32
 * the VFlowLayout manager to resize all components to expand to the
33
 * column width Warning: This causes problems when the main panel
34
 * has less space that it needs and it seems to prohibit multi-column
35
 * output
36
 * @author Larry Schuler
37
 * @version 1.0, 1/1/96
38
 * @author based on FLowLayout by AVH.
39
 */
40
public class VFlowLayout extends FlowLayout {
41
 
42
    public static final int TOP 	= 0;
43
    public static final int MIDDLE 	= 1;
44
    public static final int BOTTOM 	= 2;
45
 
46
    int align;
47
    int hgap;
48
    int vgap;
49
	boolean fill;
50
 
51
    /**
52
     * Construct a new VFlowLayout with a middle alignemnt, and
53
	 * the fill to edge flag set.
54
     */
55
 
56
    public VFlowLayout() {
57
		this(MIDDLE, 0, 0, true);
58
    }
59
 
60
	/**
61
	 * Construct a new VFlowLayout with a middle alignemnt.
62
	 * @param fill the fill to edge flag
63
	 */
64
	public VFlowLayout(boolean fill){
65
		this(MIDDLE, 5, 5, fill);
66
	}
67
 
68
    /**
69
	 * Construct a new VFlowLayout with a middle alignemnt.
70
     * @param align the alignment value
71
     */
72
    public VFlowLayout(int align) {
73
		this(align, 5, 5, true);
74
    }
75
 
76
	/**
77
	 * Construct a new VFlowLayout.
78
	 * @param align the alignment value
79
	 * @param fill the fill to edge flag
80
	 */
81
    public VFlowLayout(int align, boolean fill) {
82
		this(align, 5, 5, fill);
83
    }
84
 
85
    /**
86
	 * Construct a new VFlowLayout.
87
	 * @param align the alignment value
88
	 * @param hgap the horizontal gap variable
89
	 * @param vgap the vertical gap variable
90
	 * @param fill the fill to edge flag
91
     */
92
    public VFlowLayout(int align, int hgap, int vgap, boolean fill) {
93
		this.align = align;
94
		this.hgap = hgap;
95
		this.vgap = vgap;
96
		this.fill = fill;
97
    }
98
 
99
    /**
100
     * Returns the preferred dimensions given the components
101
     * in the target container.
102
     * @param target the component to lay out
103
     */
104
    public Dimension preferredLayoutSize(Container target) {
105
		Dimension tarsiz = new Dimension(0, 0);
106
 
107
		for (int i = 0 ; i < target.getComponentCount(); i++) {
108
	    	Component m = target.getComponent(i);
109
	    	if (m.isVisible()) {
110
				Dimension d = m.getPreferredSize();
111
				tarsiz.width = Math.max(tarsiz.width, d.width);
112
				if (i > 0) {
113
		    		tarsiz.height += hgap;
114
				}
115
				tarsiz.height += d.height;
116
	    	}
117
		}
118
		Insets insets = target.getInsets();
119
		tarsiz.width += insets.left + insets.right + hgap*2;
120
		tarsiz.height += insets.top + insets.bottom + vgap*2;
121
		return tarsiz;
122
    }
123
 
124
    /**
125
     * Returns the minimum size needed to layout the target container
126
     * @param target the component to lay out
127
     */
128
    public Dimension minimumLayoutSize(Container target) {
129
		Dimension tarsiz = new Dimension(0, 0);
130
 
131
		for (int i = 0 ; i < target.getComponentCount() ; i++) {
132
	    	Component m = target.getComponent(i);
133
	    	if (m.isVisible()) {
134
				Dimension d = m.getMinimumSize();
135
				tarsiz.width = Math.max(tarsiz.width, d.width);
136
				if (i > 0) {
137
		    		tarsiz.height += vgap;
138
				}
139
				tarsiz.height += d.height;
140
	    	}
141
		}
142
		Insets insets = target.getInsets();
143
		tarsiz.width += insets.left + insets.right + hgap*2;
144
		tarsiz.height += insets.top + insets.bottom + vgap*2;
145
		return tarsiz;
146
    }
147
 
148
    /**
149
	 * places the components defined by first to last within the target
150
	 * container using the bounds box defined
151
     * @param target the container
152
     * @param x the x coordinate of the area
153
     * @param y the y coordinate of the area
154
     * @param width the width of the area
155
     * @param height the height of the area
156
     * @param first the first component of the container to place
157
     * @param last the last component of the container to place
158
     */
159
    private void placethem(Container target, int x, int y,
160
	                            int width, int height,
161
								int first, int last) {
162
 
163
		if ( align == VFlowLayout.MIDDLE )
164
			y += height / 2;
165
		if ( align == VFlowLayout.BOTTOM )
166
			y += height;
167
 
168
		for (int i = first ; i < last ; i++) {
169
	    	Component m = target.getComponent(i);
170
			Dimension md = m.getSize();
171
	    	if (m.isVisible()) {
172
				int px = x + (width-md.width)/2;
173
				m.setLocation(x + (width-md.width)/2, y );
174
				y += vgap + md.height;
175
	    	}
176
		}
177
    }
178
 
179
    /**
180
     * Lays out the container.
181
     * @param target the container to lay out.
182
     */
183
    public void layoutContainer(Container target) {
184
		Insets insets = target.getInsets();
185
		int maxheight = target.getSize().height - (insets.top + insets.bottom + vgap*2);
186
		int maxwidth = target.getSize().width - (insets.left + insets.right + hgap*2);
187
		int numcomp = target.getComponentCount();
188
		int x = insets.left + hgap, y = 0;
189
		int colw = 0, start = 0;
190
 
191
		for (int i = 0 ; i < numcomp ; i++) {
192
	    	Component m = target.getComponent(i);
193
	    	if (m.isVisible()) {
194
				Dimension d = m.getPreferredSize();
195
				if ( this.fill ){
196
					m.setSize(maxwidth, d.height);
197
					d.width=maxwidth;
198
				} else{
199
					m.setSize(d.width, d.height);
200
				}
201
 
202
				if ( y > maxheight ){
203
					placethem(target, x, insets.top + vgap,
204
								   colw, maxheight- y, start, i);
205
					y = d.height;
206
					x += hgap + colw;
207
					colw = d.width;
208
					start = i;
209
				}else{
210
					if ( y > 0 ) y+= vgap;
211
					y += d.height;
212
					colw = Math.max(colw, d.width);
213
				}
214
			}
215
		}
216
		placethem(target, x, insets.top + vgap, colw, maxheight - y, start, numcomp);
217
    }
218
}