2008-03-03 15:15:39 +00:00

235 lines
5.7 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using VEPROMS.CSLA.Library;
namespace Volian.Controls.Library
{
public partial class DisplayRO : UserControl
{
#region Properties
private ROFST _CurROFST = null;
private ROFST _MyROFST;
public ROFST MyROFST
{
get { return _MyROFST; }
set
{
_MyROFST = value;
LoadTree();
}
}
private string _CurROLink;
public string CurROLink
{
get { return _CurROLink; }
set
{
_CurROLink = value;
if (_CurROLink != null)
{
UpdateROTree();
_SavCurROLink = _CurROLink;
}
}
}
private string _SavCurROLink;
private ItemInfo _CurItem;
public ItemInfo CurItem
{
get { return _CurItem; }
set { _CurItem = value; }
}
private DisplayRTB _MyRTB;
public DisplayRTB MyRTB
{
get { return _MyRTB; }
set { _MyRTB = value; }
}
#endregion
#region Constructors
public DisplayRO()
{
InitializeComponent();
}
//public DisplayRO(ROFST rofst, ItemInfo curitm, bool modify, DisplayRTB rtb)
//{
// _MyROFST = rofst;
// _CurItem = curitm;
// _MyRTB = rtb;
// InitializeComponent();
// LoadTree();
//}
#endregion
#region Events
private void tvROFST_AfterSelect(object sender, TreeViewEventArgs e)
{
if (e.Node.Tag is ROFST.rochild)
{
ROFST.rochild chld = (ROFST.rochild)e.Node.Tag;
if (chld.type == 1 && chld.value != null) tbROValue.Text = chld.value;
else tbROValue.Text = null;
}
else
tbROValue.Text = null;
}
private void tvROFST_BeforeExpand(object sender, TreeViewCancelEventArgs e)
{
LoadChildren(e.Node);
}
private void LoadChildren(TreeNode tn)
{
object tag = tn.Tag;
if (tn.FirstNode != null && tn.FirstNode.Text != "VLN_DUMMY_FOR_TREE") return; // already loaded.
if (tn.FirstNode != null && tn.FirstNode.Text == "VLN_DUMMY_FOR_TREE") tn.FirstNode.Remove();
ROFST.rochild[] chld = null;
if (tn.Tag is ROFST.rodbi)
{
ROFST.rodbi db = (ROFST.rodbi)tn.Tag;
chld = db.children;
}
else if (tn.Tag is ROFST.rochild)
{
ROFST.rochild ch = (ROFST.rochild)tn.Tag;
chld = ch.children;
}
else
{
Console.WriteLine("error - no type");
return;
}
// if children, add dummy node
if (chld != null && chld.Length > 0)
{
for (int i = 0; i < chld.Length; i++)
{
TreeNode tmp = null;
// if this is a group, i.e. type 0, add a dummy node
if (chld[i].type == 0 && chld[i].children == null)
//skip it.
Console.WriteLine("ro junk");
else if (chld[i].type == 1 && chld[i].value == null)
{
tmp = new TreeNode(chld[i].title);
tmp.Tag = chld[i];
tn.Nodes.Add(tmp);
TreeNode sub = new TreeNode("VLN_DUMMY_FOR_TREE");
tmp.Nodes.Add(sub);
}
else
{
tmp = new TreeNode(chld[i].title);
tmp.Tag = chld[i];
tn.Nodes.Add(tmp);
}
}
}
}
private void LoadTree()
{
if (_MyROFST == null) return;
if (_MyROFST == _CurROFST) return;
_CurROFST = _MyROFST;
for (int i = 0; i < _MyROFST.myHdr.myDbs.Length; i++)
{
TreeNode tn = new TreeNode(_MyROFST.myHdr.myDbs[i].dbiTitle);
tn.Tag = _MyROFST.myHdr.myDbs[i];
tvROFST.Nodes.Add(tn);
AddDummyGroup(_MyROFST.myHdr.myDbs[i], tn);
}
}
private void AddDummyGroup(ROFST.rodbi rodbi, TreeNode tn)
{
if (rodbi.children != null && rodbi.children.Length > 0)
{
TreeNode tmp = new TreeNode("VLN_DUMMY_FOR_TREE");
tn.Nodes.Add(tmp);
}
}
private void UpdateROTree()
{
// walk down from root of tree, expanding values in the string
// that represents the ro link.
string tmpstr = _CurROLink;
int sp = tmpstr.IndexOf(" "); // because parse of ro info is wrong!!
int rousageid = System.Convert.ToInt32(tmpstr.Substring(0, sp));
string roid = tmpstr.Substring(sp + 1, tmpstr.Length - sp - 1);
string db = roid.Substring(0, 4);
ROFST.rochild rochld = MyROFST.GetRoChild(roid.Substring(0, 12).ToUpper());
// use this to walk up tree until database - this is used to expand tree.
List<int> path = new List<int>();
int myid = rochld.ID;
while (myid > 0)
{
path.Insert(0,myid);
myid = rochld.ParentID;
rochld = MyROFST.GetRoChildFromID(myid);
if (rochld.ID == -1) myid = -1;
}
TreeNode tnExpand = null;
int titm = System.Convert.ToInt32(db);
// find database first
foreach (TreeNode tn in tvROFST.Nodes)
{
ROFST.rodbi thisdb = (ROFST.rodbi)tn.Tag;
if (thisdb.dbiID == titm)
{
LoadChildren(tn);
tnExpand = tn;
break;
}
}
if (tnExpand == null) return; // something went wrong?
// use the path id list to load/find the treeview's nodes.
foreach (int citm in path)
{
if (citm != System.Convert.ToInt32(db))
{
LoadChildren(tnExpand);
tnExpand.Expand();
foreach (TreeNode tn in tnExpand.Nodes)
{
ROFST.rochild chld = (ROFST.rochild)tn.Tag;
if (chld.ID == citm)
{
tnExpand = tn;
break;
}
}
}
}
if (tnExpand != null) tvROFST.SelectedNode = tnExpand;
}
private void btnSaveRO_Click(object sender, EventArgs e)
{
if (tbROValue.Text == null || tbROValue.Text == "")
{
MessageBox.Show("Must select an RO Value from the tree.");
return;
}
Object obj = tvROFST.SelectedNode.Tag;
if (obj is ROFST.rochild)
{
ROFST.rochild roch = (ROFST.rochild) obj;
string linktxt = string.Format("#Link:ReferencedObject: #xx {0}", roch.roid);
_MyRTB.InsertRO(tbROValue.Text, linktxt);
}
}
#endregion
private void btnCancelRO_Click(object sender, EventArgs e)
{
_CurROLink = _SavCurROLink;
UpdateROTree();
}
}
}