?? xmltreepanel.cs
字號:
// DINAMIC XML Editor
//
// Copyright (c) 2002-2003 Dusan Hlavaty
// mailto: duddo@atlas.cz
//
// This software is licensed under the terms of
// GNU General Public license
//
using System;
using System.Windows.Forms;
using System.Xml;
using Crownwood.Magic.Docking;
using XML_editor.MyForms;
using XML_editor.MyComponents;
using XML_editor.Common;
using XML_editor.TabPages;
namespace XML_editor.DockingPanels
{
/// <summary>
/// Dokovaci panel do ktoreho sa bude vykreslovat hierarchicka (stromova)
/// struktura XML dokumentu.
/// </summary>
public class XMLTreePanel : Crownwood.Magic.Docking.Content
{
/// <summary>
/// Odkaz na hlavny formular aplikacie
/// </summary>
private MainForm mainForm = null;
private TreeView treeView = null;
private Panel panel = null;
private System.Windows.Forms.Label labelRefresh = new System.Windows.Forms.Label();
/// <summary>
/// <see cref="TextAreaTabPage"/>, ktora je prave zobrazena v strome
/// </summary>
private TextAreaTabPage textAreaTabPageInTree = null;
// -------------------------------------------------------------------------
/// <summary>
/// Odkaz na hlavny formular aplikacie
/// </summary>
public MainForm MainForm
{
get
{
return this.mainForm;
}
}
// -------------------------------------------------------------------------
/// <summary>
/// Inicializuje dokovaci panel obsahujuci XML strom.
/// </summary>
/// <param name="mainForm">odkaz na hlavny formular aplikacie - <see cref="MainForm"/></param>
/// <param name="manager">odkaz na <see cref="DockingManager"/>, ku ktoremu bude tento panel patrit</param>
public XMLTreePanel(MainForm mainForm, DockingManager manager) : base(manager)
{
this.mainForm = mainForm;
this.FullTitle = this.Title = "XML Tree";
this.panel = new System.Windows.Forms.Panel();
ImageList imageList = new System.Windows.Forms.ImageList();
this.treeView = new TreeView();
ImageButton buttonRefresh = new ImageButton();
System.Windows.Forms.Label labelStatus = new System.Windows.Forms.Label();
//
// panel
//
this.panel.Controls.AddRange(new System.Windows.Forms.Control[] { buttonRefresh, labelStatus, this.labelRefresh,
this.treeView});
this.panel.Name = "panel";
this.panel.Size = new System.Drawing.Size(208, 312);
//
// imageList
//
imageList.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
imageList.ImageSize = new System.Drawing.Size(16, 16);
imageList.Images.Add( IconProvider.LoadBitmap(this, "XML_editor.Icons.xml_tag1.png"));
imageList.Images.Add( IconProvider.LoadBitmap(this, "XML_editor.Icons.xml_tag2.png"));
imageList.Images.Add( IconProvider.LoadBitmap(this, "XML_editor.Icons.xml_tag3.png"));
imageList.Images.Add( IconProvider.LoadBitmap(this, "XML_editor.Icons.xml_tag4.png"));
imageList.Images.Add( IconProvider.LoadBitmap(this, "XML_editor.Icons.refresh.png"));
imageList.Images.Add( IconProvider.LoadBitmap(this, "XML_editor.Icons.xml_tree.png"));
imageList.Images.Add( IconProvider.LoadBitmap(this, "XML_editor.Icons.Error.png"));
this.ImageList = imageList;
this.ImageIndex = 5;
//
// treeView
//
this.treeView.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right);
this.treeView.Location = new System.Drawing.Point(0, 40);
this.treeView.Size = new System.Drawing.Size(208, 272);
this.treeView.TabIndex = 0;
this.treeView.ImageList = imageList;
this.treeView.ImageIndex = this.treeView.SelectedImageIndex = -1;
this.treeView.CausesValidation = false;
this.treeView.DoubleClick += new System.EventHandler(this.treeView_DoubleClick);
//
// buttonRefresh
//
buttonRefresh.Location = new System.Drawing.Point(10, 10);
buttonRefresh.ImageList = imageList;
buttonRefresh.ImageIndex = 4;
buttonRefresh.Click += new System.EventHandler(this.Clicked_buttonRefresh);
//
// labelStatus
//
labelStatus.AutoSize = true;
labelStatus.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(238)));
labelStatus.Location = new System.Drawing.Point(35, 14);
labelStatus.Text = "Status:";
//
// labelRefresh
//
this.labelRefresh.AutoSize = true;
this.labelRefresh.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(238)));
this.labelRefresh.Location = new System.Drawing.Point(80, 14);
this.labelRefresh.Text = String.Empty;
this.Control = this.panel;
// Zaradime sa do docking managera
this.MainForm.DockingManager.Contents.Add(this);
this.MainForm.DockingManager.AddContentWithState(this, Crownwood.Magic.Docking.State.DockRight);
this.ActualTextAreaControlChanged(null, null);
this.MainForm.ActualTextAreaControlChanged += new System.EventHandler(this.ActualTextAreaControlChanged);
this.MainForm.TextAreaControlClosed += new TextAreaControlClosedHandler(this.TextAreaControlClosed);
// TODO: Toto tam dorobit
//txt.Document.DocumentChanged += new DLTextEditor.Document.DocumentAggregatorEventHandler(DocumentChanged);
}
// -------------------------------------------------------------------------
/// <summary>
/// Vyvola sa po zatvoreni nejakeho <see cref="XML_editor.TabPages.TextAreaTabPage"/>
/// </summary>
/// <param name="tab"><see cref="XML_editor.TabPages.TextAreaTabPage"/>, ktory sa zatvara.
/// Ak je <c>null</c>, tak sa zatvaraju fsetky</param>
private void TextAreaControlClosed(XML_editor.TabPages.TextAreaTabPage tab)
{
this.treeView.Nodes.Clear();
this.textAreaTabPageInTree = null;
this.labelRefresh.Text = String.Empty;
}
// -------------------------------------------------------------------------
/// <summary>
/// Vyvola sa, PO zmene aktualneho <see cref="MainForm.ActualTextAreaControl"/>
/// - teda ked uzivatel prepne na inu zalozku.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ActualTextAreaControlChanged(object sender, System.EventArgs e)
{
if (this.MainForm.ActualTextAreaControl == null)
{
this.panel.Enabled = false;
this.treeView.Nodes.Clear();
this.textAreaTabPageInTree = null;
this.labelRefresh.Text = String.Empty;
}
else
{
this.panel.Enabled = true;
}
}
// -------------------------------------------------------------------------
/// <summary>
/// Kliknutie na tlacidlo 'Refresh'
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Clicked_buttonRefresh(object sender, System.EventArgs e)
{
this.MainForm.OutputPanel.Clear();
this.MainForm.TaskListPanel.Clear();
if (this.textAreaTabPageInTree != null)
{
this.textAreaTabPageInTree.TextAreaControl.Document.DocumentChanged -= new DLTextEditor.Document.DocumentAggregatorEventHandler(DocumentChanged);
this.textAreaTabPageInTree = null;
}
this.textAreaTabPageInTree = null;
this.MainForm.OutputPanel.AppendText("------ Tree generation started: File: " + this.MainForm.SelectedTabPage.TitleToShow + " ------\r\n");
this.treeView.Nodes.Clear();
this.treeView.SuspendLayout();
string fullFileName = this.MainForm.ActualTextAreaControl.FileName;
XmlTextReader reader = null;
TreeNode rootNode = null;
try
{
//Load the reader with the data file and ignore all white space nodes.
reader = new XmlTextReader(this.MainForm.ActualTextAreaControl.Document.GetStream()); //fullFileName);
reader.WhitespaceHandling = WhitespaceHandling.None;
reader.XmlResolver = null;
reader.Namespaces = false;
TreeNode newNode = null;
TreeNode actualNode = null;
rootNode = this.treeView.Nodes.Add(this.MainForm.SelectedTabPage.TitleToShow);
rootNode.Tag = new Position(this.MainForm, fullFileName, 0, 0);
actualNode = rootNode;
actualNode.ImageIndex = actualNode.SelectedImageIndex = 5;
this.treeView.Font = new System.Drawing.Font(this.treeView.Font, System.Drawing.FontStyle.Bold);
System.Drawing.Font nodeFont = new System.Drawing.Font(this.treeView.Font, System.Drawing.FontStyle.Regular);
//Parse the file and display each of the nodes.
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
if (reader.IsEmptyElement == false)
{
newNode = actualNode.Nodes.Add("<"+reader.Name+" >");
newNode.NodeFont = nodeFont;
newNode.ImageIndex = newNode.SelectedImageIndex = 0;
newNode.Tag = new Position(this.MainForm, fullFileName, reader.LineNumber, reader.LinePosition);
actualNode = newNode;
}
else
{
newNode = actualNode.Nodes.Add("<"+reader.Name+" />");
newNode.NodeFont = nodeFont;
newNode.ImageIndex = newNode.SelectedImageIndex = 3;
newNode.Tag = new Position(this.MainForm, fullFileName, reader.LineNumber, reader.LinePosition);
}
break;
case XmlNodeType.Text:
//Console.Write(reader.Value);
break;
case XmlNodeType.CDATA:
newNode = actualNode.Nodes.Add("<![CDATA[...]>");
newNode.NodeFont = nodeFont;
newNode.ImageIndex = newNode.SelectedImageIndex = 1;
newNode.Tag = new Position(this.MainForm, fullFileName, reader.LineNumber, reader.LinePosition);
break;
case XmlNodeType.ProcessingInstruction:
newNode = actualNode.Nodes.Add("<?"+reader.Name+" ?>");
newNode.NodeFont = nodeFont;
newNode.ImageIndex = newNode.SelectedImageIndex = 2;
newNode.Tag = new Position(this.MainForm, fullFileName, reader.LineNumber, reader.LinePosition);
break;
case XmlNodeType.Comment:
newNode = actualNode.Nodes.Add("<!-- -->");
newNode.NodeFont = nodeFont;
newNode.ImageIndex = newNode.SelectedImageIndex = 1;
newNode.Tag = new Position(this.MainForm, fullFileName, reader.LineNumber, reader.LinePosition);
break;
case XmlNodeType.XmlDeclaration:
newNode = actualNode.Nodes.Add("<?" + reader.Name + " " + reader.Value + "?>");
newNode.NodeFont = nodeFont;
newNode.ImageIndex = newNode.SelectedImageIndex = 2;
newNode.Tag = new Position(this.MainForm, fullFileName, reader.LineNumber, reader.LinePosition);
break;
case XmlNodeType.Document:
break;
case XmlNodeType.DocumentType:
newNode = actualNode.Nodes.Add("<!DOCTYPE>");
newNode.NodeFont = nodeFont;
newNode.ImageIndex = newNode.SelectedImageIndex = 1;
newNode.Tag = new Position(this.MainForm, fullFileName, reader.LineNumber, reader.LinePosition);
break;
case XmlNodeType.EntityReference:
// TODO: Zisti co je toto !!!
// Console.Write(reader.Name);
break;
case XmlNodeType.EndElement:
actualNode = actualNode.Parent;
break;
} // switch
} // while
this.MainForm.OutputPanel.AppendTextLine("\r\nAction SUCCESSFUL");
this.textAreaTabPageInTree = this.MainForm.SelectedTabPage;
this.textAreaTabPageInTree.TextAreaControl.Document.DocumentChanged += new DLTextEditor.Document.DocumentAggregatorEventHandler(DocumentChanged);
this.labelRefresh.Text = "OK";
}
catch (UnauthorizedAccessException a)
{
rootNode.ImageIndex = rootNode.SelectedImageIndex = 6;
#if DEBUG
System.Diagnostics.Debug.WriteLine("UnauthorizedAccessException a");
#endif
this.MainForm.OutputPanel.AppendTextLine(a.Message);
this.MainForm.TaskListPanel.AddTask(TaskType.Error, a.Message, fullFileName, 0, 0);
this.labelRefresh.Text = "error";
}
catch (XmlException a)
{
rootNode.ImageIndex = rootNode.SelectedImageIndex = 6;
#if DEBUG
System.Diagnostics.Debug.WriteLine("XmlException a");
#endif
this.MainForm.OutputPanel.AppendTextLine(a.Message);
this.MainForm.TaskListPanel.AddTask(TaskType.Error, a.Message, fullFileName, a.LineNumber, a.LinePosition);
this.labelRefresh.Text = "error";
}
catch (Exception a)
{
rootNode.ImageIndex = rootNode.SelectedImageIndex = 6;
#if DEBUG
System.Diagnostics.Debug.WriteLine("Exception a");
#endif
this.MainForm.OutputPanel.AppendTextLine(a.Message);
this.MainForm.TaskListPanel.AddTask(TaskType.Error, a.Message, fullFileName, 0, 0);
this.labelRefresh.Text = "error";
}
finally
{
if (reader!=null)
{
reader.Close();
}
this.treeView.ResumeLayout();
}
this.MainForm.OutputPanel.AppendTextLine("\r\n---------------------- Done ----------------------\r\n");
}
// -------------------------------------------------------------------------
/// <summary>
/// Obsluha udalosti. Vyvola sa po zmene v dokumente
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DocumentChanged(object sender, DLTextEditor.Document.DocumentAggregatorEventArgs e)
{
if (this.textAreaTabPageInTree != null)
{
this.labelRefresh.Text = "need refresh";
}
}
// -------------------------------------------------------------------------
/// <summary>
/// Doublekliknutie v TreeView-e
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void treeView_DoubleClick(object sender, System.EventArgs e)
{
if (this.treeView.SelectedNode == null)
{
return;
}
#if DEBUG
System.Diagnostics.Debug.WriteLine("DBLclick");
#endif
IPosition pos = this.treeView.SelectedNode.Tag as IPosition;
#if DEBUG
System.Diagnostics.Debug.Assert(pos!=null);
#endif
if (pos != null)
{
pos.JumpToPosition();
}
}
} //public class XMLTreePanel : ...
} // namespace XML_editor.DockingPanels
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -