Exemple #1
0
        //- getting values ----------------------------------------------------

        private String findLeafValue(String path, SettingsStem subtree)
        {
            String result = null;
            int    dotpos = path.IndexOf('.');

            if (dotpos != -1)                                   //path is name.subpath
            {
                String name    = path.Substring(0, dotpos);
                String subpath = path.Substring(dotpos + 1);    //break path apart
                if (subtree.children.ContainsKey(name))
                {
                    SettingsNode val = subtree.children[name];
                    if (val != null && val is SettingsStem)
                    {
                        result = findLeafValue(subpath, (SettingsStem)val);
                    }
                }
            }
            else
            {
                if (subtree.children.ContainsKey(path))
                {
                    SettingsNode leaf = subtree.children[path];
                    if (leaf != null && leaf is SettingsLeaf)
                    {
                        result = ((SettingsLeaf)leaf).value;
                    }
                }
            }
            return(result);
        }
Exemple #2
0
        private void Initialize(XmlDocument doc)
        {
            RootNode     = doc.DocumentElement;
            SettingsNode = RootNode.SelectSingleNode(SETTINGS_NODE);
            ConfigNodes  = RootNode.SelectNodes(CONFIG_NODE);

            if (SettingsNode != null)
            {
                ActiveConfigName = SettingsNode.GetAttribute(ACTIVECONFIG_ATTR);
                ProcessModel     = SettingsNode.GetAttribute(PROCESS_ATTR);
                DomainUsage      = SettingsNode.GetAttribute(DOMAIN_ATTR);
                ProjectBase      = SettingsNode.GetAttribute(APPBASE_ATTR);
            }

            if (ActiveConfigName == null && ConfigNodes.Count > 0)
            {
                ActiveConfigName = ConfigNodes[0].GetAttribute(NAME_ATTR);
            }

            if (ProjectBase == null)
            {
                ProjectBase = Path.GetDirectoryName(ProjectPath);
            }
            else if (ProjectPath != null)
            {
                ProjectBase = Path.Combine(Path.GetDirectoryName(ProjectPath), ProjectBase);
            }
        }
 /// <summary>
 /// Used internally to invoke a node removing event.
 /// </summary>
 /// <param name="src">The node that raised this event.</param>
 /// <param name="e">The <see cref="XyratexOSC.Settings.SettingsNodeChangeEventArgs"/> instance containing the event data.</param>
 sealed internal override void RaiseRemoving(SettingsNode src, SettingsNodeChangeEventArgs e)
 {
     if (NodeRemoving != null)
     {
         NodeRemoving(src, e);
     }
 }
        private void SettingsNodeInserted(SettingsNode sender, SettingsNodeChangeEventArgs e)
        {
            string[] nodeNames = e.Node.FullPath.Split(e.Node.PathSeparator);

            if (nodeNames.Length < 1)
            {
                return;
            }

            TreeNodeCollection nodes = treeView.Nodes;

            for (int i = 0; i < nodeNames.Length - 1; i++)
            {
                TreeNode node = nodes[nodeNames[i]];
                if (node == null)
                {
                    return;
                }

                nodes = node.Nodes;
            }

            UIUtility.Invoke(this, () =>
            {
                TreeNode newNode = new TreeNode(e.Node.Name);
                newNode.Name     = newNode.Text;
                newNode.Tag      = e.Node;
                nodes.Add(newNode);
            });
        }
Exemple #5
0
        //////////////////////////////////////////////////////////////////////////
        private void OnLoad(object sender, EventArgs e)
        {
            AppMgr.Settings.LoadFromXmlFile();
            LoadLayout(AppMgr.Settings);

            SettingsNode Node = AppMgr.Settings.GetNode("Layout\\" + this.Name);

            if (Node != null)
            {
                this.TopMost = Node.GetBool("FormTopMost", false);
            }
            BtnAlwaysOnTop.Checked = this.TopMost;

            // load watches from project's registry
            try
            {
                string RegPath = _Client.Server.GetPropString("RegistryPath");
                RegPath += @"\Debug";
                using (RegistryKey Key = Registry.CurrentUser.OpenSubKey(RegPath))
                {
                    int NumWatches = int.Parse(Key.GetValue("NumWatches", 0).ToString());
                    for (int i = 0; i < NumWatches; i++)
                    {
                        string WatchName = Key.GetValue("Watch" + (i + 1).ToString(), "").ToString();
                        if (WatchName != string.Empty)
                        {
                            Watch.AddWatch(WatchName);
                        }
                    }
                }
            }
            catch
            {
            }
        }
        /// <summary>
        /// Converts the specified object to a <see cref="SettingsNode"/>.
        /// </summary>
        /// <param name="obj">The object.</param>
        /// <param name="nodeName">The node name.</param>
        /// <returns></returns>
        public static SettingsNode ConvertObjectToNode(object obj, string nodeName = "")
        {
            if (obj == null)
            {
                return(null);
            }

            Type type = obj.GetType();

            if (String.IsNullOrEmpty(nodeName))
            {
                nodeName = type.Name;
            }

            if (obj is ISettings)
            {
                return(((ISettings)obj).ConvertToSettingsNode());
            }
            else
            {
                SettingsNode node = new SettingsNode(nodeName, "", type);
                GetNodes(obj, type, node);
                return(node);
            }
        }
        private void SettingsNodeRemoved(SettingsNode sender, SettingsNodeChangeEventArgs e)
        {
            string[] nodeNames = e.Node.FullPath.Split(e.Node.PathSeparator);

            if (nodeNames.Length < 1)
            {
                return;
            }

            TreeNodeCollection nodes = treeView.Nodes;

            for (int i = 0; i < nodeNames.Length - 1; i++)
            {
                TreeNode node = nodes[nodeNames[i]];
                if (node == null)
                {
                    return;
                }

                nodes = node.Nodes;
            }

            UIUtility.Invoke(this, () =>
            {
                if (nodes.ContainsKey(e.Node.Name))
                {
                    nodes.RemoveByKey(e.Node.Name);
                }
            });
        }
        private void UpdateSettingsNodes(TreeNodeCollection nodes)
        {
            foreach (TreeNode node in nodes)
            {
                if (node.Text != ((SettingsNode)node.Tag).Name)
                {
                    try
                    {
                        SettingsNode setting = node.Tag as SettingsNode;

                        if (setting == null)
                        {
                            continue;
                        }

                        if (setting.Name == node.Text)
                        {
                            continue;
                        }

                        setting.Name = node.Text;

                        if (setting.IsAValue)
                        {
                            Logging.Log.Info("Settings", "{0} = {1}", setting.Parent.FullPath, node.Text);
                        }
                    }
                    catch (InvalidCastException)
                    {
                        node.Text = ((SettingsNode)node.Tag).Name;
                    }
                }
                UpdateSettingsNodes(node.Nodes);
            }
        }
        private static void UpdateNodeFromNode(SettingsNode sourceNode, SettingsNode destinationNode)
        {
            if (sourceNode.IsList)
            {
                destinationNode.RemoveAllChildren();
                destinationNode.ListLength = sourceNode.ListLength;
            }

            foreach (SettingsNode node in sourceNode.Nodes)
            {
                if (destinationNode.HasAValue)
                {
                    destinationNode.RemoveAllChildren();
                }

                if (destinationNode.Nodes.ContainsName(node.Name))
                {
                    UpdateNodeFromNode(node, destinationNode.Nodes[node.Name]);
                }
                else
                {
                    destinationNode.AddChild(node.Clone());
                }
            }
        }
        /// <summary>
        /// Converts the specified object to a <see cref="SettingsDocument"/>.
        /// </summary>
        /// <param name="obj">The object.</param>
        /// <param name="fileName">The file name.</param>
        /// <returns></returns>
        public static SettingsDocument ConvertObjectToDocument(object obj, string fileName)
        {
            if (obj == null)
            {
                return(null);
            }

            SettingsNode settingsNode = ConvertObjectToNode(obj);

            SettingsDocument settingsDoc = new SettingsDocument();

            settingsDoc.PathSeparator = '.';
            settingsDoc.Name          = fileName;

            foreach (SettingsNode node in settingsNode.Nodes)
            {
                if (settingsDoc.Nodes.ContainsName(node.Name))
                {
                    UpdateNodeFromNode(node, settingsDoc.Nodes[node.Name]);
                }
                else if (!node.IsAValue)
                {
                    settingsDoc.AddChild(node);
                }
            }

            return(settingsDoc);
        }
        /// <summary>
        /// Creates an object of the generic type from the specified <see cref="SettingsNode" />.
        /// </summary>
        /// <param name="node">The settings node.</param>
        /// <param name="type">The type of object to create.</param>
        /// <param name="args">The optional constructor arguments for this node object.</param>
        /// <returns>
        /// The new object.
        /// </returns>
        public static object CreateObjectFromNode(SettingsNode node, Type type, params object[] args)
        {
            if (node == null)
            {
                return(null);
            }

            if (node.IsAValue && node.Value == null)
            {
                return(null);
            }

            if (node.HasAValue && node.Value == null)
            {
                return(null);
            }

            object newObject = null;

            if (type.IsArray)
            {
                newObject = Array.CreateInstance(type.GetElementType(), node.ListLength);
            }
            else
            {
                newObject = Activator.CreateInstance(type, args);
            }

            UpdateObjectFromNode(newObject, node);

            return(newObject);
        }
        private void SettingsNodeChanged(SettingsNode sender, SettingsNodeChangeEventArgs e)
        {
            string[] nodeNames = e.Node.FullPath.Split(e.Node.PathSeparator);

            if (nodeNames.Length < 1)
            {
                return;
            }

            TreeNode changedNode = treeView.Nodes[nodeNames[0]];

            for (int i = 1; i < nodeNames.Length; i++)
            {
                if (changedNode == null)
                {
                    return;
                }

                changedNode = changedNode.Nodes[nodeNames[i]];
            }

            UIUtility.Invoke(this, () =>
            {
                TreeNode valueNode = changedNode.FirstNode;
                valueNode.Text     = e.Node.Value.ToString();
                valueNode.Name     = valueNode.Text;
            });
        }
        //////////////////////////////////////////////////////////////////////////
        public void SaveSettings(SettingsNode RootNode)
        {
            SettingsNode Section = RootNode.GetNode("General", true, true);

            if (Section == null)
            {
                return;
            }

            Section.Clear();

            Section.SetValue("SelectionColor", SelectionColor);
            Section.SetValue("BoxColor", BoxColor);
            Section.SetValue("BackgroundColor", WindowCanvas.BackColor);
            Section.SetValue("GridWidth", GridSize.Width);
            Section.SetValue("GridHeight", GridSize.Height);

            SettingsNode RecentList = new SettingsNode("RecentFiles");

            foreach (string RecentFile in _RecentFiles)
            {
                RecentList.Children.Add(new SettingsNode("RecentFile", RecentFile));
            }
            Section.Children.Add(RecentList);
        }
        //////////////////////////////////////////////////////////////////////////
        public void LoadSettings(SettingsNode RootNode)
        {
            SettingsNode Section = RootNode.GetNode("General", true);

            if (Section == null)
            {
                return;
            }

            SelectionColor         = Section.GetColor("SelectionColor", SelectionColor);
            BoxColor               = Section.GetColor("BoxColor", BoxColor);
            WindowCanvas.BackColor = Section.GetColor("BackgroundColor", WindowCanvas.BackColor);
            GridSize               = new Size(Section.GetInt("GridWidth", GridSize.Width), Section.GetInt("GridHeight", GridSize.Height));

            _RecentFiles.Clear();
            SettingsNode RecentList = Section.GetNode("RecentFiles");

            if (RecentList != null)
            {
                foreach (SettingsNode Node in RecentList.Children)
                {
                    string RecentFile = Node.GetString();
                    if (File.Exists(RecentFile))
                    {
                        _RecentFiles.Add(RecentFile);
                    }
                }
            }
        }
        public void GetLevel_TopLevel_ReturnsZero()
        {
            SettingsNode node = new SettingsNode("Test");

            int level = node.Level;

            Assert.AreEqual(0, level);
        }
