Click here to Skip to main content
15,894,896 members
Articles / Desktop Programming / WPF

MPP Viewer

Rate me:
Please Sign up or sign in to vote.
4.75/5 (13 votes)
15 Jun 2011CPOL3 min read 103.9K   4.1K   41  
MPP Viewer is a simple viewer for Microsoft Project files. I works well with 2000/2003/2007 file formats.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProjectLibrary.Mpp;

namespace ProjectViewer.ViewModel
{
    public class TasksViewModel : WorkspaceViewModel
    {
        public TasksViewModel(List<MppTask> mppTasks)
        {
            DisplayName = "Tasks";

            Tasks = new List<TaskViewModel>();
            _allTasks = new List<TaskViewModel>();

            foreach (var task in mppTasks)
            {
                _allTasks.Add(new TaskViewModel(task, this));
            }

            Tasks = _allTasks;
        }

        private List<TaskViewModel> _allTasks;

        private List<TaskViewModel> _tasks;
        public List<TaskViewModel> Tasks
        {
            get { return _tasks; }
            set
            {
                _tasks = value;
                OnPropertyChanged("Tasks");
            }
        }

        #region Public Methods
        
        public void ShowCompleted()
        {
            List<TaskViewModel> tasks = new List<TaskViewModel>();

            foreach (TaskViewModel task in _allTasks)
            {
                TaskViewModel newTask = new TaskViewModel(task.Task);
                if (filterCompletedTasks(newTask) || task.PercentageComplete == 100)
                {
                    tasks.Add(newTask);
                }
            }

            Tasks = tasks;
        }

        public void ShowCompletingTomorrow()
        {
            List<TaskViewModel> tasks = new List<TaskViewModel>();

            foreach (TaskViewModel task in _allTasks)
            {
                TaskViewModel newTask = new TaskViewModel(task.Task);
                if (filterCompletingTomorrow(newTask) || (task.FinishDate.HasValue && task.FinishDate.Value == DateTime.Today.AddDays(1)) )
                {
                    tasks.Add(newTask);
                }
            }

            Tasks = tasks;
        }

        public void ShowCompletingToday()
        {
            List<TaskViewModel> tasks = new List<TaskViewModel>();

            foreach (TaskViewModel task in _allTasks)
            {
                TaskViewModel newTask = new TaskViewModel(task.Task);
                if (filterCompletingToday(newTask) || (task.FinishDate.HasValue && task.FinishDate.Value == DateTime.Today))
                {
                    tasks.Add(newTask);
                }
            }

            Tasks = tasks;
        }

        public void ShowInCompleted()
        {
            List<TaskViewModel> tasks = new List<TaskViewModel>();

            foreach (TaskViewModel task in _allTasks)
            {
                TaskViewModel newTask = new TaskViewModel(task.Task);
                if (filterInCompletedTasks(newTask) || task.PercentageComplete < 100)
                {
                    tasks.Add(newTask);
                }
            }

            Tasks = tasks;
        }

        public void ShowAll()
        {
            Tasks = _allTasks;
        } 

        #endregion

        #region private methods

        private bool filterCompletingToday(TaskViewModel task)
        {
            bool shouldInclude = false;

            List<TaskViewModel> tasks = new List<TaskViewModel>();

            foreach (MppTask childTask in task.Task.ChildTasks)
            {
                TaskViewModel newTask = new TaskViewModel(childTask);

                if (filterCompletingToday(newTask) || (childTask.FinishDate.HasValue && childTask.FinishDate.Value == DateTime.Today))
                {
                    task.ChildTasks.Add(newTask);
                    shouldInclude = true;
                }
            }

            return shouldInclude;
        }

        private bool filterInCompletedTasks(TaskViewModel task)
        {
            bool shouldInclude = false;

            List<TaskViewModel> tasks = new List<TaskViewModel>();

            foreach (MppTask childTask in task.Task.ChildTasks)
            {
                TaskViewModel newTask = new TaskViewModel(childTask);

                if (filterInCompletedTasks(newTask) || childTask.PercentageComplete < 100 )
                {
                    task.ChildTasks.Add(newTask);
                    shouldInclude = true;
                }
            }

            return shouldInclude;
        }

        private bool filterCompletedTasks(TaskViewModel task)
        {
            bool shouldInclude = false;

            List<TaskViewModel> tasks = new List<TaskViewModel>();

            foreach (MppTask childTask in task.Task.ChildTasks)
            {
                TaskViewModel newTask = new TaskViewModel(childTask);

                if (filterCompletedTasks(newTask) || childTask.PercentageComplete == 100)
                {
                    task.ChildTasks.Add(newTask);
                    shouldInclude = true;
                }
            }

            return shouldInclude;
        }

        private bool filterCompletingTomorrow(TaskViewModel task)
        {
            bool shouldInclude = false;

            List<TaskViewModel> tasks = new List<TaskViewModel>();

            foreach (MppTask childTask in task.Task.ChildTasks)
            {
                TaskViewModel newTask = new TaskViewModel(childTask);

                if (filterCompletingTomorrow(newTask) || (childTask.FinishDate.HasValue && childTask.FinishDate.Value == DateTime.Today.AddDays(1)))
                {
                    task.ChildTasks.Add(newTask);
                    shouldInclude = true;
                }
            }

            return shouldInclude;
        }

        #endregion

    }
}

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 Code Project Open License (CPOL)


Written By
Architect
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions