KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > apps > ALayout


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.apps;
15
16 import java.awt.*;
17 import java.util.*;
18
19 import org.compiere.util.Log;
20
21 /**
22  * Application Layout Manager.
23  * <code>
24  * panel.setLayout(new ALayout());
25  * panel.add(field11, new ALayoutConstraint(0,0));
26  * panel.add(field12, null);
27  * panel.add(field13, null);
28  * panel.add(field14, null);
29  * parameterPanel.add(field21, new ALayoutConstraint(1,0));
30  * </code>
31  *
32  * @author Jorg Janke
33  * @version $Id: ALayout.java,v 1.7 2003/10/01 21:36:33 jjanke Exp $
34  */

35 public class ALayout implements LayoutManager2
36 {
37     /**
38      * Default Constructor with spacing of 2 and columns filled
39      */

40     public ALayout()
41     {
42         this (2,4, true);
43     } // ALayout
44

45     /**
46      * Detail Contructor
47      * @param spaceH horizontal space (top, between rows, button)
48      * @param spaceV vertical space (left, between columns, right)
49      * @param colFill fields are fully filled (rather then preferred size)
50      */

51     public ALayout(int spaceH, int spaceV, boolean colFill)
52     {
53         setSpaceH(spaceH);
54         setSpaceV(spaceV);
55         m_colFill = colFill;
56     } // ALayout
57

58     /** Data Storage */
59     private ALayoutCollection m_data = new ALayoutCollection();
60     /** Horizontal Space */
61     private int m_spaceH;
62     /** Vertical Space */
63     private int m_spaceV;
64     /** Column Fill */
65     private boolean m_colFill;
66
67     /**
68      * Add To Layout with NULL constraint
69      *
70      * @param name the string to be associated with the component - ignored
71      * @param comp the component to be added
72      */

73     public void addLayoutComponent(String JavaDoc name, Component comp)
74     {
75         addLayoutComponent (comp, null);
76     } // addLayoutComponent
77

78     /**
79      * Adds the specified component to the layout, using the specified
80      * constraint object. If the constraint is not a ALayoutConstraint
81      * the component is added with a NULL constraint.
82      * <p>
83      * Components with a NULL constraint are added as the next column to the last row
84      *
85      * @param component the component to be added
86      * @param constraint where/how the component is added to the layout.
87      * @see ALayoutConstraint
88      */

89     public void addLayoutComponent(Component component, Object JavaDoc constraint)
90     {
91         ALayoutConstraint con = null;
92         if (constraint instanceof ALayoutConstraint)
93             con = (ALayoutConstraint)constraint;
94         //
95
m_data.put(con, component);
96     } // addLayoutComponent
97

98     /**
99      * Removes the specified component from the layout.
100      * @param comp the component to be removed
101      */

102     public void removeLayoutComponent(Component comp)
103     {
104         if (!m_data.containsValue(comp))
105             return;
106         Iterator it = m_data.keySet().iterator();
107         while (it.hasNext())
108         {
109             Object JavaDoc key = it.next();
110             if (m_data.get(key).equals(comp))
111             {
112                 m_data.remove(key);
113                 return;
114             }
115         }
116     } // removeLayoutComponent
117

118     /**
119      * Calculates the preferred size dimensions for the specified
120      * container, given the components it contains.
121      * @param parent the container to be laid out
122      * @return Size
123      * @see #minimumLayoutSize
124      */

125     public Dimension preferredLayoutSize(Container parent)
126     {
127         return calculateLayoutSize (parent, 'P');
128     } // preferredLayoutSize
129

130     /**
131      * Calculates the minimum size dimensions for the specified
132      * container, given the components it contains.
133      * @param parent the component to be laid out
134      * @return Size
135      * @see #preferredLayoutSize
136      */

137     public Dimension minimumLayoutSize(Container parent)
138     {
139         return calculateLayoutSize (parent, 'm');
140     } // minimumLayoutSize
141

142     /**
143      * Calculates the maximum size dimensions for the specified container,
144      * given the components it contains.
145      * @param parent Parent Container
146      * @return Size
147      * @see java.awt.Component#getMaximumSize
148      * @see LayoutManager
149      */

150     public Dimension maximumLayoutSize(Container parent)
151     {
152         return calculateLayoutSize (parent, 'M');
153     } // maximumLayoutSize
154

155     /**
156      * Calculate Layout Size
157      * @param parent Parent Container
158      * @param how P=Preferred - M=Maximum = m=Mimimum
159      * @return Size
160      */

161     private Dimension calculateLayoutSize(Container parent, char how)
162     {
163         checkComponents(parent);
164         // -- Create 2D Dimension Array
165
int rows = getRowCount();
166         int cols = getColCount();
167         Dimension[][] dim = new Dimension[rows][cols];
168         //
169
Object JavaDoc[] keys = m_data.keySet().toArray();
170         Arrays.sort(keys);
171         for (int i = 0; i < keys.length; i++)
172         {
173             ALayoutConstraint constraint = (ALayoutConstraint)keys[i];
174             Component component = (Component)m_data.get(keys[i]);
175             Dimension d = null;
176             if (how == 'P')
177                 d = component.getPreferredSize();
178             else if (how == 'M')
179                 d = component.getMaximumSize();
180             else
181                 d = component.getMinimumSize();
182             if (component.isVisible())
183                 dim [constraint.getRow()][constraint.getCol()] = d;
184             else
185                 dim [constraint.getRow()][constraint.getCol()] = null;
186         }
187
188         // -- Calculate 2D Dimension Size
189
Insets insets = parent.getInsets();
190         Dimension retValue = new Dimension (insets.left+insets.right, insets.top+insets.bottom);
191         retValue.height += m_spaceH;
192         retValue.width += m_spaceV;
193         int maxWidth = 0;
194         for (int r = 0; r < rows; r++)
195         {
196             int height = 0;
197             int width = 0;
198             for (int c = 0; c < cols; c++)
199             {
200                 Dimension d = dim[r][c];
201                 if (d != null)
202                 {
203                     width += d.width;
204                     height = Math.max(height, d.height);
205                 }
206                 width += m_spaceV;
207             } // for all columns
208
retValue.height += height + m_spaceH;
209             maxWidth += Math.max(maxWidth, width);
210         } // for all rows
211
retValue.width += maxWidth;
212     // Log.trace(this,Log.l6_Database, "ALayout.calculateLayoutSize", retValue.toString());
213
return retValue;
214     } // calculateLayoutSize
215

216     /**
217      * Lays out the specified container.
218      * @param parent the container to be laid out
219      */

220     public void layoutContainer(Container parent)
221     {
222         checkComponents(parent);
223         // -- Create 2D Component Array
224
int rows = getRowCount();
225         int cols = getColCount();
226         Component[][] com = new Component[rows][cols];
227         //
228
Object JavaDoc[] keys = m_data.keySet().toArray();
229         Arrays.sort(keys);
230         for (int i = 0; i < keys.length; i++)
231         {
232             ALayoutConstraint constraint = (ALayoutConstraint)keys[i];
233             Component component = (Component)m_data.get(keys[i]);
234             if (component.isVisible())
235                 com [constraint.getRow()][constraint.getCol()] = component;
236             else
237                 com [constraint.getRow()][constraint.getCol()] = null;
238         }
239
240         // -- Calculate Column Size
241
int[] colWidth = new int[cols];
242         int[] rowHeight = new int[rows];
243         int columnWidth = m_spaceV;
244         for (int c = 0; c < cols; c++)
245         {
246             int width = 0;
247             for (int r = 0; r < rows; r++)
248             {
249                 Component component = com[r][c];
250                 if (component != null)
251                 {
252                     width = Math.max (width, component.getPreferredSize().width);
253                     rowHeight[r] = Math.max (rowHeight[r], component.getPreferredSize().height);
254                 }
255             }
256             colWidth[c] = width;
257             columnWidth += width + m_spaceV;
258         }
259
260         // -- Stretch/Squeeze Columns to fit target width
261
int parentWidth = parent.getSize().width;
262         double multiplier = (double)parentWidth / (double)columnWidth;
263         if (multiplier < .5) // limit sqeezing
264
multiplier = .5;
265         for (int c = 0; c < cols; c++)
266             colWidth[c] = (int) (colWidth[c] * multiplier);
267         int spaceV = (int)(m_spaceV * multiplier);
268         //
269
// Log.trace(Log.l6_Database, "ALayout.layoutContainer",
270
// "ParentWidth=" + parentWidth + ", ColumnWidth=" + columnWidth + ", SpaceV=" + spaceV + ", Multiplier=" + multiplier);
271

272         // -- Lay out components
273
Insets insets = parent.getInsets();
274         int posH = insets.top + m_spaceH;
275         for (int r = 0; r < rows; r++)
276         {
277             int posV = insets.left + spaceV;
278             int height = 0;
279             for (int c = 0; c < cols; c++)
280             {
281                 Component component = com[r][c];
282                 if (component != null)
283                 {
284                     Dimension ps = component.getPreferredSize();
285                     int w = ps.width;
286                     if (m_colFill || w > colWidth[c]) // limit or stretch
287
w = colWidth[c];
288                     int h = ps.height;
289                     int topSpace = 0;
290                     if (h < rowHeight[r]) // push a little bit lower
291
topSpace = (rowHeight[r] - h) / 3;
292                     height = Math.max(height, h);
293                     component.setBounds(posV, posH+topSpace, w, h);
294 // Log.trace(Log.l6_Database, "ALayout.layoutContainer",
295
// "Row=" + r + ", Col=" + c + ", PosV=" + posV + ", PosH=" + posH + "/" + topSpace + ", width=" + w + ", height=" + h);
296
}
297                 posV += colWidth[c] + spaceV;
298             } // for all columns
299
posH += height + m_spaceH;
300         } // for all rows
301
} // layoutContainer
302

303     /**
304      * Returns the alignment along the x axis. This specifies how
305      * the component would like to be aligned relative to other
306      * components. The value should be a number between 0 and 1
307      * where 0 represents alignment along the origin, 1 is aligned
308      * the furthest away from the origin, 0.5 is centered, etc.
309      * @param target target
310      * @return 0f
311      */

312     public float getLayoutAlignmentX(Container target)
313     {
314         return 0f;
315     } // getLayoutAlignmentX
316

317     /**
318      * Returns the alignment along the y axis. This specifies how
319      * the component would like to be aligned relative to other
320      * components. The value should be a number between 0 and 1
321      * where 0 represents alignment along the origin, 1 is aligned
322      * the furthest away from the origin, 0.5 is centered, etc.
323      * @param target target
324      * @return 0f
325      */

326     public float getLayoutAlignmentY(Container target)
327     {
328         return 0f;
329     } // getLayoutAlignmentY
330

331     /**
332      * Invalidates the layout, indicating that if the layout manager
333      * has cached information it should be discarded.
334      * @param target target
335      */

336     public void invalidateLayout(Container target)
337     {
338     } // invalidateLayout
339

340     /*************************************************************************/
341
342     /**
343      * Check target components and add components, which don't have no constraints
344      * @param target target
345      */

346     private void checkComponents (Container target)
347     {
348         int size = target.getComponentCount();
349         for (int i = 0; i < size; i++)
350         {
351             Component comp = target.getComponent(i);
352             if (!m_data.containsValue(comp))
353                 m_data.put(null, comp);
354         }
355     } // checkComponents
356

357     /**
358      * Get Number of Rows
359      * @return no pf rows
360      */

361     public int getRowCount()
362     {
363         return m_data.getMaxRow()+1;
364     } // getRowCount
365

366     /**
367      * Get Number of Columns
368      * @return no of cols
369      */

370     public int getColCount()
371     {
372         return m_data.getMaxCol()+1;
373     } // getColCount
374

375     /**
376      * Set Horizontal Space (top, between rows, button)
377      * @param spaceH horizontal space (top, between rows, button)
378      */

379     public void setSpaceH (int spaceH)
380     {
381         m_spaceH = spaceH;
382     } // setSpaceH
383

384     /**
385      * Get Horizontal Space (top, between rows, button)
386      * @return spaceH horizontal space (top, between rows, button)
387      */

388     public int getSpaceH()
389     {
390         return m_spaceH;
391     } // getSpaceH
392

393     /**
394      * Set Vertical Space (left, between columns, right)
395      * @param spaceV vertical space (left, between columns, right)
396      */

397     public void setSpaceV(int spaceV)
398     {
399         m_spaceV = spaceV;
400     } // setSpaceV
401

402     /**
403      * Get Vertical Space (left, between columns, right)
404      * @return spaceV vertical space (left, between columns, right)
405      */

406     public int getSpaceV()
407     {
408         return m_spaceV;
409     } // getSpaceV
410

411 } // ALayout
412
Popular Tags