KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > jsf > navigation > graph > NavigationNodeWidget


1 /*
2  * NavigationNodeWidget.java
3  *
4  * Created on February 15, 2007, 2:33 PM
5  *
6  * To change this template, choose Tools | Template Manager
7  * and open the template in the editor.
8  */

9
10 package org.netbeans.modules.web.jsf.navigation.graph;
11
12 import java.awt.Image JavaDoc;
13 import java.awt.Point JavaDoc;
14 import java.awt.Rectangle JavaDoc;
15 import java.util.Collection JavaDoc;
16 import org.netbeans.api.visual.layout.Layout;
17 import org.netbeans.api.visual.layout.LayoutFactory;
18 import org.netbeans.api.visual.vmd.VMDNodeWidget;
19 import org.netbeans.api.visual.widget.ImageWidget;
20 import org.netbeans.api.visual.widget.Scene;
21 import org.netbeans.api.visual.widget.Widget;
22 import org.openide.util.Utilities;
23
24 /**
25  *
26  * @author joelle
27  */

28 public class NavigationNodeWidget extends VMDNodeWidget{
29     
30     private static final Image JavaDoc POINT_SHAPE_IMAGE = Utilities.loadImage("org/netbeans/modules/visual/resources/vmd-pin.png"); // NOI18N
31

32     
33     /** Creates a new instance of NavigationNodeWidget
34      * @param scene
35      */

36     public NavigationNodeWidget(Scene scene) {
37         super(scene);
38         
39         setLayout( new NavigationNodeLayout(true, LayoutFactory.SerialAlignment.JUSTIFY, 0) );
40 // DefaultPinWidget imageWidget = new DefaultPinWidget(scene, POINT_SHAPE_IMAGE);
41
// header.addChild(imageWidget);
42

43         //
44
//
45
// Rectangle headerBounds = header.getPreferredBounds();
46
// int x = headerBounds.x;
47
// int y = headerBounds.y;
48
// int width = headerBounds.width;
49
// int height = headerBounds.height;
50
// int lx = x - width;
51
// int ly = -y;
52

53 // addChild(imageWidget);
54

55         // imageWidget.setPreferredLocation(new Point(lx, ly));
56

57         
58         // Widget widget = new Widget(this);
59
// ImageWidget imageWidget = new ImageWidget(this, POINT_SHAPE_IMAGE);
60
// widget.setLayout(new NodePinLayout(0) );
61
// widget.addChild(nodeWidget);
62
// widget.addChild(imageWidget);
63
//
64
// mainLayer.addChild(widget);
65
}
66     
67     private class DefaultPinWidget extends ImageWidget {
68         private DefaultPinWidget(Scene scene, Image JavaDoc image) {
69             super(scene, image);
70         }
71     }
72     
73     private static class NavigationNodeLayout implements Layout {
74         
75         private boolean verticalOrientation;
76         private LayoutFactory.SerialAlignment alignment;
77         private int gap;
78         
79         public NavigationNodeLayout(boolean verticalOrientation, LayoutFactory.SerialAlignment alignment, int gap) {
80             this.verticalOrientation = verticalOrientation;
81             this.alignment = alignment;
82             this.gap = gap;
83         }
84         
85         private DefaultPinWidget defaultPinWidget = null;
86         
87         public void layout(Widget widget) {
88             int max = 0;
89             Collection JavaDoc<Widget> children = widget.getChildren();
90             for (Widget child : children) {
91                 if (! child.isVisible())
92                     continue;
93                 Rectangle JavaDoc preferredBounds = child.getPreferredBounds();
94                 int i = preferredBounds.width;
95                 if (i > max)
96                     max = i;
97             }
98             int pos = 0;
99             
100             for (Widget child : children) {
101                 if (child instanceof DefaultPinWidget ){
102                     defaultPinWidget = (DefaultPinWidget)child;
103                     defaultPinWidget.resolveBounds(new Point JavaDoc(max,5), null);
104                     continue;
105                 }
106                 Rectangle JavaDoc preferredBounds = child.getPreferredBounds();
107                 int x = preferredBounds.x;
108                 int y = preferredBounds.y;
109                 int width = preferredBounds.width;
110                 int height = preferredBounds.height;
111                 int lx = - x;
112                 int ly = pos - y;
113                 width = max; //To Justify
114
if (child.isVisible()) {
115                     child.resolveBounds(new Point JavaDoc(lx, ly), new Rectangle JavaDoc(x, y, width, height));
116                     pos += height + gap;
117                 } else
118                     child.resolveBounds(new Point JavaDoc(lx, ly), new Rectangle JavaDoc(x, y, 0, 0));
119             }
120             
121 // if( defaultPinWidget != null ){
122
// }
123

124         }
125         
126         public boolean requiresJustification(Widget widget) {
127             return true;
128         }
129         
130         public void justify(Widget widget) {
131             for (Widget child : widget.getChildren()) {
132                 Rectangle JavaDoc bounds = widget.getClientArea();
133                 Point JavaDoc location = child.getLocation();
134                 Rectangle JavaDoc childBounds = child.getBounds();
135                 
136                 if (verticalOrientation) {
137                     int parentX1 = bounds.x;
138                     int parentX2 = parentX1 + bounds.width;
139                     int childX1 = location.x + childBounds.x;
140                     int childX2 = childX1 + childBounds.width;
141                     
142                     childBounds.x = Math.min(parentX1, childX1);
143                     childBounds.width = Math.max(parentX2, childX2) - childBounds.x;
144                     childBounds.x -= location.x;
145                 }
146                 child.resolveBounds(location, childBounds);
147             }
148         }
149     }
150 }
151
Popular Tags