private List<BuildToolRef> LoadBuildToolFlags( Project proj )
        {
            List<BuildToolRef> toolRefs = new List<BuildToolRef>();

            Dictionary<ProjectPropertyElement, ProjectPropertyGroupElement> remList = new Dictionary<ProjectPropertyElement, ProjectPropertyGroupElement>();

            foreach (ProjectPropertyGroupElement pg in proj.Xml.PropertyGroups)
            {
                Regex exp = new Regex("\\s*'\\$\\(COMPILER_TOOL\\)'\\s*==\\s*'([\\w\\W]+)'");
                BuildToolRef btr = null;

                if (exp.IsMatch(pg.Condition))
                {
                    btr = new BuildToolRef();
                    toolRefs.Add(btr);
                    btr.Name = exp.Match(pg.Condition).Groups[1].Value;
                }
                else
                {
                    continue;
                }

                btr.BuildOptions = LoadToolChainOptions(pg);

                foreach (ProjectPropertyElement bp in pg.Properties)
                {
                    remList[bp] = pg;

                    switch (bp.Name)
                    {
                        case "BUILD_TOOL_GUID":
                            btr.Guid = bp.Value;
                            break;
                        case "DEVICE_TYPE":
                            btr.BuildOptions.DeviceType = bp.Value;
                            break;

                        default:
                            remList.Remove(bp);
                            break;
                    }
                }
            }

            foreach (ProjectPropertyElement bp in remList.Keys)
            {
                remList[bp].RemoveChild(bp);
            }

            return toolRefs;
        }
        private TreeNode AddTreeElement(TreeNode parent, string name, object item, bool isNew, object owner, string filePath)
        {
            TreeNode tn = new TreeNode(name);
            filePath = filePath.ToLower();

            tn.Tag = new TreeNodeData( item, isNew, name, owner, filePath );

            if (!m_fileToColorTable.ContainsKey(filePath))
            {
                if (m_colorIndex >= c_TextColors.Count)
                {
                    m_colorIndex--; // the last color
                }
                m_fileToColorTable[filePath] = c_TextColors[m_colorIndex++];
            }

            tn.ForeColor = (Color)m_fileToColorTable[filePath];

            TreeNode folder = parent;

            if (!isNew)
            {
                //add feature folders
                Feature f = item as Feature;
                if (f != null && f.Groups != null && f.Groups.Length > 0)
                {
                    string path = f.Groups.Split(';')[0];

                    foreach (string p in path.Split('\\'))
                    {
                        if (folder.Nodes.ContainsKey(p))
                        {
                            folder = folder.Nodes[p];
                        }
                        else
                        {
                            folder = folder.Nodes.Add(p, p);
                        }
                    }
                }

                // add library folders
                Library lib = item as Library;
                if (lib != null)
                {
                    if (folder.Nodes.ContainsKey(lib.Level.ToString()))
                    {
                        folder = folder.Nodes[lib.Level.ToString()];
                    }
                    else
                    {
                        folder = folder.Nodes.Add(lib.Level.ToString(), lib.Level.ToString());
                    }

                    if (lib.Groups != null && lib.Groups.Length > 0)
                    {
                        string path = lib.Groups.Split(';')[0];

                        foreach (string p in path.Split('\\'))
                        {
                            if (folder.Nodes.ContainsKey(p))
                            {
                                folder = folder.Nodes[p];
                            }
                            else
                            {
                                folder = folder.Nodes.Add(p, p);
                            }
                        }
                    }
                }

                // add library folders
                LibraryCategory libType = item as LibraryCategory;
                if (libType != null)
                {
                    if (folder.Nodes.ContainsKey(libType.Level.ToString()))
                    {
                        folder = folder.Nodes[libType.Level.ToString()];
                    }
                    else
                    {
                        folder = folder.Nodes.Add(libType.Level.ToString(), libType.Level.ToString());
                    }
                }

            }
            // finally add the real node
            folder.Nodes.Add(tn);

            if (item == null) return tn;

            if (item is MFSolution) tn.ContextMenuStrip = contextMenuStripProject;
            else if (item is List<MiscBuildTool>) tn.ContextMenuStrip = contextMenuStripAddMiscTool;
            else if (item is List<MFComponent>) tn.ContextMenuStrip = contextMenuStripDependency;
            else if (item is List<BuildToolRef>) tn.ContextMenuStrip = contextMenuStripAddBuildToolsRef;
            else if (item is Library || item is LibraryCategory || item is MFAssembly ||
                    item is BuildTool || item is Processor || item is MFProject ||
                    item is Feature)
            {
                tn.ContextMenuStrip = contextMenuStripSave;
            }
            if (item is List<BuildParameter> || item is List<MFBuildFile> || item is List<string> || item is List<MFProperty> ||
                item is List<ToolFlag> || item is List<BuildScript> || item is List<ApiTemplate>)
            {
                return tn;
            }

            if (item is List<BuildToolRef>)
            {
                List<string> dtNames = new List<string>();
                string ptName = "";
                string isaGuid = "";
                List<BuildToolRef> btsr = item as List<BuildToolRef>;

                if (owner is ISA)
                {
                    isaGuid = (owner as ISA).Guid;
                }
                else if (owner is Processor)
                {
                    foreach (BuildToolRef btr in btsr)
                    {
                        if (!string.IsNullOrEmpty(btr.BuildOptions.DeviceType))
                        {
                            dtNames.Add(btr.BuildOptions.DeviceType.ToLower());
                        }
                    }
                    
                    ptName = ((Processor)owner).CpuName;
                }

                Dictionary<string, BuildToolRef> hash = new Dictionary<string, BuildToolRef>();

                foreach (BuildToolRef btr in btsr)
                {
                    hash[btr.Guid.ToLower()] = btr;
                    AddTreeElement(tn, btr.Name, btr, isNew, item, filePath);
                }
                if (dtNames.Count > 0 || !string.IsNullOrEmpty(ptName) || !string.IsNullOrEmpty(isaGuid))
                {
                    foreach (Inventory inv in m_fileToInventory.Values)
                    {
                        foreach (BuildTool bt in inv.BuildTools)
                        {
                            if (!string.IsNullOrEmpty(isaGuid))
                            {
                                foreach (ISA ptr in bt.SupportedISAs)
                                {
                                    if (ptr.Guid == isaGuid && !hash.ContainsKey(bt.Guid.ToLower()))
                                    {
                                        BuildToolRef newBtr = new BuildToolRef();
                                        newBtr.Guid = bt.Guid;
                                        newBtr.Name = bt.Name;

                                        btsr.Add(newBtr);
                                        AddTreeElement(tn, newBtr.Name, newBtr, isNew, item, filePath);
                                    }
                                }
                            }
                            if (dtNames.Count > 0)
                            {
                                foreach (string supportedPT in bt.SupportedCpuNames)
                                {
                                    if (dtNames.Contains(supportedPT.ToLower()) && !hash.ContainsKey(bt.Guid.ToLower()))
                                    {
                                        BuildToolRef newBtr = new BuildToolRef();
                                        newBtr.Guid = bt.Guid;
                                        newBtr.Name = bt.Name;

                                        btsr.Add(newBtr);
                                        AddTreeElement(tn, newBtr.Name, newBtr, isNew, item, filePath);

                                        hash[bt.Guid.ToLower()] = newBtr;
                                    }
                                }
                            }
                            if (!string.IsNullOrEmpty(ptName))
                            {
                                foreach (string supportedPT in bt.SupportedCpuNames)
                                {
                                    if (0 == string.Compare(supportedPT, ptName, true) && !hash.ContainsKey(bt.Guid.ToLower()))
                                    {
                                        BuildToolRef newBtr = new BuildToolRef();
                                        newBtr.Guid = bt.Guid;
                                        newBtr.Name = bt.Name;

                                        btsr.Add(newBtr);
                                        AddTreeElement(tn, newBtr.Name, newBtr, isNew, item, filePath);

                                        hash[bt.Guid.ToLower()] = newBtr;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            else if (item is IList)
            {
                foreach (object o in (IList)item)
                {
                    PropertyInfo pi = o.GetType().GetProperty("Name");
                    if (pi != null)
                    {
                        AddTreeElement(tn, (string)pi.GetValue(o, null), o, isNew, item, filePath);
                    }
                    else
                    {
                        Console.WriteLine("Warning: not expanding node: " + o.GetType().Name);
                    }
                }
            }
            else if (item is ProjectElementContainer)
            {
            }
            else
            {
                foreach (PropertyInfo pi in item.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
                {
                    //if (pi.PropertyType != typeof(List<MFComponent>) && pi.PropertyType != typeof(MFComponent))
                    {
                        if ((pi.Name != "Item") && pi.CanWrite && !pi.PropertyType.IsValueType && !(pi.PropertyType == typeof(string)))
                        {
                            object o = pi.GetValue(item, null);
                            if (o == null)
                            {
                                o = pi.PropertyType.GetConstructor(new Type[] { }).Invoke(null);
                                pi.SetValue(item, o, null);
                            }
                            TreeNode child = AddTreeElement(tn, pi.Name, o, isNew, item, filePath);
                        }
                    }
                }
            }

            return tn;
        }
 public void CopyTo(BuildToolRef dest)
 {
     CopyHelper.CopyTo(this, dest);
 }
        private void addBuildToolOptionsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            List<BuildToolRef> refs = ((TreeNodeData)treeViewInventory.SelectedNode.Tag).Data as List<BuildToolRef>;

            if (refs != null)
            {
                BuildToolRef rf = new BuildToolRef();
                rf.Name = "<NEW>";
                refs.Add(rf);
                RefreshTree();
            }
        }