UserInterface.cs

Go to the documentation of this file.
00001 using System;
00002 using System.Collections.Generic;
00003 using System.Text;
00004 
00005 namespace HouseOver
00006 {
00010     public class UserInterface
00011     {
00013         private int width = 80;
00015         private int height = 25;
00017         private int contentHeight;
00019                 private string header;
00021                 private string footer;
00022                 
00026                 public UserInterface()
00027                 {
00028             string title = "The House And Realty Manager";
00029             string version = "v. 0.01a";
00030             string headSeparator = "¨";
00031             string enterChoiceString = "Enter your choice: ";
00032             string footSeparator = "_";
00033 
00034             // Generate the header contents
00035             int topSpace = width - (title.Length + version.Length);
00036             header = title;
00037             for (int i = 0; i < topSpace; i++)
00038                 header += " ";
00039             header += version;
00040             for (int i = 0; i < width; i++)
00041                 header += headSeparator;
00042             header += System.Environment.NewLine;
00043 
00044             // Generate the footer contents
00045             for (int i = 0; i < width; i++)
00046                 footer += footSeparator;
00047             footer += enterChoiceString;
00048 
00049             // Content height is set to the height of the console, minus the height of the header and footer
00050             contentHeight = height - 4;
00051         }
00052 
00058         private void DoDraw(string content, params object[] arg)
00059         {
00060             // It fails to count the last newline
00061             int contentSize = content.Split(System.Environment.NewLine.ToCharArray(0, 1)).Length;
00062             int separateThisMuchTop = Convert.ToInt32(Math.Floor(Convert.ToDouble(this.contentHeight - contentSize) / 2));
00063             int separateThisMuchBottom = this.contentHeight - (contentSize + separateThisMuchTop);
00064 
00065             // We shouldn't be drawing anything taller than the screen... but oh well
00066             if (separateThisMuchTop < 0 || separateThisMuchBottom < 0)
00067             {
00068                 separateThisMuchTop = 0;
00069                 separateThisMuchBottom = 0;
00070             }
00071 
00072             Console.Clear();
00073             Console.Write(header);
00074 
00075             for (int i = 0; i < separateThisMuchTop; i++)
00076                 Console.WriteLine("");
00077             Console.Write(content,arg);
00078 
00079             for (int i = 0; i < separateThisMuchBottom; i++)
00080                 Console.WriteLine("");
00081             Console.Write(footer);
00082         }
00083 
00084 
00091         private void DoDraw(string content, string errorMessage, params object[] arg)
00092         {
00093             int contentSize = content.Split(System.Environment.NewLine.ToCharArray(0, 1)).Length + errorMessage.Split(System.Environment.NewLine.ToCharArray(0, 1)).Length + 1;
00094             int separateThisMuchTop = Convert.ToInt32(Math.Floor(Convert.ToDouble(contentHeight - contentSize) / 2));
00095             int separateThisMuchBottom = this.contentHeight - (contentSize + separateThisMuchTop);
00096 
00097             // We shouldn't be drawing anything taller than the screen... but oh well
00098             if (separateThisMuchTop < 0 || separateThisMuchBottom < 0)
00099             {
00100                 separateThisMuchTop = 0;
00101                 separateThisMuchBottom = 0;
00102             }
00103 
00104             Console.Clear();
00105 
00106             Console.Write(header);
00107             for (int i = 0; i < separateThisMuchTop; i++)
00108                 Console.WriteLine("");
00109 
00110             Console.BackgroundColor = ConsoleColor.DarkRed;
00111             Console.ForegroundColor = ConsoleColor.White;
00112             Console.WriteLine(errorMessage);
00113             Console.ResetColor();
00114 
00115             Console.Write(System.Environment.NewLine + content, arg);
00116 
00117             for (int i = 0; i < separateThisMuchBottom; i++)
00118                 Console.WriteLine("");
00119             Console.Write(footer);
00120         }
00121 
00128         public string Draw(string drawThis, params object[] arg)
00129                 {
00130             DoDraw(drawThis, arg);
00131             return Console.ReadLine();
00132                 }
00133 
00140         public int DrawInt(string drawThis, params object[] arg)
00141         {
00142             int theValue;
00143             string tempValue;
00144 
00145             DoDraw(drawThis, arg);
00146 
00147             while (true)
00148             {
00149                 tempValue = Console.ReadLine();
00150                 try
00151                 {
00152                     theValue = Convert.ToInt32(tempValue);
00153                     break;
00154                 }
00155                 catch
00156                 {
00157                     // If we've caught the exception, the user didn't enter an integer
00158                     DoDraw(drawThis, "You did not enter a number - please try again!", arg);
00159                 }
00160             }
00161 
00162             return theValue;
00163         }
00164 
00173         public bool DrawMenu<T>(string drawThis, Dictionary<object,string> menuItems, bool showCancel, out T chosen)
00174         {
00175             string content;
00176             string theKey;
00177             ConsoleKeyInfo ki;
00178             int i = 0, it = 0;
00179             List<object> keys = new List<object>();
00180 
00181             // Generate the screen data
00182             content = drawThis + System.Environment.NewLine + System.Environment.NewLine + System.Environment.NewLine;
00183 
00184             foreach (KeyValuePair<object, string> menuItem in menuItems)
00185             {
00186                 i++;
00187                 keys.Add(menuItem.Key);
00188                 content += "   " + i.ToString() + ". " + menuItem.Value + System.Environment.NewLine;
00189             }
00190             if (showCancel)
00191                 content += "     - - - - - - - - - - - - -" + System.Environment.NewLine + "   0. Abort/ quit";
00192 
00193             // Get the selected item
00194             while (true)
00195             {
00196                 // Do the actual drawing
00197                 DoDraw(content);
00198 
00199                 // Less than ten items... let the user simply press the key
00200                 if (i < 10)
00201                 {
00202                     ki = Console.ReadKey(true);
00203                     if (showCancel && ki.KeyChar.ToString() == "0")
00204                     {
00205                         chosen = default(T);
00206                         return false;
00207                     }
00208                     try
00209                     {
00210                         it = Convert.ToInt32(ki.KeyChar.ToString());
00211                         if (0 < it && it <= i)
00212                         {
00213                             // This is necessary to make sure we get the right object from the keys list..
00214                             it--;
00215                             // Set the chosen item
00216                             chosen = (T)keys[it];
00217                             // An item was chosen, so don't loop any longer - break out, run ree!
00218                             break;
00219                         }
00220                     }
00221                     catch (System.FormatException)
00222                     {
00223                         // Sod all, we just need to NOT CRASH!
00224                     }
00225                 }
00226                 else
00227                 {
00228                     theKey = Console.ReadLine().Trim();
00229                     if (showCancel && theKey == "0")
00230                     {
00231                         chosen = default(T);
00232                         return false;
00233                     }
00234                     try
00235                     {
00236                         it = Convert.ToInt32(theKey);
00237                         if (0 < it && it <= i)
00238                         {
00239                             // This is necessary to make sure we get the right object from the keys list..
00240                             it--;
00241                             // Set the chosen item
00242                             chosen = (T)keys[it];
00243                             // An item was chosen, so don't loop any longer - break out, run ree!
00244                             break;
00245                         }
00246                     }
00247                     catch (System.FormatException)
00248                     {
00249                         // Sod all, we just need to NOT CRASH!
00250                     }
00251                 }
00252             }
00253             return true;
00254         }
00255     }
00256 }

Generated on Wed Jan 10 03:42:42 2007 for THARM by  doxygen 1.5.1-p1