Click here to Skip to main content
15,896,063 members
Articles / Programming Languages / C#

DataStorage: Store settings type-safe

Rate me:
Please Sign up or sign in to vote.
4.50/5 (3 votes)
8 Aug 2012GPL33 min read 26.2K   234   9  
Shows how to use the DataStorage classes to generate type-safe settings classes.
using System;
using System.Windows.Forms;
using System.IO;
using pdfforge.DataStorageGenerator.DataGeneration;
using Aga.Controls.Tree.NodeControls;

namespace pdfforge.DataStorageGenerator
{
    public partial class frmMainGenerator : Form
    {
        private string currentFile = null;
        private ClassContainer classes = new ClassContainer();
        private IDataElement currentElement = null;
        
        public frmMainGenerator()
        {
            InitializeComponent();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox abt = new AboutBox();
            abt.ShowDialog();
        }

        private void updateClassList()
        {
            treeViewAdv1.Model = classes;
            classes.OnStructureChanged();
            treeViewAdv1.ExpandAll();
        }


        private void loadFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialog.Filter = "XML-Files (*.xml)|*.xml|All Files (*.*)|*.*";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                loadFile(openFileDialog.FileName); 
            }
        }

        private void loadFile(string filename)
        {
            setCurrentFile(filename);
            Properties.Settings.Default.LastOpenFile = currentFile;
            classes.loadXml(currentFile);
            updateClassList();
        }

        private void SaveFile(bool showDialog)
        {
            if (currentFile == null || showDialog)
            {
                saveFileDialog.AddExtension = true;
                saveFileDialog.DefaultExt = ".xml";
                if (currentFile != null) saveFileDialog.FileName = currentFile;
                saveFileDialog.Filter = "XML-Files (*.xml)|*.xml|All Files (*.*)|*.*";
                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                    setCurrentFile(saveFileDialog.FileName);                
            }

            if (currentFile != null)
            {
                classes.saveXml(currentFile);
            }
        }

        private void saveFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFile(false);            
        }

        private void saveFileAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFile(true);
        }


        private void frmMainGenerator_Load(object sender, EventArgs e)
        {
            if (File.Exists(Properties.Settings.Default.LastOpenFile))
            {
                loadFile(Properties.Settings.Default.LastOpenFile);
            }
        }

        private void frmMainGenerator_FormClosing(object sender, FormClosingEventArgs e)
        {
            Properties.Settings.Default.LastOpenFile = currentFile;
            Properties.Settings.Default.Save();
        }

        private void setCurrentFile(string newFile)
        {
            currentFile = newFile;
            updateFormCaption();
        }

        private void updateFormCaption()
        {
            this.Text = Application.ProductName + " " + Application.ProductVersion.ToString();
            if ((currentFile != null) && (currentFile.Length > 2))
                this.Text += " - " + currentFile;
        }

        private DataClass getCurrentClass()
        {
            if (treeViewAdv1.SelectedNode == null)
                return null;

            if (currentElement is DataClass)
                return currentElement as DataClass;

            if (treeViewAdv1.SelectedNode.Parent.Tag is DataClass)
                return treeViewAdv1.SelectedNode.Parent.Tag as DataClass;

            return null;
        }


        private void tsbAddProperty_Click(object sender, EventArgs e)
        {
            try
            {
                DataClass c = getCurrentClass();
                string name = "New Value";
                int i = 0;
                while (c.propertyExists(name))
                {
                    i++;
                    name = "New Value " + i;
                }
                c.addProperty(name);
                classes.OnStructureChanged();

            }
            catch (NullReferenceException)
            {
                MessageBox.Show("Select a class first!",Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void propertyGrid_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            if (e.ChangedItem.Label.Equals("Name"))
                classes.OnStructureChanged();
             
        }

        private void allClassesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmGeneratorOptions opt = new frmGeneratorOptions();
            opt.txtNamespace.Text = classes.Namespace;
            if (opt.ShowDialog() == DialogResult.OK)
            {
                ClassGenerator cg = new ClassGenerator();
                classes.Namespace = opt.txtNamespace.Text;
                cg.generate(classes, opt.path);
            }
        }

        private void treeViewAdv1_Click(object sender, EventArgs e)
        {
            if (treeViewAdv1.SelectedNode != null)
            {
                if (treeViewAdv1.SelectedNode.Tag == currentElement)
                    return;

                if (treeViewAdv1.SelectedNode.Tag is IDataElement)
                {
                    currentElement = treeViewAdv1.SelectedNode.Tag as IDataElement;
                    propertyGrid.SelectedObject = currentElement;
                    propertyGrid.Update();
                }
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            InputBoxDialog ib = new InputBoxDialog();

            ib.FormPrompt = "Please enter the name of the class:";
            ib.FormCaption = "Create new class";

            DialogResult res = ib.ShowDialog();
            if ((res == DialogResult.OK) && (ib.InputResponse.Length > 0))
            {
                if (classes.classExists(ib.InputResponse))
                {
                    MessageBox.Show("A class with the name " + ib.InputResponse + " already exists!", "Class already exists", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                if (getCurrentClass() == null)
                {
                    classes.addClass(ib.InputResponse);
                }
                else
                {
                    DataClass cls = getCurrentClass();
                    cls.addClass(ib.InputResponse);
                }
                updateClassList();
            }
        }

        private void tasDeleteProperty_Click(object sender, EventArgs e)
        {
            
            DataClass c = getCurrentClass();
            if ((treeViewAdv1.SelectedNode == null) || (c == null))
                return;
            if (treeViewAdv1.SelectedNode.Tag == c)
            {
                if (treeViewAdv1.SelectedNode.Level == 1)
                {
                    classes.removeClass(c);
                }
                else
                {
                    DataClass cls = treeViewAdv1.SelectedNode.Parent.Tag as DataClass;
                    IDataElement elem = treeViewAdv1.SelectedNode.Tag as IDataElement;
                    cls.removeElement(elem.getName());
                }
            }
            else
            {
                IDataElement elem = treeViewAdv1.SelectedNode.Tag as IDataElement;
                c.removeElement(elem.getName());
            }
            updateClassList();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
pdfforge GbR
Germany Germany
Philip is a Software Architect with severaly years of experience. He studied business IT in the University of Wedel, Germany, and has reached his Masters degree in 2010. Back in 2002, he started his so far best-known software PDFCreator. The second developer joined two years later and since then, both constantly improve this piece of open source software.

Comments and Discussions