Exemple #16
0
        public void SimpleSetupTest()
        {
            string       name     = "GetTested!";
            SettingsNode testNode = new SettingsNode(name);

            Assert.AreEqual(name, testNode.Name);
            Assert.IsNotNull(testNode.Settings);
        }
Exemple #17
0
        public SettingsNode ConvertToSettingsNode()
        {
            SettingsNode node = new SettingsNode("Hga");

            node.AddChild("Index", "", typeof(int), Index);
            node.AddChild("Hga_Status", "", typeof(HGAStatus), Hga_Status);

            return(node);
        }
Exemple #18
0
        /// <summary>
        /// Saves the current settings to the .ini file
        /// </summary>
        public void SaveSettings()
        {
            // Start by organizing the values into a tree
            SettingsNode baseNode = new SettingsNode();

            foreach (string value in _values.Keys)
            {
                string path = value;

                // Base settings, create values at the base node
                if (path.IndexOf("\\", StringComparison.Ordinal) == -1)
                {
                    baseNode[value] = _values[value];
                }
                else
                {
                    string[] subPath = path.Split('\\');

                    path = "";

                    for (int i = 0; i < subPath.Length - 1; i++)
                    {
                        if (path == "")
                        {
                            path += subPath[i];
                        }
                        else
                        {
                            path += "\\" + subPath[i];
                        }
                    }

                    baseNode.CreateNode(path)[subPath[subPath.Length - 1]] = _values[value];
                }
            }

            // Sort the nodes after they are created
            baseNode.Sort();

            // Mount the settings string
            StringBuilder output = new StringBuilder();

            baseNode.SaveToString(output);

            using (FileStream stream = new FileStream(_filePath, FileMode.OpenOrCreate, FileAccess.Write))
            {
                stream.SetLength(0);

                // Save the settings to the settings file now
                StreamWriter writer = new StreamWriter(stream, Encoding.UTF8);
                writer.Write(output.ToString().Trim());
                writer.Close();
                writer.Dispose();
            }

            baseNode.Clear();
        }
        public void GetNode_PathExists_ReturnsNode()
        {
            SettingsNode node = new SettingsNode("Test");

            node.AddChild("Test Child").AddChild("Child of Child");

            SettingsNode foundNode = node.GetNode("Test Child | Child of Child");

            Assert.IsNotNull(foundNode);
        }
        public void GetNode_PathDiffCasing_ReturnsNode()
        {
            SettingsNode node = new SettingsNode("Test");

            node.AddChild("Test Child").AddChild("Child of Child");

            SettingsNode foundNode = node.GetNode("TEST CHILD | ChILd oF chIld");

            Assert.IsNotNull(foundNode);
        }
        /// <summary>
        /// Creates an object of the generic type from the specified <see cref="SettingsNode" />.
        /// </summary>
        /// <typeparam name="T">The type of object to create.</typeparam>
        /// <param name="node">The settings node.</param>
        /// <param name="args">The optional constructor arguments for this node object.</param>
        /// <returns>
        /// The new object.
        /// </returns>
        public static T CreateObjectFromNode <T>(SettingsNode node, params object[] args)
        {
            if (node == null)
            {
                return(default(T));
            }

            Type type = typeof(T);

            return((T)CreateObjectFromNode(node, type, args));
        }
 /// <summary>
 /// Return the value from the node that matches nodelabel or return the defaultValue
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="node"></param>
 /// <param name="nodeLabel"></param>
 /// <param name="defaultValue"></param>
 /// <returns></returns>
 public static T UpdateFromNode <T>(SettingsNode node, string nodeLabel, T defaultValue)
 {
     if (node.ExistsAndHasAValue <T>(nodeLabel))
     {
         return(node[nodeLabel].GetValueAs <T>());
     }
     else
     {
         return(defaultValue);
     }
 }
