コード例 #1
0
ファイル: MapSetup.cs プロジェクト: CalvertYang/NPxP
        private void btnSave_Click(object sender, EventArgs e)
        {
            // Save default config file to system config
            ConfigHelper ch = new ConfigHelper();
            if (String.IsNullOrEmpty(cmbMapConfigName.Text))
            {
                cmbMapConfigName.Text = DateTime.Now.ToShortDateString();
            }
            if (!ch.SaveMapSetupConfigFile(cmbMapConfigName.Text.Trim()))
            {
                return;
            }

            // initialize map xml sechma
            string sechma_path = PathHelper.SystemConfigFolder + "map_sechma.xml";
            XmlDocument document = new XmlDocument();
            document.Load(sechma_path);
            XPathNavigator navigator = document.CreateNavigator();

            // save image_grid_rows , image_grid_columns
            navigator.SelectSingleNode("//pxptab/image_grid_rows").SetValue(nudImageRows.Value.ToString());
            navigator.SelectSingleNode("//pxptab/image_grid_columns").SetValue(nudImageColumns.Value.ToString());

            // save map_proportion
            string[] proportion = cmbMapSize.SelectedItem.ToString().Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
            navigator.SelectSingleNode("//map_window/map_chart/map_proportion[@name='x']").SetValue(proportion[0]);
            navigator.SelectSingleNode("//map_window/map_chart/map_proportion[@name='y']").SetValue(proportion[1]);

            // save grid line display
            if (rdoMapGridOn.Checked)
            {
                navigator.SelectSingleNode("//map_window/map_chart/grid_line_display").SetValue("true");
            }
            else
            {
                navigator.SelectSingleNode("//map_window/map_chart/grid_line_display").SetValue("false");
            }

            // save grid line mode :Map 底格線間隔模式 0: FixSize , 1: EachCellCount
            if (rdoFixCellSize.Checked)
            {
                navigator.SelectSingleNode("//map_window/map_chart/grid_line_mode").SetValue("0");
                // save grid line value of mode
                double md = Double.TryParse(txtFixSizeMD.Text, out md) ? md : 1;
                double cd = Double.TryParse(txtFixSizeCD.Text, out cd) ? cd : 1;
                navigator.SelectSingleNode("//map_window/map_chart/grid_line_set/fix/md").SetValue(md.ToString());
                navigator.SelectSingleNode("//map_window/map_chart/grid_line_set/fix/cd").SetValue(cd.ToString());
            }
            else if (rdoCountSize.Checked)
            {
                navigator.SelectSingleNode("//map_window/map_chart/grid_line_mode").SetValue("1");
                // save grid line value of mode
                int md = Int32.TryParse(txtCountSizeMD.Text, out md) ? md : 2;
                int cd = Int32.TryParse(txtCountSizeCD.Text, out cd) ? cd : 2;
                navigator.SelectSingleNode("//map_window/map_chart/grid_line_set/count/md").SetValue(md.ToString());
                navigator.SelectSingleNode("//map_window/map_chart/grid_line_set/count/cd").SetValue(cd.ToString());
            }
            else
            {
                navigator.SelectSingleNode("//map_window/map_chart/grid_line_mode").SetValue("1");
                // save grid line value of mode
                int md = Int32.TryParse(txtCountSizeMD.Text, out md) ? md : 2;
                int cd = Int32.TryParse(txtCountSizeCD.Text, out cd) ? cd : 2;
                navigator.SelectSingleNode("//map_window/map_chart/grid_line_set/count/md").SetValue(md.ToString());
                navigator.SelectSingleNode("//map_window/map_chart/grid_line_set/count/cd").SetValue(cd.ToString());
            }

            // save bottom axes
            string bottom = cmbBottomAxes.SelectedItem.ToString().Trim();
            navigator.SelectSingleNode("//map_window/map_chart/bottom_axes").SetValue(bottom);

            // save md_inver
            if (chkMDInverse.Checked)
            {
                navigator.SelectSingleNode("//map_window/map_chart/md_inver").SetValue("true");
            }
            else
            {
                navigator.SelectSingleNode("//map_window/map_chart/md_inver").SetValue("false");
            }
            // save cd_inver
            if (chkCDInverse.Checked)
            {
                navigator.SelectSingleNode("//map_window/map_chart/cd_inver").SetValue("true");
            }
            else
            {
                navigator.SelectSingleNode("//map_window/map_chart/cd_inver").SetValue("false");
            }

            // save flawlegends
            // remove old flawlegend for add records
            if (navigator.Select("//map_window/flaw_legend").Count > 0 && _dtbFlawLegends.Rows.Count > 0)
            {
                XPathNavigator first = navigator.SelectSingleNode("//map_window/flaw_legend[1]");
                XPathNavigator last = navigator.SelectSingleNode("//map_window/flaw_legend[last()]");
                navigator.MoveTo(first);
                navigator.DeleteRange(last);
            }
            // save _dtbFlawLegends to xml
            for (int i = 0; i < _dtbFlawLegends.Rows.Count; i++)
            {
                string flawType = _dtbFlawLegends.Rows[i]["FlawType"].ToString();
                string flawName = _dtbFlawLegends.Rows[i]["Name"].ToString();
                string shape = _dtbFlawLegends.Rows[i]["Shape"].ToString();
                string color = _dtbFlawLegends.Rows[i]["Color"].ToString();
                navigator.SelectSingleNode("//config/map_window").AppendChildElement(string.Empty, "flaw_legend", string.Empty, null);
                // Move to last column element and add name, start, end value.
                navigator.SelectSingleNode("//config/map_window/flaw_legend[last()]").AppendChildElement(string.Empty, "flaw_type", string.Empty, flawType);
                navigator.SelectSingleNode("//config/map_window/flaw_legend[last()]").AppendChildElement(string.Empty, "name", string.Empty, flawName);
                navigator.SelectSingleNode("//config/map_window/flaw_legend[last()]").AppendChildElement(string.Empty, "color", string.Empty, color);
                navigator.SelectSingleNode("//config/map_window/flaw_legend[last()]").AppendChildElement(string.Empty, "shape", string.Empty, shape);
            }

            // save
            string map_path = PathHelper.MapConfigFolder + cmbMapConfigName.Text.Trim() + ".xml";
            try
            {
                document.Save(map_path);
                // Re binding cmbMapConfigName datasource
                List<string> mapConfigs = new List<string>();
                DirectoryInfo dirInfo = new DirectoryInfo(PathHelper.MapConfigFolder);
                FileInfo[] files = dirInfo.GetFiles("*.xml");
                foreach (FileInfo file in files)
                {
                    mapConfigs.Add(file.Name.ToString().Substring(0, file.Name.ToString().LastIndexOf(".")));
                }
                // Binding datasource for cmbMapConfigName and set default value.
                cmbMapConfigName.DataSource = mapConfigs;
                cmbMapConfigName.SelectedItem = ch.GetDefaultMapConfigName().Trim();
                MessageBox.Show("Success.");
            }
            catch
            {
                //MessageBox.Show("Fail.");
            }
        }