Author: julien(a)jboss.com
Date: 2007-12-04 05:59:41 -0500 (Tue, 04 Dec 2007)
New Revision: 9274
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/UIContextImpl.java
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/UIObjectImpl.java
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/UIObject.java
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/ModelTestCase.java
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockModel.java
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockObject.java
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockObjectImpl.java
Log:
early impl of createChild
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/UIContextImpl.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/UIContextImpl.java 2007-12-04
10:59:39 UTC (rev 9273)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/UIContextImpl.java 2007-12-04
10:59:41 UTC (rev 9274)
@@ -63,11 +63,20 @@
{
public void update(StructuralObject object)
{
- // Get the corresping object
+ // Get the corresponding object
UIObjectImpl tmp = objects.get(object.getId());
- // Update its structural view
- tmp.structuralObject = object;
+ // If it is not here, create it
+ if (tmp == null)
+ {
+ tmp = createObject(object);
+ objects.put(object.getId(), tmp);
+ }
+ else
+ {
+ // Update its structural view
+ tmp.update(object);
+ }
}
public void remove(String id)
@@ -81,9 +90,7 @@
return loader.load(loader.getRootId());
}
- public UIContextImpl(
- StructuralStateContext structuralStateContext,
- NavigationalStateContext navigationalStateContext)
+ public UIContextImpl(StructuralStateContext structuralStateContext,
NavigationalStateContext navigationalStateContext)
{
super(getRootState(structuralStateContext));
@@ -101,16 +108,34 @@
*/
public UIObject getObject(String id)
{
+ return getFromUniverse(id, true);
+ }
+
+ UIObject getFromUniverse(String id, boolean loadIfAbsent)
+ {
try
{
UIObject object = objects.get(id);
- if (object == null)
+
+ //
+ if (object != null)
{
+ return object;
+ }
+
+ //
+ if (loadIfAbsent)
+ {
//Fetch the state of the UIObject in question
StructuralObject structuralObject = this.structuralStateContext.load(id);
- return loadObject(structuralObject);
+
+ //
+ return updateUniverse(structuralObject);
}
- return object;
+ else
+ {
+ throw new AssertionError("Should not be here");
+ }
}
catch (Exception e)
{
@@ -118,6 +143,23 @@
}
}
+ protected UIObjectImpl updateUniverse(StructuralObject structuralObject)
+ {
+ UIObjectImpl object = objects.get(structuralObject.getId());
+
+ //
+ if (object == null)
+ {
+ object = createObject(structuralObject);
+
+ //
+ objects.put(structuralObject.getId(), object);
+ }
+
+ //
+ return object;
+ }
+
public void addModelListener(ModelListener listener)
{
if (listener == null)
@@ -132,19 +174,8 @@
}
}
- protected UIObjectImpl loadObject(StructuralObject structuralObject)
+ private UIObjectImpl createObject(StructuralObject state)
{
- UIObjectImpl object = objects.get(structuralObject.getId());
- if (object == null)
- {
- object = getImplementation(structuralObject);
- objects.put(structuralObject.getId(), object);
- }
- return object;
- }
-
- private UIObjectImpl getImplementation(StructuralObject state)
- {
Class type = state.getState().getType();
if(type == UIPortal.class)
{
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/UIObjectImpl.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/UIObjectImpl.java 2007-12-04
10:59:39 UTC (rev 9273)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/UIObjectImpl.java 2007-12-04
10:59:41 UTC (rev 9274)
@@ -33,6 +33,7 @@
import java.io.Serializable;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -55,7 +56,7 @@
private UIObjectImpl parent;
/** This is used to assist with data needed during lazy loading, other state related
data etc... */
- StructuralObject structuralObject;
+ private StructuralObject structuralObject;
public UIObjectImpl(UIContextImpl context, StructuralObject object)
{
@@ -238,7 +239,7 @@
if (parent == null)
{
StructuralObject parentStructuralState =
context.structuralStateContext.loadParent(structuralObject);
- parent = context.loadObject(parentStructuralState);
+ parent = context.updateUniverse(parentStructuralState);
}
//
@@ -255,14 +256,33 @@
ArrayList<UIObject> tmp = new ArrayList<UIObject>();
for (StructuralObject structuralChild :
context.structuralStateContext.loadChildren(structuralObject))
{
- UIObjectImpl child = context.loadObject(structuralChild);
+ UIObjectImpl child = context.updateUniverse(structuralChild);
tmp.add(child);
}
- return tmp;
+ children = tmp;
}
return children;
}
-
+
+ private static final Map<String, String> EMPTY_STATE = Collections.emptyMap();
+
+ public <T extends UIObject> T createChild(String name, Class<T> type)
throws IllegalArgumentException
+ {
+ String id = context.structuralStateContext.create(context.listener,
structuralObject, type, name, EMPTY_STATE);
+
+ //
+ return type.cast(context.getFromUniverse(id, false));
+ }
+
+ void update(StructuralObject object)
+ {
+ structuralObject = object;
+ parent = null;
+ children = null;
+ }
+
+ protected abstract <T extends UIObject> boolean isAllowedAsChild(Class<T>
type);
+
/**
*
*/
@@ -296,7 +316,6 @@
// throw new UnsupportedOperationException("todo");
// }
- protected abstract <T extends UIObject> boolean isAllowedAsChild(Class<T>
type);
/**
@@ -469,5 +488,5 @@
return uiContext;
}
-*/
+*/
}
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/UIObject.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/UIObject.java 2007-12-04
10:59:39 UTC (rev 9273)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/UIObject.java 2007-12-04
10:59:41 UTC (rev 9274)
@@ -71,7 +71,7 @@
* @return the newly created child
* @throws IllegalArgumentException if the name is null, already exists or this kind
of object does not accept children of the specified type
*/
-// public <T extends UIObject> T createChild(String name, Class<T> type)
throws IllegalArgumentException;
+ public <T extends UIObject> T createChild(String name, Class<T> type)
throws IllegalArgumentException;
/**
*
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/ModelTestCase.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/ModelTestCase.java 2007-12-04
10:59:39 UTC (rev 9273)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/ModelTestCase.java 2007-12-04
10:59:41 UTC (rev 9274)
@@ -496,4 +496,19 @@
{
}
}
+
+ public void testCreateChild()
+ {
+ UIContext context = createContext();
+
+ assertNull(context.getChild("foo"));
+
+ UIPortal portal = context.createChild("foo", UIPortal.class);
+
+ assertNotNull(portal);
+
+ assertSame(context, portal.getParent());
+
+ assertSame(portal, context.getChild("foo"));
+ }
}
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockModel.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockModel.java 2007-12-04
10:59:39 UTC (rev 9273)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockModel.java 2007-12-04
10:59:41 UTC (rev 9274)
@@ -22,14 +22,19 @@
******************************************************************************/
package org.jboss.portal.presentation.test.model.state.structural;
+import org.jboss.portal.presentation.model.UIContext;
+import org.jboss.portal.presentation.model.UIObject;
+import org.jboss.portal.presentation.model.UIPage;
+import org.jboss.portal.presentation.model.UIPortal;
+import org.jboss.portal.presentation.model.UIWindow;
import org.jboss.portal.presentation.model.state.NoSuchStateException;
import org.jboss.portal.presentation.model.state.StaleStateException;
import org.jboss.portal.presentation.model.state.StateChangeVetoException;
import org.jboss.portal.presentation.model.state.StateException;
import
org.jboss.portal.presentation.model.state.structural.AbstractStructuralStateContext;
import org.jboss.portal.presentation.model.state.structural.StructuralObject;
+import
org.jboss.portal.presentation.model.state.structural.StructuralStateChangeListener;
import org.jboss.portal.presentation.model.state.structural.StructuralStateContext;
-import
org.jboss.portal.presentation.model.state.structural.StructuralStateChangeListener;
import java.util.ArrayList;
import java.util.HashMap;
@@ -52,6 +57,18 @@
/** . */
private final MockObjectImpl root = createObject(MockObject.Type.CONTEXT,
"");
+ /** . */
+ private static final Map<Class<? extends UIObject>, MockObject.Type> map;
+
+ static
+ {
+ map = new HashMap<Class<? extends UIObject>, MockObject.Type>();
+ map.put(UIContext.class, MockObject.Type.CONTEXT);
+ map.put(UIPage.class, MockObject.Type.PAGE);
+ map.put(UIPortal.class, MockObject.Type.PORTAL);
+ map.put(UIWindow.class, MockObject.Type.WINDOW);
+ }
+
public StructuralStateContext getStructuralStateContext()
{
return structuralStateContext;
@@ -127,6 +144,24 @@
}
}
+ public String create(StructuralStateChangeListener listener, StructuralObject
parent, Class<? extends UIObject> classType, String name, Map<String, String>
properties) throws StateChangeVetoException, StateException, IllegalArgumentException
+ {
+ MockObject mockParent = getValidMockObject(parent);
+
+ //
+ MockObject.Type type = map.get(classType);
+
+ //
+ MockObject mockChild = mockParent.addChild(name, type, properties);
+
+ //
+ listener.update(mockParent.takeSnapshot());
+ listener.update(mockChild.takeSnapshot());
+
+ //
+ return mockChild.getId();
+ }
+
public List<StructuralObject> loadChildren(StructuralObject object)
{
MockObject mockObject = getValidMockObject(object);
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockObject.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockObject.java 2007-12-04
10:59:39 UTC (rev 9273)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockObject.java 2007-12-04
10:59:41 UTC (rev 9274)
@@ -30,6 +30,7 @@
import org.jboss.portal.presentation.model.state.structural.StructuralObject;
import java.util.List;
+import java.util.Map;
import java.util.Set;
/**
@@ -111,6 +112,8 @@
Set<String> getPropertyNames();
+ MockObject addChild(String name, Type type, Map<String, String> state);
+
MockObject addChild(String name, Type type);
MockObject getParent();
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockObjectImpl.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockObjectImpl.java 2007-12-04
10:59:39 UTC (rev 9273)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockObjectImpl.java 2007-12-04
10:59:41 UTC (rev 9274)
@@ -40,6 +40,9 @@
{
/** . */
+ private static final Map<String, String> EMPTY_STATE = Collections.emptyMap();
+
+ /** . */
private final MockObject.Type type;
/** . */
@@ -119,6 +122,16 @@
public MockObject addChild(String name, MockObject.Type type)
{
+ return addChild(name, type, EMPTY_STATE);
+ }
+
+ public Set<String> getPropertyNames()
+ {
+ return propertyValues.keySet();
+ }
+
+ public MockObject addChild(String name, Type type, Map<String, String> state)
+ {
if (name == null)
{
throw new IllegalArgumentException();
@@ -134,6 +147,16 @@
MockObjectImpl child = model.createObject(type, name);
//
+ for (Map.Entry<String, String> entry : state.entrySet())
+ {
+ if (entry.getValue() == null)
+ {
+ throw new IllegalArgumentException();
+ }
+ child.propertyValues.put(entry.getKey(), entry.getValue());
+ }
+
+ //
child.parent = this;
children.put(child.id, child);
version++;
@@ -142,11 +165,6 @@
return child;
}
- public Set<String> getPropertyNames()
- {
- return propertyValues.keySet();
- }
-
public void setPropertyBehavior(String propertyName, UpdateBehavior propertyBehavior)
{
if (propertyName == null)
@@ -216,5 +234,4 @@
// We cannot use it anymore
valid = false;
}
-
}