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;
031:
032: import java.util.HashMap;
033: import java.util.Map;
034:
035: import nextapp.echo2.app.update.ServerComponentUpdate;
036:
037: /**
038: * A utility class for rendering a collection of property updates to an
039: * existing HTML representation of a component on the client browser.
040: */
041: public class PartialUpdateManager {
042:
043: /**
044: * Mapping between property names and
045: * <code>PartialUpdateParticipant</code>s.
046: */
047: private Map registry = null;
048:
049: /**
050: * Adds a <code>PartialUpdateParticipant</code> to handle a given property.
051: *
052: * @param propertyName the name of the property
053: * @param updateParticipant the <code>PartialUpdateParticipant</code>
054: */
055: public void add(String propertyName,
056: PartialUpdateParticipant updateParticipant) {
057: if (registry == null) {
058: registry = new HashMap();
059: }
060: registry.put(propertyName, updateParticipant);
061: }
062:
063: /**
064: * Determines if this <code>PartialUpdateManager</code> has participants
065: * to update all changed properties specified in <code>update</code>.
066: *
067: * @param rc the relevant <code>RenderContext</code>
068: * @param update the update
069: * @return true if this registry is capable of performing all the
070: * described property updates
071: */
072: public boolean canProcess(RenderContext rc,
073: ServerComponentUpdate update) {
074: if (registry == null) {
075: return false;
076: }
077: String[] propertyNames = update.getUpdatedPropertyNames();
078: for (int i = 0; i < propertyNames.length; ++i) {
079: PartialUpdateParticipant propertyRender = (PartialUpdateParticipant) registry
080: .get(propertyNames[i]);
081: if (propertyRender == null) {
082: return false;
083: } else if (!propertyRender.canRenderProperty(rc, update)) {
084: return false;
085: }
086: }
087: return true;
088: }
089:
090: /**
091: * Renders updates to all properties in the provided <code>update</code>.
092: * If the update contains a property for which a participant does not
093: * exist in this registry, the given property is skipped.
094: *
095: * @param rc the relevant <code>RenderContext</code>
096: * @param update the update to process
097: */
098: public void process(RenderContext rc, ServerComponentUpdate update) {
099: String[] propertyNames = update.getUpdatedPropertyNames();
100: for (int i = 0; i < propertyNames.length; ++i) {
101: PartialUpdateParticipant propertyRender = (PartialUpdateParticipant) registry
102: .get(propertyNames[i]);
103: if (propertyRender == null) {
104: // If no renderer exists, discard the property update.
105: continue;
106: }
107: propertyRender.renderProperty(rc, update);
108: }
109: }
110: }
|