ContactDetailsPresenter.cs :  » Windows-Presentation-Foundation » Caliburn » ContactManager » Presenters » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Windows Presentation Foundation » Caliburn 
Caliburn » ContactManager » Presenters » ContactDetailsPresenter.cs
namespace ContactManager.Presenters{
    using System;
    using System.ComponentModel;
    using Caliburn.ModelFramework;
    using Caliburn.PresentationFramework.ApplicationModel;
    using Caliburn.PresentationFramework.Filters;
    using Caliburn.PresentationFramework.RoutedMessaging;
    using Caliburn.PresentationFramework.Screens;
    using Caliburn.WPF.ApplicationFramework;
    using Interfaces;
    using Model;
    using Web;

    public class ContactDetailsPresenter : Screen<Contact>, IContactDetailsPresenter
    {
        private void OnPropertyChangedEvent(object s, PropertyChangedEventArgs e)
        {
            if(e.PropertyName == "IsDirty" || e.PropertyName == "IsValid")
                NotifyOfPropertyChange(() => CanSave);
        }

        protected override void OnActivate()
        {
            base.OnActivate();

            Subject.PropertyChanged += OnPropertyChangedEvent;

            Subject.BeginEdit();
            Subject.Validate();
        }

        protected override void OnDeactivate()
        {
            base.OnDeactivate();
            Subject.PropertyChanged -= OnPropertyChangedEvent;
        }

        protected override void OnShutdown()
        {
            base.OnShutdown();
            Subject.CancelEdit();
        }

        public bool CanSave
        {
            get { return Subject.IsDirty && Subject.IsValid; }
        }

        [Preview("CanSave")]
        public IResult Apply()
        {
            return SaveContact(x => Subject.BeginEdit());
        }

        [Preview("CanSave")]
        public IResult Ok()
        {
            return SaveContact(x => Close());
        }

        public void Cancel()
        {
            Close();
        }

        private IResult SaveContact(Action<AsyncCompletedEventArgs> callback)
        {
            Subject.EndEdit();
            var dto = Map.ToDto(Subject);

            if (dto.ID == Guid.Empty)
                dto.ID = Guid.NewGuid();

            return new WebServiceResult<ContactServiceClient, AsyncCompletedEventArgs>(
                x => x.UpdateContactAsync(dto),
                callback
                );
        }

        public void AddNumber()
        {
            Subject.AddPhoneNumber(new PhoneNumber());
        }

        public void RemoveNumber(PhoneNumber numberToRemove)
        {
            Subject.RemovePhoneNumber(numberToRemove);
        }

        public IResult ValidateContact()
        {
            return new ErrorResult(Subject.Validate());
        }

        public override bool CanShutdown()
        {
            return !Subject.IsDirty;
        }

        public ISubordinate CreateShutdownModel()
        {
            if (Subject.IsValid)
            {
                return new Question(
                    this,
                    string.Format(
                        "Contact '{0}' has not been saved.  Do you want to save before closing?",
                        (Subject.LastName ?? string.Empty) + ", " + (Subject.FirstName ?? string.Empty)
                        )
                    ) {Answer = Answer.Yes};
            }

            return new Question(
                this,
                string.Format(
                    "Contact '{0}' is invalid.  Changes will be lost.  Do you still want to close?",
                    (Subject.LastName ?? string.Empty) + ", " + (Subject.FirstName ?? string.Empty)
                    ),
                Answer.Yes, Answer.No
                );
        }

        public bool CanShutdown(ISubordinate shutdownModel)
        {
            var question = (Question)shutdownModel;

            if (Subject.IsValid)
            {
                if(question.Answer == Answer.Cancel)
                    return false;

                if (question.Answer == Answer.Yes)
                    Apply().Execute();

                return true;
            }

            if(question.Answer == Answer.Yes)
                return true;

            return false;
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.