• Advertisement
Sign in to follow this  

C# Is this good encapsulation

This topic is 3233 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I know there is probably a good bit of code to go over but i am just wondering if this a good encapsulation. I recently download NDepend profiler software and my LCOM is a bit over the limit and i was wondering if there is a reason for that or if it is as it sead a hard thing to avoid. Well here`s a few details on the class i will post. Language C# Libs: NeoForce Gui, and standard .Net Namespaces, XNA Game Studio 3.0 class was meant as a temp Dialog box for creating a new map in the map editor i am working on.

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Solar_Dawn_Map_Editor
{
    public class NEWMAPDATA : System.EventArgs
    {
        /// <summary>
        /// This Class is a structer desgined to return data from the new map dialog
        /// back to the class acessing it Via an Event handler.
        /// 
        /// it returns all pertent Fields of Data Eg, Map Name, Map Size, Map Chipets Etc..
        /// </summary>

        private string Name;
        private Vector2 Size;
        private string BackgroundFilePath;
        private string PlanetsFilePath;
        private string OverlayFilePath;
       
        public string GetName() { return Name; }
        public Vector2 GetSize() { return Size; }
        public string GetBgFilPath() { return BackgroundFilePath; }
        public string GetPlanetsFilPath() { return PlanetsFilePath; }
        public string GetOverlayFilePath() { return OverlayFilePath; }

        public void SetName(string Name) { this.Name = Name; }
        public void SetSizeX(int Size) { this.Size.X = Size; }
        public void SetSizeY(int Size) { this.Size.Y = Size; }
        public void SetBgFilePath(string FPath) { this.BackgroundFilePath = FPath; }
        public void SetPlanetsFilePath(string FPath) { this.PlanetsFilePath = FPath; }
        public void SetOverlayFilePath(string FPath) { this.OverlayFilePath = FPath; }

    }

    class NEWMAPDIALOG
    {

        #region Data

        //Our Copy of the GuiManager
        private TomShane.Neoforce.Controls.Manager GuiManager;

        //Our Main window
        private TomShane.Neoforce.Controls.Window MainWindow;

        //Storage and Disaply of the Map Name
        private TomShane.Neoforce.Controls.Label MapNameLabel;
        private TomShane.Neoforce.Controls.TextBox MapNameTextBox;

        //Storage and Display of the Map Width;
        private TomShane.Neoforce.Controls.Label GirdWidthLabel;
        private TomShane.Neoforce.Controls.TextBox GirdWidthTextBox;

        //Storage and Display of the Map Height
        private TomShane.Neoforce.Controls.Label GridHeightLabel;
        private TomShane.Neoforce.Controls.TextBox GridHeightTextBox;

        //Storage for Background Chipset Path and Display
        private TomShane.Neoforce.Controls.Label BgChipsetPathLabel;
        private TomShane.Neoforce.Controls.TextBox BgChipsetPathTextBox;
        private TomShane.Neoforce.Controls.Button BgChipsetPathBrowseButton;

        //Storage for Planets Chipset Path and Display
        private TomShane.Neoforce.Controls.Label PlanetsChipsetPathLabel;
        private TomShane.Neoforce.Controls.TextBox PlanetsChipsetPathTextBox;
        private TomShane.Neoforce.Controls.Button PlanetsChipsetPathBrowseButton;

        //Storage for Overlay Chipset Path and Display 
        private TomShane.Neoforce.Controls.Label OverlayChipsetPathLabel;
        private TomShane.Neoforce.Controls.TextBox OverlayChipsetPathTextBox;
        private TomShane.Neoforce.Controls.Button OverlayChipsetPathBrowseButton;

        //The ok and Cancel Buttons
        private TomShane.Neoforce.Controls.Button OkButton;
        private TomShane.Neoforce.Controls.Button CancelButton;

        private EventHandler OnReturn;

        #endregion

        #region Inilization

        public NEWMAPDIALOG(TomShane.Neoforce.Controls.Manager GuiManager,EventHandler OnReturn)
        {
            //Get a copy of the Gui Manager;
            this.GuiManager = GuiManager;

            //Initalize controls
            SetupMainWindow();
            SetupNameGui();
            SetupSizeGui();
            SetupBackGroundGui();
            SetupPlanetsGui();
            SetupOverlayGui();
            SetupOkAndCancelGui();

            //Add our window the the gui manager
            this.GuiManager.Add(MainWindow);

            //Now that everything else went well lets add on Event Handeler
            this.OnReturn = OnReturn;
        }

        protected void SetupMainWindow()
        {
            this.MainWindow = new TomShane.Neoforce.Controls.Window(GuiManager);
            this.MainWindow.Init();
            this.MainWindow.Text = "New Map";
            this.MainWindow.Width = 300;
            this.MainWindow.Height = 280;
        }
        protected void SetupNameGui()
        {
            this.MapNameLabel = new TomShane.Neoforce.Controls.Label(GuiManager);
            this.MapNameLabel.Init();
            this.MapNameLabel.Text = "Map Name:";
            this.MapNameLabel.Left = 20;
            this.MapNameLabel.Top = 20;
            this.MainWindow.Add(this.MapNameLabel);

            this.MapNameTextBox = new TomShane.Neoforce.Controls.TextBox(GuiManager);
            this.MapNameTextBox.Init();
            this.MapNameTextBox.TextColor = Color.White;
            this.MapNameTextBox.Left = 100;
            this.MapNameTextBox.Top = 20;
            this.MapNameTextBox.Width = 165;
            this.MapNameTextBox.ToolTip.Text = "The Map name is also the File name";
            this.MainWindow.Add(this.MapNameTextBox);
        }
        protected void SetupSizeGui()
        {
            this.GirdWidthLabel = new TomShane.Neoforce.Controls.Label(this.GuiManager);
            this.GirdWidthLabel.Init();
            this.GirdWidthLabel.Text = "Map Width:";
            this.GirdWidthLabel.Left = 20;
            this.GirdWidthLabel.Top = 55;
            this.MainWindow.Add(this.GirdWidthLabel);

            this.GirdWidthTextBox = new TomShane.Neoforce.Controls.TextBox(this.GuiManager);
            this.GirdWidthTextBox.Init();
            this.GirdWidthTextBox.TextColor = Color.White;
            this.GirdWidthTextBox.Left = 100;
            this.GirdWidthTextBox.Top = 55;
            this.GirdWidthTextBox.Width = 40;
            this.GirdWidthTextBox.ToolTip.Text = "For the Maps Width in Tiles!";
            this.MainWindow.Add(this.GirdWidthTextBox);

            this.GridHeightLabel = new TomShane.Neoforce.Controls.Label(this.GuiManager);
            this.GridHeightLabel.Init();
            this.GridHeightLabel.Text = "Map Height:";
            this.GridHeightLabel.Left = 150;
            this.GridHeightLabel.Top = 55;
            this.GridHeightLabel.Width = 75;
            this.MainWindow.Add(this.GridHeightLabel);

            this.GridHeightTextBox = new TomShane.Neoforce.Controls.TextBox(GuiManager);
            this.GridHeightTextBox.Init();
            this.GridHeightTextBox.TextColor = Color.White;
            this.GridHeightTextBox.Left = 225;
            this.GridHeightTextBox.Top = 55;
            this.GridHeightTextBox.Width = 40;
            this.GridHeightTextBox.ToolTip.Text = "For the Maps Height in Tiles!";
            this.MainWindow.Add(this.GridHeightTextBox);
        }
        protected void SetupBackGroundGui()
        {
            this.BgChipsetPathLabel = new TomShane.Neoforce.Controls.Label(GuiManager);
            this.BgChipsetPathLabel.Init();
            this.BgChipsetPathLabel.Text = "Background:";
            this.BgChipsetPathLabel.Left = 20;
            this.BgChipsetPathLabel.Top = 90;
            this.BgChipsetPathLabel.Width = 80;
            this.MainWindow.Add(this.BgChipsetPathLabel);

            this.BgChipsetPathTextBox = new TomShane.Neoforce.Controls.TextBox(GuiManager);
            this.BgChipsetPathTextBox.Init();
            this.BgChipsetPathTextBox.Top = 90;
            this.BgChipsetPathTextBox.Left = 100;
            this.BgChipsetPathTextBox.TextColor = Color.White;
            this.BgChipsetPathTextBox.Width = 120;
            this.BgChipsetPathTextBox.ReadOnly = true;
            this.BgChipsetPathTextBox.ToolTip.Text = "The file path for the background chipset";
            this.MainWindow.Add(this.BgChipsetPathTextBox);

            this.BgChipsetPathBrowseButton = new TomShane.Neoforce.Controls.Button(GuiManager);
            this.BgChipsetPathBrowseButton.Init();
            this.BgChipsetPathBrowseButton.Text = "...";
            this.BgChipsetPathBrowseButton.Top = 90;
            this.BgChipsetPathBrowseButton.Left = 225;
            this.BgChipsetPathBrowseButton.Height = 20;
            this.BgChipsetPathBrowseButton.Width = 40;
            this.BgChipsetPathBrowseButton.Click += OnBrowseBackground;
            this.MainWindow.Add(this.BgChipsetPathBrowseButton);
        }
        protected void SetupPlanetsGui()
        {
            this.PlanetsChipsetPathLabel = new TomShane.Neoforce.Controls.Label(GuiManager);
            this.PlanetsChipsetPathLabel.Init();
            this.PlanetsChipsetPathLabel.Text = "Planets: ";
            this.PlanetsChipsetPathLabel.Left = 20;
            this.PlanetsChipsetPathLabel.Top = 125;
            this.MainWindow.Add(this.PlanetsChipsetPathLabel);

            this.PlanetsChipsetPathTextBox = new TomShane.Neoforce.Controls.TextBox(GuiManager);
            this.PlanetsChipsetPathTextBox.Init();
            this.PlanetsChipsetPathTextBox.Top = 125;
            this.PlanetsChipsetPathTextBox.Left = 100;
            this.PlanetsChipsetPathTextBox.TextColor = Color.White;
            this.PlanetsChipsetPathTextBox.Width = 120;
            this.PlanetsChipsetPathTextBox.ReadOnly = true;
            this.PlanetsChipsetPathTextBox.ToolTip.Text = "The file path for the Planets Chipset";
            this.MainWindow.Add(this.PlanetsChipsetPathTextBox);

            this.PlanetsChipsetPathBrowseButton = new TomShane.Neoforce.Controls.Button(GuiManager);
            this.PlanetsChipsetPathBrowseButton.Init();
            this.PlanetsChipsetPathBrowseButton.Text = "...";
            this.PlanetsChipsetPathBrowseButton.Top = 125;
            this.PlanetsChipsetPathBrowseButton.Left = 225;
            this.PlanetsChipsetPathBrowseButton.Height = 20;
            this.PlanetsChipsetPathBrowseButton.Width = 40;
            this.PlanetsChipsetPathBrowseButton.Click += OnBrowsePlanets;
            this.MainWindow.Add(this.PlanetsChipsetPathBrowseButton);
        }
        protected void SetupOverlayGui()
        {
            this.OverlayChipsetPathLabel = new TomShane.Neoforce.Controls.Label(GuiManager);
            this.OverlayChipsetPathLabel.Init();
            this.OverlayChipsetPathLabel.Text = "Overlay:";
            this.OverlayChipsetPathLabel.Left = 20;
            this.OverlayChipsetPathLabel.Top = 160;
            this.MainWindow.Add(this.OverlayChipsetPathLabel);

            this.OverlayChipsetPathTextBox = new TomShane.Neoforce.Controls.TextBox(GuiManager);
            this.OverlayChipsetPathTextBox.Init();
            this.OverlayChipsetPathTextBox.Top = 160;
            this.OverlayChipsetPathTextBox.Left = 100;
            this.OverlayChipsetPathTextBox.TextColor = Color.White;
            this.OverlayChipsetPathTextBox.Width = 120;
            this.OverlayChipsetPathTextBox.ReadOnly = true;
            this.OverlayChipsetPathTextBox.ToolTip.Text = "The file path for the Overlay chipset";
            this.MainWindow.Add(this.OverlayChipsetPathTextBox);

            this.OverlayChipsetPathBrowseButton = new TomShane.Neoforce.Controls.Button(GuiManager);
            this.OverlayChipsetPathBrowseButton.Init();
            this.OverlayChipsetPathBrowseButton.Text = "...";
            this.OverlayChipsetPathBrowseButton.Top = 160;
            this.OverlayChipsetPathBrowseButton.Left = 225;
            this.OverlayChipsetPathBrowseButton.Height = 20;
            this.OverlayChipsetPathBrowseButton.Width = 40;
            this.OverlayChipsetPathBrowseButton.Click += OnBrowseOverlay;
            this.MainWindow.Add(this.OverlayChipsetPathBrowseButton);
        }
        protected void SetupOkAndCancelGui()
        {
            this.OkButton = new TomShane.Neoforce.Controls.Button(this.GuiManager);
            this.OkButton.Init();
            this.OkButton.Text = "Create";
            this.OkButton.Top = 200;
            this.OkButton.Left = 55;
            this.OkButton.Click += this.OnCreate;

            this.MainWindow.Add(this.OkButton);

            this.CancelButton = new TomShane.Neoforce.Controls.Button(this.GuiManager);
            this.CancelButton.Init();
            this.CancelButton.Text = "Cancel";
            this.CancelButton.Top = 200;
            this.CancelButton.Left = 160;
            this.CancelButton.Click += OnCancel;

            this.MainWindow.Add(this.CancelButton);
        }
        #endregion Inilization

        #region Gui Events

        protected void OnBrowseBackground(Object Sender, EventArgs E)
        {
            System.Windows.Forms.OpenFileDialog OFD = new System.Windows.Forms.OpenFileDialog();
            OFD.Title = "Select a Chipset";
            OFD.Filter = "Png (*.png)|*.png|Jpeg (*.jpg)|*.jpg";

            if (OFD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.BgChipsetPathTextBox.Text = OFD.FileName;
            }
        }

        protected void OnBrowsePlanets(Object Sender, EventArgs E)
        {
            System.Windows.Forms.OpenFileDialog OFD = new System.Windows.Forms.OpenFileDialog();
            OFD.Title = "Select a Chipset";
            OFD.Filter = "Png (*.png)|*.png|Jpeg (*.jpg)|*.jpg";

            if (OFD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.PlanetsChipsetPathTextBox.Text = OFD.FileName;
            }
        }

        protected void OnBrowseOverlay(Object Sender, EventArgs E)
        {
            System.Windows.Forms.OpenFileDialog OFD = new System.Windows.Forms.OpenFileDialog();
            OFD.Title = "Select a Chipset";
            OFD.Filter = "Png (*.png)|*.png|Jpeg (*.jpg)|*.jpg";

            if (OFD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.OverlayChipsetPathTextBox.Text = OFD.FileName;
            }
        }

        protected void OnCancel(Object Sender, EventArgs E)
        {
            this.MainWindow.Close();
        }

        protected void OnCreate(Object Sender, EventArgs E)
        {
            //First we check to be sure all data in the dialog is accepctable
            NEWMAPDATA Temp = new NEWMAPDATA();

            //Do we have a map name
            if (this.MapNameTextBox.Text == string.Empty)
            {
                System.Windows.Forms.MessageBox.Show("You have not Entered a map name!");
                return;
            }
            else
            {
                Temp.SetName(this.MapNameTextBox.Text);
            }

            //Are bolth our map sizes actualy dementions and are then more then Zero
            int MapWidth = 0;
            if (!int.TryParse(this.GirdWidthTextBox.Text, out MapWidth))
            {
                System.Windows.Forms.MessageBox.Show("The mapes Width dimension is incorrect!");
                return;
            }
            else
            {
                Temp.SetSizeX(MapWidth);
            }
                        
            int MapHeight = 0;
            if (!int.TryParse(this.GridHeightTextBox.Text, out MapHeight))
            {
                System.Windows.Forms.MessageBox.Show("The maps Height dimension is incorrect!");
                return;
            }
            else
            {
                Temp.SetSizeY(MapHeight);
            }

            //Make sure we have our file paths for the Chipsets

            if (this.BgChipsetPathTextBox.Text == string.Empty)
            {
                System.Windows.Forms.MessageBox.Show("The Background Chipset Fileds has not been filled");
                return;
            }
            else
            {
                Temp.SetBgFilePath(this.BgChipsetPathTextBox.Text);
                
            }

            if (this.PlanetsChipsetPathTextBox.Text == string.Empty)
            {
                System.Windows.Forms.MessageBox.Show("The Planet Chipdset Fields has not been filled");
                return;
            }
            else
            {
                Temp.SetPlanetsFilePath(this.PlanetsChipsetPathTextBox.Text);
            }

            if (this.OverlayChipsetPathTextBox.Text == string.Empty)
            {
                System.Windows.Forms.MessageBox.Show("The Overlay Chipset Field has not been Filled");
                return;
            }
            else
            {
                Temp.SetOverlayFilePath(this.OverlayChipsetPathTextBox.Text);
            }
            OnReturn(this, (EventArgs) Temp);
            this.MainWindow.Close();
        }

        #endregion Gui Events
    }
}


Yet again i know it is a fair bit of code. But if some one has the time to give it a quick look over i would appreciate it. Feed back of any kind is welcome. Regards Jouei.

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement