PageRenderTime 46ms CodeModel.GetById 13ms app.highlight 26ms RepoModel.GetById 2ms app.codeStats 0ms

/tags/stable-1.1.2/Client/Settings/AddEditSettingDialog.cs

#
C# | 300 lines | 218 code | 38 blank | 44 comment | 5 complexity | 11cbfb38c213482ae612055c706253ac 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.Generic;
 14using System.Windows.Forms;
 15using Microsoft.Web.Management.Client.Win32;
 16using Web.Management.PHP.Config;
 17
 18namespace Web.Management.PHP.Settings
 19{
 20
 21    internal sealed class AddEditSettingDialog : 
 22#if VSDesigner
 23        Form
 24#else
 25        TaskForm
 26#endif
 27    {
 28        private string _settingName;
 29
 30        private PHPModule _module;
 31        private bool _canAccept;
 32
 33        private Label _nameLabel;
 34        private TextBox _nameTextBox;
 35        private Label _valueLabel;
 36        private TextBox _valueTextBox;
 37        private Label _sectionLabel;
 38        private ManagementPanel _contentPanel;
 39        private TextBox _sectionTextBox;
 40        private LinkLabel _helpLinkLabel;
 41
 42        private AutoCompleteStringCollection _autoCompleteSections;
 43
 44        /// <summary>
 45        /// Required designer variable.
 46        /// </summary>
 47        private System.ComponentModel.IContainer components = null;
 48
 49        // Constructor for Add Setting Dialog
 50        public AddEditSettingDialog(PHPModule module, IEnumerable<string> sections)
 51            : base(module)
 52        {
 53            _module = module;
 54
 55            InitializeComponent();
 56            InitializeUI();
 57
 58            // The dialog is used in the "Add setting" mode
 59            Text = Resources.AddEditSettingDialogAddSetting;
 60
 61            _autoCompleteSections = new AutoCompleteStringCollection();
 62            foreach (string section in sections)
 63            {
 64                _autoCompleteSections.Add(section);
 65            }
 66            _sectionTextBox.AutoCompleteCustomSource = _autoCompleteSections;
 67            _sectionTextBox.Text = "PHP";
 68
 69            UpdateUI();
 70        }
 71
 72        // Constructor for Edit Setting Dialog
 73        public AddEditSettingDialog(PHPModule module, PHPIniSetting setting)
 74            : base(module)
 75        {
 76            _module = module;
 77
 78            InitializeComponent();
 79            InitializeUI();
 80
 81            // The dialog is used in the "Edit setting" mode
 82            Text = Resources.AddEditSettingDialogEditSetting;
 83
 84            _nameTextBox.Enabled = false;
 85            _sectionTextBox.Enabled = false;
 86            _nameTextBox.Text = setting.Name;
 87            _valueTextBox.Text = setting.Value;
 88            _sectionTextBox.Text = setting.Section;
 89
 90            UpdateUI();
 91        }
 92
 93        protected override bool CanAccept
 94        {
 95            get
 96            {
 97                return _canAccept;
 98            }
 99        }
100
101        protected override bool CanShowHelp
102        {
103            get
104            {
105                return true;
106            }
107        }
108
109        internal string SettingName
110        {
111            get
112            {
113                return _settingName;
114            }
115            set
116            {
117                _settingName = value;
118            }
119        }
120
121        /// <summary>
122        /// Clean up any resources being used.
123        /// </summary>
124        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
125        protected override void Dispose(bool disposing)
126        {
127            if (disposing && (components != null))
128            {
129                components.Dispose();
130            }
131            base.Dispose(disposing);
132        }
133
134        private void InitializeComponent()
135        {
136            this._nameLabel = new System.Windows.Forms.Label();
137            this._nameTextBox = new System.Windows.Forms.TextBox();
138            this._valueLabel = new System.Windows.Forms.Label();
139            this._valueTextBox = new System.Windows.Forms.TextBox();
140            this._sectionLabel = new System.Windows.Forms.Label();
141            this._sectionTextBox = new System.Windows.Forms.TextBox();
142            this._helpLinkLabel = new System.Windows.Forms.LinkLabel();
143            this.SuspendLayout();
144            // 
145            // _nameLabel
146            // 
147            this._nameLabel.AutoSize = true;
148            this._nameLabel.Location = new System.Drawing.Point(0, 14);
149            this._nameLabel.Name = "_nameLabel";
150            this._nameLabel.Size = new System.Drawing.Size(38, 13);
151            this._nameLabel.TabIndex = 0;
152            this._nameLabel.Text = Resources.AddEditSettingDialogName;
153            // 
154            // _nameTextBox
155            // 
156            this._nameTextBox.Location = new System.Drawing.Point(0, 30);
157            this._nameTextBox.Name = "_nameTextBox";
158            this._nameTextBox.Size = new System.Drawing.Size(259, 20);
159            this._nameTextBox.TabIndex = 1;
160            this._nameTextBox.TextChanged += new System.EventHandler(this.OnTextBoxTextChanged);
161            // 
162            // _valueLabel
163            // 
164            this._valueLabel.AutoSize = true;
165            this._valueLabel.Location = new System.Drawing.Point(0, 91);
166            this._valueLabel.Name = "_valueLabel";
167            this._valueLabel.Size = new System.Drawing.Size(37, 13);
168            this._valueLabel.TabIndex = 2;
169            this._valueLabel.Text = Resources.AddEditSettingDialogValue;
170            // 
171            // _valueTextBox
172            // 
173            this._valueTextBox.Location = new System.Drawing.Point(0, 107);
174            this._valueTextBox.Name = "_valueTextBox";
175            this._valueTextBox.Size = new System.Drawing.Size(259, 20);
176            this._valueTextBox.TabIndex = 3;
177            this._valueTextBox.TextChanged += new System.EventHandler(this.OnTextBoxTextChanged);
178            // 
179            // _sectionLabel
180            // 
181            this._sectionLabel.AutoSize = true;
182            this._sectionLabel.Location = new System.Drawing.Point(0, 143);
183            this._sectionLabel.Name = "_sectionLabel";
184            this._sectionLabel.Size = new System.Drawing.Size(46, 13);
185            this._sectionLabel.TabIndex = 4;
186            this._sectionLabel.Text = Resources.AddEditSettingDialogSection;
187            // 
188            // _sectionTextBox
189            // 
190            this._sectionTextBox.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest;
191            this._sectionTextBox.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.CustomSource;
192            this._sectionTextBox.Location = new System.Drawing.Point(0, 160);
193            this._sectionTextBox.Name = "_sectionTextBox";
194            this._sectionTextBox.Size = new System.Drawing.Size(259, 20);
195            this._sectionTextBox.TabIndex = 5;
196            // 
197            // _helpLinkLabel
198            // 
199            this._helpLinkLabel.AutoSize = true;
200            this._helpLinkLabel.Enabled = false;
201            this._helpLinkLabel.Location = new System.Drawing.Point(0, 57);
202            this._helpLinkLabel.Name = "_helpLinkLabel";
203            this._helpLinkLabel.Size = new System.Drawing.Size(143, 13);
204            this._helpLinkLabel.TabIndex = 6;
205            this._helpLinkLabel.TabStop = true;
206            this._helpLinkLabel.Text = Resources.AddEditSettingDialogLearnMore;
207            this._helpLinkLabel.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.OnHelpLinkLabelLinkClicked);
208            // 
209            // AddEditSettingDialog
210            // 
211            this.ClientSize = new System.Drawing.Size(284, 262);
212            this.Controls.Add(this._helpLinkLabel);
213            this.Controls.Add(this._sectionTextBox);
214            this.Controls.Add(this._sectionLabel);
215            this.Controls.Add(this._valueTextBox);
216            this.Controls.Add(this._valueLabel);
217            this.Controls.Add(this._nameTextBox);
218            this.Controls.Add(this._nameLabel);
219            this.Name = "AddEditSettingDialog";
220            this.ResumeLayout(false);
221#if VSDesigner
222            this.PerformLayout();
223#endif
224        }
225
226        public void InitializeUI()
227        {
228
229            this._contentPanel = new ManagementPanel();
230            this._contentPanel.SuspendLayout();
231
232            this._contentPanel.Location = new System.Drawing.Point(0, 0);
233            this._contentPanel.Dock = DockStyle.Fill;
234            this._contentPanel.Controls.Add(_nameLabel);
235            this._contentPanel.Controls.Add(_nameTextBox);
236            this._contentPanel.Controls.Add(_helpLinkLabel);
237            this._contentPanel.Controls.Add(_valueLabel);
238            this._contentPanel.Controls.Add(_valueTextBox);
239            this._contentPanel.Controls.Add(_sectionLabel);
240            this._contentPanel.Controls.Add(_sectionTextBox);
241
242            this._contentPanel.ResumeLayout(false);
243            this._contentPanel.PerformLayout();
244
245            SetContent(_contentPanel);
246
247            UpdateTaskForm();
248        }
249
250        protected override void OnAccept()
251        {
252            try
253            {
254                PHPIniSetting setting = new PHPIniSetting();
255                setting.Name = _nameTextBox.Text.Trim();
256                setting.Value = _valueTextBox.Text.Trim();
257                setting.Section = _sectionTextBox.Text.Trim();
258
259                RemoteObjectCollection<PHPIniSetting> settings = new RemoteObjectCollection<PHPIniSetting>();
260                settings.Add(setting);
261                _module.Proxy.AddOrUpdateSettings(settings);
262
263                DialogResult = DialogResult.OK;
264                SettingName = setting.Name;
265                Close();
266            }
267            catch (Exception ex)
268            {
269                DisplayErrorMessage(ex, Resources.ResourceManager);
270            }
271        }
272
273        private void OnHelpLinkLabelLinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
274        {
275            this.ShowHelp();
276        }
277
278        private void OnTextBoxTextChanged(object sender, EventArgs e)
279        {
280            UpdateUI();
281        }
282
283        protected override void ShowHelp()
284        {
285            Helper.Browse("http://www.php.net/" + _nameTextBox.Text.Trim());
286        }
287
288        private void UpdateUI()
289        {
290            string name = _nameTextBox.Text.Trim();
291            string value = _valueTextBox.Text.Trim();
292            string section = _sectionTextBox.Text.Trim();
293            _canAccept = !String.IsNullOrEmpty(name) && !String.IsNullOrEmpty(value) && !String.IsNullOrEmpty(section);
294            _helpLinkLabel.Enabled = !String.IsNullOrEmpty(name);
295
296            UpdateTaskForm();
297        }
298
299    }
300}