示例#1
0
		public bool ShowSettingsDialog(IExtension sender, Settings settings, SettingsValidator validator)
		{
			FormSettings settingsDialog = new FormSettings(settings, validator);
			try
			{
				return settingsDialog.ShowDialog() == DialogResult.OK;
			}
			finally
			{
				settingsDialog.Dispose();
			}
		}
		public FormSettings(Settings settings, SettingsValidator validator)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			// Keep track of the validator.
			this.validator = validator;

			// This assumes all fonts on the form are the same size.
			Graphics g = CreateGraphics();
			int charWidth = (int)g.MeasureString("H", Font).Width;

			// Keep a reference to the supplied Settings object.
			this.settings = settings;

			// Create a tab page for each settings group.
			for (int i = 0; i < settings.Count; i++) 
			{
				Setting setting = settings[i];
				int j = 0;
				while ((j < tabControl.TabPages.Count) && (setting.Group != tabControl.TabPages[j].Text))
					j++;
				if (j >= tabControl.TabPages.Count) 
				{
					TabPage newPage = new TabPage();
					newPage.Text = setting.Group;
					tabControl.TabPages.Add(newPage);
				}
			}

			// Create a scrollable panel on each tab page.
			controlPanels = new ScrollablePanel[tabControl.TabPages.Count];
			for (int i = 0; i < tabControl.TabPages.Count; i++)
			{
				controlPanels[i] = new ScrollablePanel();
				controlPanels[i].Location = new Point(0, 0);
				controlPanels[i].Size = tabControl.TabPages[i].ClientSize;
				tabControl.TabPages[i].Controls.Add(controlPanels[i]);
			}

			// Create controls for each setting.
			controls = new Control[settings.Count];
			settingButtonProperties = new SettingButtonProperties[settings.Count];
			int[] controlOffset = new int[tabControl.TabPages.Count];
			for (int i = 0; i < settings.Count; i++)
			{
				Setting setting = settings[i];

				// Assemble the parameters for the setting.
				int j = 0;
				System.Collections.Hashtable parameters = new Hashtable();
				if (setting.Params.Length % 2 != 0)
					throw new Exception("Params array does not have an even number of elements");
				while (j < setting.Params.Length) 
				{
					parameters[setting.Params[j]] = setting.Params[j + 1];
					j += 2;
				}

				// Find the corresponding tab page and panel.
				j = 0;
				while ((j < tabControl.TabPages.Count) && (setting.Group != tabControl.TabPages[j].Text))
					j++;
				if (j >= tabControl.TabPages.Count)
					throw new Exception("Tab page not found. Should be impossible.");
				ScrollablePanel controlPanel = controlPanels[j];
				int maxControlWidth = controlPanel.ContentWidth - 2 * marginLeft;

				// Add the appropriate control.
				if (setting.Type == "boolean") 
				{
					// The caption is part of the check box control.
					CheckBox checkBox = new CheckBox();
					checkBox.Text = setting.Caption;
					checkBox.Width = maxControlWidth;
					checkBox.Checked = (bool)setting.Value;
					controls[i] = checkBox;
				}
				else
				{
					// Display a caption with the corresponding control underneath it.
					Label caption = new Label();
					caption.Left = marginLeft;
					caption.Top = marginTop + controlOffset[j];
					caption.Width = maxControlWidth;
					caption.Text = setting.Caption;
					controlOffset[j] += caption.Height;
					controlPanel.ContentControls.Add(caption);
					switch (setting.Type) 
					{
						case "string":
						case "integer":
						case "long":
						case "double":
						{
							int maxLength = parameters.Contains("maxlength") ? (int)parameters["maxlength"] : 0;
							TextBox textBox = new TextBox();
							if (maxLength > 0) 
							{
								textBox.MaxLength = maxLength;
								textBox.ClientSize = new Size((maxLength + 1) * charWidth, textBox.ClientSize.Height);
								if (textBox.Width > maxControlWidth)
									textBox.Width = maxControlWidth;
							}
							else
							{
								textBox.Width = maxControlWidth;
							}
							textBox.Text = setting.Value.ToString();
							textBox.GotFocus += new EventHandler(textBox_GotFocus);
							textBox.LostFocus += new EventHandler(textBox_LostFocus);
							controls[i] = textBox;
							break;
						}
						case "updown":
						{
							if (! parameters.Contains("min") || ! parameters.Contains("max"))
								throw new Exception("Missing 'min' and/or 'max' parameter(s)");
							int maxLength = parameters.Contains("maxlength") ? (int)parameters["maxlength"] : 0;
							int min = (int)parameters["min"];
							int max = (int)parameters["max"];
							NumericUpDown upDown = new NumericUpDown();
							upDown.Value = (int)setting.Value;
							upDown.Minimum = min;
							upDown.Maximum = max;
							if (maxLength > 0)
							{
								upDown.ClientSize = new Size((maxLength + 1) * charWidth + 30, upDown.ClientSize.Height);
								if (upDown.Width > maxControlWidth)
									upDown.Width = maxControlWidth;
							}
							else
							{
								upDown.Width = maxControlWidth;
							}
							controls[i] = upDown;
							break;
						}
						case "comportin":
						case "comportout":
						{
							Button button = new Button();
							button.Text = "..";
							button.Width = 20;
							button.Top = marginTop + controlOffset[j];
							button.Left = controlPanel.ContentWidth - marginLeft - button.Width;
							button.Click += new EventHandler(SettingButtonClick);
							settingButtonProperties[i] = new SettingButtonProperties(button, setting.Value);
							controlPanel.ContentControls.Add(button);

							TextBox textBox = new TextBox();
							textBox.ReadOnly = true;
							textBox.Width = button.Left - marginLeft - 2;
							textBox.Text = GetComPortDescription(setting.Value.ToString());
							controls[i] = textBox;
							break;
						}
						case "combobox":
						{
							if (! parameters.Contains("items"))
								throw new Exception("Missing 'items' parameter");
							object[] items = (object[])parameters["items"];
							ComboBox combo = new ComboBox();
							combo.Width = maxControlWidth;
							for (int k = 0; k < items.Length;  k++)
								combo.Items.Add(items[k]);
							combo.SelectedItem = setting.Value;
							controls[i] = combo;
							break;
						}
						case "openfile":
						case "savefile":
						case "exefile":
						{
							object data = null;
							if (setting.Type != "exefile")
							{
								data = new FileDialogData(parameters.Contains("filter") ? (string)parameters["filter"] : "All Files (*.*)|*.*",
									parameters.Contains("filterindex") ? (int)parameters["filterindex"] : 1,
									parameters.Contains("initialdir") ? (string)parameters["initialdir"] : "");
							}
							Button button = new Button();
							button.Text = "..";
							button.Width = 20;
							button.Top = marginTop + controlOffset[j];
							button.Left = controlPanel.ContentWidth - marginLeft - button.Width;
							button.Click += new EventHandler(SettingButtonClick);
							settingButtonProperties[i] = new SettingButtonProperties(button, data);
							controlPanel.ContentControls.Add(button);

							TextBox textBox = new TextBox();
							textBox.Width = button.Left - marginLeft - 2;
							textBox.Text = setting.Value.ToString();
							textBox.GotFocus += new EventHandler(textBox_GotFocus);
							textBox.LostFocus += new EventHandler(textBox_LostFocus);
							controls[i] = textBox;
							break;
						}
						default:
						{
							throw new Exception("Unknown setting type: " + setting.Type);
						}
					}
				}
				controls[i].Left = marginLeft;
				controls[i].Top = marginTop + controlOffset[j];
				controlOffset[j] += controls[i].Height + controlSpacing;
				controlPanel.ContentControls.Add(controls[i]);
			}

			// Set the heights of the control panel contents.
			for (int i = 0; i < controlPanels.Length; i++)
				controlPanels[i].ContentHeight = controlOffset[i];
		}
		private bool ValidateSettings(Settings settings, ref string errorMessage)
		{
			bool ret = true;

			if (((int)settings["gpsProductId"].Value < 0) || ((int)settings["gpsProductId"].Value > Int16.MaxValue))
			{
				errorMessage += "Invalid GPS Product ID.\r\n";
				ret = false;
			}
			if (((int)settings["gpsSoftwareVersion"].Value < 0) || ((int)settings["gpsSoftwareVersion"].Value > Int16.MaxValue))
			{
				errorMessage += "Invalid GPS Software Version.\r\n";
				ret = false;
			}
			if ((string)settings["gpsProductDescription"].Value == "")
			{
				errorMessage += "Invalid GPS Product Description.\r\n";
				ret = false;
			}
			if (((long)settings["gpsUnitId"].Value < 0) || ((long)settings["gpsUnitId"].Value > UInt32.MaxValue))
			{
				errorMessage += "Invalid GPS Unit ID.\r\n";
				ret = false;
			}
			if ((int)settings["snrMin"].Value >= (int)settings["snrMax"].Value)
			{
				errorMessage += "SNRdB minimum value must be less than the maximum value.\r\n";
				ret = false;
			}

			return ret;
		}
		private bool ValidateSettings(Settings settings, ref string errorMessage)
		{
			bool ret = true;

			if ((double)settings["movement_threshold"].Value < 0)
			{
				errorMessage += "Invalid Movement Threshold.\r\n";
				ret = false;
			}
			if ((double)settings["dop_multiplier"].Value < 0)
			{
				errorMessage += "Invalid DOP Multiplier.\r\n";
				ret = false;
			}

			return ret;
		}