001: /*
002: * This file is part of the Echo Web Application Framework (hereinafter "Echo").
003: * Copyright (C) 2002-2005 NextApp, Inc.
004: *
005: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006: *
007: * The contents of this file are subject to the Mozilla Public License Version
008: * 1.1 (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: * http://www.mozilla.org/MPL/
011: *
012: * Software distributed under the License is distributed on an "AS IS" basis,
013: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014: * for the specific language governing rights and limitations under the
015: * License.
016: *
017: * Alternatively, the contents of this file may be used under the terms of
018: * either the GNU General Public License Version 2 or later (the "GPL"), or
019: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020: * in which case the provisions of the GPL or the LGPL are applicable instead
021: * of those above. If you wish to allow use of your version of this file only
022: * under the terms of either the GPL or the LGPL, and not to allow others to
023: * use your version of this file under the terms of the MPL, indicate your
024: * decision by deleting the provisions above and replace them with the notice
025: * and other provisions required by the GPL or the LGPL. If you do not delete
026: * the provisions above, a recipient may use your version of this file under
027: * the terms of any one of the MPL, the GPL or the LGPL.
028: */
029:
030: package nextapp.echo2.webcontainer.syncpeer;
031:
032: import org.w3c.dom.DocumentFragment;
033: import org.w3c.dom.Element;
034:
035: import nextapp.echo2.app.Alignment;
036: import nextapp.echo2.app.FillImage;
037: import nextapp.echo2.app.Border;
038: import nextapp.echo2.app.Color;
039: import nextapp.echo2.app.Component;
040: import nextapp.echo2.app.Extent;
041: import nextapp.echo2.app.Font;
042: import nextapp.echo2.app.ImageReference;
043: import nextapp.echo2.app.Insets;
044: import nextapp.echo2.app.TextArea;
045: import nextapp.echo2.app.text.TextComponent;
046: import nextapp.echo2.app.update.ServerComponentUpdate;
047: import nextapp.echo2.webcontainer.ActionProcessor;
048: import nextapp.echo2.webcontainer.ContainerInstance;
049: import nextapp.echo2.webcontainer.DomUpdateSupport;
050: import nextapp.echo2.webcontainer.FocusSupport;
051: import nextapp.echo2.webcontainer.PartialUpdateManager;
052: import nextapp.echo2.webcontainer.PartialUpdateParticipant;
053: import nextapp.echo2.webcontainer.PropertyUpdateProcessor;
054: import nextapp.echo2.webcontainer.RenderContext;
055: import nextapp.echo2.webcontainer.ComponentSynchronizePeer;
056: import nextapp.echo2.webcontainer.image.ImageRenderSupport;
057: import nextapp.echo2.webcontainer.partialupdate.BorderUpdate;
058: import nextapp.echo2.webcontainer.partialupdate.ColorUpdate;
059: import nextapp.echo2.webcontainer.partialupdate.InsetsUpdate;
060: import nextapp.echo2.webcontainer.propertyrender.AlignmentRender;
061: import nextapp.echo2.webcontainer.propertyrender.FillImageRender;
062: import nextapp.echo2.webcontainer.propertyrender.BorderRender;
063: import nextapp.echo2.webcontainer.propertyrender.ColorRender;
064: import nextapp.echo2.webcontainer.propertyrender.ExtentRender;
065: import nextapp.echo2.webcontainer.propertyrender.FontRender;
066: import nextapp.echo2.webcontainer.propertyrender.InsetsRender;
067: import nextapp.echo2.webcontainer.propertyrender.LayoutDirectionRender;
068: import nextapp.echo2.webrender.ClientProperties;
069: import nextapp.echo2.webrender.ServerMessage;
070: import nextapp.echo2.webrender.Service;
071: import nextapp.echo2.webrender.WebRenderServlet;
072: import nextapp.echo2.webrender.output.CssStyle;
073: import nextapp.echo2.webrender.servermessage.DomUpdate;
074: import nextapp.echo2.webrender.servermessage.WindowUpdate;
075: import nextapp.echo2.webrender.service.JavaScriptService;
076: import nextapp.echo2.webrender.util.DomUtil;
077:
078: /**
079: * Abstract base synchronization peer for the built-in
080: * <code>nextapp.echo2.app.text.TextComponent</code> -derived components.
081: * <p>
082: * This class should not be extended or used by classes outside of the Echo
083: * framework.
084: */
085: public abstract class TextComponentPeer implements ActionProcessor,
086: ComponentSynchronizePeer, DomUpdateSupport, FocusSupport,
087: ImageRenderSupport, PropertyUpdateProcessor {
088:
089: private static final String IMAGE_ID_BACKGROUND = "background";
090:
091: /**
092: * Service to provide supporting JavaScript library.
093: */
094: static final Service TEXT_COMPONENT_SERVICE = JavaScriptService
095: .forResource("Echo.TextComponent",
096: "/nextapp/echo2/webcontainer/resource/js/TextComponent.js");
097:
098: static {
099: WebRenderServlet.getServiceRegistry().add(
100: TEXT_COMPONENT_SERVICE);
101: }
102:
103: /**
104: * A <code>PartialUpdateParticipant</code> to update the text of
105: * a text component.
106: */
107: private class TextUpdate implements PartialUpdateParticipant {
108:
109: /**
110: * @see nextapp.echo2.webcontainer.PartialUpdateParticipant#canRenderProperty(nextapp.echo2.webcontainer.RenderContext,
111: * nextapp.echo2.app.update.ServerComponentUpdate)
112: */
113: public boolean canRenderProperty(RenderContext rc,
114: ServerComponentUpdate update) {
115: return true;
116: }
117:
118: /**
119: * @see nextapp.echo2.webcontainer.PartialUpdateParticipant#renderProperty(
120: * nextapp.echo2.webcontainer.RenderContext, nextapp.echo2.app.update.ServerComponentUpdate)
121: */
122: public void renderProperty(RenderContext rc,
123: ServerComponentUpdate update) {
124: TextComponent textComponent = (TextComponent) update
125: .getParent();
126: String elementId = ContainerInstance
127: .getElementId(textComponent);
128: ServerMessage serverMessage = rc.getServerMessage();
129: Element itemizedUpdateElement = serverMessage
130: .getItemizedDirective(
131: ServerMessage.GROUP_ID_POSTUPDATE,
132: "EchoTextComponent.MessageProcessor",
133: "set-text", new String[0], new String[0]);
134: Element itemElement = serverMessage.getDocument()
135: .createElement("item");
136: itemElement.setAttribute("eid", elementId);
137: itemElement.setAttribute("text", textComponent.getText());
138: itemizedUpdateElement.appendChild(itemElement);
139:
140: }
141: }
142:
143: private PartialUpdateManager partialUpdateManager;
144:
145: /**
146: * Default constructor.
147: */
148: public TextComponentPeer() {
149: partialUpdateManager = new PartialUpdateManager();
150: partialUpdateManager.add(TextComponent.PROPERTY_FOREGROUND,
151: new ColorUpdate(TextComponent.PROPERTY_FOREGROUND,
152: null, ColorUpdate.CSS_COLOR));
153: partialUpdateManager.add(TextComponent.PROPERTY_BACKGROUND,
154: new ColorUpdate(TextComponent.PROPERTY_BACKGROUND,
155: null, ColorUpdate.CSS_BACKGROUND_COLOR));
156: partialUpdateManager.add(TextComponent.PROPERTY_BORDER,
157: new BorderUpdate(TextComponent.PROPERTY_BORDER, null,
158: BorderUpdate.CSS_BORDER));
159: partialUpdateManager.add(TextComponent.PROPERTY_INSETS,
160: new InsetsUpdate(TextComponent.PROPERTY_INSETS, null,
161: InsetsUpdate.CSS_PADDING));
162: partialUpdateManager.add(TextComponent.TEXT_CHANGED_PROPERTY,
163: new TextUpdate());
164: }
165:
166: /**
167: * Creates a base <code>CssStyle</code> for properties common to text
168: * components.
169: *
170: * @param rc the relevant <code>RenderContext</code>
171: * @param textComponent the text component
172: * @return the style
173: */
174: protected CssStyle createBaseCssStyle(RenderContext rc,
175: TextComponent textComponent) {
176: CssStyle cssStyle = new CssStyle();
177:
178: boolean renderEnabled = textComponent.isRenderEnabled();
179:
180: Border border;
181: Color foreground, background;
182: Font font;
183: FillImage backgroundImage;
184: if (!renderEnabled) {
185: // Retrieve disabled style information.
186: background = (Color) textComponent
187: .getRenderProperty(TextComponent.PROPERTY_DISABLED_BACKGROUND);
188: backgroundImage = (FillImage) textComponent
189: .getRenderProperty(TextComponent.PROPERTY_DISABLED_BACKGROUND_IMAGE);
190: border = (Border) textComponent
191: .getRenderProperty(TextComponent.PROPERTY_DISABLED_BORDER);
192: font = (Font) textComponent
193: .getRenderProperty(TextComponent.PROPERTY_DISABLED_FONT);
194: foreground = (Color) textComponent
195: .getRenderProperty(TextComponent.PROPERTY_DISABLED_FOREGROUND);
196:
197: // Fallback to normal styles.
198: if (background == null) {
199: background = (Color) textComponent
200: .getRenderProperty(TextComponent.PROPERTY_BACKGROUND);
201: if (backgroundImage == null) {
202: // Special case:
203: // Disabled background without disabled background image will render disabled background instead of
204: // normal background image.
205: backgroundImage = (FillImage) textComponent
206: .getRenderProperty(TextComponent.PROPERTY_BACKGROUND_IMAGE);
207: }
208: }
209: if (border == null) {
210: border = (Border) textComponent
211: .getRenderProperty(TextComponent.PROPERTY_BORDER);
212: }
213: if (font == null) {
214: font = (Font) textComponent
215: .getRenderProperty(TextComponent.PROPERTY_FONT);
216: }
217: if (foreground == null) {
218: foreground = (Color) textComponent
219: .getRenderProperty(TextComponent.PROPERTY_FOREGROUND);
220: }
221: } else {
222: border = (Border) textComponent
223: .getRenderProperty(TextComponent.PROPERTY_BORDER);
224: foreground = (Color) textComponent
225: .getRenderProperty(TextComponent.PROPERTY_FOREGROUND);
226: background = (Color) textComponent
227: .getRenderProperty(TextComponent.PROPERTY_BACKGROUND);
228: font = (Font) textComponent
229: .getRenderProperty(TextComponent.PROPERTY_FONT);
230: backgroundImage = (FillImage) textComponent
231: .getRenderProperty(TextComponent.PROPERTY_BACKGROUND_IMAGE);
232: }
233:
234: Alignment alignment = (Alignment) textComponent
235: .getRenderProperty(TextComponent.PROPERTY_ALIGNMENT);
236: if (alignment != null) {
237: int horizontalAlignment = AlignmentRender
238: .getRenderedHorizontal(alignment, textComponent);
239: switch (horizontalAlignment) {
240: case Alignment.LEFT:
241: cssStyle.setAttribute("text-align", "left");
242: break;
243: case Alignment.CENTER:
244: cssStyle.setAttribute("text-align", "center");
245: break;
246: case Alignment.RIGHT:
247: cssStyle.setAttribute("text-align", "right");
248: break;
249: }
250: }
251:
252: LayoutDirectionRender.renderToStyle(cssStyle, textComponent
253: .getLayoutDirection(), textComponent.getLocale());
254: BorderRender.renderToStyle(cssStyle, border);
255: ColorRender.renderToStyle(cssStyle, foreground, background);
256: FontRender.renderToStyle(cssStyle, font);
257: FillImageRender.renderToStyle(cssStyle, rc, this ,
258: textComponent, IMAGE_ID_BACKGROUND, backgroundImage,
259: FillImageRender.FLAG_DISABLE_FIXED_MODE);
260:
261: InsetsRender
262: .renderToStyle(
263: cssStyle,
264: "padding",
265: (Insets) textComponent
266: .getRenderProperty(TextComponent.PROPERTY_INSETS));
267:
268: Extent width = (Extent) textComponent
269: .getRenderProperty(TextComponent.PROPERTY_WIDTH);
270: Extent height = (Extent) textComponent
271: .getRenderProperty(TextComponent.PROPERTY_HEIGHT);
272:
273: if (width != null) {
274: cssStyle.setAttribute("width", ExtentRender
275: .renderCssAttributeValue(width));
276: }
277:
278: if (height != null) {
279: cssStyle.setAttribute("height", ExtentRender
280: .renderCssAttributeValue(height));
281: }
282: return cssStyle;
283: }
284:
285: /**
286: * @see nextapp.echo2.webcontainer.ComponentSynchronizePeer#getContainerId(nextapp.echo2.app.Component)
287: */
288: public String getContainerId(Component child) {
289: throw new UnsupportedOperationException(
290: "Component does not support children.");
291: }
292:
293: /**
294: * @see nextapp.echo2.webcontainer.image.ImageRenderSupport#getImage(nextapp.echo2.app.Component,
295: * java.lang.String)
296: */
297: public ImageReference getImage(Component component, String imageId) {
298: if (IMAGE_ID_BACKGROUND.equals(imageId)) {
299: FillImage backgroundImage;
300: if (component.isRenderEnabled()) {
301: backgroundImage = (FillImage) component
302: .getRenderProperty(TextComponent.PROPERTY_BACKGROUND_IMAGE);
303: } else {
304: backgroundImage = (FillImage) component
305: .getRenderProperty(TextComponent.PROPERTY_DISABLED_BACKGROUND_IMAGE);
306: if (backgroundImage == null) {
307: backgroundImage = (FillImage) component
308: .getRenderProperty(TextComponent.PROPERTY_BACKGROUND_IMAGE);
309: }
310: }
311: if (backgroundImage == null) {
312: return null;
313: } else {
314: return backgroundImage.getImage();
315: }
316: } else {
317: return null;
318: }
319: }
320:
321: /**
322: * @see nextapp.echo2.webcontainer.ActionProcessor#processAction(nextapp.echo2.webcontainer.ContainerInstance,
323: * nextapp.echo2.app.Component, org.w3c.dom.Element)
324: */
325: public void processAction(ContainerInstance ci,
326: Component component, Element actionElement) {
327: ci.getUpdateManager().getClientUpdateManager()
328: .setComponentAction(component,
329: TextComponent.INPUT_ACTION, null);
330: }
331:
332: /**
333: * @see nextapp.echo2.webcontainer.PropertyUpdateProcessor#processPropertyUpdate(
334: * nextapp.echo2.webcontainer.ContainerInstance,
335: * nextapp.echo2.app.Component, org.w3c.dom.Element)
336: */
337: public void processPropertyUpdate(ContainerInstance ci,
338: Component component, Element propertyElement) {
339: String propertyName = propertyElement
340: .getAttribute(PropertyUpdateProcessor.PROPERTY_NAME);
341: if (TextComponent.TEXT_CHANGED_PROPERTY.equals(propertyName)) {
342: String propertyValue = DomUtil
343: .getElementText(propertyElement);
344: ci.getUpdateManager().getClientUpdateManager()
345: .setComponentProperty(component,
346: TextComponent.TEXT_CHANGED_PROPERTY,
347: propertyValue);
348: } else if (TextComponent.PROPERTY_HORIZONTAL_SCROLL
349: .equals(propertyName)) {
350: Extent propertyValue = new Extent(
351: Integer
352: .parseInt(propertyElement
353: .getAttribute(PropertyUpdateProcessor.PROPERTY_VALUE)));
354: ci.getUpdateManager().getClientUpdateManager()
355: .setComponentProperty(component,
356: TextComponent.PROPERTY_HORIZONTAL_SCROLL,
357: propertyValue);
358: } else if (TextComponent.PROPERTY_VERTICAL_SCROLL
359: .equals(propertyName)) {
360: Extent propertyValue = new Extent(
361: Integer
362: .parseInt(propertyElement
363: .getAttribute(PropertyUpdateProcessor.PROPERTY_VALUE)));
364: ci.getUpdateManager().getClientUpdateManager()
365: .setComponentProperty(component,
366: TextComponent.PROPERTY_VERTICAL_SCROLL,
367: propertyValue);
368: }
369: }
370:
371: /**
372: * @see nextapp.echo2.webcontainer.ComponentSynchronizePeer#renderAdd(nextapp.echo2.webcontainer.RenderContext,
373: * nextapp.echo2.app.update.ServerComponentUpdate, java.lang.String,
374: * nextapp.echo2.app.Component)
375: */
376: public void renderAdd(RenderContext rc,
377: ServerComponentUpdate update, String targetId,
378: Component component) {
379: Element domAddElement = DomUpdate.renderElementAdd(rc
380: .getServerMessage());
381: DocumentFragment htmlFragment = rc.getServerMessage()
382: .getDocument().createDocumentFragment();
383: renderHtml(rc, update, htmlFragment, component);
384: DomUpdate.renderElementAddContent(rc.getServerMessage(),
385: domAddElement, targetId, htmlFragment);
386: }
387:
388: /**
389: * @see nextapp.echo2.webcontainer.ComponentSynchronizePeer#renderDispose(nextapp.echo2.webcontainer.RenderContext,
390: * nextapp.echo2.app.update.ServerComponentUpdate,
391: * nextapp.echo2.app.Component)
392: */
393: public void renderDispose(RenderContext rc,
394: ServerComponentUpdate update, Component component) {
395: rc.getServerMessage()
396: .addLibrary(TEXT_COMPONENT_SERVICE.getId());
397: renderDisposeDirective(rc, (TextComponent) component);
398: }
399:
400: /**
401: * Renders a directive to the outgoing <code>ServerMessage</code> to
402: * dispose the state of a text component, performing tasks such as
403: * registering event listeners on the client.
404: *
405: * @param rc the relevant <code>RenderContext</code>
406: * @param textComponent the <code>TextComponent<code>
407: */
408: public void renderDisposeDirective(RenderContext rc,
409: TextComponent textComponent) {
410: String elementId = ContainerInstance
411: .getElementId(textComponent);
412: ServerMessage serverMessage = rc.getServerMessage();
413: Element itemizedUpdateElement = serverMessage
414: .getItemizedDirective(ServerMessage.GROUP_ID_PREREMOVE,
415: "EchoTextComponent.MessageProcessor",
416: "dispose", new String[0], new String[0]);
417: Element itemElement = serverMessage.getDocument()
418: .createElement("item");
419: itemElement.setAttribute("eid", elementId);
420: itemizedUpdateElement.appendChild(itemElement);
421: }
422:
423: /**
424: * Renders a directive to the outgoing <code>ServerMessage</code> to
425: * initialize the state of a text component, performing tasks such as
426: * registering event listeners on the client.
427: *
428: * @param rc the relevant <code>RenderContext</code>
429: * @param textComponent the <code>TextComponent<code>
430: */
431: public void renderInitDirective(RenderContext rc,
432: TextComponent textComponent) {
433: Extent horizontalScroll = (Extent) textComponent
434: .getRenderProperty(TextComponent.PROPERTY_HORIZONTAL_SCROLL);
435: Extent verticalScroll = (Extent) textComponent
436: .getRenderProperty(TextComponent.PROPERTY_VERTICAL_SCROLL);
437: String elementId = ContainerInstance
438: .getElementId(textComponent);
439: ServerMessage serverMessage = rc.getServerMessage();
440:
441: Element itemizedUpdateElement = serverMessage
442: .getItemizedDirective(
443: ServerMessage.GROUP_ID_POSTUPDATE,
444: "EchoTextComponent.MessageProcessor", "init",
445: new String[0], new String[0]);
446: Element itemElement = serverMessage.getDocument()
447: .createElement("item");
448: itemElement.setAttribute("eid", elementId);
449: if (horizontalScroll != null
450: && horizontalScroll.getValue() != 0) {
451: itemElement.setAttribute("horizontal-scroll",
452: ExtentRender.renderCssAttributePixelValue(
453: horizontalScroll, "0"));
454: }
455: if (verticalScroll != null && verticalScroll.getValue() != 0) {
456: itemElement.setAttribute("vertical-scroll", ExtentRender
457: .renderCssAttributePixelValue(verticalScroll, "0"));
458: }
459: if (textComponent instanceof TextArea) {
460: Integer maximumLength = (Integer) textComponent
461: .getProperty(TextComponent.PROPERTY_MAXIMUM_LENGTH);
462: if (maximumLength != null) {
463: itemElement.setAttribute("maximum-length",
464: maximumLength.toString());
465: }
466: }
467: if (textComponent instanceof TextArea
468: && rc
469: .getContainerInstance()
470: .getClientProperties()
471: .getBoolean(
472: ClientProperties.QUIRK_TEXTAREA_CONTENT)) {
473: String value = textComponent.getText();
474: if (value != null) {
475: itemElement.setAttribute("text", value);
476: }
477: }
478: if (!textComponent.isRenderEnabled()) {
479: itemElement.setAttribute("enabled", "false");
480: }
481: if (textComponent.hasActionListeners()) {
482: itemElement.setAttribute("server-notify", "true");
483: }
484:
485: itemizedUpdateElement.appendChild(itemElement);
486: }
487:
488: /**
489: * @see nextapp.echo2.webcontainer.FocusSupport#renderSetFocus(nextapp.echo2.webcontainer.RenderContext,
490: * nextapp.echo2.app.Component)
491: */
492: public void renderSetFocus(RenderContext rc, Component component) {
493: WindowUpdate.renderSetFocus(rc.getServerMessage(),
494: ContainerInstance.getElementId(component));
495: }
496:
497: /**
498: * @see nextapp.echo2.webcontainer.ComponentSynchronizePeer#renderUpdate(nextapp.echo2.webcontainer.RenderContext,
499: * nextapp.echo2.app.update.ServerComponentUpdate, java.lang.String)
500: */
501: public boolean renderUpdate(RenderContext rc,
502: ServerComponentUpdate update, String targetId) {
503: boolean fullReplace = false;
504: if (update.hasUpdatedProperties()) {
505: if (!partialUpdateManager.canProcess(rc, update)) {
506: fullReplace = true;
507: }
508: }
509:
510: if (fullReplace) {
511: // Perform full update.
512: DomUpdate.renderElementRemove(rc.getServerMessage(),
513: ContainerInstance.getElementId(update.getParent()));
514: renderAdd(rc, update, targetId, update.getParent());
515: } else {
516: partialUpdateManager.process(rc, update);
517: }
518:
519: return false;
520: }
521: }
|