You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by jc...@apache.org on 2007/08/01 15:18:30 UTC

svn commit: r561792 - in /wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/model: ComponentDetachableModel.java ComponentModel.java

Author: jcompagner
Date: Wed Aug  1 06:18:29 2007
New Revision: 561792

URL: http://svn.apache.org/viewvc?view=rev&rev=561792
Log:
Detachable model of the component model for easier merging from 1.2 to 1.3
Also just handy to have when you do want to component in the get and set object calls

Added:
    wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/model/ComponentDetachableModel.java   (with props)
Modified:
    wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/model/ComponentModel.java

Added: wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/model/ComponentDetachableModel.java
URL: http://svn.apache.org/viewvc/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/model/ComponentDetachableModel.java?view=auto&rev=561792
==============================================================================
--- wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/model/ComponentDetachableModel.java (added)
+++ wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/model/ComponentDetachableModel.java Wed Aug  1 06:18:29 2007
@@ -0,0 +1,193 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.model;
+
+import org.apache.wicket.Component;
+
+/**
+ * Quick detachable model that is implements the IComponentAssignedModel and the
+ * IModel interfaces. Its a quick replacement for the current
+ * setObject(Component,Object) and getObject(Component) methods when the
+ * component is needed in a detachable model.
+ * 
+ * @author jcompagner
+ */
+public class ComponentDetachableModel implements IModel, IComponentAssignedModel
+{
+	private static final long serialVersionUID = 1L;
+
+	/**
+	 * Transient flag to prevent multiple detach/attach scenario. We need to
+	 * maintain this flag as we allow 'null' model values.
+	 */
+	private transient boolean attached = false;
+
+	/**
+	 * This getObject throws an exception.
+	 * 
+	 * @see org.apache.wicket.model.IModel#getObject()
+	 */
+	public final Object getObject()
+	{
+		throw new RuntimeException("get object call not expected on a IComponentAssignedModel");
+	}
+
+	/**
+	 * @see org.apache.wicket.model.IModel#setObject(java.lang.Object)
+	 */
+	public final void setObject(Object object)
+	{
+		throw new RuntimeException("set object call not expected on a IComponentAssignedModel");
+	}
+
+	/**
+	 * Gets whether this model has been attached to the current session.
+	 * 
+	 * @return whether this model has been attached to the current session
+	 */
+	public boolean isAttached()
+	{
+		return attached;
+	}
+
+	/**
+	 * Detaches from the current request. Implement this method with custom
+	 * behavior, such as setting the model object to null.
+	 */
+	public void detach()
+	{
+	}
+
+	/**
+	 * Attaches to the current request. Implement this method with custom
+	 * behavior, such as loading the model object.
+	 */
+	protected void attach()
+	{
+
+	}
+
+	/**
+	 * Called when getObject is called in order to retrieve the detachable
+	 * object. Before this method is called, attach() is always called to ensure
+	 * that the object is attached.
+	 * 
+	 * @param component
+	 *            The component asking for the object
+	 * @return The object
+	 */
+	protected Object getObject(Component component)
+	{
+		return null;
+	}
+
+	/**
+	 * Called when setObject is called in order to change the detachable object.
+	 * Before this method is called, attach() is always called to ensure that
+	 * the object is attached.
+	 * 
+	 * @param component
+	 *            The component asking for replacement of the model object
+	 * @param object
+	 *            The new model object
+	 */
+	protected void setObject(Component component, Object object)
+	{
+	}
+
+	/**
+	 * @see org.apache.wicket.model.IComponentAssignedModel#wrapOnAssignment(org.apache.wicket.Component)
+	 */
+	public IWrapModel wrapOnAssignment(Component comp)
+	{
+		return new WrapModel(comp);
+	}
+
+	private class WrapModel implements IWrapModel
+	{
+		private static final long serialVersionUID = 1L;
+
+		private final Component component;
+
+		/**
+		 * @param comp
+		 */
+		public WrapModel(Component comp)
+		{
+			component = comp;
+		}
+
+		/**
+		 * @see org.apache.wicket.model.IWrapModel#getWrappedModel()
+		 */
+		public IModel getWrappedModel()
+		{
+			return ComponentDetachableModel.this;
+		}
+
+		/**
+		 * Attaches the model.
+		 */
+		private void attach()
+		{
+			if (!attached)
+			{
+				attached = true;
+				ComponentDetachableModel.this.attach();
+			}
+		}
+
+		/**
+		 * @see org.apache.wicket.model.IModel#getObject()
+		 */
+		public Object getObject()
+		{
+			attach();
+			return ComponentDetachableModel.this.getObject(component);
+		}
+
+		/**
+		 * @see org.apache.wicket.model.IModel#setObject(java.lang.Object)
+		 */
+		public void setObject(Object object)
+		{
+			attach();
+			ComponentDetachableModel.this.setObject(component, object);
+		}
+
+		/**
+		 * @see org.apache.wicket.model.IDetachable#detach()
+		 */
+		public void detach()
+		{
+			if (attached)
+			{
+				attached = false;
+				ComponentDetachableModel.this.detach();
+			}
+
+// IModel nestedModel = getChainedModel();
+// if (nestedModel != null)
+// {
+// // do detach the nested model because this one could be attached
+// // if the model is used not through this compound model
+// nestedModel.detach();
+// }
+		}
+
+	}
+}

Propchange: wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/model/ComponentDetachableModel.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/model/ComponentModel.java
URL: http://svn.apache.org/viewvc/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/model/ComponentModel.java?view=diff&rev=561792&r1=561791&r2=561792
==============================================================================
--- wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/model/ComponentModel.java (original)
+++ wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/model/ComponentModel.java Wed Aug  1 06:18:29 2007
@@ -19,9 +19,10 @@
 import org.apache.wicket.Component;
 
 /**
- * quick model that is implements the IComponentAssignedModel and the IModel interfaces.
- * Its a quick replacement for the current setObject(Component,Object) and
- * getObject(Component) methods when the component is needed in the model.
+ * Quick model that is implements the IComponentAssignedModel and the IModel
+ * interfaces. Its a quick replacement for the current
+ * setObject(Component,Object) and getObject(Component) methods when the
+ * component is needed in the model.
  * 
  * @author jcompagner
  */
@@ -48,6 +49,31 @@
 	}
 
 	/**
+	 * Returns the object from the model with the use of the component where it
+	 * is attached to.
+	 * 
+	 * @param component
+	 *            The component which has this model.
+	 * @return The object of the model.
+	 */
+	public Object getObject(Component component)
+	{
+		return null;
+	}
+
+	/**
+	 * Sets the model object for this model.
+	 * 
+	 * @param component
+	 *            The component which has this model.
+	 * @param object
+	 *            The object that will be set in the model.
+	 */
+	public void setObject(Component component, Object object)
+	{
+	}
+
+	/**
 	 * @see org.apache.wicket.model.IDetachable#detach()
 	 */
 	public void detach()
@@ -66,7 +92,7 @@
 	{
 		private static final long serialVersionUID = 1L;
 
-		private Component component;
+		private final Component component;
 
 		/**
 		 * @param comp
@@ -108,21 +134,5 @@
 			ComponentModel.this.detach();
 		}
 
-	}
-
-	/**
-	 * @param component
-	 */
-	public Object getObject(Component component)
-	{
-		return null;
-	}
-
-	/**
-	 * @param component
-	 * @param object
-	 */
-	public void setObject(Component component, Object object)
-	{
 	}
 }