TaskList.cs
上传用户:hbhltzc
上传日期:2022-06-04
资源大小:1925k
文件大小:11k
源码类别:

xml/soap/webservice

开发平台:

Visual C++

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.IO;
  9. using System.Xml;
  10. namespace XmlNotepad {
  11.     public enum Severity { None, Hint, Warning, Error }
  12.     public delegate void NavigateEventHandler(object sender, Task task);
  13.     public partial class TaskList : UserControl {
  14.         public event NavigateEventHandler Navigate;
  15.         public event KeyEventHandler GridKeyDown;
  16.         ImageList imageList = new ImageList();
  17.         DataGridViewRow navigated;
  18.         public TaskList() {
  19.             InitializeComponent();
  20.             // NOTE: do not dispose this stream - it belongs to the image.
  21.             Stream stream = this.GetType().Assembly.GetManifestResourceStream("XmlNotepad.Resources.errorlist.bmp");
  22.             Image strip = Image.FromStream(stream);
  23.             imageList.Images.AddStrip(strip);
  24.             imageList.TransparentColor = Color.FromArgb(0, 255, 0);
  25.             dataGridView1.DoubleClick += new EventHandler(dataGridView1_DoubleClick);
  26.             dataGridView1.KeyDown += new KeyEventHandler(dataGridView1_KeyDown);
  27.         }
  28.         void dataGridView1_KeyDown(object sender, KeyEventArgs e) {
  29.             if (GridKeyDown != null) GridKeyDown(sender, e);
  30.         }
  31.         public Task this[int index] {
  32.             get {
  33.                 if (this.dataGridView1.Rows.Count == 0) return null;
  34.                 DataGridViewRow row = this.dataGridView1.Rows[index];
  35.                 return row.Tag as Task;
  36.             }
  37.             set {
  38.                 DataGridViewRow row = this.dataGridView1.Rows[index];
  39.                 row.SetValues(GetValues(value));
  40.                 row.Tag = value; // keep mapping to original task!
  41.             }
  42.         }
  43.         void AddRow(Task t) {
  44.         }
  45.         public ImageList Images {
  46.             get { return this.imageList; }
  47.             set { this.imageList = value; }
  48.         }
  49.         static object[] GetValues(Task t) {
  50.             return new object[] { t.SeverityImage, t.Description, t.FileName, t.Line, t.Column };
  51.         }
  52.         public int Add(Task t) {            
  53.             t.Parent = this;
  54.             int index = this.dataGridView1.Rows.Add(GetValues(t));
  55.             DataGridViewRow row = this.dataGridView1.Rows[index];
  56.             row.Tag = t; // keep mapping to original task!
  57.             return index;
  58.         }
  59.         public int GetTaskIndex(Task t) {
  60.             for (int i = 0, n = this.Count; i < n; i++) {
  61.                 DataGridViewRow row = this.dataGridView1.Rows[i];           
  62.                 if (row.Tag == t) {
  63.                     return i;
  64.                 }
  65.             }
  66.             return -1;
  67.         }
  68.         public bool Remove(Task t) {
  69.             int i = GetTaskIndex(t);
  70.             if (i >= 0) {
  71.                 t.Parent = null;
  72.                 if (navigated != null && navigated.Index == i)
  73.                     navigated = null;
  74.                 this.dataGridView1.Rows.RemoveAt(i);
  75.                 return true;
  76.             }
  77.             return false;
  78.         }
  79.         public bool Contains(Task t) {
  80.             foreach (DataGridViewRow row in this.dataGridView1.Rows) {
  81.                 Task o = row.Tag as Task;
  82.                 if (o != null && o.Equals(t)) {
  83.                     return true;
  84.                 }
  85.             }
  86.             return false;
  87.         }
  88.         public void Insert(int index, Task t) {
  89.             t.Parent = this;
  90.             this.dataGridView1.Rows.Insert(index, GetValues(t));
  91.             DataGridViewRow row = this.dataGridView1.Rows[index];
  92.             row.Tag = t;
  93.         }
  94.         public void Clear() {
  95.             navigated = null;
  96.             foreach (DataGridViewRow row in this.dataGridView1.Rows) {
  97.                 Task t = row.Tag as Task;
  98.                 t.Parent = null;
  99.             }
  100.             this.dataGridView1.Rows.Clear();
  101.         }
  102.         public int Count {
  103.             get { return this.dataGridView1.Rows.Count; }
  104.         }
  105.         internal void OnTaskChanged(Task t) {
  106.             int i = GetTaskIndex(t);
  107.             if (i >= 0) {
  108.                 t.Parent = this;
  109.                 this.dataGridView1.Rows[i].SetValues(GetValues(t));
  110.             }
  111.         }
  112.         public bool NavigateNextError() {
  113.             int index = -1;
  114.             if (navigated != null) {
  115.                 index = navigated.Index;
  116.             }
  117.             if (index + 1 >= this.dataGridView1.Rows.Count) {
  118.                 index = -1; // wrap around
  119.             }
  120.             if (index + 1 < this.dataGridView1.Rows.Count) {
  121.                 this.dataGridView1.ClearSelection();
  122.                 this.dataGridView1.Rows[index + 1].Selected = true;
  123.                 NavigateSelectedError();
  124.                 return true;
  125.             } 
  126.             return false;
  127.         }
  128.         public void NavigateSelectedError() {
  129.             if (this.dataGridView1.SelectedRows.Count > 0) {
  130.                 DataGridViewRow row = this.dataGridView1.SelectedRows[0];
  131.                 Task task = this[row.Index];
  132.                 if (task != null && this.Navigate != null) {
  133.                     Navigate(this, task);
  134.                     navigated = row;
  135.                 }
  136.             }
  137.         }
  138.         void dataGridView1_DoubleClick(object sender, EventArgs e) {
  139.             NavigateSelectedError();
  140.         }
  141.         public void Save(string filename) {
  142.             XmlWriterSettings settings = new XmlWriterSettings();
  143.             Utilities.InitializeWriterSettings(settings, this.Site as IServiceProvider);
  144.             using (XmlWriter w = XmlWriter.Create(filename, settings)) {
  145.                 w.WriteStartElement("ErrorList");
  146.                 foreach (DataGridViewRow row in this.dataGridView1.Rows) {
  147.                     Task t = row.Tag as Task;
  148.                     w.WriteStartElement("Error");
  149.                     w.WriteElementString("Severity", t.Severity.ToString());
  150.                     w.WriteElementString("Description", t.Description);
  151.                     w.WriteElementString("Line", t.Line.ToString());
  152.                     w.WriteElementString("Column", t.Column.ToString());
  153.                     w.WriteElementString("FileName", t.FileName.ToString());
  154.                     w.WriteEndElement();
  155.                 }
  156.             }
  157.         }
  158.     }
  159.     public class Task {
  160.         Severity severity;
  161.         string description;
  162.         string fileName;
  163.         int line;
  164.         int column;
  165.         TaskList parent;
  166.         object data;
  167.         Image img;
  168.         internal Task() {
  169.         }
  170.         public Task(Severity sev, string description, string fileName, int line, int column, object data) {
  171.             this.severity = sev;
  172.             this.description = description;
  173.             this.fileName = fileName;
  174.             this.line = line;
  175.             this.column = column;
  176.             this.data = data;
  177.         }
  178.         public TaskList Parent {
  179.             get { return this.parent; }
  180.             set { this.parent = value; }
  181.         }
  182.         [System.ComponentModel.Browsable(false)]
  183.         public Severity Severity {
  184.             get { return this.severity; }
  185.             set {
  186.                 if (this.severity != value) {
  187.                     this.severity = value;
  188.                     OnChanged();
  189.                 }
  190.             }
  191.         }
  192.         public Image SeverityImage {
  193.             get {
  194.                 if (img == null) {
  195.                     if (this.parent != null && this.parent.Images != null) {
  196.                         return this.parent.Images.Images[(int)this.Severity];
  197.                     }
  198.                 }
  199.                 return img; 
  200.             }
  201.             set {
  202.                 this.img = value;
  203.             }
  204.         }
  205.         public string Description {
  206.             get { return this.description; }
  207.             set {
  208.                 if (this.description != value) {
  209.                     this.description = value;
  210.                     OnChanged();
  211.                 }
  212.             }
  213.         }
  214.         public string FileName {
  215.             get { return this.fileName; }
  216.             set {
  217.                 if (this.fileName != value) {
  218.                     this.fileName = value;
  219.                     OnChanged();
  220.                 }
  221.             }
  222.         }
  223.         public int Line {
  224.             get { return this.line; }
  225.             set {
  226.                 if (this.line != value) {
  227.                     this.line = value;
  228.                     OnChanged();
  229.                 }
  230.             }
  231.         }
  232.         public int Column {
  233.             get { return this.column; }
  234.             set {
  235.                 if (this.column != value) {
  236.                     this.column = value;
  237.                     OnChanged();
  238.                 } 
  239.             }
  240.         }
  241.         public object Data {
  242.             get { return this.data; }
  243.             set { this.data = value; }
  244.         }
  245.         private void OnChanged() {
  246.             if (this.parent != null)
  247.                 this.parent.OnTaskChanged(this);
  248.         }
  249.         public override bool Equals(object obj) {
  250.             if (obj is Task) {
  251.                 Task other = (Task)obj;
  252.                 return this.severity == other.severity && this.description == other.description &&
  253.                     this.fileName == other.fileName && this.line == other.line && this.column == other.column;
  254.             }
  255.             return false;
  256.         }
  257.         public override int GetHashCode() {
  258.             int hash1 = 0;
  259.             int hash2 = 0;
  260.             if (this.description != null) hash1 = this.description.GetHashCode();
  261.             if (this.fileName != null) hash2 = this.fileName.GetHashCode();
  262.             int hash = hash1 ^ hash2 ^ (int)this.severity ^ this.line ^ this.column;
  263.             return hash;
  264.         }
  265.     }
  266.     public class TaskHandler : ErrorHandler {
  267.         TaskList list;
  268.         System.Collections.Hashtable unique;
  269.         IList<Task> errors;
  270.         public TaskHandler(TaskList list){
  271.             this.list = list;
  272.         }
  273.         public void Start() {
  274.             this.unique = new System.Collections.Hashtable();
  275.             this.errors = new List<Task>();
  276.         }
  277.         public override void HandleError(Severity sev, string reason, string filename, int line, int col, object data) {
  278.             Task nt = new Task(sev, reason, filename, line, col, data);
  279.             if (!unique.Contains(nt)) {
  280.                 unique[nt] = nt;
  281.                 errors.Add(nt); // preserve order
  282.             }
  283.         }
  284.         public void Finish() {
  285.             // Now merge the lists.
  286.             IList<Task> copy = new List<Task>();
  287.             for (int i = 0, n = list.Count; i < n; i++) {
  288.                 Task t = list[i];
  289.                 copy.Add(t);
  290.             }
  291.             // Remove tasks that are no longer reported.
  292.             foreach (Task t in copy){
  293.                 if (!unique.Contains(t)) {
  294.                     this.list.Remove(t);
  295.                 }                
  296.             }
  297.             // Remove any new tasks that have appeared.
  298.             for (int i = 0, n = errors.Count; i < n; i++) {
  299.                 Task t = errors[i];
  300.                 if (!list.Contains(t)) {
  301.                     this.list.Insert(i, t);
  302.                 }
  303.             }
  304.         }
  305.     }
  306. }