PageRenderTime 55ms CodeModel.GetById 11ms app.highlight 38ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Client/Setup/SelectSiteAndUrlDialog.cs

#
C# | 388 lines | 302 code | 51 blank | 35 comment | 17 complexity | 408fe4bd3681d192c534014102d5aec6 MD5 | raw file
  1//-----------------------------------------------------------------------
  2// <copyright>
  3// Copyright (C) Ruslan Yakushev for the PHP Manager for IIS project.
  4//
  5// This file is subject to the terms and conditions of the Microsoft Public License (MS-PL).
  6// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL for more details.
  7// </copyright>
  8//----------------------------------------------------------------------- 
  9
 10//#define VSDesigner
 11
 12using System;
 13using System.Collections;
 14using System.Collections.Generic;
 15using System.ComponentModel;
 16using System.Windows.Forms;
 17using Microsoft.Web.Management.Client;
 18using Microsoft.Web.Management.Client.Win32;
 19using Microsoft.Web.Management.Server;
 20
 21namespace Web.Management.PHP.Setup
 22{
 23
 24    internal sealed class SelectSiteAndUrlDialog :
 25#if VSDesigner
 26        Form
 27#else
 28        TaskForm
 29#endif
 30    {
 31        private readonly Guid _preferenceServiceGuid = new Guid("68a2a947-eeb6-40d9-9e5a-977bf7753bce");
 32        private const string ServerSelectSitePreferenceKey = "ServerSitePreferenceKey";
 33        private const string ServerSelectUrlPreferenceKey = "ServerUrlPreferenceKey";
 34
 35        private readonly PHPModule _module;
 36        private readonly Connection _connection;
 37        private string _preferenceUrl;
 38        private string _preferenceSite;
 39        private PreferencesStore _store;
 40
 41        private ManagementPanel _contentPanel;
 42        private ComboBox _urlsComboBox;
 43        private ComboBox _sitesComboBox;
 44        private Label _urlsLabel;
 45        private Label _sitesLabel;
 46
 47        /// <summary>
 48        /// Required designer variable.
 49        /// </summary>
 50        private readonly IContainer components = null;
 51
 52        public SelectSiteAndUrlDialog(PHPModule module, Connection connection)
 53            : base(module)
 54        {
 55            _module = module;
 56            _connection = connection;
 57
 58            InitializeComponent();
 59            InitializeUI();
 60
 61            Update();
 62        }
 63
 64        protected override bool CanAccept
 65        {
 66            get
 67            {
 68                return _urlsComboBox.SelectedIndex >= 0;
 69            }
 70        }
 71
 72        protected override bool CanShowHelp
 73        {
 74            get
 75            {
 76                return true;
 77            }
 78        }
 79
 80        private PreferencesStore PreferencesStore
 81        {
 82            get
 83            {
 84                if (_store == null)
 85                {
 86                    var prefService = (IPreferencesService)GetService(typeof(IPreferencesService));
 87                    if (prefService != null)
 88                    {
 89                        _store = prefService.GetPreferencesStore(_preferenceServiceGuid);
 90                    }
 91                }
 92                return _store;
 93            }
 94        }
 95
 96        public string RelativePath
 97        {
 98            get
 99            {
100                if (_connection.ConfigurationPath.PathType != ConfigurationPathType.Server)
101                {
102                    return _connection.ConfigurationPath.GetEffectiveConfigurationPath(ManagementScope.Site);
103                }
104                return String.Empty;
105            }
106        }
107
108        public string SelectedUrl
109        {
110            get
111            {
112                return _urlsComboBox.SelectedItem as string;
113            }
114        }
115
116        public string SiteName
117        {
118            get
119            {
120                return _sitesComboBox.SelectedItem as string;
121            }
122        }
123
124        /// <summary>
125        /// Clean up any resources being used.
126        /// </summary>
127        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
128        protected override void Dispose(bool disposing)
129        {
130            if (disposing && (components != null))
131            {
132                components.Dispose();
133            }
134            base.Dispose(disposing);
135        }
136
137        private static string GetSitePreferenceKey(Connection connection)
138        {
139            return connection.ConfigurationPath.SiteName + "/" + connection.ConfigurationPath.GetEffectiveConfigurationPath(ManagementScope.Site);
140        }
141
142        #region Windows Form Designer generated code
143
144        /// <summary>
145        /// Required method for Designer support - do not modify
146        /// the contents of this method with the code editor.
147        /// </summary>
148        private void InitializeComponent()
149        {
150            _urlsComboBox = new System.Windows.Forms.ComboBox();
151            _sitesComboBox = new System.Windows.Forms.ComboBox();
152            _urlsLabel = new System.Windows.Forms.Label();
153            _sitesLabel = new System.Windows.Forms.Label();
154            SuspendLayout();
155            // 
156            // _domainsComboBox
157            // 
158            _urlsComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
159            _urlsComboBox.FormattingEnabled = true;
160            _urlsComboBox.Location = new System.Drawing.Point(0, 90);
161            _urlsComboBox.Name = "_domainsComboBox";
162            _urlsComboBox.Size = new System.Drawing.Size(385, 21);
163            _urlsComboBox.TabIndex = 3;
164            _urlsComboBox.SelectedIndexChanged += new System.EventHandler(OnUrlsComboBoxSelectedIndexChanged);
165            // 
166            // _sitesComboBox
167            // 
168            _sitesComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
169            _sitesComboBox.FormattingEnabled = true;
170            _sitesComboBox.Location = new System.Drawing.Point(0, 25);
171            _sitesComboBox.Name = "_sitesComboBox";
172            _sitesComboBox.Size = new System.Drawing.Size(385, 21);
173            _sitesComboBox.TabIndex = 1;
174            _sitesComboBox.SelectedIndexChanged += new System.EventHandler(OnSitesComboBoxSelectedIndexChanged);
175            // 
176            // _domainsLabel
177            // 
178            _urlsLabel.Location = new System.Drawing.Point(0, 65);
179            _urlsLabel.Name = "_domainsLabel";
180            _urlsLabel.Size = new System.Drawing.Size(385, 22);
181            _urlsLabel.TabIndex = 2;
182            _urlsLabel.Text = Resources.SelectSiteAndUrlDialogSelectAUrl;
183            // 
184            // _sitesLabel
185            // 
186            _sitesLabel.Location = new System.Drawing.Point(0, 0);
187            _sitesLabel.Name = "_sitesLabel";
188            _sitesLabel.Size = new System.Drawing.Size(385, 22);
189            _sitesLabel.TabIndex = 0;
190            _sitesLabel.Text = Resources.SelectSiteAndUrlDialogSelectASite;
191            // 
192            // SelectSiteDomainDialog
193            // 
194            ClientSize = new System.Drawing.Size(414, 192);
195            Controls.Add(_sitesComboBox);
196            Controls.Add(_sitesLabel);
197            Controls.Add(_urlsLabel);
198            Controls.Add(_urlsComboBox);
199            Name = "SelectSiteDomainDialog";
200            ResumeLayout(false);
201#if VSDesigner
202            PerformLayout();
203#endif
204        }
205
206        #endregion
207
208        public void InitializeUI()
209        {
210            _contentPanel = new ManagementPanel();
211            _contentPanel.SuspendLayout();
212
213            _contentPanel.Location = new System.Drawing.Point(0, 0);
214            _contentPanel.Dock = DockStyle.Fill;
215            _contentPanel.Controls.Add(_urlsLabel);
216            _contentPanel.Controls.Add(_urlsComboBox);
217            _contentPanel.Controls.Add(_sitesLabel);
218            _contentPanel.Controls.Add(_sitesComboBox);
219
220            _contentPanel.ResumeLayout(false);
221            _contentPanel.PerformLayout();
222
223            Text = Resources.SelectSiteAndUrlDialogTitle;
224
225            SetContent(_contentPanel);
226            UpdateTaskForm();
227        }
228
229        private void LoadServerPreferences(PreferencesStore store)
230        {
231            _preferenceSite = store.GetValue(ServerSelectSitePreferenceKey, String.Empty);
232            _preferenceUrl = store.GetValue(ServerSelectUrlPreferenceKey, String.Empty);
233        }
234
235        private void LoadSitePreferences(PreferencesStore store)
236        {
237            _preferenceUrl = store.GetValue(GetSitePreferenceKey(_connection), String.Empty);
238        }
239
240        protected override void OnAccept()
241        {
242            if (_connection.ConfigurationPath.PathType == ConfigurationPathType.Server)
243            {
244                SaveServerPreferences(PreferencesStore);
245            }
246            else
247            {
248                SaveSitePreferences(PreferencesStore);
249            }
250
251            DialogResult = DialogResult.OK;
252            Close();
253        }
254
255        protected override void OnLoad(EventArgs e)
256        {
257            base.OnLoad(e);
258
259            if (_connection.ConfigurationPath.PathType == ConfigurationPathType.Server)
260            {
261                LoadServerPreferences(PreferencesStore);
262                StartAsyncTask(OnSiteWorkerDoWork, OnSiteWorkerDoWorkCompleted);
263            }
264            else
265            {
266                LoadSitePreferences(PreferencesStore);
267                _sitesComboBox.Items.Add(_connection.ConfigurationPath.SiteName);
268                _sitesComboBox.SelectedIndex = 0;
269                _sitesComboBox.Enabled = false;
270            }
271        }
272
273        private void OnSitesComboBoxSelectedIndexChanged(object sender, EventArgs e)
274        {
275            StartAsyncTask(OnUrlsWorkerDoWork, OnUrlsWorkerDoWorkCompleted, null, _sitesComboBox.SelectedItem);
276        }
277
278        private void OnSiteWorkerDoWork(object sender, DoWorkEventArgs e) 
279        {
280            e.Result = _module.Proxy.GetSites();
281        }
282
283        private void OnSiteWorkerDoWorkCompleted(object sender, RunWorkerCompletedEventArgs e)
284        {
285            _sitesComboBox.BeginUpdate();
286            _sitesComboBox.SuspendLayout();
287            
288            try
289            {
290                _sitesComboBox.Items.Clear();
291
292                var sites = e.Result as ArrayList;
293                sites.Sort();
294                foreach (string siteName in sites)
295                {
296                    _sitesComboBox.Items.Add(siteName);
297                }
298
299                if (!String.IsNullOrEmpty(_preferenceSite))
300                {
301                    var selectedIndex = _sitesComboBox.Items.IndexOf(_preferenceSite);
302                    if (selectedIndex >= 0)
303                    {
304                        _sitesComboBox.SelectedIndex = selectedIndex;
305                    }
306                }
307            }
308            catch (Exception ex)
309            {
310                DisplayErrorMessage(ex, Resources.ResourceManager);
311            }
312            finally
313            {
314                _sitesComboBox.ResumeLayout();
315                _sitesComboBox.EndUpdate();
316            }
317
318            _sitesComboBox.Focus();
319        }
320
321        private void OnUrlsComboBoxSelectedIndexChanged(object sender, EventArgs e)
322        {
323            Update();
324        }
325
326        private void OnUrlsWorkerDoWork(object sender, DoWorkEventArgs e)
327        {
328            var siteName = (string)e.Argument;
329
330            e.Result = Helper.GetUrlListFromBindings(_connection.ScopePath.ServerName, _module.Proxy.GetSiteBindings(siteName), RelativePath);
331        }
332
333        private void OnUrlsWorkerDoWorkCompleted(object sender, RunWorkerCompletedEventArgs e)
334        {
335            _urlsComboBox.BeginUpdate();
336            _urlsComboBox.SuspendLayout();
337
338            try
339            {
340                _urlsComboBox.Items.Clear();
341
342                var domains = e.Result as List<string>;
343                foreach (string domain in domains)
344                {
345                    _urlsComboBox.Items.Add(domain);
346                }
347
348                _urlsComboBox.SelectedIndex = 0;
349                if (!String.IsNullOrEmpty(_preferenceUrl))
350                {
351                    int selectedIndex = _urlsComboBox.Items.IndexOf(_preferenceUrl);
352                    if (selectedIndex > 0)
353                    {
354                        _urlsComboBox.SelectedIndex = selectedIndex;
355                    }
356                }
357            }
358            catch (Exception ex)
359            {
360                DisplayErrorMessage(ex, Resources.ResourceManager);
361            }
362            finally
363            {
364                _urlsComboBox.ResumeLayout();
365                _urlsComboBox.EndUpdate();
366            }
367
368            _urlsComboBox.Focus();
369        }
370
371        private void SaveServerPreferences(PreferencesStore store)
372        {
373            store.SetValue(ServerSelectSitePreferenceKey, SiteName, String.Empty);
374            store.SetValue(ServerSelectUrlPreferenceKey, SelectedUrl, String.Empty);
375        }
376
377        private void SaveSitePreferences(PreferencesStore store)
378        {
379            store.SetValue(GetSitePreferenceKey(_connection), SelectedUrl, String.Empty);
380        }
381
382        protected override void ShowHelp()
383        {
384            Helper.Browse(Globals.SelectSiteDomainOnlineHelp);
385        }
386
387    }
388}