Subversion Repositories SoapBoxCore

Rev

Rev 12 | Rev 18 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
8 scott 1
#region "SoapBox.Core License"
2
/// <header module="SoapBox.Core"> 
3
/// Copyright (C) 2009 SoapBox Automation Inc., All Rights Reserved.
4
/// Contact: SoapBox Automation Licencing (license@soapboxautomation.com)
5
/// 
6
/// This file is part of SoapBox Core.
7
/// 
8
/// Commercial Usage
9
/// Licensees holding valid SoapBox Automation Commercial licenses may use  
10
/// this file in accordance with the SoapBox Automation Commercial License
11
/// Agreement provided with the Software or, alternatively, in accordance 
12
/// with the terms contained in a written agreement between you and
13
/// SoapBox Automation Inc.
14
/// 
15
/// GNU Lesser General Public License Usage
16
/// SoapBox Core is free software: you can redistribute it and/or modify 
17
/// it under the terms of the GNU Lesser General Public License
18
/// as published by the Free Software Foundation, either version 3 of the
19
/// License, or (at your option) any later version.
20
/// 
21
/// SoapBox Core is distributed in the hope that it will be useful, 
22
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
23
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
/// GNU Lesser General Public License for more details.
25
/// 
26
/// You should have received a copy of the GNU Lesser General Public License 
27
/// along with SoapBox Core. If not, see <http://www.gnu.org/licenses/>.
28
/// </header>
29
#endregion
30
 
31
using System;
32
using System.Collections.Generic;
33
using System.Linq;
34
using System.Text;
35
using System.Windows;
36
using System.ComponentModel.Composition;
37
 
38
namespace SoapBox.Core.Options
39
{
40
    /// <summary>
41
    /// ViewModel for the Options Dialog
42
    /// Inheriting from AbstractOptionsItem is just a hack
43
    /// because it makes it easy to set the Items property.
44
    /// </summary>
12 scott 45
    [Export(CompositionPoints.Options.OptionsDialog, typeof(OptionsDialog))]
46
    class OptionsDialog : AbstractOptionsItem, IPartImportsSatisfiedNotification
8 scott 47
    {
12 scott 48
        public OptionsDialog()
8 scott 49
        {
50
            OptionChanged += new EventHandler(OptionsDialog_OptionChanged);
51
        }
52
 
53
        void OptionsDialog_OptionChanged(object sender, EventArgs e)
54
        {
55
            m_dirtyCondition.SetCondition(true);
56
        }
57
 
12 scott 58
        [Import(Services.Host.ExtensionService)]
59
        private IExtensionService extensionService { get; set; }
60
 
61
        [ImportMany(ExtensionPoints.Options.OptionsDialog.OptionsItems, typeof(IOptionsItem), AllowRecomposition=true)]
62
        private IEnumerable<IOptionsItem> items { get; set; }
63
 
64
        public void OnImportsSatisfied()
65
        {
66
            Items = extensionService.Sort(items);
67
        }
68
 
69
        [Import(CompositionPoints.Host.MainWindow)]
8 scott 70
        private Lazy<Window> mainWindowExport { get; set; }
71
 
72
        /// <summary>
73
        /// Displays the Options Dialog as modal
74
        /// </summary>
17 scott 75
        public void ShowDialog(Window optionsDialogView)
8 scott 76
        {
77
            Window mainWindow = mainWindowExport.Value;
17 scott 78
            optionsDialogView.Owner = mainWindow;
79
            optionsDialogView.DataContext = this;
8 scott 80
            logger.Info("Showing options dialog...");
81
            m_dirtyCondition.SetCondition(false);
17 scott 82
            optionsDialogView.ShowDialog();
8 scott 83
            ReloadSavedValues();
84
            logger.Info("Options dialog closed.");
85
        }
86
 
87
        private ICondition dirtyCondition
88
        {
89
            get
90
            {
91
                return m_dirtyCondition;
92
            }
93
        }
94
        private ConcreteCondition m_dirtyCondition = new ConcreteCondition(false);
95
 
96
        #region " OK Button "
97
        public IControl OKButton
98
        {
99
            get
100
            {
101
                if (m_OKButton == null)
102
                {
103
                    m_OKButton = new CommitChangesButton(this);
104
                }
105
                return m_OKButton;
106
            }
107
        }
108
        private IControl m_OKButton = null;
109
 
110
        private class CommitChangesButton : AbstractButton
111
        {
112
            public CommitChangesButton(OptionsDialog dlg)
113
            {
114
                m_OptionsDialog = dlg;
115
                EnableCondition = dlg.dirtyCondition;
116
            }
117
 
118
            private OptionsDialog m_OptionsDialog = null;
119
 
120
            protected override void Run()
121
            {
122
                foreach (IOptionsItem item in m_OptionsDialog.Items)
123
                {
124
                    item.CommitChanges();
125
                }
126
            }
127
        }
128
        #endregion
129
 
130
        #region " Cancel Button "
131
        public IControl CancelButton
132
        {
133
            get
134
            {
135
                if (m_CancelButton == null)
136
                {
137
                    m_CancelButton = new CancelChangesButton(this);
138
                }
139
                return m_CancelButton;
140
            }
141
        }
142
        private IControl m_CancelButton = null;
143
 
144
        private class CancelChangesButton : AbstractButton
145
        {
146
            public CancelChangesButton(OptionsDialog dlg)
147
            {
148
                m_OptionsDialog = dlg;
149
            }
150
 
151
            private OptionsDialog m_OptionsDialog = null;
152
 
153
            protected override void Run()
154
            {
155
                foreach (IOptionsItem item in m_OptionsDialog.Items)
156
                {
157
                    item.CancelChanges();
158
                }
159
            }
160
        }
161
        #endregion
162
 
163
        private void ReloadSavedValues()
164
        {
165
            foreach (IOptionsItem item in Items)
166
            {
167
                item.CancelChanges();
168
            }
169
        }
170
 
171
    }
172
}