using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Nevron.Diagram.WinForm; using Nevron.Diagram; using Nevron.GraphicsCore; using Nevron.Diagram.Filters; using System.Diagnostics; using Nevron.Reflection; using Nevron; using Nevron.Dom; namespace TestNevron { public partial class TestForm : Form { #region Private Variables private NMyDataObjectAdaptor adapter = null; private const string CustomFormat = "TableFormat"; #endregion #region Constructor public TestForm() { InitializeComponent(); } #endregion #region UI events private void TestForm_Load(object sender, EventArgs e) { view.BeginInit(); view.DocumentPadding = new Nevron.Diagram.NMargins(20); view.Grid.Visible = false; view.GlobalVisibility.ShowPorts = false; NTool tool = view.Controller.Tools.GetToolByName(NDWFR.ToolDragDropTarget); int index = view.Controller.Tools.IndexOf(tool); view.Controller.Tools.Remove(tool); tool = new NMyDragDropTargetTool(); tool.Enabled = true; view.Controller.Tools.Insert(index, tool); document.BeginInit(); adapter = new NMyDataObjectAdaptor(); document.DataObjectAdaptors.Add(adapter); document.Style.TextStyle.TextFormat = TextFormat.XML; document.EndInit(); view.EndInit(); } private void btnExit_Click(object sender, EventArgs e) { this.Close(); } private void trvTables_ItemDrag(object sender, ItemDragEventArgs e) { TreeNode node = (TreeNode)e.Item; if (node.Level != 0) { DataObject data = new DataObject(CustomFormat, string.Concat(node.Tag.ToString())); trvTables.DoDragDrop(data, DragDropEffects.Copy); } } private void btnConnector_Click(object sender, EventArgs e) { NCreateConnectorTool tool = (view.Controller.Tools.GetToolByName(NDWFR.ToolCreateConnector) as NCreateConnectorTool); if (tool == null) return; tool.ConnectorType = ConnectorType.Bezier; view.Controller.Tools.SingleEnableTool(NDWFR.ToolCreateConnector); } private void btnDefault_Click(object sender, EventArgs e) { view.Controller.Tools.SingleEnableTools(new string[] { NDWFR.ToolSelector, NDWFR.ToolMove, NDWFR.ToolKeyboard }); } private void btnLinks_Click(object sender, EventArgs e) { NNodeList links = document.ActiveLayer.Children(NFilters.Shape1D); int linkCount = links.Count; for (int i = 0; i < linkCount; i++) { NBezierCurveShape link = links[i] as NBezierCurveShape; if (link != null) { // Display a meassage for the current relation DisplayMessage(link); } } } #endregion #region Private Methods private void DisplayMessage(NBezierCurveShape link) { StringBuilder sb = new StringBuilder(); NTableShape source = (NTableShape)link.FromShape; NTableShape destination = (NTableShape)link.ToShape; int? sourceRow = GetNumber(link.StartPlug.InwardPort); int? destRow = GetNumber(link.EndPlug.InwardPort); if (sourceRow.HasValue && destRow.HasValue) { sb.Append("Relationship is:"); sb.AppendLine(); sb.Append("source column: \""); sb.Append(source[1, sourceRow.Value - 1].Text); sb.Append("\" is mapped with "); sb.Append("destination column: \""); sb.Append(destination[1, destRow.Value - 1].Text); sb.Append("\""); MessageBox.Show(sb.ToString(), "Relationship", MessageBoxButtons.OK, MessageBoxIcon.Information); } } private int? GetNumber(NPort port) { int? result = null; int index = 0; if (port != null) { string[] values = port.Name.Split('_'); if (values.Length > 0 && int.TryParse(values[values.Length - 1], out index)) { result = index; } } return result; } #endregion #region Nested Classes [Serializable()] public class NMyDragDropTargetTool : NDragDropTargetTool { public NMyDragDropTargetTool() { } public override bool ProcessDragDrop(DragEventArgs e) { // check whether a group is hit, and if not use base implementation // which adds shapes to the active layer NPointF mouseInDevice = View.GetMousePositionInDevice(); NGroup group = View.LastActiveDocumentContentHit(mouseInDevice, -1, NFilters.TypeNGroup) as NGroup; if (group == null) return base.ProcessDragDrop(e); // a group was hit - check desired effect if (e.Effect != DragDropEffects.Copy && e.Effect != DragDropEffects.Move) return false; // need to manually instruct that the move has ended EndMove(false); // make a custom transaction, which simply modifies the group text // this code can be extended to perform group specific tasks Document.StartTransaction("Drop elements in group"); try { group.Text += "\n data in: " + e.Data.GetFormats()[0] + " format dropped"; View.Focus(); } catch (Exception ex) { Trace.WriteLine("Failed to drop. Exception was: " + ex.Message); Document.Rollback(); return false; } Document.Commit(); Document.SmartRefreshAllViews(); return true; } } [Serializable()] public class NMyDataObjectAdaptor : NDataObjectAdaptor { private List m_Sources; private List m_Destinations; [NonSerialized] [NReferenceField] internal NDrawingDocument m_Document; public NMyDataObjectAdaptor() { m_Sources = new List(); m_Destinations = new List(); } public NDrawingDocument Document { get { return this.m_Document; } } public List DestinationTables { get { return this.m_Sources; } } public List SourceTables { get { return this.m_Destinations; } } public override void UpdateReferences(INReferenceProvider provider) { if (provider != null) { m_Document = (provider.ProvideReference(typeof(NDrawingDocument)) as NDrawingDocument); } else { m_Document = null; } base.UpdateReferences(provider); } public override bool CanAdapt(IDataObject dataObject) { if (dataObject == null) throw new ArgumentNullException("dataObject"); if (dataObject.GetDataPresent(CustomFormat)) return true; return false; } public override object Adapt(IDataObject dataObject) { if (CanAdapt(dataObject) == false) return null; // adapt my custom format if (dataObject.GetDataPresent(CustomFormat)) { string myDataObject = (dataObject.GetData(CustomFormat) as string); if (string.IsNullOrEmpty(myDataObject)) return null; return AdaptTableFormat(myDataObject); } return null; } protected virtual NDrawingDataObject AdaptTableFormat(string tableName) { NTableShape shape = new NTableShape(); shape.Name = tableName.Remove(0, 1); shape.InitTable(3, 5); shape.BeginUpdate(); shape.CellPadding = new Nevron.Diagram.NMargins(2); shape.ShowGrid = true; shape.PortDistributionMode = TablePortDistributionMode.GridBased; shape.SizeToContent(); shape[0, 0].ColumnSpan = 3; shape[0, 0].Text = tableName.Substring(1); shape[0, 0].Text = string.Concat("", shape[0, 0].Text, ""); if (tableName.Substring(1) == "Patient") { shape[0, 1].Text = "PK"; shape[1, 1].Text = "Id"; shape[2, 1].Text = "Integer"; shape[0, 2].Text = String.Empty; shape[1, 2].Text = "Firstname"; shape[2, 2].Text = "String"; shape[0, 3].Text = String.Empty; shape[1, 3].Text = "Surname"; shape[2, 3].Text = "String"; shape[0, 4].Text = String.Empty; shape[1, 4].Text = "DOB"; shape[2, 4].Text = "DateTime"; } else if (tableName.Substring(1) == "PatientDemographic") { shape[0, 1].Text = "PK"; shape[1, 1].Text = "PatientId"; shape[2, 1].Text = "Integer"; shape[0, 2].Text = String.Empty; shape[1, 2].Text = "Firstname"; shape[2, 2].Text = "String"; shape[0, 3].Text = String.Empty; shape[1, 3].Text = "Lastname"; shape[2, 3].Text = "String"; shape[0, 4].Text = String.Empty; shape[1, 4].Text = "DateOfBirth"; shape[2, 4].Text = "DateTime"; } shape.EndUpdate(); // S mean source table, D mean destination table. switch (tableName[0]) { case 'S': m_Sources.Add(shape); break; case 'D': m_Destinations.Add(shape); break; default: throw new Exception("New table type ?"); } NDrawingDataObject ddo = new NDrawingDataObject(null, new INDiagramElement[] { shape }); return ddo; } } #endregion } }