Rich 518c79216a Cleanup OLD MRI list
If the ItemID doesn't exist, remove the item.
If the Menu text and Tooltip don't match, ask if the item should be removed.
Always use the current Menu text and tooltip on the menu, rather than the one that was saved.
2009-04-16 14:56:48 +00:00

171 lines
4.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using VEPROMS.CSLA.Library;
namespace Volian.Controls.Library
{
public class MostRecentItemList : List<MostRecentItem>
{
private int _MaxItems = 10;
public int MaxItems
{
get { return _MaxItems; }
set { _MaxItems = value; }
}
public new MostRecentItem Add(MostRecentItem myMRI)
{
MostRecentItem tmp = null;
// Look for the ItemID
foreach (MostRecentItem mri in this)
if (mri.ItemID == myMRI.ItemID)
tmp = mri;
// If it exists - remove it
if (tmp != null)
Remove(tmp);
// Insert it in the first place in the list
Insert(0, myMRI);
// If more than MaxItems exist remove the items beyond MaxItems
while (Count > MaxItems) RemoveAt(MaxItems);
return myMRI;
}
public MostRecentItem Add(int itemID)
{
return Add(new MostRecentItem(ItemInfo.Get(itemID)));
}
public MostRecentItem Add(ItemInfo myItem)
{
return Add(new MostRecentItem(myItem));
}
public MostRecentItem Add(string s)
{
MostRecentItem mri = MostRecentItem.ParseString(s);
if(mri != null)
Add(mri);
return mri;
}
public MostRecentItem Add(IVEDrillDownReadOnly myDrillDown)
{
if (typeof(ItemInfo).IsAssignableFrom(myDrillDown.GetType()))
return Add(new MostRecentItem((ItemInfo)myDrillDown));
return null;
}
public static MostRecentItemList GetMRILst(System.Collections.Specialized.StringCollection list)
{
MostRecentItemList mril = new MostRecentItemList();
if (list != null)
for (int i = list.Count - 1; i >= 0;i-- )// Add in reverse order so first is last and last is first
mril.Add(list[i]);
return mril;
}
public System.Collections.Specialized.StringCollection ToSettings()
{
if (Count == 0) return null;
System.Collections.Specialized.StringCollection retval = new System.Collections.Specialized.StringCollection();
foreach (MostRecentItem mri in this)
retval.Add(mri.ToString());
return retval;
}
} // Class
public class MostRecentItem
{
[NonSerialized]
private ItemInfo _MyItemInfo;
public ItemInfo MyItemInfo
{
get
{
if (_MyItemInfo == null)
_MyItemInfo = ItemInfo.Get(_ItemID);
return _MyItemInfo;
}
set
{
_ItemID = value.ItemID;
_MyItemInfo = value;
_MenuTitle = GetMenuTitle();
_ToolTip = GetToolTip(_MyItemInfo);
}
}
private int _ItemID;
public int ItemID
{
get { return _ItemID; }
set { _ItemID = value; }
}
private string _MenuTitle;
public string MenuTitle
{
get
{
if (_MenuTitle == null)
_MenuTitle = GetMenuTitle();
return _MenuTitle;
}
set { _MenuTitle = value; }
}
private string GetMenuTitle()
{
return MyItemInfo.Path;
}
private string _ToolTip;
public string ToolTip
{
get
{
if (_ToolTip == null)
_ToolTip = GetToolTip(MyItemInfo);
return _ToolTip;
}
set { _ToolTip = value; }
}
private static string GetToolTip(ItemInfo item)
{
// reset active parent if null
// if (MyItemInfo.MyProcedure.ActiveParent == null) MyItemInfo.MyProcedure.ActiveParent = null;
DocVersionInfo tmp = (DocVersionInfo)(item.MyProcedure.ActiveParent);
StringBuilder sb = new StringBuilder();
int indent = BuildPath(tmp.MyFolder, ref sb);
return sb.ToString();
}
private static int BuildPath(FolderInfo folderInfo, ref StringBuilder sb)
{
if (folderInfo.MyParent == null) return 0;
int indent = BuildPath(folderInfo.MyParent, ref sb);
sb.Append(string.Format("{0}{1}\n", "".PadLeft(indent * 2), folderInfo.Name));
return indent;
}
public override string ToString()
{
return string.Format("{0}~{1}~{2}", _ItemID, MenuTitle, ToolTip);
}
public static MostRecentItem ParseString(string str)
{
string[] parts = str.Split("~".ToCharArray());
int itemID = int.Parse(parts[0]);
ItemInfo item = ItemInfo.Get(itemID);
if (item == null) return null;
if (parts.Length > 1 && parts[1] != item.Path && !RetainBadMRIs())
return null; // Path doesn't match
if (parts.Length > 2 && parts[2] != GetToolTip(item) && !RetainBadMRIs())
return null;// Tooltip doesn't match
Console.WriteLine("{0},'{1}'", parts[0], parts[1]);
return new MostRecentItem(item);
}
private static int _RetainBadMRIs = 0;
private static bool RetainBadMRIs()
{
if (_RetainBadMRIs == 0)
{
System.Windows.Forms.DialogResult result = System.Windows.Forms.MessageBox.Show("Do you want to retain old Recently Used Entries?", "Retain Recent Locations", System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Question);
_RetainBadMRIs = result == System.Windows.Forms.DialogResult.Yes ? 2 : 1;
}
return _RetainBadMRIs == 2;
}
public MostRecentItem(ItemInfo myItem)
{
MyItemInfo = myItem;
}
}
}