KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.netbeans.modules.xml.wsdl.ui.view.grapheditor.layout;
20
21 import java.awt.Point JavaDoc;
22 import java.awt.Rectangle JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.netbeans.api.visual.layout.Layout;
26 import org.netbeans.api.visual.layout.LayoutFactory.SerialAlignment;
27 import org.netbeans.api.visual.widget.Widget;
28 import org.netbeans.modules.visual.layout.SerialLayout;
29
30 public class OneSideJustifiedLayout implements Layout {
31     
32     int gap = 0;
33     int otherGap = 0;
34     boolean isRightSided = false;
35     
36     SerialLayout layout;
37     
38     /**
39      * if isRightSided is true, the first widget is expanded, such that it occupies remaining space in parent.
40      * else the second widget is expanded.
41      *
42      *
43      * Increases the height of the second widget by heightIncrease.
44      *
45      *
46      * @param isRightSided
47      * @param gap specifies the gap between widget
48      * @param heightIncrease
49      */

50     public OneSideJustifiedLayout(boolean isRightSided, int gap, int heightIncrease) {
51         this.isRightSided = isRightSided;
52         this.gap = gap;
53         this.otherGap = heightIncrease;
54         layout = new SerialLayout(false, SerialAlignment.JUSTIFY, gap);
55     }
56     
57     
58     /**
59      * if isRightSided is true, the first widget is expanded, such that it occupies remaining space in parent.
60      * else the second widget is expanded.
61      *
62      * Increases the height of the second widget by heightIncrease.
63      *
64      * @param isRightSided
65      * @param heightIncrease
66      */

67     public OneSideJustifiedLayout(boolean isRightSided, int heightIncrease) {
68         this(isRightSided, 0, heightIncrease);
69     }
70
71     /**
72      * if isRightSided is true, the first widget is expanded, such that it occupies remaining space in parent.
73      * else the second widget is expanded.
74      *
75      * @param isRightSided
76      */

77     public OneSideJustifiedLayout(boolean isRightSided) {
78         this(isRightSided, 0, 0);
79     }
80     
81     
82     public void justify(Widget widget) {
83         
84         List JavaDoc<Widget> children = widget.getChildren();
85         assert children.size() == 2 : "this layout cannot take more than 2 child widgets";
86
87         layout.justify(widget);
88         
89         Widget first = children.get(0);
90         Widget second = children.get(1);
91         
92         Rectangle JavaDoc parentBounds = widget.getClientArea();
93         //adjust width of first widget to be justified
94

95         Rectangle JavaDoc firstBounds = first.getBounds();
96         Point JavaDoc firstLocation = first.getLocation();
97         
98         Rectangle JavaDoc secondBounds = second.getBounds();
99         Point JavaDoc secondLocation = second.getLocation();
100         
101         int width = parentBounds.width - secondBounds.width;
102         
103         
104         firstBounds.width = width;
105         
106         if (isRightSided) {
107             secondLocation.x = width - secondBounds.x;
108             first.resolveBounds(firstLocation, firstBounds);
109             second.resolveBounds(secondLocation, secondBounds);
110         } else {
111             secondLocation.x = secondBounds.width - secondBounds.x;
112             firstLocation.x += secondBounds.x;
113             first.resolveBounds(secondLocation, firstBounds);
114             second.resolveBounds(firstLocation, secondBounds);
115         }
116         
117     }
118
119     public void layout(Widget widget) {
120         layout.layout(widget);
121     }
122
123     public boolean requiresJustification(Widget widget) {
124         return true;
125     }
126
127 }
128
Popular Tags