001: // WebOnSwing - Web Application Framework
002: //Copyright (C) 2003 Fernando Damian Petrola
003: //
004: //This library is free software; you can redistribute it and/or
005: //modify it under the terms of the GNU Lesser General Public
006: //License as published by the Free Software Foundation; either
007: //version 2.1 of the License, or (at your option) any later version.
008: //
009: //This library is distributed in the hope that it will be useful,
010: //but WITHOUT ANY WARRANTY; without even the implied warranty of
011: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: //Lesser General Public License for more details.
013: //
014: //You should have received a copy of the GNU Lesser General Public
015: //License along with this library; if not, write to the Free Software
016: //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017:
018: package net.ar.webonswing.remote;
019:
020: import java.util.*;
021:
022: import net.ar.webonswing.wrapping.*;
023:
024: /**
025: * Genera RemoteEvents a partir de una url y de un mapa de parametros. Estos
026: * eventos no tienen un orden deterministico solo se asegura que todos los de
027: * tipo "update" queden primeros y el evento principal (el que genero el
028: * "request") quede ultimo. A futuro: guardar el orden de los eventos generados
029: * del lado del cliente y procesarlos en el orden exacto que sucedieron.
030: *
031: * @author Fernando Damian Petrola
032: */
033: public class UpdatesFirstEventDispatcher extends RemoteEventDispatcher {
034: protected VisualComponent theRootComponent;
035:
036: public UpdatesFirstEventDispatcher(VisualComponent aRootComponent) {
037: theRootComponent = aRootComponent;
038: }
039:
040: public void generateAndDispatchEvents(String anUrl, Map aParameters) {
041: Map theResult = new HashMap();
042: List theListResult = new Vector();
043:
044: if (anUrl.endsWith(".event")) {
045: int theTypePos = anUrl.substring(0, anUrl.length() - 6)
046: .lastIndexOf('.');
047: String theMainEventType = anUrl.substring(theTypePos + 1,
048: anUrl.length() - 6);
049: String theMainEventSource = anUrl.substring(anUrl
050: .indexOf('.') + 1, theTypePos);
051: List theMainComponentEvents = new Vector();
052: VisualComponent theMainEventComponent = theRootComponent
053: .findComponent(theMainEventSource);
054:
055: for (Iterator i = aParameters.entrySet().iterator(); i
056: .hasNext();) {
057: Map.Entry theEntry = (Map.Entry) i.next();
058:
059: VisualComponent theComponent = theRootComponent
060: .findComponent(theEntry.getKey().toString());
061:
062: if (theComponent != null) {
063: RemoteEvent theComponentEvent = new RemoteEvent(
064: theComponent, "update", theEntry.getKey()
065: .toString(), (String[]) theEntry
066: .getValue());
067:
068: if (theEntry.getKey().toString().startsWith(
069: theMainEventSource))
070: theMainComponentEvents.add(theComponentEvent);
071: else {
072: Vector theComponentEvents = (Vector) theResult
073: .get(theComponent);
074:
075: if (theComponentEvents == null)
076: theResult.put(theComponent,
077: theComponentEvents = new Vector());
078:
079: theComponentEvents.add(theComponentEvent);
080: }
081: }
082:
083: }
084:
085: if (theMainEventComponent != null)
086: theMainComponentEvents.add(new RemoteEvent(
087: theMainEventComponent, theMainEventType,
088: theMainEventType, new Object[0]));
089:
090: for (Iterator i = theResult.entrySet().iterator(); i
091: .hasNext();) {
092: Map.Entry theEntry = (Map.Entry) i.next();
093: theListResult.add(new Object[] { theEntry.getKey(),
094: theEntry.getValue() });
095: }
096: if (theMainEventComponent != null)
097: theListResult.add(new Object[] { theMainEventComponent,
098: theMainComponentEvents });
099: }
100:
101: dispatchEvents(theListResult);
102: }
103: }
|