You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by hu...@apache.org on 2006/12/06 20:07:10 UTC

svn commit: r483177 - in /struts/sandbox/trunk/overdrive/Nexus: Core/Core.csproj Core/EntryDictionary.cs Core/Nexus.Core.xml Web/GridControl.ascx.cs

Author: husted
Date: Wed Dec  6 11:07:10 2006
New Revision: 483177

URL: http://svn.apache.org/viewvc?view=rev&rev=483177
Log:
Extend support for flipping to a page in the datagrid, based on a key field. Clients can override ExecuteList to specify the name of the key field. 

Added:
    struts/sandbox/trunk/overdrive/Nexus/Core/EntryDictionary.cs
Modified:
    struts/sandbox/trunk/overdrive/Nexus/Core/Core.csproj
    struts/sandbox/trunk/overdrive/Nexus/Core/Nexus.Core.xml
    struts/sandbox/trunk/overdrive/Nexus/Web/GridControl.ascx.cs

Modified: struts/sandbox/trunk/overdrive/Nexus/Core/Core.csproj
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/overdrive/Nexus/Core/Core.csproj?view=diff&rev=483177&r1=483176&r2=483177
==============================================================================
--- struts/sandbox/trunk/overdrive/Nexus/Core/Core.csproj (original)
+++ struts/sandbox/trunk/overdrive/Nexus/Core/Core.csproj Wed Dec  6 11:07:10 2006
@@ -104,6 +104,11 @@
                     BuildAction = "Compile"
                 />
                 <File
+                    RelPath = "EntryDictionary.cs"
+                    SubType = "Code"
+                    BuildAction = "Compile"
+                />
+                <File
                     RelPath = "IEntryList.cs"
                     SubType = "Code"
                     BuildAction = "Compile"

Added: struts/sandbox/trunk/overdrive/Nexus/Core/EntryDictionary.cs
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/overdrive/Nexus/Core/EntryDictionary.cs?view=auto&rev=483177
==============================================================================
--- struts/sandbox/trunk/overdrive/Nexus/Core/EntryDictionary.cs (added)
+++ struts/sandbox/trunk/overdrive/Nexus/Core/EntryDictionary.cs Wed Dec  6 11:07:10 2006
@@ -0,0 +1,121 @@
+using System;
+using System.Collections;
+using System.Text;
+
+namespace Nexus.Core
+{
+	/// <summary>
+	/// Expose field attributes as public properties.
+	/// </summary>
+	/// <remarks><p>
+	/// The EntryDictionary is used for integration with libraries that 
+	/// can use only public properties, such as DataGrid. 
+	/// It is also used to pass properties as Event argument, 
+	/// in which case it is used like a data transfer object.
+	/// </p><p>
+	/// The values are available both as an IDictionary and 
+	/// (optionally) as Properties. 
+	/// (The properties should use the IDictionary for storage.)
+	/// To define properties, extend EntryDictionary.
+	/// </p></remarks>
+	public class EntryDictionary
+	{
+		private IDictionary _Value = new Hashtable();
+
+		public EntryDictionary()
+		{
+			// Default contstructor	
+		}
+
+		public EntryDictionary(IDictionary sources)
+		{
+			AddAll(sources);
+		}
+
+		/// <summary>
+		/// Provide a string representation of the field values 
+		/// in the format "key=value\n".
+		/// </summary>
+		/// <returns>String representation</returns>
+		public override String ToString()
+		{
+			StringBuilder builder = new StringBuilder(128);
+			string tab = "=";
+			string eol = "\n";
+			ICollection keys = _Value.Keys;
+			foreach (string key in keys)
+			{
+				builder.Append(key);
+				builder.Append(tab);
+				builder.Append(Get(key));
+				builder.Append(eol);
+			}
+			return builder.ToString();
+		}
+
+		public void Add(string key, string value)
+		{
+			_Value.Add(key, value);
+		}
+
+		public void AddAll(IDictionary sources)
+		{
+			ICollection keys = sources.Keys;
+			foreach (string key in keys)
+			{
+				object value = sources[key];
+				if (value == null)
+					Add(key, value as string);
+				else
+					Add(key, value.ToString());
+			}
+		}
+
+		protected ICollection Keys
+		{
+			get { return _Value.Keys; }
+		}
+
+		public string Get(string key)
+		{
+			return _Value[key] as string;
+		}
+
+		public void Set(string key, string value)
+		{
+			if (value == null)
+				_Value[key] = null;
+			else _Value[key] = value.Trim();
+		}
+
+		public Boolean Contains(Object key)
+		{
+			return _Value.Contains(key);
+		}
+
+		public IDictionary Criteria
+		{
+			get { return _Value; }
+		}
+
+		/// <summary>
+		/// Call calculated properties so that 
+		/// they are cached as entries in the table.
+		/// </summary>
+		/// <remarks>
+		/// <p>Override to provide functionality</p>
+		/// </remarks>
+		/// 
+		public virtual void CacheText()
+		{
+		}
+
+		/*
+		public string Property
+		{
+			get { return return Get(App.PROPERTY); }
+			set { Set(App.PROPERTY, value); }
+		}
+		*/
+	}
+}
\ No newline at end of file

