KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nextapp > echo2 > webcontainer > syncpeer > ButtonPeer


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package nextapp.echo2.webcontainer.syncpeer;
31
32 import nextapp.echo2.app.Alignment;
33 import nextapp.echo2.app.Border;
34 import nextapp.echo2.app.CheckBox;
35 import nextapp.echo2.app.Color;
36 import nextapp.echo2.app.Component;
37 import nextapp.echo2.app.Extent;
38 import nextapp.echo2.app.FillImage;
39 import nextapp.echo2.app.Font;
40 import nextapp.echo2.app.ImageReference;
41 import nextapp.echo2.app.Insets;
42 import nextapp.echo2.app.RadioButton;
43 import nextapp.echo2.app.ResourceImageReference;
44 import nextapp.echo2.app.button.AbstractButton;
45 import nextapp.echo2.app.button.ButtonGroup;
46 import nextapp.echo2.app.button.ToggleButton;
47 import nextapp.echo2.app.update.ServerComponentUpdate;
48 import nextapp.echo2.webcontainer.ActionProcessor;
49 import nextapp.echo2.webcontainer.ContainerInstance;
50 import nextapp.echo2.webcontainer.DomUpdateSupport;
51 import nextapp.echo2.webcontainer.PropertyUpdateProcessor;
52 import nextapp.echo2.webcontainer.RenderContext;
53 import nextapp.echo2.webcontainer.ComponentSynchronizePeer;
54 import nextapp.echo2.webcontainer.image.ImageRenderSupport;
55 import nextapp.echo2.webcontainer.image.ImageTools;
56 import nextapp.echo2.webcontainer.propertyrender.AlignmentRender;
57 import nextapp.echo2.webcontainer.propertyrender.BorderRender;
58 import nextapp.echo2.webcontainer.propertyrender.ColorRender;
59 import nextapp.echo2.webcontainer.propertyrender.ExtentRender;
60 import nextapp.echo2.webcontainer.propertyrender.FillImageRender;
61 import nextapp.echo2.webcontainer.propertyrender.FontRender;
62 import nextapp.echo2.webcontainer.propertyrender.ImageReferenceRender;
63 import nextapp.echo2.webcontainer.propertyrender.InsetsRender;
64 import nextapp.echo2.webcontainer.propertyrender.LayoutDirectionRender;
65 import nextapp.echo2.webrender.ServerMessage;
66 import nextapp.echo2.webrender.Service;
67 import nextapp.echo2.webrender.WebRenderServlet;
68 import nextapp.echo2.webrender.output.CssStyle;
69 import nextapp.echo2.webrender.servermessage.DomUpdate;
70 import nextapp.echo2.webrender.service.JavaScriptService;
71
72 import org.w3c.dom.Document JavaDoc;
73 import org.w3c.dom.DocumentFragment JavaDoc;
74 import org.w3c.dom.Element JavaDoc;
75 import org.w3c.dom.Node JavaDoc;
76 import org.w3c.dom.Text JavaDoc;
77
78 /**
79  * Synchronization peer for
80  * <code>nextapp.echo2.app.AbstractButton</code>-derived components.
81  * <p>
82  * This class should not be extended or used by classes outside of the
83  * Echo framework.
84  */

