Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: jcurses/widgets/GridLayoutManager.java


1   package jcurses.widgets;
2   
3   import jcurses.util.Rectangle;
4   
5   /******************************************************
6    * This class is a layout manager that works like as the <code>DefaultLayoutManager</code>
7    * with an difference: the painting rectangle is shared in many grid cells and the constraints
8    * are stated not in real coodinates on the painting rectangle, but in 'grid-coordinates'
9    */
10  
11  public class GridLayoutManager implements LayoutManager, WidgetsConstants {
12    
13    private DefaultLayoutManager _defLayout = new DefaultLayoutManager();
14    private WidgetContainer _father = null;
15    
16    private int _width = 0;
17    private int _height = 0;
18    
19    private Grid _grid = null;
20    
21    
22      public void bindToContainer(WidgetContainer container) {
23      if (_father != null) {
24        throw new RuntimeException ("Already bound!!!");
25      }
26      _father = container;
27    }
28    
29    
30    public void unbindFromContainer() {
31      _father = null;
32    }
33    
34    /**
35    *  The constructor
36      * 
37      * @param width the width of the grid ( in cells )
38      * @param height the height of the grid ( in cells )
39      * 
40    */
41    public GridLayoutManager(int width, int height) {
42      _width = width;
43      _height = height;
44    }
45    
46    
47    
48    
49    public void layout(Widget widget, Object constraint) {
50      if (! (constraint instanceof GridLayoutConstraint)) {
51       throw new RuntimeException("unknown constraint: "+constraint.getClass().getName());
52      }
53      
54      Rectangle rect = (_father.getChildsRectangle() == null)?_father.getSize():_father.getChildsRectangle();
55      _grid = new Grid(rect, _width, _height);
56      _defLayout.layout(widget, ((GridLayoutConstraint)constraint).getDefaultLayoutConstraint(_grid));
57    
58    }
59    
60    
61    
62    
63      /**
64    *  Adds a widget to the boundeb container
65      * 
66      * @param widget widget to be added
67      * @param x the x coordinate of the top left corner of the rectangle, within that the widget is placed
68      * @param y the y coordinate of the top left corner of the rectangle, within that the widget is placed
69      * @param width the width of the rectangle, within that the widget is placed
70      * @param height the hight of the rectangle, within that the widget is placed
71      * @param verticalConstraint vertical alignment constraint. Following values a possible: 
72      * <code>WidgetConstraints.ALIGNMENT_CENTER</code>,<code>WidgetConstraints.ALIGNMENT_TOP</code>,<code>WidgetConstraints.ALIGNMENT_BOTTOM</code>
73      * @param horizontalConstraint vertical alignment constraint, Following values are possible:
74      *  * <code>WidgetConstraints.ALIGNMENT_CENTER</code>,<code>WidgetConstraints.ALIGNMENT_LEFT</code>,<code>WidgetConstraints.ALIGNMENT_RIGHT</code>
75    */
76      public void addWidget(Widget widget, int x, int y, int width, int height, int verticalConstraint, int horizontalConstraint) {
77      _father.addWidget(widget, new GridLayoutConstraint(x, y, width, height, horizontalConstraint, verticalConstraint));
78      
79    }
80    
81    
82      /**
83      *  Removes a widget 
84      * @param widget widget to remove
85      */
86      public void removeWidget(Widget widget) {
87      _father.removeWidget(widget);
88      
89    }
90  }
91  
92  
93  class GridLayoutConstraint {
94    
95    int x =0;
96    int y =0;
97    int width=0;
98    int height=0;
99    int horizontalConstraint=0;
100   int verticalConstraint=0;
101   
102   GridLayoutConstraint(int x, int y, int width, int height, int horizontalConstraint, int verticalConstraint) {
103     this.x=x;
104     this.y=y;
105     this.width=width;
106     this.height=height;
107     this.horizontalConstraint=horizontalConstraint;
108     this.verticalConstraint=verticalConstraint;
109   }
110   
111   DefaultLayoutConstraint getDefaultLayoutConstraint(Grid grid) {
112     
113     Rectangle rect = grid.getRectangle(x,y,width,height);
114     return new DefaultLayoutConstraint(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight(),
115                        horizontalConstraint, verticalConstraint);
116     
117   }
118 
119 }
120 
121 class Grid {
122   
123   int [] _widths;
124   int [] _heights;
125   
126   Grid(Rectangle rect, int width, int height) {
127     if (((rect.getWidth()/width) <1) || ((rect.getHeight()/height) <1)) {
128       throw new RuntimeException (" the grid is to fine: "+rect.getWidth()+":"+rect.getHeight()+":"+width+":"+height);
129     }
130     
131     _widths = new int [width];
132     _heights = new int [height];
133     
134     fillArray(_widths, rect.getWidth(), width);
135     fillArray(_heights, rect.getHeight(), height);
136     
137   }
138   
139   
140   private void fillArray(int [] array, int rectWidth, int width) {
141     int mod = rectWidth%width;
142     int cellWidth = rectWidth/width;
143     
144     for (int i=0; i<width; i++) {
145       if (mod > 0) {
146         array [i] = cellWidth+1;
147         mod --;
148       } else {
149         array [i] = cellWidth;
150       }
151     }
152     
153   }
154   
155   Rectangle getRectangle(int x, int y, int width, int height) {
156     return new Rectangle(getWidth(_widths, 0,x), getWidth(_heights, 0,y), getWidth(_widths, x,x+width), 
157                getWidth(_heights, y, y+height));
158     
159   }
160   
161   
162   private int getWidth(int [] array, int begin, int end) {
163     int width = 0;
164     for (int i=begin; i<end; i++) {
165       width+=array[i];
166     }
167     
168     return width;
169   }
170   
171   
172 }
173   
174 
175 
176