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.managers.persistence;
019:
020: import java.io.*;
021: import java.util.*;
022:
023: import net.ar.webonswing.helpers.*;
024: import net.ar.webonswing.wrapping.*;
025:
026: /**
027: * Administra la persistencia de componentes, guarda en su HashMap los datos que
028: * cada contribuidor decide persistir.
029: *
030: * @author Fernando Damian Petrola
031: */
032: public class DefaultPersistenceContributionContainer implements
033: PersistenceContributionContainer {
034: private static final String EXTERNAL_DATA = "external-data";
035: private static final String COMPONENT_DATA = "component-data";
036: protected Map persistedData;
037: protected Map componentPersistedData;
038: protected Map externalPersistedData;
039:
040: public DefaultPersistenceContributionContainer() {
041: clearData();
042: }
043:
044: public void persistValue(VisualComponent aComponent,
045: Serializable aContent) {
046: if (aContent != null)
047: componentPersistedData.put(aComponent.getName(), aContent);
048: }
049:
050: public Serializable restoreValue(VisualComponent aComponent) {
051: return (Serializable) componentPersistedData.get(aComponent
052: .getName());
053: }
054:
055: public void persistValue(String theKey, Serializable aSerializable) {
056: externalPersistedData.put(theKey, aSerializable);
057: }
058:
059: public Serializable restoreValue(String theKey) {
060: return (Serializable) externalPersistedData.get(theKey);
061: }
062:
063: public void restoreAllValues(VisualComponent aRootComponent) {
064: String theKey = "";
065:
066: try {
067: aRootComponent.accept(new ComponentVisitorAdapter() {
068: public void visitComponentBegin(
069: VisualComponent aComponent) {
070: if (componentPersistedData
071: .get(aComponent.getName()) != null) {
072: ((PersistenceContributor) aComponent
073: .getContributor())
074: .restorePersistedValue(DefaultPersistenceContributionContainer.this );
075: new ComponentStandardizer()
076: .standardizeChildsComponentHierarchy(aComponent);
077: }
078: }
079:
080: public void visitComponentEnd(VisualComponent aComponent) {
081: }
082: });
083: } catch (WebOnSwingException e) {
084: throw new PersistenceDoNotMatchException(this ,
085: aRootComponent, theKey, e);
086: }
087: }
088:
089: public List getChangedComponents(VisualComponent aRootComponent) {
090: Vector theChangedComponents = new Vector();
091:
092: for (Iterator i = componentPersistedData.keySet().iterator(); i
093: .hasNext();) {
094: String theKey = (String) i.next();
095: VisualComponent theComponent = aRootComponent
096: .findComponent(theKey);
097:
098: if (!((PersistenceContributor) theComponent
099: .getContributor())
100: .isPersistedValueEqualToModel(this ))
101: theChangedComponents.add(theComponent.getName());
102: }
103:
104: return theChangedComponents;
105: }
106:
107: public Map getPersistedData() {
108: return persistedData;
109: }
110:
111: public void setPersistedData(Map aPersistedData) {
112: persistedData = new Hashtable(aPersistedData);
113: }
114:
115: public void clearData() {
116: persistedData = new Hashtable();
117: persistedData.put(COMPONENT_DATA,
118: componentPersistedData = new Hashtable());
119: persistedData.put(EXTERNAL_DATA,
120: externalPersistedData = new Hashtable());
121: }
122:
123: public Object clone() throws CloneNotSupportedException {
124: return super .clone();
125: }
126:
127: public Map getComponentPersistedData() {
128: return componentPersistedData;
129: }
130:
131: public void setComponentPersistedData(Map aComponentPersistedData) {
132: this .componentPersistedData = aComponentPersistedData;
133: }
134:
135: public Map getExternalPersistedData() {
136: return externalPersistedData;
137: }
138:
139: public void setExternalPersistedData(Map aExternalPersistedData) {
140: this.externalPersistedData = aExternalPersistedData;
141: }
142: }
|