Exemple #23
0
 public void UpdateFromSettingsNode(SettingsNode node)
 {
     if (node.ExistsAndHasAValue <int>("Index"))
     {
         Index = node["Index"].GetValueAs <int>();
     }
     if (node.ExistsAndHasAValue <HGAStatus>("Hga_Status"))
     {
         Hga_Status = node["Hga_Status"].GetValueAs <HGAStatus>();
     }
 }
Exemple #24
0
        //////////////////////////////////////////////////////////////////////////
        public virtual void SaveControlLayout(SettingsNode RootNode)
        {
            SettingsNode Node = RootNode.GetNode(this.Name, false, true);

            if (Node != null)
            {
                for (int i = 0; i < Columns.Count; i++)
                {
                    Node.SetValue("Col" + i.ToString() + "Width", Columns[i].Width);
                }
            }
        }
        public void GetLevel_ChildReturnsOneMoreThanParent()
        {
            SettingsNode node      = new SettingsNode("Test");
            SettingsNode childNode = new SettingsNode("Child");

            node.AddChild(childNode);

            int parentLevel = node.Level;
            int childLevel  = childNode.Level;

            Assert.AreEqual(parentLevel + 1, childLevel);
        }
        /// <summary>
        /// Used internally to invoke a node removed event.
        /// </summary>
        /// <param name="src">The node that raised this event.</param>
        /// <param name="e">The <see cref="XyratexOSC.Settings.SettingsNodeChangeEventArgs"/> instance containing the event data.</param>
        sealed internal override void RaiseRemoved(SettingsNode src, SettingsNodeChangeEventArgs e)
        {
            if (NodeRemoved != null)
            {
                NodeRemoved(src, e);
            }

            if (_persistToFile)
            {
                Save(LoadedFile);
            }
        }
        /// <summary>
        /// Returns a list of all of the lines in this file.
        /// </summary>
        public IList <string> GetFileLines()
        {
            IList <string> lines       = new List <string>();
            SettingsNode   currentNode = this[0];

            while (currentNode != null)
            {
                // Write node to file
                List <string> fileComments = currentNode.GetPrecedingComments();

                for (int i = 0; i < fileComments.Count; i++)
                {
                    lines.Add(fileComments[i]);
                }

                lines.Add(NodeToFileLine(currentNode));

                // Find next line
                if (currentNode.Nodes.Count > 0)
                {
                    currentNode = currentNode.Nodes[0];
                }
                else if (currentNode.NextSibling != null)
                {
                    currentNode = currentNode.NextSibling;
                }
                else
                {
                    // Trace back to the next unread sibling
                    currentNode = currentNode.Parent;
                    while (currentNode != null)
                    {
                        if (currentNode.NextSibling == null)
                        {
                            currentNode = currentNode.Parent;
                        }
                        else
                        {
                            currentNode = currentNode.NextSibling;
                            break;
                        }
                    }
                }
            }

            for (int i = 0; i < _trailingComments.Count; i++)
            {
                lines.Add(_trailingComments[i]);
            }

            return(lines);
        }
        /// <summary>
        /// Writes this document to the specified TextWriter.
        /// </summary>
        /// <param name="file">The file.</param>
        private void WriteToFile(TextWriter file)
        {
            SettingsNode currentNode = this[0];

            while (currentNode != null)
            {
                // Write node to file
                List <string> fileComments = currentNode.GetPrecedingComments();

                for (int i = 0; i < fileComments.Count; i++)
                {
                    file.WriteLine(fileComments[i]);
                }

                file.WriteLine(NodeToFileLine(currentNode));
                file.Flush();

                // Find next line
                if (currentNode.Nodes.Count > 0)
                {
                    currentNode = currentNode.Nodes[0];
                }
                else if (currentNode.NextSibling != null)
                {
                    currentNode = currentNode.NextSibling;
                }
                else
                {
                    // Trace back to the next unread sibling
                    currentNode = currentNode.Parent;
                    while (currentNode != null)
                    {
                        if (currentNode.NextSibling == null)
                        {
                            currentNode = currentNode.Parent;
                        }
                        else
                        {
                            currentNode = currentNode.NextSibling;
                            break;
                        }
                    }
                }
            }

            for (int i = 0; i < _trailingComments.Count; i++)
            {
                file.WriteLine(_trailingComments[i]);
            }

            file.Flush();
        }
        /// <summary>
        /// Creates an object of the generic type from the specified <see cref="SettingsNode"/>.
        /// </summary>
        /// <typeparam name="T">The type of object to create.</typeparam>
        /// <param name="node">The settings node.</param>
        /// <returns>The new object.</returns>
        public static T CreateObjectFromNode <T>(SettingsNode node) where T : new()
        {
            if (node == null)
            {
                return(default(T));
            }

            T newObject = new T();

            UpdateObjectFromNode(newObject, node);

            return(newObject);
        }
Exemple #30
0
        //////////////////////////////////////////////////////////////////////////
        public virtual void LoadControlLayout(SettingsNode RootNode)
        {
            SettingsNode Node = RootNode.GetNode(this.Name, false, true);

            if (Node != null)
            {
                for (int i = 0; i < Columns.Count; i++)
                {
                    TreeColumn Col = Columns[i];
                    Col.Width = Node.GetInt("Col" + i.ToString() + "Width", Col.Width);
                }
            }
        }