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

Quick Search    Search Deep

Source code: jcurses/widgets/DefaultLayoutManager.java


1   package jcurses.widgets;
2   
3   import jcurses.util.Protocol;
4   import jcurses.util.Rectangle;
5   
6   /**
7   *  This is a default layout manager. The constraints state for each widget 
8   * to layout a coordinates of the rectangle, within that the widget is placed
9   * and the alignment of the widget, if its preferred size is smaller as the rectangle's
10  * size-
11  */
12  public class DefaultLayoutManager implements LayoutManager, WidgetsConstants {
13    
14    private WidgetContainer _father = null;
15    
16      public void bindToContainer(WidgetContainer container) {
17      if (_father != null) {
18        throw new RuntimeException ("Already bound!!!");
19      }
20      _father = container;
21    }
22    
23    
24    public void unbindFromContainer() {
25      _father = null;
26    }
27    
28    
29    
30    
31    public void layout(Widget widget, Object constraint) {
32      if (! (constraint instanceof DefaultLayoutConstraint)) {
33        throw new RuntimeException("unknown constraint: "+constraint.getClass().getName());
34      }
35      
36      DefaultLayoutConstraint cstr = (DefaultLayoutConstraint)constraint;
37      
38      Rectangle prefSize = widget.getPreferredSize();
39      
40      int prefWidth = prefSize.getWidth();
41      int prefHeight = prefSize.getHeight();
42      /**
43      *  Negativ oder 0 bedeutet, daß keine bevorzugte Grösse angegeben wurde
44      */
45      if (prefWidth<=0) {
46        prefWidth = cstr.width;
47      }
48      
49      if (prefHeight<=0) {
50        prefHeight = cstr.height;
51      }
52      
53      int width = 0;
54      int height = 0;
55      
56      if (prefWidth < cstr.width) {
57         widget.setX(getAlignedCoordinate(prefWidth, cstr.width, cstr.x, cstr.horizontalConstraint));
58            width = prefWidth; 
59      } else {
60        widget.setX(cstr.x);
61        width = cstr.width;
62      }
63      
64      
65      if (prefHeight < cstr.height) {
66         widget.setY(getAlignedCoordinate(prefHeight, cstr.height, cstr.y, cstr.verticalConstraint));
67            height = prefHeight; 
68      } else {
69        widget.setY(cstr.y);
70        height = cstr.height;
71      }
72      
73      widget.setSize(new Rectangle(width, height));
74    }
75    
76    
77    private int getAlignedCoordinate(int prefG, int contG, int contC, int alignment) {
78      
79      
80      
81      if (alignment == ALIGNMENT_CENTER) {
82        alignment = 0;
83      } else if ((alignment == ALIGNMENT_BOTTOM) || (alignment == ALIGNMENT_RIGHT)) {
84        alignment = 1;
85      } else {
86        alignment = 2;
87      }
88      
89      int result = 0;
90      if (alignment == 2) {
91        result = contC;
92      } else if (alignment == 1) {
93        result = contC+contG-prefG;
94      } else {
95        result = contC+(contG-prefG)/2;
96      }
97      return result;
98    }
99    
100   
101   /**
102   *  Adds a widget to the boundeb container
103     * 
104     * @param widget widget to be added
105     * @param x the x coordinate of the top left corner of the rectangle, within that the widget is placed
106     * @param y the y coordinate of the top left corner of the rectangle, within that the widget is placed
107     * @param width the width of the rectangle, within that the widget is placed
108     * @param height the hight of the rectangle, within that the widget is placed
109     * @param verticalConstraint vertical alignment constraint. Following values a possible: 
110     * <code>WidgetConstraints.ALIGNMENT_CENTER</code>,<code>WidgetConstraints.ALIGNMENT_TOP</code>,<code>WidgetConstraints.ALIGNMENT_BOTTOM</code>
111     * @param horizontalConstraint vertical alignment constraint, Following values are possible:
112     *  * <code>WidgetConstraints.ALIGNMENT_CENTER</code>,<code>WidgetConstraints.ALIGNMENT_LEFT</code>,<code>WidgetConstraints.ALIGNMENT_RIGHT</code>
113   */
114 
115     public void addWidget(Widget widget, int x, int y, int width, int height, int verticalConstraint, int horizontalConstraint) {
116     _father.addWidget(widget, new DefaultLayoutConstraint(x, y, width, height, horizontalConstraint, verticalConstraint));
117     
118   }
119   
120   
121   /**
122   *  Removes a widget from the container
123     * 
124     * @param widget widget to be removed
125   */
126 
127     public void removeWidget(Widget widget) {
128     _father.removeWidget(widget);
129     
130   }
131 }
132 
133 
134 class DefaultLayoutConstraint {
135   
136   int x =0;
137   int y =0;
138   int width=0;
139   int height=0;
140   int horizontalConstraint=0;
141   int verticalConstraint=0;
142   
143   DefaultLayoutConstraint(int x, int y, int width, int height, int horizontalConstraint, int verticalConstraint) {
144     this.x=x;
145     this.y=y;
146     this.width=width;
147     this.height=height;
148     this.horizontalConstraint=horizontalConstraint;
149     this.verticalConstraint=verticalConstraint;
150   }
151 
152 
153 }