KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > ui > view > grapheditor > layout > TableLayout


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.wsdl.ui.view.grapheditor.layout;
21
22 import java.awt.Point JavaDoc;
23 import java.awt.Rectangle JavaDoc;
24 import java.util.List JavaDoc;
25 import org.netbeans.api.visual.layout.Layout;
26 import org.netbeans.api.visual.widget.Widget;
27
28 /**
29  *
30  * @author anjeleevich
31  */

32 public class TableLayout implements Layout {
33     
34     private int columnCount;
35     private int hgap;
36     private int vgap;
37     private int minColumnWidth;
38
39     public TableLayout(int columnCount, int hgap, int vgap,
40             int minColumnWidth)
41     {
42         this.minColumnWidth = minColumnWidth;
43         this.columnCount = columnCount;
44         this.hgap = hgap;
45         this.vgap = vgap;
46     }
47     
48     
49     public void layout(Widget widget) {
50         List JavaDoc<Widget> children = widget.getChildren();
51
52         int width = (minColumnWidth + hgap) * columnCount - hgap;;
53         int y = 0;
54
55         for (int i = 0; i < children.size(); i += columnCount) {
56             
57             int rowHeight = 0;
58             
59             for (int j = 0; j < columnCount; j++) {
60                 Widget w = children.get(i + j);
61                 Rectangle JavaDoc b = w.getPreferredBounds();
62                 rowHeight = Math.max(rowHeight, b.height);
63             }
64             
65             int x = 0;
66             
67             for (int j = 0; j < columnCount; j++) {
68                 Widget w = children.get(i + j);
69                 Rectangle JavaDoc b = w.getPreferredBounds();
70                 b.height = rowHeight;
71                 b.width = minColumnWidth;
72                 w.resolveBounds(new Point JavaDoc(x - b.x, y - b.y), b);
73                 
74                 x += minColumnWidth + hgap;
75             }
76             
77             y += rowHeight + vgap;
78         }
79     }
80
81     public boolean requiresJustification(Widget widget) {
82         return true;
83     }
84
85     public void justify(Widget widget) {
86         Rectangle JavaDoc bounds = widget.getClientArea();
87
88         int y0 = bounds.y;
89
90         List JavaDoc<Widget> children = widget.getChildren();
91
92         for (int i = 0; i < children.size(); i += columnCount) {
93             int rowHeight = 0;
94
95             for (int j = 0; j < columnCount; j++) {
96                 Widget w = children.get(i + j);
97                 Rectangle JavaDoc b = w.getBounds();
98                 rowHeight = Math.max(rowHeight, b.height);
99             }
100             
101             int x0 = bounds.x;
102             int width = bounds.width;
103             
104             for (int j = 0; j < columnCount; j++) {
105                 Widget w = children.get(i + j);
106                 Rectangle JavaDoc b = w.getBounds();
107                 
108                 int columnWidth = (width - hgap * (columnCount - 1 - j))
109                         / (columnCount - j);
110                 
111                 b.width = columnWidth;
112                 b.height = rowHeight;
113                 
114                 w.resolveBounds(new Point JavaDoc(x0 - b.x, y0 - b.y), b);
115                 
116                 width -= columnWidth + hgap;
117                 x0 += columnWidth + hgap;
118             }
119             
120             y0 += rowHeight + vgap;
121         }
122     }
123 }
124
Popular Tags