public static void Parse(string input, out Cells.Cell[,] CellResult, out Agent[] AgentResult) { CellResult = null; AgentResult = null; int height = 0; int width = 0; int itr = 0; int startItr = 0; int lineItr = 0; for (; itr < input.Length; ++itr) { switch (input[itr]) { case ' ': goto end_height; case ':': throw new FormatException("Board Width is missing."); } } end_height: if (itr == 0) { throw new FormatException("Invalid Text."); } height = int.Parse(input.Substring(0, itr)); itr++; startItr = itr; for (; itr < input.Length; ++itr) { switch (input[itr]) { case ' ': throw new FormatException("Board Size Information is invalid."); case ':': goto end_width; } } end_width: if (itr == startItr) { throw new FormatException("Invalid Text."); } width = int.Parse(input.Substring(startItr, itr - startItr)); itr++; startItr = itr; CellResult = new Cells.Cell[width, height]; for (; itr < input.Length && lineItr < height; ++itr) { if (input[itr] == ':') { lineItr++; if (lineItr == height) { goto end_lines; } } } end_lines: if (lineItr != height) { throw new FormatException("Text is too short."); } var cellData = input.Substring(startItr, itr - startItr).Split(':').Select((x, y) => x.Split().Select(xx => int.Parse(xx)).ToArray()).ToArray(); for (int y = 0; y < cellData.Length; ++y) { var currentLine = cellData[y]; if (currentLine.Length != width) { throw new FormatException("Width is too short in y-" + y.ToString() + "."); } for (int x = 0; x < currentLine.Length; ++x) { CellResult[x, y] = new Cells.Cell(currentLine[x]); } } itr++; startItr = itr; AgentResult = new Agent[4]; var nums = input.Substring(startItr).Split(new[] { ':', ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(x => int.Parse(x)).ToArray(); AgentResult[0] = new Agent(); AgentResult[1] = new Agent(); AgentResult[0].Point = new Point(nums[1] - 1, nums[0] - 1); AgentResult[1].Point = new Point(nums[3] - 1, nums[2] - 1); }
/// <summary> /// Load the grid using the parameters specified /// </summary> /// <param name="dateTimeStart"></param> /// <param name="dateTimeEnd"></param> /// <param name="minAppointmentLength"></param> public void LoadPlanning(DateTime dateTimeStart, DateTime dateTimeEnd, int minAppointmentLength) { m_DateTimeStart = dateTimeStart; m_DateTimeEnd = dateTimeEnd; m_MinAppointmentLength = minAppointmentLength; if (dateTimeStart >= dateTimeEnd) { throw new ApplicationException("Invalid Planning Range"); } if (dateTimeStart.TimeOfDay >= dateTimeEnd.TimeOfDay) { throw new ApplicationException("Invalid Plannnin Range"); } if (dateTimeStart.TimeOfDay.Minutes != 0 || dateTimeEnd.TimeOfDay.Minutes != 0) { throw new ApplicationException("Invalid Start or End hours must be with 0 minutes"); } if (minAppointmentLength <= 0 || minAppointmentLength > 60) { throw new ApplicationException("Invalid Minimum Appointment Length"); } if (60 % minAppointmentLength != 0) { throw new ApplicationException("Invalid Minimum Appointment Length must be multiple of 60"); } TimeSpan dayInterval = dateTimeEnd - dateTimeStart; TimeSpan timeInterval = dateTimeEnd.TimeOfDay - dateTimeStart.TimeOfDay; int partsForHour = 60 / minAppointmentLength; if (dayInterval.TotalDays > 30) { throw new ApplicationException("Range too big"); } if (timeInterval.TotalMinutes < minAppointmentLength) { throw new ApplicationException("Invalid Minimum Appointment Length for current Planning Range"); } //Redim Grid grid.Redim((int)((timeInterval.TotalHours + 1) * partsForHour + c_RowsHeader), (int)(dayInterval.TotalDays + 1 + c_ColumnsHeader)); //Load Header grid[0, 0] = new Header00(null); grid[0, 0].RowSpan = 2; grid[0, 0].ColumnSpan = 2; //create day caption DateTime captionDate = dateTimeStart; for (int c = c_ColumnsHeader; c < grid.ColumnsCount; c++) { grid[0, c] = new HeaderDay1(captionDate.ToShortDateString()); grid[1, c] = new HeaderDay2(captionDate.ToString("dddd")); captionDate = captionDate.AddDays(1); } //create hour caption int hours = dateTimeStart.Hour; for (int r = c_RowsHeader; r < grid.RowsCount; r += partsForHour) { grid[r, 0] = new HeaderHour1(hours); grid[r, 0].RowSpan = partsForHour; int minutes = 0; for (int rs = r; rs < (r + partsForHour); rs++) { grid[rs, 1] = new HeaderHour2(minutes); minutes += minAppointmentLength; } hours++; } grid.FixedColumns = c_ColumnsHeader; grid.FixedRows = c_RowsHeader; //Fix the width of the first 2 columns grid.Columns[0].Width = 40; grid.Columns[0].AutoSizeMode = SourceGrid.AutoSizeMode.None; grid.Columns[1].Width = 40; grid.Columns[1].AutoSizeMode = SourceGrid.AutoSizeMode.None; grid.AutoStretchColumnsToFitWidth = true; grid.AutoStretchRowsToFitHeight = true; grid.AutoSizeCells(); //Create Appointment Cells //Days for (int c = c_ColumnsHeader; c < grid.ColumnsCount; c++) { DateTime currentTime = dateTimeStart.AddDays(c - c_ColumnsHeader); int indexAppointment = -1; Cells.Cell appointmentCell = null; //Hours for (int r = c_RowsHeader; r < grid.RowsCount; r += partsForHour) { //Minutes for (int rs = r; rs < (r + partsForHour); rs++) { bool l_bFound = false; //Appointments for (int i = 0; i < m_Appointments.Count; i++) { if (m_Appointments[i].ContainsDateTime(currentTime)) { l_bFound = true; if (indexAppointment != i) { appointmentCell = new CellAppointment(m_Appointments[i]); appointmentCell.View = m_Appointments[i].View; if (m_Appointments[i].Controller != null) { appointmentCell.AddController(m_Appointments[i].Controller); } grid[rs, c] = appointmentCell; indexAppointment = i; } else { grid[rs, c] = null; appointmentCell.RowSpan++; } break; } } if (l_bFound) { } else { grid[rs, c] = new CellEmpty(currentTime, currentTime.AddMinutes(minAppointmentLength)); indexAppointment = -1; appointmentCell = null; } currentTime = currentTime.AddMinutes(minAppointmentLength); } } } }
public void LoadPlanning(DateTime p_DateTimeStart, DateTime p_DateTimeEnd, int p_MinimumAppointmentLength) { m_DateTimeStart = p_DateTimeStart; m_DateTimeEnd = p_DateTimeEnd; m_MinimumAppointmentLength = p_MinimumAppointmentLength; if (p_DateTimeStart >= p_DateTimeEnd) { throw new ApplicationException("Invalid Planning Range"); } if (p_DateTimeStart.TimeOfDay >= p_DateTimeEnd.TimeOfDay) { throw new ApplicationException("Invalid Plannnin Range"); } if (p_DateTimeStart.TimeOfDay.Minutes != 0 || p_DateTimeEnd.TimeOfDay.Minutes != 0) { throw new ApplicationException("Invalid Start or End hours must be with 0 minutes"); } if (p_MinimumAppointmentLength <= 0 || p_MinimumAppointmentLength > 60) { throw new ApplicationException("Invalid Minimum Appointment Length"); } if (60 % p_MinimumAppointmentLength != 0) { throw new ApplicationException("Invalid Minimum Appointment Length must be multiple of 60"); } TimeSpan l_TotalInterval = p_DateTimeEnd - p_DateTimeStart; TimeSpan l_DayInterval = p_DateTimeEnd.TimeOfDay - p_DateTimeStart.TimeOfDay; int l_PartPerHour = 60 / p_MinimumAppointmentLength; if (l_TotalInterval.TotalDays > 30) { throw new ApplicationException("Range too big"); } if (l_DayInterval.TotalMinutes < p_MinimumAppointmentLength) { throw new ApplicationException("Invalid Minimum Appointment Length for current Planning Range"); } //Redim Grid grid.Redim((int)((l_DayInterval.TotalHours + 1) * l_PartPerHour + c_RowsHeader), (int)(l_TotalInterval.TotalDays + 1 + c_ColumnsHeader)); //Load Header grid[0, 0] = new Header00(null); grid[0, 0].RowSpan = 2; grid[0, 0].ColumnSpan = 2; //create day caption DateTime l_Start = p_DateTimeStart; for (int c = c_ColumnsHeader; c < grid.ColumnsCount; c++) { grid[0, c] = new HeaderDay1(l_Start.ToShortDateString()); grid[1, c] = new HeaderDay2(l_Start.ToString("dddd")); l_Start = l_Start.AddDays(1); } //create hour caption int l_Hours = p_DateTimeStart.Hour; for (int r = c_RowsHeader; r < grid.RowsCount; r += l_PartPerHour) { grid[r, 0] = new HeaderHour1(l_Hours); grid[r, 0].RowSpan = l_PartPerHour; int l_Minutes = 0; for (int rs = r; rs < (r + l_PartPerHour); rs++) { grid[rs, 1] = new HeaderHour2(l_Minutes); l_Minutes += p_MinimumAppointmentLength; } l_Hours++; } grid.FixedColumns = c_ColumnsHeader; grid.FixedRows = c_RowsHeader; grid.AutoStretchColumnsToFitWidth = true; grid.AutoStretchRowsToFitHeight = true; grid.AutoSizeCells(); //Create Appointment Cells //Days for (int c = c_ColumnsHeader; c < grid.ColumnsCount; c++) { DateTime l_CurrentTime = p_DateTimeStart.AddDays(c - c_ColumnsHeader); int l_IndexAppointment = -1; Cells.Cell l_AppointmentCell = null; //Hours for (int r = c_RowsHeader; r < grid.RowsCount; r += l_PartPerHour) { //Minutes for (int rs = r; rs < (r + l_PartPerHour); rs++) { bool l_bFound = false; //Appointments for (int i = 0; i < m_Appointments.Count; i++) { if (m_Appointments[i].ContainsDateTime(l_CurrentTime)) { l_bFound = true; if (l_IndexAppointment != i) { l_AppointmentCell = new CellAppointment(m_Appointments[i].Title); l_AppointmentCell.View = m_Appointments[i].View; if (m_Appointments[i].Controller != null) { l_AppointmentCell.AddController(m_Appointments[i].Controller); } grid[rs, c] = l_AppointmentCell; l_IndexAppointment = i; } else { grid[rs, c] = null; l_AppointmentCell.RowSpan++; } break; } } if (l_bFound) { } else { grid[rs, c] = new CellEmpty(null); l_IndexAppointment = -1; l_AppointmentCell = null; } l_CurrentTime = l_CurrentTime.AddMinutes(p_MinimumAppointmentLength); } } } }