85 public class ButtonPeer
86 implements ActionProcessor, DomUpdateSupport, ImageRenderSupport, PropertyUpdateProcessor, ComponentSynchronizePeer {
87
88     private static final Alignment DEFAULT_TEXT_POSITION = new Alignment(Alignment.TRAILING, Alignment.DEFAULT);
89     private static final Alignment DEFAULT_STATE_POSITION = new Alignment(Alignment.LEADING, Alignment.DEFAULT);
90     private static final Extent DEFAULT_ICON_TEXT_MARGIN = new Extent(5);
91     private static final ImageReference DEFAULT_CHECKBOX_ICON
92             = new ResourceImageReference("/nextapp/echo2/webcontainer/resource/image/CheckBoxOff.gif");
93     private static final ImageReference DEFAULT_SELECTED_CHECKBOX_ICON
94             = new ResourceImageReference("/nextapp/echo2/webcontainer/resource/image/CheckBoxOn.gif");
95     private static final ImageReference DEFAULT_RADIOBUTTON_ICON
96             = new ResourceImageReference("/nextapp/echo2/webcontainer/resource/image/RadioButtonOff.gif");
97     private static final ImageReference DEFAULT_SELECTED_RADIOBUTTON_ICON
98             = new ResourceImageReference("/nextapp/echo2/webcontainer/resource/image/RadioButtonOn.gif");
99     
100     private static final String JavaDoc[] BUTTON_INIT_KEYS = new String JavaDoc[]{"default-style", "rollover-style", "pressed-style"};
101     
102     private static final String JavaDoc IMAGE_ID_BACKGROUND = "background";
103     private static final String JavaDoc IMAGE_ID_ICON = "icon";
104     private static final String JavaDoc IMAGE_ID_ROLLOVER_BACKGROUND = "rolloverBackground";
105     private static final String JavaDoc IMAGE_ID_ROLLOVER_ICON = "rolloverIcon";
106     private static final String JavaDoc IMAGE_ID_ROLLOVER_STATE_ICON = "rolloverStateIcon";
107     private static final String JavaDoc IMAGE_ID_ROLLOVER_SELECTED_STATE_ICON = "rolloverSelectedStateIcon";
108     private static final String JavaDoc IMAGE_ID_PRESSED_BACKGROUND = "pressedBackground";
109     private static final String JavaDoc IMAGE_ID_PRESSED_ICON = "pressedIcon";
110     private static final String JavaDoc IMAGE_ID_PRESSED_STATE_ICON = "pressedStateIcon";
111     private static final String JavaDoc IMAGE_ID_PRESSED_SELECTED_STATE_ICON = "pressedSelectedStateIcon";
112     private static final String JavaDoc IMAGE_ID_STATE_ICON = "stateIcon";
113     private static final String JavaDoc IMAGE_ID_SELECTED_STATE_ICON = "selectedStateIcon";
114     
115     private static final String JavaDoc CONTAINER_TABLE_CSS_TEXT_DEFAULT = "border:0px none;border-collapse:collapse;";
116     private static final String JavaDoc CONTAINER_TABLE_CSS_TEXT_LEFT = "border:0px none;border-collapse:collapse; margin: 0 auto 0 0";
117     private static final String JavaDoc CONTAINER_TABLE_CSS_TEXT_CENTER = "border:0px none;border-collapse:collapse; margin: 0 auto;";
118     private static final String JavaDoc CONTAINER_TABLE_CSS_TEXT_RIGHT = "border:0px none;border-collapse:collapse; margin: 0 0 0 auto;";
119     
120     /**
121      * Service to provide supporting JavaScript library.
122      */

123     private static final Service BUTTON_SERVICE = JavaScriptService.forResource("Echo.Button",
124             "/nextapp/echo2/webcontainer/resource/js/Button.js");
125
126     static {
127         WebRenderServlet.getServiceRegistry().add(BUTTON_SERVICE);
128     }
129     
130     /**
131      * Determines the CSS text which should be placed in the 'style' attribute
132      * of the button's container TABLE element.
133      *
134      * @param button the rendering <code>AbstractButton</code>
135      * @return the CSS text
136      */

137     private static String JavaDoc getContainerTableCssText(AbstractButton button) {
138         Alignment alignment = (Alignment) button.getRenderProperty(AbstractButton.PROPERTY_ALIGNMENT);
139         if (alignment != null) {
140             int horizontal = AlignmentRender.getRenderedHorizontal(alignment, button);
141             switch (horizontal) {
142             case Alignment.LEFT:
143                 return CONTAINER_TABLE_CSS_TEXT_LEFT;
144             case Alignment.CENTER:
145                 return CONTAINER_TABLE_CSS_TEXT_CENTER;
146             case Alignment.RIGHT:
147                 return CONTAINER_TABLE_CSS_TEXT_RIGHT;
148             }
149         }
150         return CONTAINER_TABLE_CSS_TEXT_DEFAULT;
151     }
152     
153     /**
154      * @see nextapp.echo2.webcontainer.ComponentSynchronizePeer#getContainerId(nextapp.echo2.app.Component)
155      */

156     public String JavaDoc getContainerId(Component child) {
157         throw new UnsupportedOperationException JavaDoc("Component does not support children.");
158     }
159     
160     /**
161      * Combines the properties of two <code>Alignment</code> objects together.
162      * Properties of the <code>secondary</code> object will override default
163      * properties of the <code>primary</code>.
164      *
165      * @param primary the first <code>Alignment</code> (may be null)
166      * @param secondary the second <code>Alignment</code> (may be null)
167      * @return a new <code>Alignment</code> combining the values of both (or
168      * null if both input <code>Alignment</code>s were null
169      */

170     private Alignment combineAlignment(Alignment primary, Alignment secondary) {
171         if (primary == null) {
172             return secondary;
173         } else if (secondary == null) {
174             return primary;
175         }
176         int horizontal = primary.getHorizontal();
177         int vertical = primary.getVertical();
178         Alignment alignment = new Alignment(horizontal == Alignment.DEFAULT ? secondary.getHorizontal() : horizontal,
179                 vertical == Alignment.DEFAULT ? secondary.getVertical() : vertical);
180         return alignment;
181     }
182     
183     /**
184      * @see nextapp.echo2.webcontainer.image.ImageRenderSupport#getImage(nextapp.echo2.app.Component,
185      * java.lang.String)
186      */

187     public ImageReference getImage(Component component, String JavaDoc imageId) {
188         if (IMAGE_ID_ICON.equals(imageId)) {
189             if (component.isRenderEnabled()) {
190                 return (ImageReference) component.getRenderProperty(AbstractButton.PROPERTY_ICON);
191             } else {
192                 ImageReference icon = (ImageReference) component.getRenderProperty(AbstractButton.PROPERTY_DISABLED_ICON);
193                 if (icon == null) {
194                     icon = (ImageReference) component.getRenderProperty(AbstractButton.PROPERTY_ICON);
195                 }
196                 return icon;
197             }
198         } else if (IMAGE_ID_ROLLOVER_ICON.equals(imageId)) {
199             return (ImageReference) component.getRenderProperty(AbstractButton.PROPERTY_ROLLOVER_ICON);
200         } else if (IMAGE_ID_PRESSED_ICON.equals(imageId)) {
201             return (ImageReference) component.getRenderProperty(AbstractButton.PROPERTY_PRESSED_ICON);
202         } else if (IMAGE_ID_STATE_ICON.equals(imageId)) {
203             return getStateIcon((ToggleButton) component);
204         } else if (IMAGE_ID_SELECTED_STATE_ICON.equals(imageId)) {
205             return getSelectedStateIcon((ToggleButton) component);
206         } else if (IMAGE_ID_BACKGROUND.equals(imageId)) {
207             FillImage backgroundImage;
208             if (component.isRenderEnabled()) {
209                 backgroundImage = (FillImage) component.getRenderProperty(AbstractButton.PROPERTY_BACKGROUND_IMAGE);
210             } else {
211                 backgroundImage = (FillImage) component.getRenderProperty(AbstractButton.PROPERTY_DISABLED_BACKGROUND_IMAGE);
212                 if (backgroundImage == null) {
213                     backgroundImage = (FillImage) component.getRenderProperty(AbstractButton.PROPERTY_BACKGROUND_IMAGE);
214                 }
215             }
216             if (backgroundImage == null) {
217                 return null;
218             } else {
219                 return backgroundImage.getImage();
220             }
221         } else if (IMAGE_ID_ROLLOVER_BACKGROUND.equals(imageId)) {
222             FillImage backgroundImage
223                     = (FillImage) component.getRenderProperty(AbstractButton.PROPERTY_ROLLOVER_BACKGROUND_IMAGE);
224             if (backgroundImage == null) {
225                 return null;
226             } else {
227                 return backgroundImage.getImage();
228             }
229         } else if (IMAGE_ID_PRESSED_BACKGROUND.equals(imageId)) {
230             FillImage backgroundImage
231                     = (FillImage) component.getRenderProperty(AbstractButton.PROPERTY_PRESSED_BACKGROUND_IMAGE);
232             if (backgroundImage == null) {
233                 return null;
234             } else {
235                 return backgroundImage.getImage();
236             }
237         } else if (IMAGE_ID_ROLLOVER_STATE_ICON.equals(imageId)) {
238             ImageReference icon = (ImageReference) component.getRenderProperty(ToggleButton.PROPERTY_ROLLOVER_STATE_ICON);
239             return icon == null ? getStateIcon((ToggleButton) component) : icon;
240         } else if (IMAGE_ID_ROLLOVER_SELECTED_STATE_ICON.equals(imageId)) {
241             ImageReference icon = (ImageReference) component.getRenderProperty(ToggleButton.PROPERTY_ROLLOVER_SELECTED_STATE_ICON);
242             return icon == null ? getSelectedStateIcon((ToggleButton) component) : icon;
243         } else if (IMAGE_ID_PRESSED_STATE_ICON.equals(imageId)) {
244             ImageReference icon = (ImageReference) component.getRenderProperty(ToggleButton.PROPERTY_PRESSED_STATE_ICON);
245             return icon == null ? getStateIcon((ToggleButton) component) : icon;
246         } else if (IMAGE_ID_PRESSED_SELECTED_STATE_ICON.equals(imageId)) {
247             ImageReference icon = (ImageReference) component.getRenderProperty(ToggleButton.PROPERTY_PRESSED_SELECTED_STATE_ICON);
248             return icon == null ? getSelectedStateIcon((ToggleButton) component) : icon;
249         } else {
250             return null;
251         }
252     }
253
254     /**
255      * Determines the selected state icon of the specified
256      * <code>ToggleButton</code>.
257      *
258      * @param toggleButton the <code>ToggleButton</code>
259      * @return the selected state icon
260      */

261     private ImageReference getSelectedStateIcon(ToggleButton toggleButton) {
262         ImageReference selectedStateIcon
263                 = (ImageReference) toggleButton.getRenderProperty(ToggleButton.PROPERTY_SELECTED_STATE_ICON);
264         if (selectedStateIcon == null) {
265             if (toggleButton instanceof CheckBox) {
266                 selectedStateIcon = DEFAULT_SELECTED_CHECKBOX_ICON;
267             } else if (toggleButton instanceof RadioButton) {
268                 selectedStateIcon = DEFAULT_SELECTED_RADIOBUTTON_ICON;
269             }
270         }
271         return selectedStateIcon;
272     }
273     
274     /**
275      * Determines the default (non-selected) state icon of the specified
276      * <code>ToggleButton</code>.
277      *
278      * @param toggleButton the <code>ToggleButton</code>
279      * @return the state icon
280      */

281     private ImageReference getStateIcon(ToggleButton toggleButton) {
282         ImageReference stateIcon = (ImageReference) toggleButton.getRenderProperty(ToggleButton.PROPERTY_STATE_ICON);
283         if (stateIcon == null) {
284             if (toggleButton instanceof CheckBox) {
285                 stateIcon = DEFAULT_CHECKBOX_ICON;
286             } else if (toggleButton instanceof RadioButton) {
287                 stateIcon = DEFAULT_RADIOBUTTON_ICON;
288             }
289         }
290         return stateIcon;
291     }
292     
293     /**
294      * @see nextapp.echo2.webcontainer.ActionProcessor#processAction(nextapp.echo2.webcontainer.ContainerInstance,
295      * nextapp.echo2.app.Component, org.w3c.dom.Element)
296      */

297     public void processAction(ContainerInstance ci, Component component, Element JavaDoc actionElement) {
298         ci.getUpdateManager().getClientUpdateManager().setComponentAction(component, AbstractButton.INPUT_CLICK, null);
299     }
300
301     /**
302      * @see nextapp.echo2.webcontainer.PropertyUpdateProcessor#processPropertyUpdate(
303      * nextapp.echo2.webcontainer.ContainerInstance, nextapp.echo2.app.Component, org.w3c.dom.Element)
304      */

305     public void processPropertyUpdate(ContainerInstance ci, Component component, Element JavaDoc propertyElement) {
306         
307         String JavaDoc propertyName = propertyElement.getAttribute(PropertyUpdateProcessor.PROPERTY_NAME);
308         if (ToggleButton.SELECTED_CHANGED_PROPERTY.equals(propertyName)) {
309             Boolean JavaDoc propertyValue = new Boolean JavaDoc("true".equals(propertyElement.getAttribute("value")));
310             ci.getUpdateManager().getClientUpdateManager().setComponentProperty(component,
311                     ToggleButton.SELECTED_CHANGED_PROPERTY, propertyValue);
312         }
313     }
314
315     /**
316      * @see nextapp.echo2.webcontainer.ComponentSynchronizePeer#renderAdd(nextapp.echo2.webcontainer.RenderContext,
317      * nextapp.echo2.app.update.ServerComponentUpdate, java.lang.String, nextapp.echo2.app.Component)
318      */

319     public void renderAdd(RenderContext rc, ServerComponentUpdate update, String JavaDoc targetId, Component component) {
320         Element JavaDoc domAddElement = DomUpdate.renderElementAdd(rc.getServerMessage());
321         DocumentFragment JavaDoc htmlFragment = rc.getServerMessage().getDocument().createDocumentFragment();
322         renderHtml(rc, update, htmlFragment, component);
323         DomUpdate.renderElementAddContent(rc.getServerMessage(), domAddElement, targetId, htmlFragment);
324     }
325     
326     /**
327      * Renders the containing DIV element of a button.
328      *
329      * @param rc the relevant <code>RenderContext</code>
330      * @param parentNode the parent node
331      * @param button the <code>AbstractButton</code> being rendered
332      * @return the rendered DIV element (note that this element will already
333      * have been appended to the parent)
334      */

335     private Element JavaDoc renderButtonContainer(RenderContext rc, Node JavaDoc parentNode, AbstractButton button) {
336         Element JavaDoc divElement = parentNode.getOwnerDocument().createElement("div");
337         divElement.setAttribute("id", ContainerInstance.getElementId(button));
338         
339         if (button.isFocusTraversalParticipant()) {
340             divElement.setAttribute("tabindex", Integer.toString(button.getFocusTraversalIndex()));
341         } else {
342             divElement.setAttribute("tabindex", "-1");
343         }
344         
345         boolean renderEnabled = button.isRenderEnabled();
346         
347         String JavaDoc toolTipText = (String JavaDoc) button.getRenderProperty(AbstractButton.PROPERTY_TOOL_TIP_TEXT);
348         if (renderEnabled && toolTipText != null) {
349             divElement.setAttribute("title", toolTipText);
350         }
351         divElement.setAttribute("style", "visibility:hidden;");
352         
353         parentNode.appendChild(divElement);
354         return divElement;
355     }
356     
357     /**
358      * Renders the content of the button, i.e., its text, icon, and/or state icon.
359      *
360      * @param rc the relevant <code>RenderContext</code>
361      * @param buttonContainerElement the <code>Element</code> which will
362      * contain the content
363      * @param button the <code>AbstractButton</code> being rendered
364      */

365     private void renderButtonContent(RenderContext rc, Element JavaDoc buttonContainerElement, AbstractButton button) {
366         Node JavaDoc contentNode;
367         Document JavaDoc document = rc.getServerMessage().getDocument();
368         ToggleButton toggleButton = button instanceof ToggleButton ? (ToggleButton) button : null;
369         String JavaDoc elementId = ContainerInstance.getElementId(button);
370         
371         String JavaDoc text = (String JavaDoc) button.getRenderProperty(AbstractButton.PROPERTY_TEXT);
372         ImageReference icon = (ImageReference) button.getRenderProperty(AbstractButton.PROPERTY_ICON);
373         
374         // Create entities.
375
Text JavaDoc textNode = text == null ? null : rc.getServerMessage().getDocument().createTextNode(
376                 (String JavaDoc) button.getRenderProperty(AbstractButton.PROPERTY_TEXT));
377         
378         Element JavaDoc iconElement;
379         if (icon == null) {
380             iconElement = null;
381         } else {
382             iconElement = ImageReferenceRender.renderImageReferenceElement(rc, ButtonPeer.this, button,
383                     IMAGE_ID_ICON);
384             iconElement.setAttribute("id", elementId + "_icon");
385         }
386
387         Element JavaDoc stateIconElement;
388         if (toggleButton == null) {
389             stateIconElement = null;
390         } else {
391             stateIconElement = ImageReferenceRender.renderImageReferenceElement(rc, ButtonPeer.this, button,
392                     toggleButton.isSelected() ? IMAGE_ID_SELECTED_STATE_ICON : IMAGE_ID_STATE_ICON);
393             stateIconElement.setAttribute("id", elementId + "_stateicon");
394         }
395         
396         int entityCount = (textNode == null ? 0 : 1) + (iconElement == null ? 0 : 1) + (stateIconElement == null ? 0 : 1);
397         
398         Extent iconTextMargin;
399         Alignment textPosition;
400         Element JavaDoc tableElement;
401         
402         switch (entityCount) {
403         case 1:
404             if (textNode != null) {
405                 contentNode = textNode;
406             } else if (iconElement != null) {
407                 contentNode = iconElement;
408             } else { // stateIconElement must not be null.
409
contentNode = stateIconElement;
410             }
411             break;
412         case 2:
413             iconTextMargin = (Extent) button.getRenderProperty(AbstractButton.PROPERTY_ICON_TEXT_MARGIN,
414                     DEFAULT_ICON_TEXT_MARGIN);
415             TriCellTable tct;
416             textPosition = (Alignment) button.getRenderProperty(AbstractButton.PROPERTY_TEXT_POSITION,
417                     DEFAULT_TEXT_POSITION);
418             if (stateIconElement == null) {
419                 // Not rendering a ToggleButton.
420
int orientation = TriCellTableConfigurator.convertIconTextPositionToOrientation(textPosition, button);
421                 tct = new TriCellTable(rc, document, elementId, orientation, iconTextMargin);
422                 
423                 renderCellText(tct, textNode, button);
424                 renderCellIcon(tct, iconElement, 1, button);
425             } else {
426                  // Rendering a ToggleButton.
427
Extent stateMargin = (Extent) button.getRenderProperty(ToggleButton.PROPERTY_STATE_MARGIN,
428                         DEFAULT_ICON_TEXT_MARGIN);
429                 Alignment statePosition = (Alignment) button.getRenderProperty(ToggleButton.PROPERTY_STATE_POSITION,
430                         DEFAULT_STATE_POSITION);
431                 int orientation = TriCellTableConfigurator.convertStatePositionToOrientation(statePosition, button);
432                 tct = new TriCellTable(rc, document, elementId, orientation, stateMargin);
433
434                 if (textNode == null) {
435                     renderCellIcon(tct, iconElement, 0, button);
436                 } else {
437                     renderCellText(tct, textNode, button);
438                 }
439                 renderCellState(tct, stateIconElement, 1, button);
440             }
441
442             tct.addCellCssText("padding:0px;");
443             tableElement = tct.getTableElement();
444             tableElement.setAttribute("id", elementId + "_table");
445             tableElement.setAttribute("style", getContainerTableCssText(button));
446             contentNode = tableElement;
447             break;
448         case 3:
449             iconTextMargin = (Extent) button.getRenderProperty(AbstractButton.PROPERTY_ICON_TEXT_MARGIN,
450                     DEFAULT_ICON_TEXT_MARGIN);
451             textPosition = (Alignment) button.getRenderProperty(AbstractButton.PROPERTY_TEXT_POSITION,
452                     DEFAULT_TEXT_POSITION);
453             Extent stateMargin = (Extent) button.getRenderProperty(ToggleButton.PROPERTY_STATE_MARGIN,
454                     DEFAULT_ICON_TEXT_MARGIN);
455             Alignment statePosition = (Alignment) button.getRenderProperty(ToggleButton.PROPERTY_STATE_POSITION,
456                     DEFAULT_STATE_POSITION);
457             int stateOrientation = TriCellTableConfigurator.convertStatePositionToOrientation(statePosition, button);
458             int orientation = TriCellTableConfigurator.convertIconTextPositionToOrientation(textPosition, button);
459             tct = new TriCellTable(rc, document, elementId, orientation, iconTextMargin, stateOrientation, stateMargin);
460
461             renderCellText(tct, textNode, button);
462             renderCellIcon(tct, iconElement, 1, button);
463             renderCellState(tct, stateIconElement, 2, button);
464
465             tct.addCellCssText("padding:0px;");
466             tableElement = tct.getTableElement();
467             tableElement.setAttribute("id", elementId + "_table");
468             tableElement.setAttribute("style", getContainerTableCssText(button));
469             contentNode = tableElement;
470             break;
471         default:
472             // 0 element button.
473
contentNode = null;
474         }
475         
476         if (contentNode != null) {
477             buttonContainerElement.appendChild(contentNode);
478         }
479     }
480     
481     /**
482      * Renders the content of the <code>TriCellTable</code> cell which
483      * contains the button's icon.
484      *
485      * @param tct the <code>TriCellTable</code> to update
486      * @param iconElement the icon element
487      * @param cellIndex the index of the cell in the <code>TriCellTable</code>
488      * that should contain the icon
489      */

490     private void renderCellIcon(TriCellTable tct, Element JavaDoc iconElement, int cellIndex, AbstractButton button) {
491         Element JavaDoc iconTdElement = tct.getTdElement(cellIndex);
492         Alignment alignment = (Alignment) button.getRenderProperty(AbstractButton.PROPERTY_ALIGNMENT);
493         if (alignment != null) {
494             CssStyle style = new CssStyle();
495             AlignmentRender.renderToStyle(style, alignment, button);
496             iconTdElement.setAttribute("style", style.renderInline());
497         }
498         iconTdElement.appendChild(iconElement);
499     }
500     
501     /**
502      * Renders the content of the <code>TriCellTable</code> cell which
503      * contains the button's state icon.
504      *
505      * @param tct the <code>TriCellTable</code> to update
506      * @param stateIconElement the state icon element
507      * @param cellIndex the index of the cell in the <code>TriCellTable</code>
508      * that should contain the state icon
509      * @param button the <code>AbstractButton</code> being rendered
510      */

511     private void renderCellState(TriCellTable tct, Element JavaDoc stateIconElement, int cellIndex, AbstractButton button) {
512         Element JavaDoc stateTdElement = tct.getTdElement(cellIndex);
513         CssStyle stateTdCssStyle = new CssStyle();
514         AlignmentRender.renderToStyle(stateTdCssStyle,
515                 (Alignment) button.getRenderProperty(ToggleButton.PROPERTY_STATE_ALIGNMENT), button);
516         stateTdElement.setAttribute("style", stateTdCssStyle.renderInline());
517         stateTdElement.appendChild(stateIconElement);
518     }
519     
520     /**
521      * Renders the content of the <code>TriCellTable</code> cell which
522      * contains the button's text.
523      * Text is always rendered in cell #0 of the table.
524      *
525      * @param tct the <code>TriCellTable</code> to update
526      * @param textNode the text
527      * @param button the <code>AbstractButton</code> being rendered
528      */

529     private void renderCellText(TriCellTable tct, Text JavaDoc textNode, AbstractButton button) {
530         Element JavaDoc textTdElement = tct.getTdElement(0);
531         CssStyle textTdCssStyle = new CssStyle();
532         
533         if (Boolean.FALSE.equals(button.getRenderProperty(AbstractButton.PROPERTY_LINE_WRAP))) {
534             textTdCssStyle.setAttribute("white-space", "nowrap");
535         }
536         
537         Alignment alignment = combineAlignment((Alignment) button.getRenderProperty(AbstractButton.PROPERTY_TEXT_ALIGNMENT),
538                 (Alignment) button.getRenderProperty(AbstractButton.PROPERTY_ALIGNMENT));
539         AlignmentRender.renderToStyle(textTdCssStyle, alignment, button);
540         
541         if (textTdCssStyle.hasAttributes()) {
542             textTdElement.setAttribute("style", textTdCssStyle.renderInline());
543         }
544         
545         textTdElement.appendChild(textNode);
546     }
547     
548     /**
549      * Render default CSS style.
550      */

551     private CssStyle renderDefaultStyle(RenderContext rc, AbstractButton button) {
552         CssStyle cssStyle = new CssStyle();
553         LayoutDirectionRender.renderToStyle(cssStyle, button.getLayoutDirection(), button.getLocale());
554         ExtentRender.renderToStyle(cssStyle, "width", (Extent) button.getRenderProperty(AbstractButton.PROPERTY_WIDTH));
555         Extent height = (Extent) button.getRenderProperty(AbstractButton.PROPERTY_HEIGHT);
556         if (height != null) {
557             ExtentRender.renderToStyle(cssStyle, "height", height);
558             cssStyle.setAttribute("overflow", "hidden");
559         }
560         if (Boolean.FALSE.equals(button.getRenderProperty(AbstractButton.PROPERTY_LINE_WRAP))) {
561             cssStyle.setAttribute("white-space", "nowrap");
562         }
563
564         boolean renderEnabled = button.isRenderEnabled();
565
566         Border border;
567         Color foreground, background;
568         Font font;
569         FillImage backgroundImage;
570         if (!renderEnabled) {
571             // Retrieve disabled style information.
572
background = (Color) button.getRenderProperty(AbstractButton.PROPERTY_DISABLED_BACKGROUND);
573             backgroundImage = (FillImage) button.getRenderProperty(AbstractButton.PROPERTY_DISABLED_BACKGROUND_IMAGE);
574             border = (Border) button.getRenderProperty(AbstractButton.PROPERTY_DISABLED_BORDER);
575             font = (Font) button.getRenderProperty(AbstractButton.PROPERTY_DISABLED_FONT);
576             foreground = (Color) button.getRenderProperty(AbstractButton.PROPERTY_DISABLED_FOREGROUND);
577
578             // Fallback to normal styles.
579
if (background == null) {
580                 background = (Color) button.getRenderProperty(AbstractButton.PROPERTY_BACKGROUND);
581                 if (backgroundImage == null) {
582                     // Special case:
583
// Disabled background without disabled background image will render disabled background instead of
584
// normal background image.
585
backgroundImage = (FillImage) button.getRenderProperty(AbstractButton.PROPERTY_BACKGROUND_IMAGE);
586                 }
587             }
588             if (border == null) {
589                 border = (Border) button.getRenderProperty(AbstractButton.PROPERTY_BORDER);
590             }
591             if (font == null) {
592                 font = (Font) button.getRenderProperty(AbstractButton.PROPERTY_FONT);
593             }
594             if (foreground == null) {
595                 foreground = (Color) button.getRenderProperty(AbstractButton.PROPERTY_FOREGROUND);
596             }
597         } else {
598             border = (Border) button.getRenderProperty(AbstractButton.PROPERTY_BORDER);
599             foreground = (Color) button.getRenderProperty(AbstractButton.PROPERTY_FOREGROUND);
600             background = (Color) button.getRenderProperty(AbstractButton.PROPERTY_BACKGROUND);
601             font = (Font) button.getRenderProperty(AbstractButton.PROPERTY_FONT);
602             backgroundImage = (FillImage) button.getRenderProperty(AbstractButton.PROPERTY_BACKGROUND_IMAGE);
603         }
604         
605         BorderRender.renderToStyle(cssStyle, border);
606         ColorRender.renderToStyle(cssStyle, foreground, background);
607         FontRender.renderToStyle(cssStyle, font);
608         FillImageRender.renderToStyle(cssStyle, rc, this, button, IMAGE_ID_BACKGROUND, backgroundImage,
609                 FillImageRender.FLAG_DISABLE_FIXED_MODE);
610         InsetsRender.renderToStyle(cssStyle, "padding", (Insets) button.getRenderProperty(AbstractButton.PROPERTY_INSETS));
611         
612         AlignmentRender.renderToStyle(cssStyle,
613                 (Alignment) button.getRenderProperty(AbstractButton.PROPERTY_ALIGNMENT), button);
614         return cssStyle;
615     }
616     
617     /**
618      * @see nextapp.echo2.webcontainer.ComponentSynchronizePeer#renderDispose(nextapp.echo2.webcontainer.RenderContext,
619      * nextapp.echo2.app.update.ServerComponentUpdate, nextapp.echo2.app.Component)
620      */

621     public void renderDispose(RenderContext rc, ServerComponentUpdate update, Component component) {
622         rc.getServerMessage().addLibrary(BUTTON_SERVICE.getId());
623         renderDisposeDirective(rc, (AbstractButton) component);
624     }
625
626     /**
627      * Renders a directive to the outgoing <code>ServerMessage</code> to
628      * dispose the state of a button, performing tasks such as unregistering
629      * event listeners on the client.
630      *
631      * @param rc the relevant <code>RenderContext</code>
632      * @param button the button
633      */

634     private void renderDisposeDirective(RenderContext rc, AbstractButton button) {
635         ServerMessage serverMessage = rc.getServerMessage();
636         Element JavaDoc itemizedUpdateElement = serverMessage.getItemizedDirective(ServerMessage.GROUP_ID_PREREMOVE,
637                 "EchoButton.MessageProcessor", "dispose", new String JavaDoc[0], new String JavaDoc[0]);
638         Element JavaDoc itemElement = serverMessage.getDocument().createElement("item");
639         itemElement.setAttribute("eid", ContainerInstance.getElementId(button));
640         itemizedUpdateElement.appendChild(itemElement);
641     }
642     
643     /**
644      * @see nextapp.echo2.webcontainer.DomUpdateSupport#renderHtml(nextapp.echo2.webcontainer.RenderContext,
645      * nextapp.echo2.app.update.ServerComponentUpdate, org.w3c.dom.Node, nextapp.echo2.app.Component)
646      */

647     public void renderHtml(RenderContext rc, ServerComponentUpdate update, Node JavaDoc parentNode, Component component) {
648         ServerMessage serverMessage = rc.getServerMessage();
649         serverMessage.addLibrary(BUTTON_SERVICE.getId());
650         AbstractButton button = (AbstractButton) component;
651         Element JavaDoc containerDivElement = renderButtonContainer(rc, parentNode, button);
652         renderInitDirective(rc, button);
653         renderButtonContent(rc, containerDivElement, button);
654     }
655
656     /**
657      * Renders a directive to the outgoing <code>ServerMessage</code> to
658      * initialize the state of a button, performing tasks such as registering
659      * event listeners on the client.
660      *
661      * @param rc the relevant <code>RenderContext</code>
662      * @param button the button
663      */

664     private void renderInitDirective(RenderContext rc, AbstractButton button) {
665         String JavaDoc elementId = ContainerInstance.getElementId(button);
666         ServerMessage serverMessage = rc.getServerMessage();
667         FillImage backgroundImage = (FillImage) button.getRenderProperty(AbstractButton.PROPERTY_BACKGROUND_IMAGE);
668         
669         boolean rolloverEnabled = ((Boolean JavaDoc) button.getRenderProperty(AbstractButton.PROPERTY_ROLLOVER_ENABLED,
670                 Boolean.FALSE)).booleanValue();
671         boolean pressedEnabled = ((Boolean JavaDoc) button.getRenderProperty(AbstractButton.PROPERTY_PRESSED_ENABLED,
672                 Boolean.FALSE)).booleanValue();
673         
674         String JavaDoc pressedStyle = "";
675         String JavaDoc rolloverStyle = "";
676         
677         String JavaDoc defaultIconUri = null;
678         String JavaDoc rolloverIconUri = null;
679         String JavaDoc pressedIconUri = null;
680         
681         if (rolloverEnabled || pressedEnabled) {
682             boolean hasIcon = button.getRenderProperty(AbstractButton.PROPERTY_ICON) != null;
683             if (hasIcon) {
684                 defaultIconUri = ImageTools.getUri(rc, this, button, IMAGE_ID_ICON);
685             }
686             
687             if (rolloverEnabled) {
688                 CssStyle rolloverCssStyle = new CssStyle();
689                 BorderRender.renderToStyle(rolloverCssStyle,
690                         (Border) button.getRenderProperty(AbstractButton.PROPERTY_ROLLOVER_BORDER));
691                 ColorRender.renderToStyle(rolloverCssStyle,
692                         (Color) button.getRenderProperty(AbstractButton.PROPERTY_ROLLOVER_FOREGROUND),
693                         (Color) button.getRenderProperty(AbstractButton.PROPERTY_ROLLOVER_BACKGROUND));
694                 FontRender.renderToStyle(rolloverCssStyle,
695                         (Font) button.getRenderProperty(AbstractButton.PROPERTY_ROLLOVER_FONT));
696                 if (backgroundImage != null) {
697                     FillImageRender.renderToStyle(rolloverCssStyle, rc, this, button, IMAGE_ID_ROLLOVER_BACKGROUND,
698                             (FillImage) button.getRenderProperty(AbstractButton.PROPERTY_ROLLOVER_BACKGROUND_IMAGE),
699                             FillImageRender.FLAG_DISABLE_FIXED_MODE);
700                 }
701                 if (rolloverCssStyle.hasAttributes()) {
702                     rolloverStyle = rolloverCssStyle.renderInline();
703                 }
704                 if (hasIcon) {
705                     ImageReference rolloverIcon = (ImageReference) button.getRenderProperty(AbstractButton.PROPERTY_ROLLOVER_ICON);
706                     if (rolloverIcon != null) {
707                         rolloverIconUri = ImageTools.getUri(rc, this, button, IMAGE_ID_ROLLOVER_ICON);
708                     }
709                 }
710             }
711             
712             if (pressedEnabled) {
713                 CssStyle pressedCssStyle = new CssStyle();
714                 BorderRender.renderToStyle(pressedCssStyle,
715                         (Border) button.getRenderProperty(AbstractButton.PROPERTY_PRESSED_BORDER));
716                 ColorRender.renderToStyle(pressedCssStyle,
717                         (Color) button.getRenderProperty(AbstractButton.PROPERTY_PRESSED_FOREGROUND),
718                         (Color) button.getRenderProperty(AbstractButton.PROPERTY_PRESSED_BACKGROUND));
719                 FontRender.renderToStyle(pressedCssStyle,
720                         (Font) button.getRenderProperty(AbstractButton.PROPERTY_PRESSED_FONT));
721                 if (backgroundImage != null) {
722                     FillImageRender.renderToStyle(pressedCssStyle, rc, this, button, IMAGE_ID_PRESSED_BACKGROUND,
723                             (FillImage) button.getRenderProperty(AbstractButton.PROPERTY_PRESSED_BACKGROUND_IMAGE),
724                             FillImageRender.FLAG_DISABLE_FIXED_MODE);
725                 }
726                 if (pressedCssStyle.hasAttributes()) {
727                     pressedStyle = pressedCssStyle.renderInline();
728                 }
729                 if (hasIcon) {
730                     ImageReference pressedIcon = (ImageReference) button.getRenderProperty(AbstractButton.PROPERTY_PRESSED_ICON);
731                     if (pressedIcon != null) {
732                         pressedIconUri = ImageTools.getUri(rc, this, button, IMAGE_ID_PRESSED_ICON);
733                     }
734                 }
735             }
736         }
737         
738         CssStyle defaultCssStyle = renderDefaultStyle(rc, button);
739         String JavaDoc defaultStyle = defaultCssStyle.renderInline();
740         
741         Element JavaDoc itemizedUpdateElement = serverMessage.getItemizedDirective(ServerMessage.GROUP_ID_POSTUPDATE,
742                 "EchoButton.MessageProcessor", "init", BUTTON_INIT_KEYS, new String JavaDoc[]{defaultStyle, rolloverStyle, pressedStyle});
743         Element JavaDoc itemElement = serverMessage.getDocument().createElement("item");
744         itemElement.setAttribute("eid", elementId);
745         if (defaultIconUri != null) {
746             itemElement.setAttribute("default-icon", defaultIconUri);
747         }
748         if (rolloverIconUri != null) {
749             itemElement.setAttribute("rollover-icon", rolloverIconUri);
750         }
751         if (pressedIconUri != null) {
752             itemElement.setAttribute("pressed-icon", pressedIconUri);
753         }
754         if (!button.hasActionListeners()) {
755             itemElement.setAttribute("server-notify", "false");
756         }
757         if (!button.isRenderEnabled()) {
758             itemElement.setAttribute("enabled", "false");
759         }
760
761         if (button instanceof ToggleButton) {
762             ToggleButton toggleButton = (ToggleButton) button;
763             itemElement.setAttribute("toggle", "true");
764             itemElement.setAttribute("selected", toggleButton.isSelected() ? "true" : "false");
765             itemElement.setAttribute("state-icon", ImageTools.getUri(rc, this, toggleButton, IMAGE_ID_STATE_ICON));
766             itemElement.setAttribute("selected-state-icon", ImageTools.getUri(rc, this, toggleButton,
767                     IMAGE_ID_SELECTED_STATE_ICON));
768             
769             if (rolloverEnabled && toggleButton.getRenderProperty(ToggleButton.PROPERTY_ROLLOVER_STATE_ICON) != null
770                     && toggleButton.getRenderProperty(ToggleButton.PROPERTY_ROLLOVER_SELECTED_STATE_ICON) != null) {
771                 itemElement.setAttribute("rollover-state-icon",
772                         ImageTools.getUri(rc, this, toggleButton, IMAGE_ID_ROLLOVER_STATE_ICON));
773                 itemElement.setAttribute("rollover-selected-state-icon",
774                         ImageTools.getUri(rc, this, toggleButton, IMAGE_ID_ROLLOVER_SELECTED_STATE_ICON));
775             }
776             if (pressedEnabled && toggleButton.getRenderProperty(ToggleButton.PROPERTY_PRESSED_STATE_ICON) != null
777                     && toggleButton.getRenderProperty(ToggleButton.PROPERTY_PRESSED_SELECTED_STATE_ICON) != null) {
778                 itemElement.setAttribute("pressed-state-icon",
779                         ImageTools.getUri(rc, this, toggleButton, IMAGE_ID_PRESSED_STATE_ICON));
780                 itemElement.setAttribute("pressed-selected-state-icon",
781                         ImageTools.getUri(rc, this, toggleButton, IMAGE_ID_PRESSED_SELECTED_STATE_ICON));
782             }
783             
784             if (button instanceof RadioButton) {
785                 ButtonGroup buttonGroup = ((RadioButton) toggleButton).getGroup();
786                 if (buttonGroup != null) {
787                     rc.getContainerInstance().getIdTable().register(buttonGroup);
788                     itemElement.setAttribute("group", buttonGroup.getRenderId());
789                 }
790             }
791         }
792         
793         itemizedUpdateElement.appendChild(itemElement);
794     }
795     
796     /**
797      * @see nextapp.echo2.webcontainer.ComponentSynchronizePeer#renderUpdate(nextapp.echo2.webcontainer.RenderContext,
798      * nextapp.echo2.app.update.ServerComponentUpdate, java.lang.String)
799      */

800     public boolean renderUpdate(RenderContext rc, ServerComponentUpdate update, String JavaDoc targetId) {
801         String JavaDoc parentId = ContainerInstance.getElementId(update.getParent());
802         DomUpdate.renderElementRemove(rc.getServerMessage(), parentId);
803         renderAdd(rc, update, targetId, update.getParent());
804         return false;
805     }
806 }
807
Popular Tags