View Javadoc

1   /*
2      Copyright 2002-2006 Martin van den Bemt
3   
4      Licensed under the Apache License, Version 2.0 (the "License");
5      you may not use this file except in compliance with the License.
6      You may obtain a copy of the License at
7   
8          http://www.apache.org/licenses/LICENSE-2.0
9   
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15  */
16  package org.xulux.guilayer.swing.layouts;
17  
18  import java.awt.Component;
19  import java.awt.Container;
20  import java.awt.Dimension;
21  import java.awt.Insets;
22  import java.awt.LayoutManager2;
23  import java.awt.Point;
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import org.xulux.api.gui.IWidget;
28  
29  /**
30   * The autosize layoutmanager autosizes the components based on the preferred size.
31   * It also makes sure when resizing the component can stay on the specified position
32   * (eg LEFT means, stay left, do not center).
33   *
34   * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
35   * @version $Id: AutoSizeLayoutManager.java,v 1.2 2004/10/20 00:00:02 mvdb Exp $
36   */
37  public class AutoSizeLayoutManager extends SwingLayoutAbstract implements LayoutManager2 {
38  
39  
40    /**
41     * A map containing widgets and their components
42     */
43    private Map map;
44  
45    public AutoSizeLayoutManager() {
46      map = new HashMap();
47    }
48    /**
49     * The constructor that imidiately sets the parent
50     * @param parent the parent widget.
51     */
52    public AutoSizeLayoutManager(IWidget parent) {
53      this();
54      setParent(parent);
55    }
56  
57    /**
58     * @see java.awt.LayoutManager2#getLayoutAlignmentX(java.awt.Container)
59     */
60    public float getLayoutAlignmentX(Container target) {
61      return 0;
62    }
63  
64    /**
65     * @see java.awt.LayoutManager2#getLayoutAlignmentY(java.awt.Container)
66     */
67    public float getLayoutAlignmentY(Container target) {
68      return 0;
69    }
70  
71    /**
72     * @see java.awt.LayoutManager2#invalidateLayout(java.awt.Container)
73     */
74    public void invalidateLayout(Container target) {
75    }
76  
77    /**
78     * @see java.awt.LayoutManager2#maximumLayoutSize(java.awt.Container)
79     */
80    public Dimension maximumLayoutSize(Container target) {
81      return null;
82    }
83  
84    /**
85     * @see java.awt.LayoutManager2#addLayoutComponent(java.awt.Component, java.lang.Object)
86     */
87    public void addLayoutComponent(Component comp, Object constraints) {
88      map.put(comp, constraints);
89    }
90  
91    /**
92     * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
93     */
94    public void removeLayoutComponent(Component comp) {
95    }
96  
97    /**
98     * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
99     */
100   public void layoutContainer(Container parent) {
101 //    System.out.println("Parent : " + parent);
102     int compCount = parent.getComponentCount();
103     Insets insets = parent.getInsets();
104     for (int i = 0; i < compCount; i++) {
105       Component component = parent.getComponent(i);
106       IWidget widget = null;
107       Object obj = map.get(component);
108       if (obj instanceof IWidget) {
109         widget = (IWidget) obj;
110       }
111       Dimension prefSize = null;
112       Point location = new Point(0,0);
113       if (widget != null) {
114         prefSize = widget.getRectangle().getRectangle().getSize();
115         location = widget.getRectangle().getRectangle().getLocation();
116       } else {
117         prefSize = component.getPreferredSize();
118         if (obj instanceof String) {
119           String newValue = ((String) obj).toLowerCase();
120           if (newValue.indexOf("width=auto") != -1) {
121             prefSize.width = parent.getSize().width;
122           }
123           if (newValue.indexOf("height=auto") != -1) {
124             prefSize.height = parent.getSize().height;
125           }
126         }
127       }
128       int height = prefSize.height+insets.top+insets.bottom;
129       int width = prefSize.width+insets.left;
130       if (widget != null && widget.getProperty("width") != null) {
131         String widthProp = widget.getProperty("width");
132         if ("auto".equalsIgnoreCase(widthProp)) {
133 	        int newX =  parent.getSize().width;
134 	          width = newX;
135         }
136         String heightProp = widget.getProperty("height");
137         if ("auto".equalsIgnoreCase(heightProp)) {
138            height = parent.getSize().height-location.y;
139         }
140       }
141       component.setBounds(location.x, location.y, width, height);
142     }
143   }
144 
145   /**
146    * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
147    */
148   public void addLayoutComponent(String name, Component comp) {
149   }
150 
151   /**
152    * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
153    */
154   public Dimension minimumLayoutSize(Container parent) {
155     return null;
156   }
157 
158   /**
159    * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
160    */
161   public Dimension preferredLayoutSize(Container parent) {
162 //    System.out.println("Parent : " + parent);
163     int compCount = parent.getComponentCount();
164     Insets insets = parent.getInsets();
165     int width = insets.left;
166     int height = insets.top=insets.bottom;
167     for (int i = 0; i < compCount; i++) {
168       Component component = parent.getComponent(i);
169       Object obj = map.get(component);
170       IWidget widget = null; 
171       if (obj instanceof IWidget) {
172         widget = (IWidget) obj;
173       }
174       Dimension prefSize = null;
175       if (widget != null) {
176         prefSize = widget.getRectangle().getRectangle().getSize();
177       } else {
178         prefSize = component.getPreferredSize();
179       }
180       width = Math.max(width, prefSize.width);
181       height+=prefSize.height;
182     }
183 //    System.out.println("returning : " + width + " height " + height);
184     return new Dimension(width, height);
185   }
186 
187   /**
188    * @see org.xulux.api.gui.IXuluxLayout#addWidget(org.xulux.api.gui.IWidget)
189    */
190   public void addWidget(IWidget widget) {
191     Object nativeWidget = widget.getNativeWidget();
192     if (nativeWidget instanceof Component) {
193       addLayoutComponent((Component) nativeWidget, widget);
194     }
195   }
196 
197   /**
198    * @see org.xulux.api.gui.IXuluxLayout#removeWidget(org.xulux.api.gui.IWidget)
199    */
200   public void removeWidget(IWidget widget) {
201   }
202 
203   /**
204    * @see org.xulux.api.gui.IXuluxLayout#destroy()
205    */
206   public void destroy() {
207     if (map != null) {
208       map.clear();
209       map = null;
210     }
211   }
212 
213 }