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

xml/soap/webservice

开发平台:

Visual C++

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Text;
  6. using System.Windows.Forms;
  7. using System.Xml.Schema;
  8. using System.Xml;
  9. using System.Diagnostics;
  10. namespace XmlNotepad {
  11.     /// <summary>
  12.     /// FormSchemas provides a simple grid view interface on top of the SchemaCache and provides
  13.     /// a way to add and remove schemas from the cache.  You can also "disable" certain schemas
  14.     /// from being used in validation by checking the disabled checkbox next to the schema.
  15.     /// All this is persisted in the Settings class so it's remembered across sessions.
  16.     /// </summary>
  17.     public partial class FormSchemas : Form {
  18.         SchemaCache cache;
  19.         UndoManager undoManager = new UndoManager(1000);
  20.         bool inUndoRedo;
  21.         // private cache so we can get the "old" values for undo/redo support.
  22.         List<SchemaItem> items = new List<SchemaItem>();
  23.         public FormSchemas() {
  24.             InitializeComponent();
  25.             DataGridViewBrowseCell template = new DataGridViewBrowseCell();
  26.             template.UndoManager = this.undoManager;
  27.             template.OpenFileDialog = this.openFileDialog1;
  28.             this.columnBrowse.CellTemplate = template;
  29.             this.dataGridView1.KeyDown += new KeyEventHandler(dataGridView1_KeyDown);
  30.             this.dataGridView1.CellValidating += new DataGridViewCellValidatingEventHandler(dataGridView1_CellValidating);
  31.             this.undoManager.StateChanged += new EventHandler(undoManager_StateChanged);            
  32.         }
  33.         void undoManager_StateChanged(object sender, EventArgs e) {
  34.             this.UpdateMenuState();
  35.         }
  36.         protected override void OnLoad(EventArgs e) {            
  37.             HelpProvider hp = this.Site.GetService(typeof(HelpProvider)) as HelpProvider;
  38.             if (hp != null) {
  39.                 hp.SetHelpKeyword(this, "Schemas");
  40.                 hp.SetHelpNavigator(this, HelpNavigator.KeywordIndex);
  41.             }
  42.             UpdateMenuState();
  43.             LoadSchemas();
  44.             this.dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
  45.             this.dataGridView1.RowsAdded += new DataGridViewRowsAddedEventHandler(dataGridView1_RowsAdded);
  46.         }
  47.         void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e) {
  48.             if (!inUndoRedo) {
  49.                 SchemaDialogNewRow cmd = new SchemaDialogNewRow(this.dataGridView1, this.items, this.dataGridView1.Rows[e.RowIndex]);
  50.                 Push(cmd);
  51.             }
  52.             return;
  53.         }
  54.         void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) {
  55.             if (e.ColumnIndex == 2) {
  56.                 DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
  57.                 DataGridViewCell cell = row.Cells[2];
  58.                 string filename = cell.Value as string;
  59.                 if (!inUndoRedo) {
  60.                     SchemaDialogNewRow newRow = this.undoManager.Peek() as SchemaDialogNewRow;
  61.                     SchemaDialogEditCommand cmd = new SchemaDialogEditCommand(this.dataGridView1, this.items, row, filename);
  62.                     if (newRow != null) {
  63.                         this.undoManager.Pop(); // merge new row command with this edit command.
  64.                         cmd.IsNewRow = true;
  65.                     }
  66.                     Push(cmd);
  67.                 }
  68.             }
  69.         }
  70.         void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
  71.             if (e.ColumnIndex == 2) {
  72.                 DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
  73.                 string filename = e.FormattedValue as string;
  74.                 if (string.IsNullOrEmpty(filename))
  75.                     return;
  76.                 if (SchemaDialogCommand.ValidateSchema(row, filename) == null) {
  77.                     e.Cancel = true;
  78.                 }
  79.             }
  80.         }
  81.         void LoadSchemas() {
  82.             if (this.cache == null) {
  83.                 if (this.Site != null) {
  84.                     this.cache = (SchemaCache)this.Site.GetService(typeof(SchemaCache));
  85.                 }
  86.             }
  87.             items.Clear();
  88.             DataGridViewRowCollection col = this.dataGridView1.Rows;
  89.             col.Clear();
  90.             if (this.cache != null) {
  91.                 foreach (CacheEntry e in this.cache.GetSchemas()) {
  92.                     Uri uri = e.Location;
  93.                     string filename = uri.IsFile ? uri.LocalPath : uri.AbsoluteUri;
  94.                     SchemaItem item = new SchemaItem(e.Disabled, e.TargetNamespace, filename);
  95.                     items.Add(item);
  96.                     int i = col.Add(item.Values);
  97.                     col[i].Tag = item;
  98.                 }
  99.             }
  100.         }
  101.         protected override void OnClosing(CancelEventArgs e) {
  102.             if (!Cancel())
  103.                 e.Cancel = true;
  104.         }
  105.         public bool Cancel() {
  106.             if (this.undoManager.CanUndo) {
  107.                 if (MessageBox.Show(this, SR.DiscardChanges, SR.DiscardChangesCaption, MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == DialogResult.Cancel) {
  108.                     return false;
  109.                 }
  110.             }
  111.             return true;
  112.         }
  113.         
  114.         private void buttonOk_Click(object sender, EventArgs e) {
  115.             Ok();
  116.         }
  117.         public void Ok() {
  118.             if (Commit()) {
  119.                 this.DialogResult = DialogResult.OK;
  120.             }
  121.         }
  122.         public bool Commit() {
  123.             IList<CacheEntry> oldList = cache.GetSchemas();
  124.             XmlResolver resolver = this.cache.Resolver;
  125.             foreach (DataGridViewRow row in this.dataGridView1.Rows) {
  126.                 string filename = row.Cells[2].Value as string;
  127.                 SchemaItem item = (SchemaItem)row.Tag;
  128.                 if (!string.IsNullOrEmpty(filename)) {
  129.                     CacheEntry ce = this.cache.FindSchemaByUri(filename);
  130.                     bool isNew = (ce == null);
  131.                     if (ce == null || ce.Schema == null) {
  132.                         try {
  133.                             XmlSchema s = resolver.GetEntity(new Uri(filename), "", typeof(XmlSchema)) as XmlSchema;
  134.                             if (ce == null) {
  135.                                 ce = this.cache.Add(s);
  136.                             } else {
  137.                                 ce.Schema = s;
  138.                             }
  139.                         } catch (Exception e) {
  140.                             DialogResult rc = MessageBox.Show(this, string.Format(SR.SchemaLoadError, filename, e.Message),
  141.                                 SR.SchemaError, MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
  142.                             if (rc == DialogResult.Cancel) {
  143.                                 row.Selected = true;
  144.                                 return false;
  145.                             }
  146.                         }
  147.                     } 
  148.                     if (!isNew){
  149.                         oldList.Remove(ce);
  150.                     }
  151.                     if (row.Cells[0].Value != null) {
  152.                         ce.Disabled = (bool)row.Cells[0].Value;
  153.                     }
  154.                 }
  155.             }            
  156.             // Remove schemas from the cache that have been removed from the grid view.
  157.             foreach (CacheEntry toRemove in oldList) {
  158.                 cache.Remove(toRemove);
  159.             }
  160.             this.undoManager.Clear();
  161.             return true;
  162.         }
  163.         private void clearToolStripMenuItem_Click(object sender, EventArgs e) {
  164.             foreach (DataGridViewRow row in this.dataGridView1.Rows) {
  165.                 row.Selected = true;
  166.             }
  167.             Push(new SchemaDialogCutCommand(this.dataGridView1, this.items));
  168.         }
  169.         private void addSchemasToolStripMenuItem_Click(object sender, EventArgs e) {
  170.             this.openFileDialog1.Multiselect = true;
  171.             if (this.openFileDialog1.ShowDialog(this) == DialogResult.OK) {
  172.                 Push(new SchemaDialogAddFiles(this.dataGridView1, this.items, this.openFileDialog1.FileNames));                
  173.             }
  174.         }
  175.         void Push(Command cmd) {
  176.             this.inUndoRedo = true;
  177.             undoManager.Push(cmd);
  178.             UpdateMenuState();
  179.             this.inUndoRedo = false;
  180.         }
  181.         void dataGridView1_KeyDown(object sender, KeyEventArgs e) {
  182.             bool isControl = e.Modifiers == Keys.Control;
  183.             bool isGridEditing = this.dataGridView1.EditingControl != null;
  184.             switch (e.KeyCode & ~Keys.Modifiers){
  185.                 case Keys.V:
  186.                     if (isControl && !isGridEditing) {
  187.                         pasteToolStripMenuItem_Click(this, e);
  188.                         e.Handled = true;
  189.                     }
  190.                     break;
  191.                 case Keys.X:
  192.                     if (isControl && !isGridEditing) {
  193.                         cutToolStripMenuItem_Click(this, e);
  194.                         e.Handled = true;
  195.                     }
  196.                     break;
  197.                 case Keys.C:
  198.                     if (isControl && !isGridEditing) {
  199.                         copyToolStripMenuItem_Click(this, e);
  200.                         e.Handled = true;
  201.                     }
  202.                     break;
  203.                 case Keys.Delete:
  204.                     if (!isGridEditing) {
  205.                         deleteToolStripMenuItem_Click(this, e);
  206.                         e.Handled = true;
  207.                     }
  208.                     break;
  209.                 case Keys.Enter:
  210.                     if (!isGridEditing) {
  211.                         e.Handled = true;
  212.                         Ok();
  213.                     }
  214.                     break;
  215.                 case Keys.Escape:
  216.                     if (!isGridEditing) {
  217.                         e.Handled = true;
  218.                         Close();
  219.                     }
  220.                     break;
  221.             }
  222.         }
  223.         private void cutToolStripMenuItem_Click(object sender, EventArgs e) {            
  224.             SchemaDialogCutCommand cmd = new SchemaDialogCutCommand(this.dataGridView1, this.items);
  225.             Push(cmd);
  226.             if (!string.IsNullOrEmpty(cmd.Clip)) {
  227.                 Clipboard.SetText(cmd.Clip);
  228.             }
  229.         }
  230.         private void copyToolStripMenuItem_Click(object sender, EventArgs e) {
  231.             SchemaDialogCutCommand cmd = new SchemaDialogCutCommand(this.dataGridView1, this.items);
  232.             StringBuilder clip = new StringBuilder();
  233.             cmd.ProcessSelectedRows(delegate(DataGridViewRow row, SchemaItem item) {
  234.                cmd.AddEscapedUri(clip, row.Cells[2].Value as string);
  235.             });
  236.             if (clip.Length > 0) {
  237.                 Clipboard.SetText(clip.ToString());
  238.             }
  239.         }
  240.         private void pasteToolStripMenuItem_Click(object sender, EventArgs e) {
  241.             string text = Clipboard.GetText();
  242.             if (!string.IsNullOrEmpty(text)) {
  243.                 Push(new SchemaDialogAddFiles(this.dataGridView1, this.items,
  244.                     text.Split(new char[] { 'r', 'n' }, StringSplitOptions.RemoveEmptyEntries)));
  245.             }
  246.         }
  247.         private void deleteToolStripMenuItem_Click(object sender, EventArgs e) {
  248.             Push(new SchemaDialogCutCommand(this.dataGridView1, this.items));        
  249.         }
  250.         void UpdateMenuState() {
  251.             this.undoToolStripMenuItem.Enabled = this.undoManager.CanUndo;
  252.             this.redoToolStripMenuItem.Enabled = this.undoManager.CanRedo;
  253.         }
  254.         private void undoToolStripMenuItem_Click(object sender, EventArgs e) {
  255.             inUndoRedo = true;
  256.             this.undoManager.Undo();
  257.             UpdateMenuState();
  258.             inUndoRedo = false;
  259.         }
  260.         private void redoToolStripMenuItem_Click(object sender, EventArgs e) {
  261.             inUndoRedo = true;
  262.             this.undoManager.Redo();
  263.             UpdateMenuState();
  264.             inUndoRedo = false;
  265.         }
  266.     }
  267.     public class SchemaItem {
  268.         bool disabled;
  269.         string targetNamespace;
  270.         string filename;
  271.         XmlSchema schema;
  272.         public SchemaItem() {
  273.         }
  274.         public SchemaItem(bool disabled, string targetNamespace, string filename) {
  275.             this.disabled = disabled;
  276.             this.targetNamespace = targetNamespace;
  277.             this.filename = filename;
  278.         }
  279.         public bool Disabled {
  280.             get { return disabled; }
  281.             set { disabled = value; }
  282.         }
  283.         public string TargetNamespace {
  284.             get { return targetNamespace; }
  285.             set { targetNamespace = value; }
  286.         }
  287.         public string Filename {
  288.             get { return filename; }
  289.             set { filename = value; }
  290.         }
  291.         public XmlSchema Schema {
  292.             get { return schema; }
  293.             set { schema = value; }
  294.         }
  295.         public object[] Values {
  296.             get { return new object[] { this.disabled, this.targetNamespace, this.filename }; }
  297.         }
  298.     }
  299.     public class DataGridViewBrowseCell : DataGridViewButtonCell {
  300.         OpenFileDialog fd;
  301.         UndoManager undoManager;
  302.         public DataGridViewBrowseCell(OpenFileDialog fd, UndoManager um) {
  303.             this.UseColumnTextForButtonValue = true;
  304.             this.undoManager = um;
  305.             this.fd = fd;
  306.         }
  307.         public DataGridViewBrowseCell() {
  308.             this.UseColumnTextForButtonValue = true;
  309.         }
  310.         public UndoManager UndoManager {
  311.             get { return this.undoManager; }
  312.             set { this.undoManager = value; }
  313.         }
  314.         public OpenFileDialog OpenFileDialog {
  315.             get { return this.fd; }
  316.             set { this.fd = value; }
  317.         }
  318.         public override object Clone() {
  319.             return new DataGridViewBrowseCell(this.fd, this.undoManager);
  320.         }
  321.         protected override object GetFormattedValue(object value, int rowIndex, ref DataGridViewCellStyle cellStyle, TypeConverter valueTypeConverter, TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context) {
  322.             object x = base.GetFormattedValue(value, rowIndex, ref cellStyle, valueTypeConverter, formattedValueTypeConverter, context);
  323.             return "...";
  324.         }
  325.         protected override void OnClick(DataGridViewCellEventArgs e) {
  326.             DataGridViewRow row = this.OwningRow;
  327.             DataGridView view = row.DataGridView;
  328.             string filename = row.Cells[2].Value as string;
  329.             if (!string.IsNullOrEmpty(filename)) {
  330.                 Uri uri = new Uri(filename);
  331.                 if (uri.IsFile) {
  332.                     fd.FileName = uri.LocalPath;
  333.                 }
  334.             }
  335.             fd.Multiselect = false;
  336.             if (fd.ShowDialog(this.DataGridView.FindForm()) == DialogResult.OK) {
  337.                 if (SchemaDialogCommand.ValidateSchema(row, fd.FileName) != null) {
  338.                     row.Cells[2].Value = fd.FileName;
  339.                 }
  340.             }
  341.         }
  342.     }
  343.     abstract class SchemaDialogCommand : Command {
  344.         DataGridView view;
  345.         List<SchemaItem> items;
  346.         public SchemaDialogCommand(DataGridView view, List<SchemaItem> items) {
  347.             this.view = view;
  348.             this.items = items;
  349.         }
  350.         public DataGridView View {
  351.             get { return view; }
  352.         }
  353.         public override bool IsNoop {
  354.             get {                
  355.                 return false;
  356.             }
  357.         }
  358.         public void InvalidateRow(DataGridViewRow row) {
  359.             foreach (DataGridViewRow vr in this.view.Rows) {
  360.                 View.InvalidateRow(vr.Index);
  361.             }
  362.         }
  363.         protected void SelectRows(IList<DataGridViewRow> list) {
  364.             this.view.ClearSelection();
  365.             foreach (DataGridViewRow vr in list) {
  366.                 vr.Selected = true;                
  367.             }
  368.         }
  369.         public bool IsSamePath(string a, string b) {
  370.             try {
  371.                 if (string.IsNullOrEmpty(a)) return string.IsNullOrEmpty(b);
  372.                 Uri ua = new Uri(a);
  373.                 Uri ub = new Uri(b);
  374.                 return ua == ub;
  375.             } catch {
  376.                 return a == b;
  377.             }
  378.         }
  379.         public DataGridViewRow FindExistingRow(string schema) {
  380.             foreach (DataGridViewRow row in this.view.Rows) {
  381.                 DataGridViewCell cell = row.Cells[2];
  382.                 string path = cell.Value as string;
  383.                 if (IsSamePath(path, schema)) {
  384.                     // already there!!
  385.                     return row;
  386.                 }
  387.             }
  388.             return null;
  389.         }
  390.         public SchemaItem FindExistingItem(string schema) {
  391.             foreach (SchemaItem row in this.items) {
  392.                 if (IsSamePath(row.Filename, schema)) {                    
  393.                     return row;
  394.                 }
  395.             }
  396.             return null;
  397.         }
  398.         public DataGridViewRow InsertRow(string schema) {
  399.             XmlSchema s = LoadSchema(schema);
  400.             if (s != null) {
  401.                 int i = InsertRow(false, s.TargetNamespace, schema);
  402.                 return this.view.Rows[i];
  403.             }            
  404.             return null;
  405.         }
  406.         public int InsertRow(bool disabled, string targetNamespace, string filename) {
  407.             SchemaItem item = new SchemaItem(disabled, targetNamespace, filename);
  408.             int i = this.view.Rows.Add(item.Values);
  409.             DataGridViewRow row = this.view.Rows[i];
  410.             AttachItem(row, item);
  411.             return i;
  412.         }
  413.         public void InsertRow(int i, DataGridViewRow row) {
  414.             this.view.Rows.Insert(i, row);
  415.             if (row.Tag != null) {
  416.                 AttachItem(row, row.Tag as SchemaItem);
  417.             }
  418.         }
  419.         public void AttachItem(DataGridViewRow row, SchemaItem item) {
  420.             row.Tag = item;
  421.             this.items.Insert(row.Index, item);
  422.         }        
  423.         public delegate void DataRowHandler(DataGridViewRow row, SchemaItem item);
  424.         public void ProcessSelectedRows(DataRowHandler handler) {
  425.             this.view.SuspendLayout();
  426.             foreach (DataGridViewRow row in this.view.SelectedRows) {
  427.                 handler(row, row.Tag as SchemaItem);
  428.             }
  429.             this.view.ResumeLayout();
  430.         }
  431.         public void AddEscapedUri(StringBuilder sb, string filename) {
  432.             if (!string.IsNullOrEmpty(filename)) {
  433.                 try {
  434.                     Uri uri = new Uri(filename);
  435.                     if (sb.Length > 0) {
  436.                         sb.Append("rn");
  437.                     }
  438.                     sb.Append(uri.GetComponents(UriComponents.AbsoluteUri, UriFormat.UriEscaped));
  439.                 } catch {
  440.                 }
  441.             }
  442.         }
  443.         public void AddRows(IList<DataGridViewRow> rows) {
  444.             DataGridViewRowCollection col = this.view.Rows;
  445.             foreach (DataGridViewRow row in rows) {
  446.                 int i = col.Add(row);
  447.                 SchemaItem item = row.Tag as SchemaItem;
  448.                 if (item != null) {
  449.                     items.Insert(i, item);
  450.                 }
  451.             }
  452.             SelectRows(rows);
  453.         }
  454.         public DataGridViewRow RemoveRow(DataGridViewRow row) {
  455.             if (row.Index != -1) {
  456.                 this.view.Rows.Remove(row);
  457.             }
  458.             SchemaItem item = row.Tag as SchemaItem;
  459.             if (item != null) {
  460.                 this.items.Remove(item);
  461.             }
  462.             return row;
  463.         }
  464.         public IList<DataGridViewRow> RemoveRows(IList<DataGridViewRow> rows) {
  465.             DataGridViewRowCollection col = this.view.Rows;
  466.             foreach (DataGridViewRow row in rows) {
  467.                 col.Remove(row);
  468.                 SchemaItem item = row.Tag as SchemaItem;
  469.                 if (item != null) {
  470.                     this.items.Remove(item);
  471.                 }
  472.             }
  473.             return rows;
  474.         }
  475.         protected void Verify() {
  476.             foreach (DataGridViewRow row in this.view.Rows) {
  477.                 string matches = "null";
  478.                 if (row.Tag != null) {
  479.                     SchemaItem item = (SchemaItem)row.Tag;
  480.                     if (item != items[row.Index]) {
  481.                         matches = "false";
  482.                     } else {
  483.                         matches = "true";
  484.                     }
  485.                 }
  486.                 string name = row.Cells[2].Value as string;                
  487.                 Trace.WriteLine("row[" + name + "]=" + matches);
  488.             }
  489.         }
  490.         public static XmlSchema ValidateSchema(DataGridViewRow row, string filename) {
  491.             try {
  492.                 XmlSchema schema = SchemaDialogCommand.LoadSchema(filename); // make sure we can load it!            
  493.                 return schema;
  494.             } catch (Exception ex) {
  495.                 MessageBox.Show(string.Format(SR.SchemaLoadError, filename, ex.Message),
  496.                     SR.SchemaError, MessageBoxButtons.OK, MessageBoxIcon.Error);
  497.                 return null;
  498.             }
  499.         }
  500.         
  501.         public static XmlSchema LoadSchema(string filename) {
  502.             if (string.IsNullOrEmpty(filename)) return null;
  503.             return XmlSchema.Read(new XmlTextReader(filename, new NameTable()), null);
  504.         }
  505.     }
  506.     class SchemaDialogNewRow : SchemaDialogCommand {
  507.         DataGridViewRow row;
  508.         int index;
  509.         public SchemaDialogNewRow(DataGridView view, List<SchemaItem> items, DataGridViewRow row)
  510.             : base(view, items) {
  511.             this.row = row;
  512.         }
  513.         public override string Name {
  514.             get { return "New Row"; }
  515.         }
  516.         public override bool IsNoop {
  517.             get {
  518.                 return this.index == View.Rows.Count;
  519.             }
  520.         }
  521.         public override void Do() {
  522.             this.index = row.Index;
  523.             Verify();
  524.         }
  525.         public override void Undo() {
  526.             RemoveRow(row);
  527.             Verify();
  528.         }
  529.         public override void Redo() {
  530.             InsertRow(index, row);
  531.             this.index = row.Index;
  532.             Verify();
  533.         }
  534.     }
  535.     class SchemaDialogEditCommand : SchemaDialogCommand {
  536.         DataGridViewRow row;
  537.         string newSchema;
  538.         string newNamespace;
  539.         string oldSchema;
  540.         XmlSchema schema;
  541.         string oldNamespace;
  542.         bool isNewRow;
  543.         int index;
  544.         public SchemaDialogEditCommand(DataGridView view, List<SchemaItem> items, DataGridViewRow row, string newSchema)
  545.             : base(view, items) {
  546.             this.newSchema = newSchema;
  547.             this.row = row;
  548.             SchemaItem item = row.Tag as SchemaItem;
  549.             if (item != null) {
  550.                 oldSchema = item.Filename;
  551.                 oldNamespace = item.TargetNamespace;
  552.                 schema = item.Schema; 
  553.             }
  554.             // should succeed because previous code already validated the filename.
  555.             schema = SchemaDialogCommand.LoadSchema(newSchema);
  556.             newNamespace = schema == null ? "" : schema.TargetNamespace;
  557.             index = row.Index;
  558.         }
  559.         public override string Name {
  560.             get { return SR.EditSchemaCommand; }
  561.         }
  562.         public override bool IsNoop {
  563.             get {
  564.                 return newSchema == oldSchema;
  565.             }
  566.         }
  567.         public bool IsNewRow {
  568.             get { return isNewRow; }
  569.             set { isNewRow = value; }
  570.         }
  571.         public override void Do() {
  572.             SchemaItem item = row.Tag as SchemaItem;
  573.             if (row.Index == this.View.Rows.Count - 1) {
  574.                 isNewRow = true;
  575.             }
  576.             this.View.CurrentCell = row.Cells[2];
  577.             this.View.NotifyCurrentCellDirty(true);
  578.             row.Cells[1].Value = newNamespace;
  579.             if (item == null) { // then it was a new row
  580.                 item = new SchemaItem(false, newNamespace, newSchema);
  581.                 item.Schema = schema;
  582.                 AttachItem(row, item);
  583.             } else {
  584.                 item.TargetNamespace = newNamespace;
  585.                 item.Filename = newSchema;
  586.             }
  587.             InvalidateRow(row);
  588.             this.View.NotifyCurrentCellDirty(false);
  589.             Verify();
  590.         }
  591.         public override void Undo() {
  592.             if (IsNewRow) {
  593.                 row = RemoveRow(row);
  594.             } else {
  595.                 row.Cells[2].Value = oldSchema;
  596.                 row.Cells[1].Value = oldNamespace;
  597.                 SchemaItem item = row.Tag as SchemaItem;
  598.                 if (item != null) {
  599.                     item.Filename = oldSchema;
  600.                     item.TargetNamespace = oldNamespace;
  601.                 }
  602.                 InvalidateRow(row);
  603.             }
  604.             Verify();
  605.         }
  606.         public override void Redo() {
  607.             if (IsNewRow) {
  608.                 this.InsertRow(index, row);
  609.             } else {
  610.                 row.Cells[2].Value = newSchema;
  611.                 row.Cells[1].Value = newNamespace;
  612.                 InvalidateRow(row);
  613.             }
  614.             Verify();
  615.         }
  616.     }
  617.     class SchemaDialogCutCommand : SchemaDialogCommand {
  618.         string clip;
  619.         IList<DataGridViewRow> deletedRows = new List<DataGridViewRow>();
  620.         public SchemaDialogCutCommand(DataGridView view, List<SchemaItem> items)
  621.             : base(view, items) {
  622.         }
  623.         public string Clip {
  624.             get { return clip; }
  625.             set { clip = value; }
  626.         }
  627.         public override string Name {
  628.             get { return SR.CutSchemaCommand; }
  629.         }
  630.         public override void Do() {
  631.             // This builds what should go on clipboard, but doesn't actually
  632.             // mess with the clipboard - the caller does that so this command
  633.             // can also be used for plain delete oepration.
  634.             StringBuilder sb = new StringBuilder();
  635.             ProcessSelectedRows(delegate(DataGridViewRow row, SchemaItem item) {
  636.                 string uri = row.Cells[2].Value as string;
  637.                 AddEscapedUri(sb, uri);
  638.                 if (!string.IsNullOrEmpty(uri)) {
  639.                     deletedRows.Add(row);
  640.                     this.RemoveRow(row);
  641.                 }                
  642.             });
  643.             this.clip = sb.ToString();
  644.             Verify();
  645.         }
  646.         public override void Undo() {
  647.             AddRows(deletedRows);
  648.             Verify();
  649.         }
  650.         public override void Redo() {
  651.             deletedRows = RemoveRows(deletedRows);
  652.             Verify();
  653.         }
  654.     }
  655.     class SchemaDialogAddFiles : SchemaDialogCommand {
  656.         string[] files;
  657.         IList<DataGridViewRow> newRows = new List<DataGridViewRow>();
  658.         public SchemaDialogAddFiles(DataGridView view, List<SchemaItem> items, string[] files)
  659.             : base(view, items) {
  660.             this.files = files;
  661.         }
  662.         public override string Name {
  663.             get { return SR.AddSchemaCommand; }
  664.         }
  665.         public override void Do() {
  666.             List<DataGridViewRow> list = new List<DataGridViewRow>();
  667.             foreach (string file in this.files) {
  668.                 try {
  669.                     Uri uri = new Uri(file);
  670.                     string path = uri.IsFile ? uri.LocalPath : uri.AbsoluteUri;
  671.                     DataGridViewRow row = FindExistingRow(path);
  672.                     if (row != null) {
  673.                         XmlSchema s = LoadSchema(path);
  674.                         if (s != null) {
  675.                             SchemaItem item = row.Tag as SchemaItem;
  676.                             if (item == null) {
  677.                                 item = new SchemaItem(false, s.TargetNamespace, path);
  678.                                 AttachItem(row, item);
  679.                             }
  680.                             row.Cells[1].Value = s.TargetNamespace;
  681.                             item.TargetNamespace = s.TargetNamespace;
  682.                             row.Cells[2].Value = path;
  683.                             item.Filename = path;
  684.                         }
  685.                     } else {
  686.                         row = InsertRow(path);
  687.                         if (row != null) {
  688.                             newRows.Add(row);
  689.                             list.Add(row);
  690.                         }
  691.                     }
  692.                     list.Add(row);
  693.                 } catch {
  694. #if DEBUG
  695.                     Trace.WriteLine("Bad file name:" + file);
  696. #endif
  697.                 }
  698.             }
  699.             SelectRows(list);
  700.             Verify();
  701.         }
  702.         public override void Undo() {
  703.             newRows = RemoveRows(newRows);
  704.             Verify();
  705.         }
  706.         public override void Redo() {
  707.             AddRows(newRows);
  708.             Verify();
  709.         }
  710.     }
  711. }