View Javadoc

1   /*
2    * Copyright 2004-2008 the Seasar Foundation and the Others.
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, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.uruma.component.base;
17  
18  import org.seasar.uruma.annotation.ComponentAttribute;
19  import org.seasar.uruma.component.UIComponent;
20  import org.seasar.uruma.component.UIComponentContainer;
21  import org.seasar.uruma.context.PartContext;
22  import org.seasar.uruma.context.WidgetHandle;
23  import org.seasar.uruma.context.WindowContext;
24  import org.seasar.uruma.core.UrumaConstants;
25  import org.seasar.uruma.core.UrumaMessageCodes;
26  import org.seasar.uruma.log.UrumaLogger;
27  import org.seasar.uruma.renderer.Renderer;
28  import org.seasar.uruma.renderer.RendererExtender;
29  import org.seasar.uruma.util.AssertionUtil;
30  
31  /**
32   * {@link UIComponent} を表す基底クラスです。<br />
33   * 
34   * @author y-komori
35   */
36  public abstract class AbstractUIComponent extends AbstractUIElement implements
37          UIComponent {
38      private UrumaLogger logger = UrumaLogger.getLogger(getClass());
39  
40      private UIComponentContainer parent;
41  
42      @ComponentAttribute
43      private String id;
44  
45      @ComponentAttribute
46      private String style;
47  
48      private Renderer renderer;
49  
50      /**
51       * レンダラ呼び出し中に独自のレンダリング処理を追加するためのメソッドです。<br />
52       * <p>
53       * 本メソッドは {@link AbstractUIComponent#preRender(WidgetHandle, WindowContext)}
54       * メソッドの中で、{@link Renderer レンダラ} の
55       * {@link Renderer#preRender(UIComponent, WidgetHandle, WindowContext)}
56       * メソッドを呼び出した後に呼び出されます。<br />
57       * </p>
58       * <p>
59       * このタイミングでサブクラスで独自のレンダリング処理を行う場合、本メソッドをオーバーライドしてください。<br />
60       * </p>
61       * 
62       * @param parent
63       *            親 {@link WidgetHandle} オブジェクト
64       * @param context
65       *            {@link WindowContext} オブジェクト
66       */
67      protected void doPreRender(final WidgetHandle parent,
68              final WindowContext context) {
69      }
70  
71      /**
72       * レンダラ呼び出し中に独自のレンダリング処理を追加するためのメソッドです。<br />
73       * <p>
74       * 本メソッドは {@link AbstractUIComponent#renderer} メソッドの中で、{@link Renderer レンダラ}
75       * の
76       * {@link Renderer#render(UIComponent, WidgetHandle, PartContext) render()}
77       * メソッドと
78       * {@link Renderer#renderAfter(WidgetHandle, UIComponent, WidgetHandle, PartContext) renderAfter()}
79       * メソッドを呼び出す間に呼び出されます。<br />
80       * </p>
81       * <p>
82       * このタイミングでサブクラスで独自のレンダリング処理を行う場合、本メソッドをオーバーライドしてください。<br />
83       * </p>
84       * 
85       * @param parent
86       *            親 {@link WidgetHandle} オブジェクト
87       * @param context
88       *            {@link PartContext} オブジェクト
89       */
90      protected void doRender(final WidgetHandle parent, final PartContext context) {
91      }
92  
93      /*
94       * @see org.seasar.uruma.component.UIComponent#preRender(org.seasar.uruma.context.WidgetHandle,
95       *      org.seasar.uruma.context.WindowContext)
96       */
97      public void preRender(final WidgetHandle parent, final WindowContext context) {
98          if (logger.isDebugEnabled()) {
99              logger.log(UrumaMessageCodes.PRE_RENDER_START, this);
100         }
101 
102         setupId();
103 
104         WidgetHandle handle = getRenderer().preRender(this, parent, context);
105         if (handle != null) {
106             context.putWidgetHandle(handle);
107         }
108 
109         doPreRender(parent, context);
110 
111         if (logger.isDebugEnabled()) {
112             logger.log(UrumaMessageCodes.PRE_RENDER_END, this);
113         }
114     }
115 
116     /*
117      * @see org.seasar.uruma.component.UIComponent#render(org.seasar.uruma.context.WidgetHandle,
118      *      org.seasar.uruma.context.PartContext)
119      */
120     public void render(final WidgetHandle parent, final PartContext context) {
121         if (logger.isDebugEnabled()) {
122             logger.log(UrumaMessageCodes.RENDER_START, this);
123         }
124 
125         setupId();
126 
127         WidgetHandle handle = getRenderer().render(this, parent, context);
128         if (handle != null) {
129             context.putWidgetHandle(handle);
130         }
131 
132         doRender(parent, context);
133 
134         RendererExtender.doExtRender(this, handle, context);
135 
136         getRenderer().renderAfter(handle, this, parent, context);
137 
138         if (logger.isDebugEnabled()) {
139             logger.log(UrumaMessageCodes.RENDER_END, this);
140         }
141     }
142 
143     /*
144      * @see org.seasar.uruma.component.UIComponent#getId()
145      */
146     public String getId() {
147         return this.id;
148     }
149 
150     /*
151      * @see org.seasar.uruma.component.UIComponent#getParent()
152      */
153     public UIComponentContainer getParent() {
154         return parent;
155     }
156 
157     /*
158      * @see org.seasar.uruma.component.UIComponent#getRenderer()
159      */
160     public Renderer getRenderer() {
161         return this.renderer;
162     }
163 
164     /*
165      * @see org.seasar.uruma.component.UIComponent#getStyle()
166      */
167     public String getStyle() {
168         return this.style;
169     }
170 
171     /*
172      * @see org.seasar.uruma.component.UIComponent#setId(java.lang.String)
173      */
174     public void setId(final String id) {
175         this.id = id;
176     }
177 
178     /*
179      * @see org.seasar.uruma.component.UIComponent#setParent(org.seasar.uruma.component.UIContainer)
180      */
181     public void setParent(final UIComponentContainer parent) {
182         this.parent = parent;
183     }
184 
185     /*
186      * @see org.seasar.uruma.component.UIComponent#setRenderer(org.seasar.uruma.renderer.Renderer)
187      */
188     public void setRenderer(final Renderer renderer) {
189         AssertionUtil.assertNotNull("renderer", renderer);
190         this.renderer = renderer;
191     }
192 
193     /*
194      * @see org.seasar.uruma.component.UIComponent#setStyle(java.lang.String)
195      */
196     public void setStyle(final String style) {
197         this.style = style;
198     }
199 
200     private void setupId() {
201         if (this.id == null) {
202             setId(getClass().getName() + UrumaConstants.AT_MARK
203                     + Integer.toHexString(hashCode()));
204         }
205     }
206 
207     /*
208      * @see org.seasar.uruma.component.base.AbstractUIElement#toString()
209      */
210     @Override
211     public String toString() {
212         return getPath() + " id:" + getId();
213     }
214 }