1 /**
2 * This file Copyright (c) 2003-2010 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: 32667 $ ($Author: gjoseph $)
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 public Control getParent() {
88 return parent;
89 }
90
91 /**
92 * @param parent The parent to set.
93 */
94 public void setParent(Control parent) {
95 this.parent = parent;
96 }
97
98 /**
99 * If no name set yet just set one.
100 */
101 public void addChild(Control control) {
102 control.setParent(this);
103 if (StringUtils.isEmpty(control.getName())) {
104 control.setName(this.getName() + "_" + this.children.size());
105 }
106 this.children.put(control.getName(), control);
107 }
108
109 public Control getChild(String name) {
110 return (Control) this.children.get(name);
111 }
112
113 public void removeChild(String name){
114 this.children.remove(name);
115 }
116
117 public Collection getChildren() {
118 return this.children.values();
119 }
120
121 public void removeAllChildren(){
122 this.children.clear();
123 }
124
125 /**
126 * @return Returns the name.
127 */
128 public String getName() {
129 return name;
130 }
131
132 /**
133 * @param name The name to set.
134 */
135 public void setName(String name) {
136 this.name = name;
137 }
138
139 /**
140 * @return Returns the renderKit.
141 */
142 public RenderKit getRenderKit() {
143 if (this.renderKit == null) {
144 if (this.getParent() != null) {
145 this.renderKit = this.getParent().getRenderKit();
146 }
147 }
148 return renderKit;
149 }
150
151 /**
152 * @param renderKit The renderKit to set.
153 */
154 public void setRenderKit(RenderKit renderKit) {
155 this.renderKit = renderKit;
156 }
157
158 /**
159 * Get the Renderer assigned to this renderer type and call its renderer() method.
160 */
161 public String render() {
162 return this.getRenderer().render(this);
163 }
164
165 /**
166 * @return Returns the renderType.
167 */
168 public String getRenderType() {
169 return renderType;
170 }
171
172 /**
173 * @param renderType The renderType to set.
174 */
175 public void setRenderType(String renderType) {
176 this.renderType = renderType;
177 }
178
179 /**
180 * @return Returns the renderer.
181 */
182 public Renderer getRenderer() {
183 if (this.renderer == null) {
184 this.renderer = this.getRenderKit().getRenderer(this.getRenderType());
185 }
186
187 return this.renderer;
188 }
189
190 /**
191 * @param renderer The renderer to set.
192 */
193 public void setRenderer(Renderer renderer) {
194 this.renderer = renderer;
195 }
196
197 }