~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/DisplayBindings/Data/ICSharpCode.Data.Core.UI/Windows/WizardWindowInnards.xaml.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
 
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
 
3
 
 
4
#region Usings
 
5
 
 
6
using System;
 
7
using System.Collections.Generic;
 
8
using System.Linq;
 
9
using System.Text;
 
10
using System.Windows;
 
11
using System.Windows.Controls;
 
12
using System.Windows.Data;
 
13
using System.Windows.Documents;
 
14
using System.Windows.Input;
 
15
using System.Windows.Media;
 
16
using System.Windows.Media.Imaging;
 
17
using System.Windows.Shapes;
 
18
using System.Collections.ObjectModel;
 
19
using ICSharpCode.Data.Core.UI.UserControls;
 
20
using System.ComponentModel;
 
21
using System.Collections.Specialized;
 
22
 
 
23
#endregion
 
24
 
 
25
namespace ICSharpCode.Data.Core.UI.Windows
 
26
{
 
27
    /// <summary>
 
28
    /// Interaction logic for WizardWindow.xaml
 
29
    /// </summary>
 
30
    public partial class WizardWindowInnards : UserControl, INotifyPropertyChanged
 
31
    {
 
32
        #region Fields
 
33
 
 
34
        internal static readonly DependencyProperty IsReadyForNextStepProperty =
 
35
            DependencyProperty.Register("IsReadyForNextStep", typeof(bool), typeof(WizardWindowInnards), new FrameworkPropertyMetadata(false, IsReadyForNextStep_Changed)); 
 
36
        private WizardWindow _wizardWindow = null;
 
37
        private ObservableCollection<WizardUserControl> _wizardUserControls = new ObservableCollection<WizardUserControl>();
 
38
        private WizardErrorUserControl _wizardErrorUserControl = null;
 
39
        private WizardUserControl _currentWizardUserControl = null;
 
40
        private int _currentIndex = 0;
 
41
        private int _previousIndex = 0;
 
42
 
 
43
        #endregion
 
44
 
 
45
        #region Properties
 
46
 
 
47
        /// <summary>
 
48
        /// Returns the ObservableCollection of WizardUserControls, which are displayed in this WizardWindow.
 
49
        /// </summary>
 
50
        public ObservableCollection<WizardUserControl> WizardUserControls
 
51
        {
 
52
            get { return _wizardUserControls; }
 
53
        }
 
54
 
 
55
        /// <summary>
 
56
        /// Gets or sets the current WizardWindows' user control for displaying errors.
 
57
        /// </summary>
 
58
        public WizardErrorUserControl WizardErrorUserControl
 
59
        {
 
60
            get { return _wizardErrorUserControl; }
 
61
            set { _wizardErrorUserControl = value; }
 
62
        }
 
63
 
 
64
        /// <summary>
 
65
        /// Returns the current WizardUserControls index of the WizardWindow.
 
66
        /// </summary>
 
67
        public int CurrentIndex
 
68
        {
 
69
            get { return _currentIndex; }
 
70
            protected set
 
71
            {
 
72
                _previousIndex = _currentIndex;
 
73
                _currentIndex = value;
 
74
                _currentWizardUserControl = null;
 
75
                OnPropertyChanged("CurrentIndex");
 
76
                OnPropertyChanged("CurrentWizardUserControl");
 
77
            }
 
78
        }
 
79
 
 
80
        /// <summary>
 
81
        /// Returns the current WizardUserControl of the WizardWindow.
 
82
        /// </summary>
 
83
        public WizardUserControl CurrentWizardUserControl
 
84
        {
 
85
            get 
 
86
            {
 
87
                if (_currentWizardUserControl != null)
 
88
                    return _currentWizardUserControl;
 
89
                else
 
90
                {
 
91
 
 
92
                    if (_currentIndex == -1)
 
93
                    {
 
94
                        _currentWizardUserControl = _wizardErrorUserControl;
 
95
                    }
 
96
                    else
 
97
                    {
 
98
                        _currentWizardUserControl = _wizardUserControls.FirstOrDefault(wuc => wuc.Index == _currentIndex);
 
99
                        BindingBase binding = new Binding("IsReadyForNextStep") { Source = _currentWizardUserControl };
 
100
                        SetBinding(WizardWindowInnards.IsReadyForNextStepProperty, binding);
 
101
                    }
 
102
 
 
103
                    if (_currentWizardUserControl != null)
 
104
                    {
 
105
                        if (_currentIndex != -1 && _currentIndex - 1 == _previousIndex)
 
106
                            _currentWizardUserControl.Activate(true);
 
107
                        else
 
108
                            _currentWizardUserControl.Activate(false);
 
109
 
 
110
                        ToggleEnabledButtons();
 
111
                    }
 
112
 
 
113
                    return _currentWizardUserControl;
 
114
                }
 
115
            }
 
116
        }
 
117
 
 
118
        #endregion
 
119
 
 
120
        #region Constructor
 
121
 
 
122
        /// <summary>
 
123
        /// Initizales the WizardWindow.
 
124
        /// </summary>
 
125
        public WizardWindowInnards(WizardWindow wizardWindow)
 
126
        {
 
127
            InitializeComponent();
 
128
            _wizardWindow = wizardWindow;
 
129
            _wizardUserControls.CollectionChanged += new NotifyCollectionChangedEventHandler(WizardUserControls_CollectionChanged);
 
130
            DataContext = wizardWindow;
 
131
        }
 
132
 
 
133
        #endregion
 
134
 
 
135
        #region Methods
 
136
 
 
137
        private void ToggleEnabledButtons()
 
138
        {
 
139
            if (!IsInitialized)
 
140
                return;
 
141
            
 
142
            if (_currentIndex == 0)
 
143
            {
 
144
                btnPrevious.IsEnabled = false;
 
145
            }
 
146
            else
 
147
            {
 
148
                btnPrevious.IsEnabled = true;
 
149
            }
 
150
 
 
151
            if (_currentIndex == -1 || _currentIndex == _wizardUserControls.Count - 1)
 
152
            {
 
153
                btnNext.IsEnabled = false;
 
154
            }
 
155
            else
 
156
            {
 
157
                if (CurrentWizardUserControl.IsReadyForNextStep)
 
158
                    btnNext.IsEnabled = true;
 
159
                else
 
160
                    btnNext.IsEnabled = false;
 
161
            }
 
162
 
 
163
            if (CurrentWizardUserControl.CanFinish)
 
164
            {
 
165
                if (CurrentWizardUserControl.IsReadyForNextStep)
 
166
                    btnFinish.IsEnabled = true;
 
167
            }
 
168
            else
 
169
                btnFinish.IsEnabled = false;
 
170
        }
 
171
 
 
172
        #endregion
 
173
 
 
174
        #region Event handlers
 
175
 
 
176
        private static void IsReadyForNextStep_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
 
177
        {
 
178
            WizardWindowInnards wizardWindowInnards = d as WizardWindowInnards;
 
179
            wizardWindowInnards.ToggleEnabledButtons();
 
180
        }
 
181
 
 
182
        private void WizardUserControls_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
 
183
        {
 
184
            OnPropertyChanged("CurrentWizardUserControl");
 
185
        }
 
186
 
 
187
        private void btnPrevious_Click(object sender, RoutedEventArgs e)
 
188
        {
 
189
            if (CurrentIndex == 0)
 
190
                return;
 
191
            else if (CurrentIndex == -1)
 
192
            {
 
193
                CurrentIndex = _wizardErrorUserControl.PreviousIndex;
 
194
            }
 
195
            else
 
196
            {
 
197
                CurrentIndex--;
 
198
            }
 
199
        }
 
200
 
 
201
        private void btnNext_Click(object sender, RoutedEventArgs e)
 
202
        {
 
203
            if (CurrentIndex == _wizardUserControls.Count - 1)
 
204
                return;
 
205
            else
 
206
            {
 
207
                CurrentIndex++;
 
208
            }
 
209
        }
 
210
 
 
211
        private void btnFinish_Click(object sender, RoutedEventArgs e)
 
212
        {
 
213
            try
 
214
            {
 
215
                _wizardWindow.OnFinished();
 
216
            }
 
217
            catch (Exception ex)
 
218
            {
 
219
                if (_wizardErrorUserControl != null)
 
220
                {
 
221
                    _wizardErrorUserControl.Exception = ex;
 
222
                    _wizardErrorUserControl.PreviousIndex = _currentIndex;
 
223
                    CurrentIndex = -1;
 
224
 
 
225
                    return;
 
226
                }
 
227
                else
 
228
                {
 
229
                    throw ex;
 
230
                }                    
 
231
            }
 
232
            
 
233
            _wizardWindow.DialogResult = true;
 
234
            _wizardWindow.Close();
 
235
        }
 
236
 
 
237
        private void btnCancel_Click(object sender, RoutedEventArgs e)
 
238
        {
 
239
            _wizardWindow.DialogResult = false;
 
240
            _wizardWindow.Close();
 
241
        }
 
242
 
 
243
        #endregion
 
244
 
 
245
        #region INotifyPropertyChanged
 
246
 
 
247
        public event PropertyChangedEventHandler PropertyChanged;
 
248
 
 
249
        protected void OnPropertyChanged(string property)
 
250
        {
 
251
            if (PropertyChanged != null)
 
252
            {
 
253
                PropertyChanged(this, new PropertyChangedEventArgs(property));
 
254
            }
 
255
        }
 
256
 
 
257
        #endregion
 
258
    }
 
259
}