PageRenderTime 60ms CodeModel.GetById 25ms app.highlight 27ms RepoModel.GetById 2ms app.codeStats 0ms

/tags/stable-1.2.0/Client/Setup/SelectSiteAndUrlDialog.cs

#
C# | 391 lines | 305 code | 51 blank | 35 comment | 17 complexity | dfce5651551f8bca3d3b5b6c0de269c8 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 Guid PreferenceServiceGuid = new Guid("68a2a947-eeb6-40d9-9e5a-977bf7753bce");
 32        private const string ServerSelectSitePreferenceKey = "ServerSitePreferenceKey";
 33        private const string ServerSelectUrlPreferenceKey = "ServerUrlPreferenceKey";
 34
 35        private PHPModule _module;
 36        private 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 System.ComponentModel.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                    IPreferencesService 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                else
105                {
106                    return String.Empty;
107                }
108            }
109        }
110
111        public string SelectedUrl
112        {
113            get
114            {
115                return _urlsComboBox.SelectedItem as string;
116            }
117        }
118
119        public string SiteName
120        {
121            get
122            {
123                return _sitesComboBox.SelectedItem as string;
124            }
125        }
126
127        /// <summary>
128        /// Clean up any resources being used.
129        /// </summary>
130        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
131        protected override void Dispose(bool disposing)
132        {
133            if (disposing && (components != null))
134            {
135                components.Dispose();
136            }
137            base.Dispose(disposing);
138        }
139
140        private static string GetSitePreferenceKey(Connection connection)
141        {
142            return connection.ConfigurationPath.SiteName + "/" + connection.ConfigurationPath.GetEffectiveConfigurationPath(ManagementScope.Site);
143        }
144
145        #region Windows Form Designer generated code
146
147        /// <summary>
148        /// Required method for Designer support - do not modify
149        /// the contents of this method with the code editor.
150        /// </summary>
151        private void InitializeComponent()
152        {
153            this._urlsComboBox = new System.Windows.Forms.ComboBox();
154            this._sitesComboBox = new System.Windows.Forms.ComboBox();
155            this._urlsLabel = new System.Windows.Forms.Label();
156            this._sitesLabel = new System.Windows.Forms.Label();
157            this.SuspendLayout();
158            // 
159            // _domainsComboBox
160            // 
161            this._urlsComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
162            this._urlsComboBox.FormattingEnabled = true;
163            this._urlsComboBox.Location = new System.Drawing.Point(0, 90);
164            this._urlsComboBox.Name = "_domainsComboBox";
165            this._urlsComboBox.Size = new System.Drawing.Size(385, 21);
166            this._urlsComboBox.TabIndex = 3;
167            this._urlsComboBox.SelectedIndexChanged += new System.EventHandler(this.OnUrlsComboBoxSelectedIndexChanged);
168            // 
169            // _sitesComboBox
170            // 
171            this._sitesComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
172            this._sitesComboBox.FormattingEnabled = true;
173            this._sitesComboBox.Location = new System.Drawing.Point(0, 25);
174            this._sitesComboBox.Name = "_sitesComboBox";
175            this._sitesComboBox.Size = new System.Drawing.Size(385, 21);
176            this._sitesComboBox.TabIndex = 1;
177            this._sitesComboBox.SelectedIndexChanged += new System.EventHandler(this.OnSitesComboBoxSelectedIndexChanged);
178            // 
179            // _domainsLabel
180            // 
181            this._urlsLabel.Location = new System.Drawing.Point(0, 65);
182            this._urlsLabel.Name = "_domainsLabel";
183            this._urlsLabel.Size = new System.Drawing.Size(385, 22);
184            this._urlsLabel.TabIndex = 2;
185            this._urlsLabel.Text = Resources.SelectSiteAndUrlDialogSelectAUrl;
186            // 
187            // _sitesLabel
188            // 
189            this._sitesLabel.Location = new System.Drawing.Point(0, 0);
190            this._sitesLabel.Name = "_sitesLabel";
191            this._sitesLabel.Size = new System.Drawing.Size(385, 22);
192            this._sitesLabel.TabIndex = 0;
193            this._sitesLabel.Text = Resources.SelectSiteAndUrlDialogSelectASite;
194            // 
195            // SelectSiteDomainDialog
196            // 
197            this.ClientSize = new System.Drawing.Size(414, 192);
198            this.Controls.Add(this._sitesComboBox);
199            this.Controls.Add(this._sitesLabel);
200            this.Controls.Add(this._urlsLabel);
201            this.Controls.Add(this._urlsComboBox);
202            this.Name = "SelectSiteDomainDialog";
203            this.ResumeLayout(false);
204#if VSDesigner
205            this.PerformLayout();
206#endif
207        }
208
209        #endregion
210
211        public void InitializeUI()
212        {
213            this._contentPanel = new ManagementPanel();
214            _contentPanel.SuspendLayout();
215
216            this._contentPanel.Location = new System.Drawing.Point(0, 0);
217            this._contentPanel.Dock = DockStyle.Fill;
218            this._contentPanel.Controls.Add(_urlsLabel);
219            this._contentPanel.Controls.Add(_urlsComboBox);
220            this._contentPanel.Controls.Add(_sitesLabel);
221            this._contentPanel.Controls.Add(_sitesComboBox);
222
223            this._contentPanel.ResumeLayout(false);
224            this._contentPanel.PerformLayout();
225
226            this.Text = Resources.SelectSiteAndUrlDialogTitle;
227
228            SetContent(_contentPanel);
229            UpdateTaskForm();
230        }
231
232        private void LoadServerPreferences(PreferencesStore store)
233        {
234            _preferenceSite = store.GetValue(ServerSelectSitePreferenceKey, String.Empty);
235            _preferenceUrl = store.GetValue(ServerSelectUrlPreferenceKey, String.Empty);
236        }
237
238        private void LoadSitePreferences(PreferencesStore store)
239        {
240            _preferenceUrl = store.GetValue(GetSitePreferenceKey(_connection), String.Empty);
241        }
242
243        protected override void OnAccept()
244        {
245            if (_connection.ConfigurationPath.PathType == ConfigurationPathType.Server)
246            {
247                SaveServerPreferences(PreferencesStore);
248            }
249            else
250            {
251                SaveSitePreferences(PreferencesStore);
252            }
253
254            this.DialogResult = DialogResult.OK;
255            this.Close();
256        }
257
258        protected override void OnLoad(EventArgs e)
259        {
260            base.OnLoad(e);
261
262            if (_connection.ConfigurationPath.PathType == ConfigurationPathType.Server)
263            {
264                LoadServerPreferences(PreferencesStore);
265                StartAsyncTask(OnSiteWorkerDoWork, OnSiteWorkerDoWorkCompleted);
266            }
267            else
268            {
269                LoadSitePreferences(PreferencesStore);
270                _sitesComboBox.Items.Add(_connection.ConfigurationPath.SiteName);
271                _sitesComboBox.SelectedIndex = 0;
272                _sitesComboBox.Enabled = false;
273            }
274        }
275
276        private void OnSitesComboBoxSelectedIndexChanged(object sender, EventArgs e)
277        {
278            StartAsyncTask(OnUrlsWorkerDoWork, OnUrlsWorkerDoWorkCompleted, null, (string)_sitesComboBox.SelectedItem);
279        }
280
281        private void OnSiteWorkerDoWork(object sender, DoWorkEventArgs e) 
282        {
283            e.Result = _module.Proxy.GetSites();
284        }
285
286        private void OnSiteWorkerDoWorkCompleted(object sender, RunWorkerCompletedEventArgs e)
287        {
288            _sitesComboBox.BeginUpdate();
289            _sitesComboBox.SuspendLayout();
290            
291            try
292            {
293                _sitesComboBox.Items.Clear();
294
295                ArrayList sites = e.Result as ArrayList;
296                sites.Sort();
297                foreach (string siteName in sites)
298                {
299                    _sitesComboBox.Items.Add(siteName);
300                }
301
302                if (!String.IsNullOrEmpty(_preferenceSite))
303                {
304                    int selectedIndex = _sitesComboBox.Items.IndexOf(_preferenceSite);
305                    if (selectedIndex >= 0)
306                    {
307                        _sitesComboBox.SelectedIndex = selectedIndex;
308                    }
309                }
310            }
311            catch (Exception ex)
312            {
313                DisplayErrorMessage(ex, Resources.ResourceManager);
314            }
315            finally
316            {
317                _sitesComboBox.ResumeLayout();
318                _sitesComboBox.EndUpdate();
319            }
320
321            _sitesComboBox.Focus();
322        }
323
324        private void OnUrlsComboBoxSelectedIndexChanged(object sender, System.EventArgs e)
325        {
326            Update();
327        }
328
329        private void OnUrlsWorkerDoWork(object sender, DoWorkEventArgs e)
330        {
331            string siteName = (string)e.Argument;
332
333            e.Result = Helper.GetUrlListFromBindings(_connection.ScopePath.ServerName, _module.Proxy.GetSiteBindings(siteName), this.RelativePath);
334        }
335
336        private void OnUrlsWorkerDoWorkCompleted(object sender, RunWorkerCompletedEventArgs e)
337        {
338            _urlsComboBox.BeginUpdate();
339            _urlsComboBox.SuspendLayout();
340
341            try
342            {
343                _urlsComboBox.Items.Clear();
344
345                List<string> domains = e.Result as List<string>;
346                foreach (string domain in domains)
347                {
348                    _urlsComboBox.Items.Add(domain);
349                }
350
351                _urlsComboBox.SelectedIndex = 0;
352                if (!String.IsNullOrEmpty(_preferenceUrl))
353                {
354                    int selectedIndex = _urlsComboBox.Items.IndexOf(_preferenceUrl);
355                    if (selectedIndex > 0)
356                    {
357                        _urlsComboBox.SelectedIndex = selectedIndex;
358                    }
359                }
360            }
361            catch (Exception ex)
362            {
363                DisplayErrorMessage(ex, Resources.ResourceManager);
364            }
365            finally
366            {
367                _urlsComboBox.ResumeLayout();
368                _urlsComboBox.EndUpdate();
369            }
370
371            _urlsComboBox.Focus();
372        }
373
374        private void SaveServerPreferences(PreferencesStore store)
375        {
376            store.SetValue(ServerSelectSitePreferenceKey, SiteName, String.Empty);
377            store.SetValue(ServerSelectUrlPreferenceKey, SelectedUrl, String.Empty);
378        }
379
380        private void SaveSitePreferences(PreferencesStore store)
381        {
382            store.SetValue(GetSitePreferenceKey(_connection), SelectedUrl, String.Empty);
383        }
384
385        protected override void ShowHelp()
386        {
387            Helper.Browse(Globals.SelectSiteDomainOnlineHelp);
388        }
389
390    }
391}