Modified: struts/sandbox/trunk/overdrive/Nexus/Core/Nexus.Core.xml
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/overdrive/Nexus/Core/Nexus.Core.xml?view=diff&rev=483177&r1=483176&r2=483177
==============================================================================
--- struts/sandbox/trunk/overdrive/Nexus/Core/Nexus.Core.xml (original)
+++ struts/sandbox/trunk/overdrive/Nexus/Core/Nexus.Core.xml Wed Dec  6 11:07:10 2006
@@ -1741,5 +1741,38 @@
             Token for Entry Item command name.
             </summary>
         </member>
+        <member name="T:Nexus.Core.EntryDictionary">
+            <summary>
+            Expose field attributes as public properties.
+            </summary>
+            <remarks><p>
+            The EntryDictionary is used for integration with libraries that 
+            can use only public properties, such as DataGrid. 
+            It is also used to pass properties as Event argument, 
+            in which case it is used like a data transfer object.
+            </p><p>
+            The values are available both as an IDictionary and 
+            (optionally) as Properties. 
+            (The properties should use the IDictionary for storage.)
+            To define properties, extend EntryDictionary.
+            </p></remarks>
+        </member>
+        <member name="M:Nexus.Core.EntryDictionary.ToString">
+            <summary>
+            Provide a string representation of the field values 
+            in the format "key=value\n".
+            </summary>
+            <returns>String representation</returns>
+        </member>
+        <member name="M:Nexus.Core.EntryDictionary.CacheText">
+            <summary>
+            Call calculated properties so that 
+            they are cached as entries in the table.
+            </summary>
+            <remarks>
+            <p>Override to provide functionality</p>
+            </remarks>
+            
+        </member>
     </members>
 </doc>

Modified: struts/sandbox/trunk/overdrive/Nexus/Web/GridControl.ascx.cs
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/overdrive/Nexus/Web/GridControl.ascx.cs?view=diff&rev=483177&r1=483176&r2=483177
==============================================================================
--- struts/sandbox/trunk/overdrive/Nexus/Web/GridControl.ascx.cs (original)
+++ struts/sandbox/trunk/overdrive/Nexus/Web/GridControl.ascx.cs Wed Dec  6 11:07:10 2006
@@ -550,12 +550,84 @@
 		/// <param name="helper">The helper to examine</param>
 		/// <returns>Total count of items for all pages</returns>
 		/// 	
-		private int GetItemCount(IViewHelper helper)
+		protected int GetItemCount(IViewHelper helper)
 		{
 			return Convert.ToInt32(helper.Criteria[ITEM_COUNT]);
 		}
 
 		/// <summary>
+		/// Obtain the item page from Helper, or zero if no page set.
+		/// </summary>
+		/// <param name="helper">The helper to examine</param>
+		/// <returns>Current page number within data set</returns>
+		/// 	
+		protected int GetItemPage(IViewHelper helper)
+		{
+			object page = helper.Criteria[ITEM_PAGE];
+			if (page==null) return 0;			
+			return Convert.ToInt32(page);
+		}
+
+		/// <summary>
+		/// Update the item page from Helper.
+		/// </summary>
+		/// <param name="helper">The helper to examine</param>
+		/// <returns>Current page number for current item</returns>
+		/// 	
+		protected void SetItemPage(IViewHelper helper, int page)
+		{
+			helper.Criteria[ITEM_PAGE] = Convert.ToString(page);
+		}
+
+		
+		/// <summary>
+		/// Update the item offset from Helper.
+		/// </summary>
+		/// <param name="helper">The helper to examine</param>
+		/// <returns>Current page number for current item</returns>
+		/// 	
+		protected void SetItemOffset(IViewHelper helper, int ofs)
+		{
+			helper.Criteria[ITEM_OFFSET] = Convert.ToString(ofs);
+		}
+
+
+		/// <summary>
+		/// Store the item key field name.
+		/// </summary>
+		/// <param name="context">The context to examine</param>
+		/// <param name="key">The name of the item key field</param>
+		/// 	
+		protected void SetItemKey(IDictionary context, string key)
+		{
+			context[ITEM_KEY] = key;
+		}
+		
+		/// <summary>
+		/// Obtain the item key field name.
+		/// </summary>
+		/// <param name="context">The context to examine</param>
+		/// <returns>Name of key field</returns>
+		/// 	
+		protected object GetItemKey(IDictionary context)
+		{
+			return context[ITEM_KEY];
+		}
+		
+		/// <summary>
+		/// Obtain the item page from Helper, or zero if no page set.
+		/// </summary>
+		/// <param name="context">The context to examine</param>
+		/// <returns>Current page number within data set</returns>
+		/// 	
+		protected object GetItemKeyValue(IDictionary context)
+		{
+			object key = context[ITEM_KEY];
+			if (key==null) return null;
+			return context[key];
+		}
+		
+		/// <summary>
 		/// Configure the DataGrid for initial display.
 		/// </summary>
 		/// <param name="helper">The Helper with an outcome to bind as a DataSource</param>
