View Javadoc

1   /**
2    * This file Copyright (c) 2003-2013 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.cms.gui.controlx.impl;
35  
36  import info.magnolia.cms.gui.controlx.Control;
37  import info.magnolia.cms.gui.controlx.RenderKit;
38  import info.magnolia.cms.gui.controlx.Renderer;
39  
40  import java.util.Collection;
41  
42  import org.apache.commons.collections.OrderedMap;
43  import org.apache.commons.collections.map.ListOrderedMap;
44  import org.apache.commons.lang.StringUtils;
45  
46  
47  /**
48   * Default Implementation. Gets the nearest RenderKit in the controls tree.
49   * @author Philipp Bracher
50   * @version $Revision$ ($Author$)
51   */
52  public class AbstractControl implements Control {
53  
54      /**
55       * Name of the control
56       */
57      private String name;
58  
59      /**
60       * The renderer kit used. layzy bound.
61       */
62      private RenderKit renderKit;
63  
64      /**
65       * The name of the renderer to use.
66       */
67      private String renderType;
68  
69      /**
70       * The renderer used. If not set the renderType is used to get the renderer from the RenderKit
71       */
72      private Renderer renderer;
73  
74      /**
75       * Parent control
76       */
77      private Control parent;
78  
79      /**
80       * The ordered children
81       */
82      private OrderedMap children = new ListOrderedMap();
83  
84      /**
85       * @return Returns the parent.
86       */
87      @Override
88      public Control getParent() {
89          return parent;
90      }
91  
92      /**
93       * @param parent The parent to set.
94       */
95      @Override
96      public void setParent(Control parent) {
97          this.parent = parent;
98      }
99  
100     /**
101      * If no name set yet just set one.
102      */
103     @Override
104     public void addChild(Control control) {
105         control.setParent(this);
106         if (StringUtils.isEmpty(control.getName())) {
107             control.setName(this.getName() + "_" + this.children.size());
108         }
109         this.children.put(control.getName(), control);
110     }
111 
112     @Override
113     public Control getChild(String name) {
114         return (Control) this.children.get(name);
115     }
116 
117     public void removeChild(String name){
118         this.children.remove(name);
119     }
120 
121     public Collection getChildren() {
122         return this.children.values();
123     }
124 
125     public void removeAllChildren(){
126         this.children.clear();
127     }
128 
129     /**
130      * @return Returns the name.
131      */
132     @Override
133     public String getName() {
134         return name;
135     }
136 
137     /**
138      * @param name The name to set.
139      */
140     @Override
141     public void setName(String name) {
142         this.name = name;
143     }
144 
145     /**
146      * @return Returns the renderKit.
147      */
148     @Override
149     public RenderKit getRenderKit() {
150         if (this.renderKit == null) {
151             if (this.getParent() != null) {
152                 this.renderKit = this.getParent().getRenderKit();
153             }
154         }
155         return renderKit;
156     }
157 
158     /**
159      * @param renderKit The renderKit to set.
160      */
161     @Override
162     public void setRenderKit(RenderKit renderKit) {
163         this.renderKit = renderKit;
164     }
165 
166     /**
167      * Get the Renderer assigned to this renderer type and call its renderer() method.
168      */
169     @Override
170     public String render() {
171         return this.getRenderer().render(this);
172     }
173 
174     /**
175      * @return Returns the renderType.
176      */
177     @Override
178     public String getRenderType() {
179         return renderType;
180     }
181 
182     /**
183      * @param renderType The renderType to set.
184      */
185     @Override
186     public void setRenderType(String renderType) {
187         this.renderType = renderType;
188     }
189 
190     /**
191      * @return Returns the renderer.
192      */
193     public Renderer getRenderer() {
194         if (this.renderer == null) {
195             this.renderer = this.getRenderKit().getRenderer(this.getRenderType());
196         }
197 
198         return this.renderer;
199     }
200 
201     /**
202      * @param renderer The renderer to set.
203      */
204     public void setRenderer(Renderer renderer) {
205         this.renderer = renderer;
206     }
207 
208 }