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.FlowLayout;
22  import java.awt.LayoutManager;
23  import java.util.List;
24  
25  import javax.swing.JComponent;
26  
27  import org.xulux.api.gui.IWidget;
28  import org.xulux.gui.ContainerWidget;
29  
30  /**
31   * The flow layout. This is a wrapper around the swing flow layout
32   * 
33   * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt </a>
34   * @version $Id: SwingFlowLayout.java,v 1.3 2005/02/18 09:10:32 mvdb Exp $
35   */
36  public class SwingFlowLayout extends SwingLayoutAbstract implements
37      LayoutManager {
38  
39    boolean widgetsRefreshed = false;
40  
41    protected FlowLayout layout;
42  
43    public SwingFlowLayout() {
44    }
45  
46    /**
47     * Not used
48     * @see org.xulux.api.gui.IXuluxLayout#addWidget(org.xulux.api.gui.IWidget)
49     */
50    public void addWidget(IWidget widget) {
51    }
52  
53    /**
54     * Not used
55     * @see org.xulux.api.gui.IXuluxLayout#removeWidget(org.xulux.api.gui.IWidget)
56     */
57    public void removeWidget(IWidget widget) {
58  
59    }
60  
61    /**
62     * @see org.xulux.api.gui.IXuluxLayout#destroy()
63     */
64    public void destroy() {
65  
66    }
67  
68    /**
69     * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
70     */
71    public void addLayoutComponent(String name, Component comp) {
72    }
73  
74    /**
75     * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
76     */
77    public void removeLayoutComponent(Component comp) {
78      layout.removeLayoutComponent(comp);
79    }
80  
81    /**
82     * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
83     */
84    public Dimension preferredLayoutSize(Container parent) {
85      if (!widgetsRefreshed) {
86        setWidgetsPreferredSize();
87      }
88      return layout.preferredLayoutSize(parent);
89    }
90  
91    /**
92     * Make sure the preferredsize is set
93     */
94    public void setWidgetsPreferredSize() {
95      if (widgetsRefreshed) {
96        return;
97      }
98      System.err.println("PARENt WIDGET : "+ getParent());
99      System.err.println("CHILDWIDGETS : " + getParent().getChildWidgets());
100     
101     List list = getParent().getChildWidgets();
102     if (list != null && list.size() > 0) {
103       for (int j = 0; j < list.size(); j++) {
104         IWidget w = (IWidget) list.get(j);
105         if (w instanceof ContainerWidget) {
106           List childList = w.getChildWidgets();
107           if (childList == null) {
108               continue;
109           }
110           for (int i = 0; i < childList.size(); i++) {
111             IWidget w2 = (IWidget) childList.get(i);
112             Dimension dim =  w2.getRectangle().getRectangle().getSize();
113             ((JComponent) w2.getNativeWidget()).setPreferredSize(dim);
114           }
115         } else if (w.getNativeWidget() instanceof JComponent) {
116           ((JComponent) w.getNativeWidget()).setPreferredSize(w.getRectangle()
117               .getRectangle().getSize());
118         }
119       }
120     }
121     widgetsRefreshed = true;
122   }
123 
124   /**
125    * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
126    */
127   public Dimension minimumLayoutSize(Container parent) {
128     if (!widgetsRefreshed) {
129       setWidgetsPreferredSize();
130     }
131     return layout.minimumLayoutSize(parent);
132   }
133 
134   /**
135    * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
136    */
137   public void layoutContainer(Container target) {
138     if (!widgetsRefreshed) {
139       setWidgetsPreferredSize();
140     }
141     layout.layoutContainer(target);
142   }
143 
144   /**
145    * When setting the parent, we already create the layout
146    *
147    * @see org.xulux.api.gui.IXuluxLayout#setParent(org.xulux.api.gui.IWidget)
148    */
149   public void setParent(IWidget widget) {
150     super.setParent(widget);
151     if (layout == null) {
152       String alignment = getParent().getProperty("layout-alignment");
153       // default to left..
154       int align = FlowLayout.LEFT;
155       if ("right".equalsIgnoreCase(alignment)) {
156         align = FlowLayout.RIGHT;
157       } else if ("center".equalsIgnoreCase(alignment)) {
158         align = FlowLayout.CENTER;
159       }
160       layout = new FlowLayout(align,0,0);
161     }
162   }
163 }