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.LayoutManager2;
22  
23  import javax.swing.BoxLayout;
24  
25  import org.xulux.api.gui.IWidget;
26  
27  /**
28   * The box layout. This is a wrapper around the swing box layout
29   * 
30   * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
31   * @version $Id: SwingBoxLayout.java,v 1.2 2005/02/18 09:10:32 mvdb Exp $
32   */
33  public class SwingBoxLayout extends SwingLayoutAbstract implements LayoutManager2 {
34  
35  
36      protected BoxLayout layout;
37    
38  
39      public SwingBoxLayout() {
40      }
41  
42      /**
43       * @widgetProprety layout-orientation
44       * @see org.xulux.api.gui.IXuluxLayout#addWidget(org.xulux.api.gui.IWidget)
45       */
46      public void addWidget(IWidget widget) {
47          if (layout == null) {
48            int axis = BoxLayout.X_AXIS;
49            String orientation = getParent().getProperty("layout-orientation");
50            if (orientation != null && orientation.equalsIgnoreCase("y")) {
51              axis = BoxLayout.Y_AXIS;
52            }
53            layout = new BoxLayout((Container) getParent().getNativeWidget(), axis); 
54          }
55      }
56  
57      /**
58       * @see org.xulux.api.gui.IXuluxLayout#removeWidget(org.xulux.api.gui.IWidget)
59       */
60      public void removeWidget(IWidget widget) {
61    
62      }
63  
64      /**
65       * @see org.xulux.api.gui.IXuluxLayout#destroy()
66       */
67      public void destroy() {
68    
69      }
70      /**
71       * @see java.awt.LayoutManager2#addLayoutComponent(java.awt.Component, java.lang.Object)
72       */
73      public void addLayoutComponent(Component comp, Object constraints) {
74          addWidget((IWidget) constraints);
75          // since jdk1.6 uses this to invalidate layout and if the componant
76          // is null, will throw an NPE (even though the JavaDoc says the
77          // method doesn't do anything), I will add a null check here.
78          if (comp != null) {
79              layout.addLayoutComponent(comp, constraints);
80          }
81      }
82      /**
83       * @see java.awt.LayoutManager2#maximumLayoutSize(java.awt.Container)
84       */
85      public Dimension maximumLayoutSize(Container target) {
86          return layout.maximumLayoutSize(target);
87      }
88      /**
89       * @see java.awt.LayoutManager2#getLayoutAlignmentX(java.awt.Container)
90       */
91      public float getLayoutAlignmentX(Container target) {
92          return layout.getLayoutAlignmentX(target);
93      }
94      /**
95       * @see java.awt.LayoutManager2#getLayoutAlignmentY(java.awt.Container)
96       */
97      public float getLayoutAlignmentY(Container target) {
98          return layout.getLayoutAlignmentY(target);
99      }
100     /**
101      * @see java.awt.LayoutManager2#invalidateLayout(java.awt.Container)
102      */
103     public void invalidateLayout(Container target) {
104         if (layout != null) {
105           layout.invalidateLayout(target);
106         }
107     }
108     /**
109      * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
110      */
111     public void addLayoutComponent(String name, Component comp) {
112         System.out.println("layoutComponent called : " + name);
113     }
114     /**
115      * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
116      */
117     public void removeLayoutComponent(Component comp) {
118         // since jdk1.6 uses this to invalidate layout and if the componant
119         // is null, will throw an NPE (even though the JavaDoc says the
120         // method doesn't do anything), I will add a null check here.
121         if (comp != null) {
122             layout.removeLayoutComponent(comp);
123         }
124     }
125     /**
126      * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
127      */
128     public Dimension preferredLayoutSize(Container parent) {
129         return layout.preferredLayoutSize(parent);
130     }
131     /**
132      * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
133      */
134     public Dimension minimumLayoutSize(Container parent) {
135         return layout.minimumLayoutSize(parent);
136     }
137     /**
138      * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
139      */
140     public void layoutContainer(Container parent) {
141         if (layout == null) {
142             addWidget(null);
143         }
144         layout.layoutContainer(parent);
145     }
146 
147 }