View Javadoc

1   /*
2    * File:    AbstractContainer.java
3    * Created: 22.10.2006
4    *
5    * Copyright 2006 Michal Burda.
6    *
7    * This program is free software; you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation; either version 2 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program; if not, write to the Free Software
19   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20   */
21  
22  package net.sf.webmancer.widget;
23  
24  import java.util.List;
25  import java.util.Vector;
26  
27  import net.sf.webmancer.base.Event;
28  import net.sf.webmancer.model.IModelBuilder;
29  import net.sf.webmancer.model.ModelingException;
30  import net.sf.webmancer.util.ContractChecker;
31  
32  /**
33   * @author Michal Burda
34   */
35  public abstract class AbstractContainer extends AbstractWidget {
36      /**
37       * 
38       */
39      private List<IWidget> children;
40  
41      /**
42       * 
43       */
44      public AbstractContainer() {
45          super();
46          this.children = new Vector<IWidget>();
47      }
48  
49      /**
50       * @see net.sf.webmancer.widget.AbstractWidget#handleEvent(net.sf.webmancer.base.Event)
51       */
52      @Override
53      public void handleEvent(final Event event) {
54          ContractChecker.mustNotBeNull(event, "event");
55  
56          super.handleEvent(event);
57          if (!event.isHandled()) {
58              for (IWidget child : this.children) {
59                  child.handleEvent(event);
60                  if (event.isHandled()) {
61                      break;
62                  }
63              }
64          }
65      }
66  
67      /**
68       * @param widget
69       */
70      public void addChild(final IWidget widget) {
71          ContractChecker.mustNotBeNull(widget, "widget");
72  
73          if (widget.getParent() != null) {
74              throw new IllegalStateException(
75                      "Cannot add widget as a child to this container - the widget has parent already");
76          }
77          this.children.add(widget);
78          widget.setParent(this);
79      }
80  
81      /**
82       * @param children
83       */
84      public void setChildren(List<IWidget> children) {
85          ContractChecker.mustNotBeNull(children, "children");
86          
87          for (IWidget child : children) {
88              this.addChild(child);
89          }
90      }
91      
92      /**
93       * @see net.sf.webmancer.widget.AbstractWidget#build(net.sf.webmancer.model.IModelBuilder)
94       */
95      @Override
96      public void build(final IModelBuilder builder) throws ModelingException {
97          ContractChecker.mustNotBeNull(builder, "builder");
98          for (IWidget child : this.children) {
99              buildChild(child, builder);
100         }
101     }
102 
103     /**
104      * @param child
105      * @param builder
106      * @throws ModelingException 
107      */
108     protected void buildChild(IWidget child, IModelBuilder builder) throws ModelingException {
109         ContractChecker.mustNotBeNull(child, "child");
110         ContractChecker.mustNotBeNull(builder, "builder");
111         child.build(builder);
112     }
113 
114 }