PageRenderTime 28ms CodeModel.GetById 14ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

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

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