KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nextapp > echo2 > app > update > ClientUpdateManager


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.app.update;
31
32 import java.io.Serializable JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.Map JavaDoc;
36
37 import nextapp.echo2.app.ApplicationInstance;
38 import nextapp.echo2.app.Component;
39
40 /**
41  * Stores inputs received from the application container and notifies
42  * components about them via the <code>Component.processInput()</code> method.
43  *
44  * @see nextapp.echo2.app.Component#processInput(java.lang.String, java.lang.Object)
45  */

46 public class ClientUpdateManager
47 implements Serializable JavaDoc {
48      
49     private Map JavaDoc clientComponentUpdateMap = new HashMap JavaDoc();
50     private Map JavaDoc applicationUpdateMap = new HashMap JavaDoc();
51     private Component actionComponent;
52     private String JavaDoc actionName;
53     private Object JavaDoc actionValue;
54     private ApplicationInstance applicationInstance;
55     
56     /**
57      * Creates a new <Code>ClientUpdateManager</code>.
58      *
59      * @param applicationInstance the <code>ApplicationInstance</code> being supported
60      */

61     ClientUpdateManager(ApplicationInstance applicationInstance) {
62         this.applicationInstance = applicationInstance;
63     }
64     
65     /**
66      * Retrieves the <code>ClientComponentUpdate</code> object representing
67      * the specified <code>Component</code>, or null, if no client updates
68      * have been made to the <code>Component</code>.
69      *
70      * @param component the <code>Component</code>
71      * @return the representing <code>ClientComponentUpdate</code>
72      */

73     ClientComponentUpdate getComponentUpdate(Component component) {
74         return (ClientComponentUpdate) clientComponentUpdateMap.get(component);
75     }
76
77     /**
78      * Retrieves the value of the application-level property update with the
79      * specified name.
80      *
81      * @param propertyName the name of the property update
82      * @return the value of the property update, or null if none exists
83      */

84     Object JavaDoc getApplicationUpdatePropertyValue(String JavaDoc propertyName) {
85         return applicationUpdateMap.get(propertyName);
86     }
87     
88     /**
89      * Notifies components of input from the client via the
90      * <code>Component.processInput()</code> method.
91      *
92      * @see nextapp.echo2.app.Component#processInput(java.lang.String, java.lang.Object)
93      */

94     void process() {
95         // Process application-level property updates.
96
Iterator JavaDoc applicationUpdateIt = applicationUpdateMap.keySet().iterator();
97         while (applicationUpdateIt.hasNext()) {
98             String JavaDoc propertyName = (String JavaDoc) applicationUpdateIt.next();
99             Object JavaDoc propertyValue = applicationUpdateMap.get(propertyName);
100             applicationInstance.processInput(propertyName, propertyValue);
101         }
102         
103         // Process property updates.
104
Iterator JavaDoc componentUpdateIt = clientComponentUpdateMap.values().iterator();
105         while (componentUpdateIt.hasNext()) {
106             ClientComponentUpdate update = (ClientComponentUpdate) componentUpdateIt.next();
107             Iterator JavaDoc inputNameIt = update.getInputNames();
108             while (inputNameIt.hasNext()) {
109                 String JavaDoc inputName = (String JavaDoc) inputNameIt.next();
110                 update.getComponent().processInput(inputName, update.getInputValue(inputName));
111             }
112         }
113         
114         // Process action.
115
if (actionComponent != null) {
116             actionComponent.processInput(actionName, actionValue);
117         }
118     }
119
120     /**
121      * Purges all updates from the <code>ClientUpdateManager</code>.
122      */

123     void purge() {
124         clientComponentUpdateMap.clear();
125         applicationUpdateMap.clear();
126         actionComponent = null;
127         actionName = null;
128         actionValue = null;
129     }
130     
131     /**
132      * Sets an application-level property received from the client.
133      *
134      * @param propertyName the name of the property
135      * @param propertyValue the value of the property
136      */

137     public void setApplicationProperty(String JavaDoc propertyName, Object JavaDoc propertyValue) {
138         applicationUpdateMap.put(propertyName, propertyValue);
139     }
140
141     /**
142      * Sets the action received from the client. The 'action' describes the
143      * client-side update which necessitated the occurrence of this
144      * client-server interaction. The application will be notified of the
145      * action AFTER it has been notified of all other property updates.
146      *
147      * @param actionComponent the action-producing component
148      * @param actionName the name of the action
149      * @param actionValue the value of the action
150      */

151     public void setComponentAction(Component actionComponent, String JavaDoc actionName, Object JavaDoc actionValue) {
152         if (!actionComponent.verifyInput(actionName, actionValue)) {
153             // Invalid input.
154
return;
155         }
156
157         this.actionComponent = actionComponent;
158         this.actionName = actionName;
159         this.actionValue = actionValue;
160     }
161     
162     /**
163      * Adds a property update received from the client.
164      *
165      * @param component the updated component
166      * @param inputName the name of the input property
167      * @param inputValue the value of the input property
168      */

169     public void setComponentProperty(Component component, String JavaDoc inputName, Object JavaDoc inputValue) {
170         if (!component.verifyInput(inputName, inputValue)) {
171             // Invalid input.
172
return;
173         }
174         
175         ClientComponentUpdate clientUpdate = (ClientComponentUpdate) clientComponentUpdateMap.get(component);
176         if (clientUpdate == null) {
177             clientUpdate = new ClientComponentUpdate(component);
178             clientComponentUpdateMap.put(component, clientUpdate);
179         }
180         clientUpdate.addInput(inputName, inputValue);
181     }
182     
183 }
184
Popular Tags