private EnvVars FindMatchingSet(EnvVars sets, string name)
        {
            if (sets.Name == name)
            {
                return sets;
            }

            foreach (EnvVars set in sets.EnvVarsCollection)
            {
                if (set.Name == name)
                {
                    return set;
                }
            }
            return null;
        }
        private MemberPrecidence LoadScatterElements(object owner, object conditional, List<MemoryMap> maps, ArrayList conditionalStack)
        {
            MemberPrecidence ret = MemberPrecidence.Unknown;

            List<If> ifs = GetCollectionFromObject(conditional, typeof(List<If>)) as List<If>;
            if (ifs != null && ifs.Count > 0)
            {
                LoadScatterConditional(owner, ifs, maps, conditionalStack);
            }
            List<IfDefined> ifdefs = GetCollectionFromObject(conditional, typeof(List<IfDefined>)) as List<IfDefined>;
            if (ifdefs != null)
            {
                LoadScatterConditional(owner, ifdefs, maps, conditionalStack);
            }
            List<IfNotDefined> ifndefs = GetCollectionFromObject(conditional, typeof(List<IfNotDefined>)) as List<IfNotDefined>;
            if (ifndefs != null)
            {
                LoadScatterConditional(owner, ifndefs, maps, conditionalStack);
            }

            List<LoadRegion> lrCollection = GetCollectionFromObject(conditional, typeof(List<LoadRegion>)) as List<LoadRegion>;
            if (lrCollection != null && lrCollection.Count > 0)
            {
                List<MemoryRegion> regionSet = GetCollectionFromObject(owner, typeof(List<MemoryRegion>)) as List<MemoryRegion>;

                ret = MemberPrecidence.LoadRegion;

                foreach (LoadRegion lr in lrCollection)
                {
                    MemoryRegion region = new MemoryRegion();
                    region.Name = lr.Name;
                    region.Address = lr.Base;
                    region.Options = lr.Options;
                    region.Size = lr.Size;
//                    region.Order = regionSet.Count;
                    region.Conditional = GetConditionalValue(conditional);

                    regionSet.Add(region);

                    ret = LoadScatterElements(region, lr, maps, conditionalStack);
                }
            }

            List<ExecRegion> erCollection = GetCollectionFromObject(conditional, typeof(List<ExecRegion>)) as List<ExecRegion>;
            if (erCollection != null && erCollection.Count > 0)
            {
                List<MemorySection> sectionSet = GetCollectionFromObject(owner, typeof(List<MemorySection>)) as List<MemorySection>;

                ret = MemberPrecidence.ExecRegion;

                foreach (ExecRegion er in erCollection)
                {
                    MemorySection section = new MemorySection();
                    section.Name        = er.Name;
                    section.Address     = er.Base;
                    section.Options     = er.Options;
                    section.Size        = er.Size;
                    section.Order       = (int)m_execRegionOrderMap[(owner as MemoryRegion).Name + ":" + er.Name];
                    section.Conditional = GetConditionalValue(conditional);

                    sectionSet.Add(section);

                    ret = LoadScatterElements(section, er, maps, conditionalStack);
                }
            }

            List<FileMapping> fileCollection = GetCollectionFromObject(conditional, typeof(List<FileMapping>)) as List<FileMapping>;
            if (fileCollection != null && fileCollection.Count > 0)
            {
                List<MemorySymbol> symSet = GetCollectionFromObject(owner, typeof(List<MemorySymbol>)) as List<MemorySymbol>;

                ret = MemberPrecidence.FileMapping;


                foreach (FileMapping fm in fileCollection)
                {
                    MemorySymbol sym = new MemorySymbol();
                    sym.Name       = fm.Name;
                    sym.Options    = fm.Options;
                    sym.Conditional = GetConditionalValue(conditional);

                    symSet.Add(sym);
                }
            }

            List<Set> setCollection = GetCollectionFromObject(conditional, typeof(List<Set>)) as List<Set>;
            if (setCollection != null && setCollection.Count > 0)
            {
                EnvVars envSets = GetObjectFromProperty(owner, typeof(EnvVars)) as EnvVars;

                if (ret < MemberPrecidence.SetMember)
                {
                    ret = MemberPrecidence.SetMember;
                }

                EnvVars envSet = null;

                // conditional belongs to the map if we have a load region in this conditional
                if(conditionalStack.Count == 0)
                {
                    envSet = FindMatchingSet(envSets, "Global");

                    if (envSet == null)
                    {
                        envSet = new EnvVars();
                        envSet.Name = "Global";
                        envSets.EnvVarsCollection.Add(envSet);
                    }
                }
                else
                {
                    for (int i = 0; i < conditionalStack.Count; i++)
                    {
                        object cond = conditionalStack[i];

                        if (i == (conditionalStack.Count-1) && owner is MemoryMap && ret >= MemberPrecidence.LoadRegion)
                        {
                            ((MemoryMap)owner).Name        = GetConditionalName(conditional);
                            ((MemoryMap)owner).Conditional = GetConditionalValue(conditional);
                        }
                        else
                        {
                            string name = GetConditionalName(cond);
                            string value = GetConditionalValue(cond);

                            name += "_" + value.Replace(" ", "_").Replace("\"", "");

                            envSet = FindMatchingSet(envSets, name);

                            if (envSet == null)
                            {
                                envSet = new EnvVars();
                                envSet.Name = name;
                                envSet.Conditional = value;

                                envSets.EnvVarsCollection.Add(envSet);
                            }

                            //envSets = envSet.EnvVarsCollection;
                        }
                    }
                }


                if ((int)ret < (int)MemberPrecidence.SetMember)
                {
                    ret = MemberPrecidence.SetMember;
                }

                foreach (Set set in GetCollectionFromObject(conditional, typeof(List<Set>)))
                {
                    EnvVar var = new EnvVar();
                    var.Name = set.Name;
                    var.Value = set.Value;

                    envSet.EnvVarCollection.Add(var);
                }
            }

            return ret;
        }
 public void CopyTo(EnvVars dest)
 {
     CopyHelper.CopyTo(this, dest);
 }
        private void AddEnvVarCollection(object owner, EnvVars varSet)
        {
            object setOwner = AddConditional(owner, varSet.Conditional);

            foreach (EnvVar var in varSet.EnvVarCollection)
            {
                object varOwner = AddConditional(setOwner, var.Conditional);

                List<Set> list = GetCollectionFromObject(varOwner, typeof(List<Set>)) as List<Set>;

                Set env = new Set();
                env.Name = var.Name;
                env.Value = var.Value;

                list.Add(env);
            }
            foreach (EnvVars set in varSet.EnvVarsCollection)
            {
                AddEnvVarCollection(setOwner, set);
            }
        }
        private void addEnvironmentalVariableSetToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TreeNode tn = treeViewInventory.SelectedNode;
            TreeNodeData tnd = treeViewInventory.SelectedNode.Tag as TreeNodeData;

            if (tnd != null)
            {
                EnvVars ms = new EnvVars();
                ms.Name = "<New Env Var Set>";
                (tnd.Data as List<EnvVars>).Add(ms);
                TreeNode child = AddTreeElement(tn, ms.Name, ms, true, tnd.Data, tnd.FilePath);
                child.EnsureVisible();
                treeViewInventory.SelectedNode = child;
                child.BeginEdit();
            }
        }