@@ -752,19 +824,61 @@
 			if (okay) BindGrid(helper); // DoBindGrid = helper;
 			return helper;
 		}
-
+		
 		/// <summary>
-		/// Invoke a ListCommand that uses a criteria.
+		/// Look for a IssueEventKey, and scroll to it, if found.
 		/// </summary>
-		/// <returns>Executed helper</returns>
+		/// <param name="criteria">Input/outpout values</param>
+		/// <returns>Helper after obtaining list</returns>
 		public virtual IViewHelper ExecuteList(IDictionary criteria)
 		{
+			
 			IViewHelper helper = ReadExecute(ListCommand, criteria);
+
+			object issue_event_key = GetItemKeyValue(criteria);
+			int count = GetItemCount(helper);
 			bool okay = helper.IsNominal;
-			if (okay) BindGrid(helper); // DoBindGrid = helper;
+			bool skip_to_item = (issue_event_key == null) || (count==0) || !okay;
+			if (skip_to_item)
+			{
+				if (okay) BindGrid(helper); // DoBindGrid = helper;
+				return helper;
+			}
+			
+			bool found = false; 
+			int page = -1;
+			int item = -1;
+			object key = GetItemKey(criteria);
+			while ((!found) && (count>item) && helper.IsNominal)
+			{
+				page++;
+				if (helper.IsNominal)
+				{
+					IList outcome = helper.Outcome;
+					foreach (EntryDictionary e in outcome)
+					{
+						found = found || (issue_event_key.Equals(e.Criteria[key]));
+					}
+					if (!found)
+					{
+						item = item + outcome.Count;
+						SetItemOffset(helper,item+1);
+						helper.Execute();
+					}
+				}
+			}
+			
+			if (helper.IsNominal)
+			{
+				if (found) SetItemPage(helper,page);							
+				BindGrid(helper); // DoBindGrid = helper;
+			} 
 			return helper;
 		}
-
+		
+		
+		
+		
 		/// <summary>
 		/// Setup the DataGrid when the page is first initialized.
 		/// </summary>
@@ -780,15 +894,18 @@
 				HasCriteria = true;
 			}
 
-			if (HasCriteria)
+			if (HasCriteria) 
+			{
 				helper = ExecuteList(criteria);
+			}
 			else
 				helper = ExecuteList();
 			
 			if (Grid.AllowCustomPaging)
 			{
+				int page = GetItemPage(helper);
 				int count = GetItemCount(helper);
-				ListPageIndexChanged_Raise(this, 0, Grid.PageSize, count);								
+				ListPageIndexChanged_Raise(this, page, Grid.PageSize, count);								
 			}
 			
 			return helper;
@@ -1046,6 +1163,16 @@
 		/// </summary>
 		public const string ITEM_COUNT = "item_count";
 
+		/// <summary>
+		/// Provide key to store item page in criteria.
+		/// </summary>
+		public const string ITEM_PAGE = "item_page";
+
+		/// <summary>
+		/// Provide key to store item key in criteria.
+		/// </summary>
+		public const string ITEM_KEY = "item_key";
+		
 		#endregion
 
 		#region ListPageIndexChanged 
@@ -1137,6 +1264,7 @@
 				a.ItemFrom = from;
 				a.ItemThru = thru;
 				a.ItemCount = count;
+				Grid.CurrentPageIndex = page;
 				ListPageIndexChanged(sender, a);
 			}
 		}
@@ -1574,17 +1702,17 @@
 			/// <param name="insertNullKey">Whether to prepend a -v- item to the list</param>
 			public DropDownListTemplate(string id, IKeyValueList list, bool insertNullKey)
 			{
-					if (insertNullKey) 
+				if (insertNullKey) 
+				{
+					lock(list)
 					{
-						lock(list)
-						{
-							IKeyValue e = list[0] as KeyValue;
-							if (!NULL_TOKEN.Equals(e.Text))
-							{								
-								list.Insert(0, new KeyValue(String.Empty, NULL_TOKEN));						
-							}
+						IKeyValue e = list[0] as KeyValue;
+						if (!NULL_TOKEN.Equals(e.Text))
+						{								
+							list.Insert(0, new KeyValue(String.Empty, NULL_TOKEN));						
 						}
-					}					
+					}
+				}					
 
 				_DataField = id;
 				_Control = new DropDownList();