123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615 |
- using System.Collections.Generic;
- using System.Data;
- using Tofly.Core.Stereotype;
- using Tofly.CoreUI.Win.Form.Forms;
- using Tofly.SearchUI.Select;
- using Tofly.SearchUI.Common;
- using Tofly.Core.Context.Support;
- using Tofly.CoreUI.Message;
- using System.Xml;
- using System;
- using DevExpress.XtraGrid.Columns;
- namespace Tofly.SearchUI.Win
- {
-
-
-
- [Component(IsSingleton = "false", Name = "RemoteMappingSetting")]
- public partial class RemoteMappingSetting : FormClass, IRemoteMappingSetting
- {
- private readonly string xmlFilePath = RemoteMappingHelper.xmlFilePath;
- private DataTable dataTable = null;
- private XmlDocument xmlDoc = null;
- private string wordDirectory = string.Empty;
- private string lastValue = string.Empty;
- public RemoteMappingSetting()
- {
- InitializeComponent();
- }
- private void buttonSelectOnMap_Click(object sender, System.EventArgs e)
- {
-
- }
- private void FormRemoteMapping_Load(object sender, EventArgs e)
- {
-
- if (!System.IO.File.Exists(xmlFilePath))
- {
- MessageManager.Show(MessageType.Information, "请加载名为'RemoteMappingSetting.xml'的配置文件!", "提示");
- this.Close();
- return;
- }
- BindDataAndSetDisplaySchema();
- this.textFolderPath.Text = wordDirectory;
- this.calcEdit.Text = this.xmlDoc.SelectSingleNode("//MaxSheetCount").InnerText;
- this.comboBox.Text = this.xmlDoc.SelectSingleNode("//DataType").InnerText;
-
-
- }
- private void buttonOK_Click(object sender, System.EventArgs e)
- {
- if (string.IsNullOrEmpty(this.textFolderPath.Text))
- {
- MessageManager.Show(MessageType.Information, "请选择文件目录!", "提示");
- return;
- }
- if (string.IsNullOrEmpty(this.comboBox.Text))
- {
- MessageManager.Show(MessageType.Information, "请选择数据类型!", "提示");
- return;
- }
- if (string.IsNullOrEmpty(this.textFolderPath.Text))
- {
- return;
- }
- if (!CheckDataIsValid())
- {
- return;
- }
-
- SaveEditValue(this.textFolderPath.Text);
- this.Close();
- }
- private bool CheckDataIsValid()
- {
- DataTable dataTable = RemoveInvalidRowsAndFillValueForEmptyCell(this.dataTable);
- return CheckIsValid(dataTable);
- }
-
- private DataTable RemoveInvalidRowsAndFillValueForEmptyCell(DataTable dataTable)
- {
- if (dataTable == null)
- {
- return null;
- }
- DataTable copyTable = dataTable.Clone();
- for (int i = 0; i < this.dataTable.Rows.Count; i++)
- {
- DataRow dataRow = dataTable.Rows[i];
- if (CheckRowIsEmpty(dataRow))
- {
- continue;
- }
-
- DataRow newRow = copyTable.NewRow();
- if (CheckFirstCellIsEmpty(dataRow))
- {
- newRow[0] = dataRow[0];
- newRow[1] = 0;
- newRow[2] = dataRow[2];
- }
- else if (CheckSecondCellIsEmpty(dataRow))
- {
- newRow[0] = dataRow[0];
- newRow[2] = long.MaxValue;
- newRow[1] = dataRow[1];
- }
- else
- {
- newRow[0] = dataRow[0];
- newRow[1] = dataRow[1];
- newRow[2] = dataRow[2];
- }
-
- copyTable.Rows.Add(newRow);
- }
- return copyTable;
- }
-
- private bool CheckIsValid(DataTable dataTable)
- {
- foreach (DataRow item in dataTable.Rows)
- {
- if (!CheckRowIsValid(item))
- {
- return false;
- }
- }
- for (int i = 0; i < dataTable.Rows.Count - 1; i++)
- {
- for (int j = 0; j < dataTable.Rows.Count - i - 1; j++)
- {
- DataRow rowOne = dataTable.Rows[j];
- DataRow rowTwo = dataTable.Rows[j + 1];
- if (CheckRoleHasIntersect(rowOne, rowTwo) || CheckRoleHasIntersect(rowTwo, rowOne))
- {
- MessageManager.Show(MessageType.Information, "请检查值是否有重叠!", "提示");
- return false;
- }
- else
- {
- if (GetFirstCellValueL(rowOne) > GetFirstCellValueL(rowTwo))
- {
-
- DataRow tempRow = dataTable.NewRow();
- tempRow[0] = rowOne[0];
- tempRow[1] = rowOne[1];
- tempRow[2] = rowOne[2];
- dataTable.Rows.InsertAt(tempRow, j + 2);
- dataTable.Rows.RemoveAt(j);
- }
- }
- }
- }
- return true;
- }
-
- bool CheckCellValueIsValid(object value)
- {
- if (value == null || !CanConvertToNumber(value))
- {
- return false;
- }
- if (CanConvertToNumber(value))
- {
- long myValue = long.Parse(value.ToString());
- if (myValue < 0)
- {
- return false;
- }
- }
- return true;
- }
-
- bool CheckRoleHasIntersect(DataRow rowOne, DataRow rowTwo)
- {
- long firstCellValue = long.MinValue;
- bool firstCanChange = long.TryParse(rowOne[1].ToString(), out firstCellValue);
- long secondCellValue = long.MinValue;
- bool secondCanChange = long.TryParse(rowOne[2].ToString(), out secondCellValue);
- long rowTwoFirstCellValue = long.Parse(rowTwo[1].ToString());
- if (rowTwoFirstCellValue > firstCellValue && rowTwoFirstCellValue < secondCellValue)
- {
- return true;
- }
- long rowTwoSecondCellValue = long.Parse(rowTwo[2].ToString());
- if (rowTwoSecondCellValue > firstCellValue && rowTwoSecondCellValue < secondCellValue)
- {
- return true;
- }
- return false;
- }
- private void gridView_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
- {
- }
- private void gridView_MouseEnter(object sender, EventArgs e)
- {
- }
- private bool IsFirstCell(long rowIndex, long columnIndex)
- {
- return rowIndex == 0 && columnIndex == 1;
- }
-
- private bool IsFirstColumn(int columnCount)
- {
- if (columnCount == 1)
- {
- return true;
- }
- return false;
- }
-
- private bool IsLastRow(int rowCount)
- {
- if (rowCount == this.dataTable.Rows.Count - 1)
- {
- return true;
- }
- return false;
- }
-
- private bool IsFirstRow(int rowCount)
- {
- if (rowCount == 0)
- {
- return true;
- }
- return false;
- }
-
- private string ConvertToString(object value)
- {
- try
- {
- if (value == null) return string.Empty;
- return Convert.ToString(value);
- }
- catch (Exception)
- {
- return string.Empty;
- }
- }
- private long GetFirstCellValueL(DataRow dataRow)
- {
- object firstCell = GetFirstCellValue(dataRow);
- if (firstCell != null)
- {
- return long.Parse(firstCell.ToString());
- }
- return long.MinValue;
- }
- private long GetSecondCellValueL(DataRow dataRow)
- {
- object secondCell = GetSecondCellValue(dataRow);
- if (secondCell != null)
- {
- return long.Parse(secondCell.ToString());
- }
- return long.MinValue;
- }
-
- private bool CheckRowIsEmpty(DataRow dataRow)
- {
- if (dataRow == null) return true;
-
- string firstCellValue = ConvertToString(GetFirstCellValue(dataRow));
- string secondCellValue = ConvertToString(GetSecondCellValue(dataRow));
- if (string.IsNullOrEmpty(firstCellValue) && string.IsNullOrEmpty(secondCellValue))
- {
- return true;
- }
- return false;
- }
-
- private bool CheckRowIsValid(DataRow dataRow)
- {
- long firstCellValue = long.MinValue;
- bool firstCanChange = long.TryParse(dataRow[1].ToString(), out firstCellValue);
- long secondCellValue = long.MinValue;
- bool secondCanChange = long.TryParse(dataRow[2].ToString(), out secondCellValue);
- if (!firstCanChange || !secondCanChange)
- {
- MessageManager.Show(MessageType.Information, "请检查是否含有非数值类型的值!", "提示");
- return false;
- }
- if (firstCellValue - secondCellValue > 0)
- {
- MessageManager.Show(MessageType.Information, "请检查值是否有重叠部分!", "提示");
- return false;
- }
- return true;
- }
-
- private bool CheckFirstCellIsEmpty(DataRow dataRow)
- {
- if (dataRow == null) return true;
-
- string firstCellValue = ConvertToString(GetFirstCellValue(dataRow));
- if (string.IsNullOrEmpty(firstCellValue))
- {
- return true;
- }
- return false;
- }
-
- private bool CheckSecondCellIsEmpty(DataRow dataRow)
- {
- if (dataRow == null) return true;
-
- string SecondCellValue = ConvertToString(GetSecondCellValue(dataRow));
- if (string.IsNullOrEmpty(SecondCellValue))
- {
- return true;
- }
- return false;
- }
-
- private bool IsLastColumn(int columnCount)
- {
- if (columnCount == 2)
- {
- return true;
- }
- return false;
- }
-
- private object GetFirstCellValue(DataRow dataRow)
- {
- return dataRow[1];
- }
-
- private object GetSecondCellValue(DataRow dataRow)
- {
- return dataRow[2];
- }
- private bool IsLastCell(long rowIndex, long columnIndex)
- {
- return (rowIndex == this.dataTable.Rows.Count - 1) && (columnIndex == this.dataTable.Columns.Count - 1);
- }
- private void calcEdit_EditValueChanged(object sender, EventArgs e)
- {
- int value;
- if (!int.TryParse(this.calcEdit.Value.ToString(), out value))
- {
- MessageManager.Show(MessageType.Information, "必须整形的!", "提示");
- }
- }
- private void buttonCancel_Click(object sender, System.EventArgs e)
- {
- this.Close();
- }
- #region 内部调用函数
-
- private void SaveEditValue(string path)
- {
-
- if (SaveDataTableToXmlConfig(this.dataTable, this.xmlDoc, path))
- MessageManager.Show(MessageType.Information, "保存成功!", "提示");
- else MessageManager.Show(MessageType.Information, "保存失败!", "提示");
- }
-
- private DataTable ReadXmlConfig(string filePath)
- {
- DataTable dataTable = CreateTable();
- xmlDoc = Tofly.Core.Util.XmlUtils.OpenXml(filePath);
-
- wordDirectory = xmlDoc.SelectSingleNode("//DirectoryPath").InnerText;
- XmlNodeList nodeList = xmlDoc.SelectNodes("//MatchTable/MatchItem");
- if (nodeList == null) return null;
- foreach (XmlNode item in nodeList)
- {
- DataRow dataRow = dataTable.NewRow();
- dataRow["ColumnScale"] = item.Attributes["scale"].Value;
- if (item.Attributes["minValue"].Value.Equals("MINI"))
- {
- dataRow["ColumnMiniValue"] = string.Empty;
- }
- else
- {
- dataRow["ColumnMiniValue"] = item.Attributes["minValue"].Value;
- }
- if (item.Attributes["maxValue"].Value.Equals("MAX"))
- {
- dataRow["ColumnMaxiValue"] = string.Empty;
- }
- else
- {
- dataRow["ColumnMaxiValue"] = item.Attributes["maxValue"].Value;
- }
- dataTable.Rows.Add(dataRow);
- }
- return dataTable;
- }
-
- private bool CanConvertToNumber(object value)
- {
- try
- {
- Convert.ToInt32(value);
- }
- catch
- {
- return false;
- }
- return true;
- }
-
- private void BindDataAndSetDisplaySchema()
- {
- dataTable = ReadXmlConfig(xmlFilePath);
- if (dataTable != null) this.gridControl.DataSource = dataTable;
- for (int i = 0; i < this.gridView.Columns.Count; i++)
- {
- GridColumn item = this.gridView.Columns[i];
- if (i == 0)
- {
- item.OptionsColumn.ReadOnly = true;
- }
- item.OptionsColumn.AllowSort = DevExpress.Utils.DefaultBoolean.False;
- }
- }
-
- private bool SaveDataTableToXmlConfig(DataTable dataTable, XmlDocument xmlDoc, string dataPath)
- {
- try
- {
- if (xmlDoc == null) return false;
- XmlNode xmlNode = xmlDoc.SelectSingleNode("//DirectoryPath");
- if (xmlNode == null) return false;
- xmlNode.InnerText = dataPath;
-
- XmlNode sheetCount = xmlDoc.SelectSingleNode("//MaxSheetCount");
- if (sheetCount != null)
- {
- sheetCount.InnerText = this.calcEdit.Text;
- }
- XmlNode dataType = xmlDoc.SelectSingleNode("//DataType");
- if (dataType != null)
- {
- dataType.InnerText = this.comboBox.Text;
- }
-
- for (int i = 0; i < dataTable.Rows.Count; i++)
- {
- DataRow dataRow = dataTable.Rows[i];
- if (dataRow != null)
- {
- string scale = dataRow["ColumnScale"].ToString();
- XmlNode nodeItem = xmlDoc.SelectSingleNode("//MatchItem[@scale='" + scale + "']");
- string miniValue = dataRow["ColumnMiniValue"].ToString();
- string maxValue = dataRow["ColumnMaxiValue"].ToString();
- if (i == 0 && string.IsNullOrEmpty(miniValue))
- {
- nodeItem.Attributes["minValue"].Value = string.Empty;
- }
- else
- {
- nodeItem.Attributes["minValue"].Value = miniValue;
- }
- if (i == dataTable.Rows.Count - 1 && string.IsNullOrEmpty(maxValue))
- {
- nodeItem.Attributes["maxValue"].Value = string.Empty;
- }
- else
- {
- nodeItem.Attributes["maxValue"].Value = maxValue;
- }
- }
- }
- xmlDoc.Save(this.xmlFilePath);
- return true;
- }
- catch (System.Exception)
- {
- return false;
- }
- }
-
- private DataTable CreateTable()
- {
- DataTable dataTable = new DataTable();
- DataColumn scaleColumn = new DataColumn();
- scaleColumn.ColumnName = "ColumnScale";
- scaleColumn.Caption = "栅格数据比例尺";
- DataColumn minValueColumn = new DataColumn();
- minValueColumn.ColumnName = "ColumnMiniValue";
- minValueColumn.Caption = "最小值";
- DataColumn maxValueColumn = new DataColumn();
- maxValueColumn.ColumnName = "ColumnMaxiValue";
- maxValueColumn.Caption = "最大值";
- dataTable.Columns.Add(scaleColumn);
- dataTable.Columns.Add(minValueColumn);
- dataTable.Columns.Add(maxValueColumn);
- return dataTable;
- }
- private bool OneIsLargerThanTwo(long one, long two)
- {
- if (one - two > 0)
- {
- return true;
- }
- return false;
- }
- #endregion
- #region IRemoteMappingSetting 成员
- public bool GridEdit
- {
- get
- {
- return this.groupControl.Enabled;
- }
- set
- {
- groupControl.Enabled = value;
- }
- }
- public bool MaxEdit
- {
- get
- {
- return this.calcEdit.Enabled;
- }
- set
- {
- calcEdit.Enabled = value;
- }
- }
- #endregion
- private void textFolderPath_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
- {
- IFolderDialog folderdialog = ContextRegistry.GetContext().GetObject("IFolderDialog") as IFolderDialog;
- string path = folderdialog.GetFolderPath("请选择文件目录?", true);
- if (!string.IsNullOrEmpty(path))
- this.textFolderPath.Text = path;
- }
- }
- }
|