using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Collections.Generic; using System.Drawing; using System.Data.OleDb; using System.IO; using System.Net; using System.Text; using Telerik.WebControls; using System.Web.Caching; using System.Collections.Specialized; using Nevron.Diagram; using Nevron.Diagram.DataImport; using Nevron.Diagram.Layout; using Nevron.Diagram.Shapes; using Nevron.Diagram.WebForm; using Nevron.Diagram.Filters; using Nevron.Dom; using Nevron.Filters; using Nevron.GraphicsCore; using Nevron.UI.WebForm.Controls; using Nevron.Diagram.DataStructures; using Nevron.Diagram.Extensions; using System.Drawing.Drawing2D; namespace MyCompany.Web.Console.Pages { public class diagramTest : NSimpleNetworkShapesFactory { public static Random random = new Random(); private NDrawingDocument doc; public NDrawingDocument Doc { get { return doc; } set { doc = value; } } private NMaster nmaster; public NMaster Nmaster { get { return nmaster; } set { nmaster = value; } } public diagramTest() { } protected override NShape CreateComputer() { NPersistencyManager manager = new NPersistencyManager(); NLibraryDocument library = manager.LoadLibraryFromFile("c:\\mylib.nlx"); NMaster master = library.GetChildByName("CompositeShape_mao") as NMaster; //shapesFactory.Nmaster = master; float p1=(float)random.Next(1000); float p2=(float)random.Next(1000); NNodeList shape = master.CreateInstance(this.Doc, new NPointF(p1, p2)); return (NShape)shape[0]; } } public partial class Devices : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { //SetPermissions(); NDrawingView1.ViewLayout = CanvasLayout.Normal; NDrawingView1.Document.BeginInit(); InitDocument(); NDrawingView1.Document.EndInit(); } } /// /// Habilita o deshabilita menús de acuerdo a los permisos asignados al usuario /// private void SetPermissions() { if (!Controller.HasPermition(2)) { Response.Redirect("../Default.aspx"); } else { Controller.ReviewAction(2); } } private void Import(List diagramnodes) { // create the tree data source importer ArandaGraphDataSourceImporter graphImporter = new ArandaGraphDataSourceImporter(); // set the document in the active layer of which the shapes will be imported NDrawingView1.Document.ActiveLayer.RemoveAllChildren(); graphImporter.Document = NDrawingView1.Document; Aranda.APA.Business.Facade.Device device = new Aranda.APA.Business.Facade.Device(); //consulta la lista de links para la graficacion de la red. List diagramlinks = device.ListLinks(); DataTable links = new DataTable(); links.Columns.Add("FromId", System.Type.GetType("System.Int32")); links.Columns.Add("ToId", System.Type.GetType("System.Int32")); DataTable nodes = new DataTable(); nodes.Columns.Add("Id", System.Type.GetType("System.Int32")); nodes.Columns.Add("Title", System.Type.GetType("System.String")); nodes.Columns.Add("Type", System.Type.GetType("System.String")); nodes.Columns.Add("Status", System.Type.GetType("System.String")); //crea los nodos para graficar la red. foreach (DiagramNodesInfo item in diagramnodes) { nodes.Rows.Add(item.Id, item.Title, item.Type, item.Status); System.Diagnostics.Trace.WriteLine(item.Id.ToString() + ";" + item.Title + ";" + item.Type + ";" + item.Status); } //crea las asociaciones entre los nodos para la grafica de la red. foreach (DiagramLinkInfo item in diagramlinks) { links.Rows.Add(item.FromId, item.ToId); System.Diagnostics.Trace.WriteLine(item.FromId.ToString() + ";" + item.ToId.ToString()); } graphImporter.VertexDataSource = nodes.DefaultView; graphImporter.EdgeDataSource = links.DefaultView; // vertex records are uniquely identified by their Id (in the Pages table) // edges link the vertices with the FromId and ToId (in the Links table) graphImporter.VertexIdColumnName = "Id"; graphImporter.VertexTypeColumnName = "Type"; graphImporter.FromVertexIdColumnName = "FromId"; graphImporter.ToVertexIdColumnName = "ToId"; // create vertices as rectangles shapes, with default size (60,30) NSimpleNetworkShapesFactory shapesFactory = new NSimpleNetworkShapesFactory(); shapesFactory.DefaultSize = new NSizeF(60, 30); graphImporter.VertexShapesFactory = shapesFactory; graphImporter.VertexShapesName = SimpleNetworkShapes.Computer.ToString(); // set stylesheets to be applied to imported vertices and edges graphImporter.VertexStyleSheetName = "Vertices"; graphImporter.EdgeStyleSheetName = "Edges"; // use layered graph layout //*NOrthogonalGraphLayout layout = new NOrthogonalGraphLayout(); //*graphImporter.Layout = layout; //*************************************************************** ///**************************************************************** ///Para generar el layout en estrella. /*float zoomFactor = NDrawingView1.ViewportSize.Width / document.Bounds.Width; NDrawingView1.ScaleX = 50f; NDrawingView1.ScaleY = 50f;*/ NSymmetricalLayout layout = new NSymmetricalLayout(); layout.BounceBackForce.Padding = 100f; layout.BounceBackForce.Enabled = true; // get the shapes to layout NNodeList shapes = NDrawingView1.Document.ActiveLayer.Children(NFilters.Shape2D); // layout the shapes layout.Layout(shapes, new NDrawingLayoutContext(NDrawingView1.Document)); graphImporter.Layout = layout; /////************************************************************** //****************************************************************** // subscribe for the vertex imported event, // which is raised when a shape was created for a data source record graphImporter.VertexImported += new ShapeImportedDelegate(OnVertexImported); // import graphImporter.Import(); } private void InitDocument() { NDrawingDocument document = NDrawingView1.Document; document.BackgroundStyle.FrameStyle.Visible = false; // create two stylesheets - one for the vertices and one for the edges NStyleSheet vertexStyleSheet = new NStyleSheet(); vertexStyleSheet.Name = "Vertices"; vertexStyleSheet.Style.FillStyle = new NColorFillStyle(Color.FromArgb(236, 97, 49)); document.StyleSheets.AddChild(vertexStyleSheet); NStyleSheet edgeStyleSheet = new NStyleSheet(); edgeStyleSheet.Name = "Edges"; edgeStyleSheet.Style.StrokeStyle = new NStrokeStyle(Color.FromArgb(68, 90, 108)); document.StyleSheets.AddChild(edgeStyleSheet); // create the tree data source importer ArandaGraphDataSourceImporter graphImporter = new ArandaGraphDataSourceImporter(); // set the document in the active layer of which the shapes will be imported graphImporter.Document = document; Aranda.APA.Business.Facade.Device device = new Aranda.APA.Business.Facade.Device(); List diagramnodes = device.ListNodes(); //consulta la lista de links para la graficacion de la red. List diagramlinks = device.ListLinks(); DataTable links = new DataTable(); links.Columns.Add("FromId", System.Type.GetType("System.Int32")); links.Columns.Add("ToId", System.Type.GetType("System.Int32")); DataTable nodes = new DataTable(); nodes.Columns.Add("Id", System.Type.GetType("System.Int32")); nodes.Columns.Add("Title", System.Type.GetType("System.String")); nodes.Columns.Add("Type", System.Type.GetType("System.String")); nodes.Columns.Add("Status", System.Type.GetType("System.String")); nodes.Rows.Add(1, "Switch 1", "Hub", "On"); nodes.Rows.Add(2, "PC1", "Computer", "On"); nodes.Rows.Add(3, "PC2", "Computer", "On"); nodes.Rows.Add(4, "PC3", "Computer", "Off"); nodes.Rows.Add(5, "PC4", "Computer", "On"); links.Rows.Add(1, 2); links.Rows.Add(1, 3); links.Rows.Add(1, 4); links.Rows.Add(1, 5); graphImporter.VertexDataSource = nodes.DefaultView; graphImporter.EdgeDataSource = links.DefaultView; // vertex records are uniquely identified by their Id (in the Pages table) // edges link the vertices with the FromId and ToId (in the Links table) graphImporter.VertexIdColumnName = "Id"; graphImporter.VertexTypeColumnName = "Type"; graphImporter.FromVertexIdColumnName = "FromId"; graphImporter.ToVertexIdColumnName = "ToId"; //diagramTest diagramTest shapesFactory = new diagramTest(); shapesFactory.Doc = NDrawingView1.Document; shapesFactory.Nmaster = master; graphImporter.VertexShapesFactory = shapesFactory; graphImporter.VertexShapesName = SimpleNetworkShapes.Computer.ToString(); // set stylesheets to be applied to imported vertices and edges graphImporter.VertexStyleSheetName = "Vertices"; graphImporter.EdgeStyleSheetName = "Edges"; NSymmetricalLayout layout = new NSymmetricalLayout(); layout.BounceBackForce.Padding = 100f; layout.BounceBackForce.Enabled = true; // get the shapes to layout NNodeList shapes = NDrawingView1.Document.ActiveLayer.Children(NFilters.Shape2D); // layout the shapes layout.Layout(shapes, new NDrawingLayoutContext(NDrawingView1.Document)); graphImporter.Layout = layout; // subscribe for the vertex imported event, // which is raised when a shape was created for a data source record graphImporter.VertexImported += new ShapeImportedDelegate(OnVertexImported); // import graphImporter.Import(); } private void OnVertexImported(NDataSourceImporter importer, NShape shape, INDataRecord dataRecord) { string text = ""; int id = 0; string status = ""; status = (string)dataRecord.GetColumnValue("Status"); text = (string)dataRecord.GetColumnValue("Title"); id = (int)dataRecord.GetColumnValue("Id"); shape.Style.InteractivityStyle = new NInteractivityStyle(true, id.ToString(), "Click Derecho para Ver Detalle"); shape.Text = text.ToString(); shape.Tag = id; shape.SizeToText(new NMarginsF(10)); //todo: cambiar la presentacion grafica por estado if (status == "Off") { shape.Style.FillStyle = new NColorFillStyle(Color.Red); } } protected void RadMenu1_ItemClick(object sender, RadMenuEventArgs e) { ProcessRequest(e.Item.Value); } private void ProcessRequest(string accion) { string id = Request.Form["radGridClickedRowIndex"].ToString(); if (accion == "View") { if (id != null && id != "") { Response.Redirect("Device.aspx?Id=" + id + "&view=true"); } } else if (accion == "List") { if (id != null && id != "") { Response.Redirect("monitors.aspx?command=View&deviceId=" + id + ""); } else { MessageBox.ShowGlobal("YouMustSelectDevice"); } } else if (accion == "New") { if (id != null && id != "") { Response.Redirect("monitor.aspx?command=New&deviceId=" + id + ""); } else { MessageBox.ShowGlobal("YouMustSelectDevice"); } } } protected void NDrawingView1_QueryAjaxTools(object sender, EventArgs e) { NDrawingView1.AjaxTools.Add(new NAjaxTooltipTool(true)); } protected void NDrawingView1_AsyncQueryCommandResult(object sender, EventArgs e) { NCallbackQueryCommandResultArgs args = e as NCallbackQueryCommandResultArgs; NCallbackCommand command = args.Command; NAjaxXmlTransportBuilder resultBuilder = args.ResultBuilder; switch (command.Name) { case "queryPosition": int deviceId = int.Parse(command.Arguments["id"].ToString()); NNodeList hitResult = NDrawingView1.Document.ActiveLayer.Children(null); hitResult = hitResult.Filter(Nevron.Diagram.Filters.NFilters.Shape2D); if (hitResult.Count == 0) return; NShape shape = null; int x = 0; int y = 0; foreach (INNode node in hitResult) { if ((int)((Nevron.Diagram.NShape)node).Tag == deviceId) { shape = ((Nevron.Diagram.NShape)(node)); break; } } if (shape != null) { x = Convert.ToInt32(((Nevron.Diagram.NModel)(shape)).Bounds.Center.X) - 400; //400 es la mitad del div del scroll width. y = Convert.ToInt32(((Nevron.Diagram.NModel)(shape)).Bounds.Center.Y) - 180;//180 es la mitad del div del scroll height. } NAjaxXmlDataSection dataSection = new NAjaxXmlDataSection("positionx"); NAjaxXmlDataSection dataSection2 = new NAjaxXmlDataSection("positiony"); dataSection.Data = x.ToString(); dataSection2.Data = y.ToString(); resultBuilder.AddDataSection(dataSection); resultBuilder.AddDataSection(dataSection2); break; //case "redraw": // List lista; // lista = (List)Session["SearchDeviceList"]; // if (lista != null) // { // if (lista.Count > 0) // { // Import(lista); // } // } // NDrawingView1.Document.RefreshAllViews(); // if (!resultBuilder.ContainsRedrawDataSection()) // { // resultBuilder.AddRedrawDataSection(NDrawingView1); // } // break; } } protected void NDrawingView1_AsyncRefresh(object sender, EventArgs e) { List lista; lista = (List)Session["SearchDeviceList"]; if (lista != null) { if (lista.Count > 0) { Import(lista); } } NDrawingView1.Document.RefreshAllViews(); } } }