DigitTool.DigitDockableWindow.SelectionChanged C# (CSharp) Method

SelectionChanged() public method

public SelectionChanged ( ) : void
return void
		public void SelectionChanged()
		{

			try
			{
				System.Windows.Forms.Control ctrl = null;
				System.Windows.Forms.Control ctrl2 = null;
				Control[] ctrls = null;
				System.Collections.ArrayList ctrlstoremove = new System.Collections.ArrayList();
				string labelName = "";
				string featureClass = "";
				System.Windows.Forms.ComboBox cbo = null;
				System.Windows.Forms.Label lblMain = null;

				if (m_digitCommand == null)
					return;

				//clear any current elements
				if (m_schematicFeature1 != null)
				{
					m_schematicFeature1 = null;
					m_digitCommand.SchematicFeature1(m_schematicFeature1);
				}

				if (m_schematicFeature2 != null)
				{
					m_schematicFeature2 = null;
					m_digitCommand.SchematicFeature2(m_schematicFeature2);
				}

				if (m_curPanel == Splitter.Panel1)
				{
					labelName = "lblNodeLabel";
					featureClass = "descendant::NodeFeature";
					cbo = cboNodeType;
					lblMain = lblNodeLabel;
				}
				else
				{
					labelName = "lblLinkLabel";
					featureClass = "descendant::LinkFeature";
					cbo = cboLinkType;
					lblMain = lblLinkLabel;
				}

				foreach (System.Windows.Forms.Control ctrlfor in m_curPanel.Controls)
				{
					if (ctrlfor.Name.StartsWith("lbl") && (ctrlfor.Name.ToString() != labelName))
					{
						ctrls = m_curPanel.Controls.Find(ctrlfor.Name.Substring(3), true);
						ctrl2 = ctrls[0];
						ctrlstoremove.Add(ctrlfor);
						ctrlstoremove.Add(ctrl2);
					}
				}

				if (ctrlstoremove.Count > 0)
				{
					foreach (System.Windows.Forms.Control ctrol in ctrlstoremove)
					{
						m_curPanel.Controls.Remove(ctrol);
					}
				}

				XmlNodeList elems = null;
				m_curfrmWidth = m_curPanel.Width;
				elems = m_dom.SelectNodes(featureClass);

				bool blnFound = false;
				XmlElement elem = null;
				foreach (XmlElement elem0 in elems)
				{
					if (elem0.GetAttribute("FeatureClassName").ToString() == cbo.Text.ToString())
					{
						elem = elem0;
						blnFound = true;
						break;
					}
				}

				if (blnFound == false)
				{
					// CR229717: If this is deactivate, lost the Schematic ElementClass and can not retrieve it
					//m_schEltClass = null;
					return;
				}

				if (m_curPanel == Splitter.Panel1)
					m_curNode = elem;
				else
					m_curLink = elem;

				//set grid
				elems = elem.SelectNodes("descendant::Field");
				int x = Splitter.Location.X;
				int y = lblMain.Location.Y + lblMain.Height + 5;

				System.Drawing.Point p = new System.Drawing.Point();

				foreach (XmlElement f in elems)
				{
					System.Windows.Forms.Label lbl = new System.Windows.Forms.Label();
					lbl.Name = "lbl" + f.GetAttribute("DBColumnName").ToString();
					lbl.Text = f.GetAttribute("DisplayName").ToString();
					lbl.AutoSize = true;
					m_curPanel.Controls.Add(lbl);
					p.X = 3;
					p.Y = y;
					lbl.Location = p;
					y = y + lbl.Height + 10;

					switch (f.GetAttribute("Type").ToString())
					{
						case "Text":
							System.Windows.Forms.TextBox tx = new System.Windows.Forms.TextBox();
							ctrl = tx;
							tx.Name = f.GetAttribute("DBColumnName").ToString();
							if (f.GetAttribute("Length").Length > 0)
								tx.MaxLength = System.Convert.ToInt32(f.GetAttribute("Length"));

							if (f.GetAttribute("Default").Length > 0)
								tx.Text = f.GetAttribute("Default");

							m_curPanel.Controls.Add(tx);
							break;

						case "Combo":

							System.Windows.Forms.ComboBox cb = new System.Windows.Forms.ComboBox();
							string defaulttext = "";
							ctrl = cb;
							cb.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
							cb.Name = f.GetAttribute("DBColumnName").ToString();
							XmlNodeList vlist = f.SelectNodes("descendant::Value");

							foreach (XmlElement v in vlist)
							{
								cb.Items.Add(v.InnerText.ToString());
								if (v.GetAttribute("Default").Length > 0)
									defaulttext = v.InnerText;
							}

							if (defaulttext.Length > 0)
								cb.Text = defaulttext;

							m_curPanel.Controls.Add(cb);
							break;

						case "MaskText":
							System.Windows.Forms.MaskedTextBox MaskControl = new System.Windows.Forms.MaskedTextBox();
							ctrl = MaskControl;
							string mask = "";
							MaskControl.Name = f.GetAttribute("DBColumnName").ToString();
							if (f.GetAttribute("Mask").Length > 0)
								mask = f.GetAttribute("Mask");
							else
								mask = "";

							MaskControl.Mask = mask;

							if (f.GetAttribute("Default").Length > 0)
								MaskControl.Text = f.GetAttribute("Default");

							MaskControl.Modified = false;
							m_curPanel.Controls.Add(MaskControl);
							MaskControl.TextChanged += new System.EventHandler(MaskedTextBox);

							break;

						case "Number":
							System.Windows.Forms.MaskedTextBox MaskControl2 = new System.Windows.Forms.MaskedTextBox();
							ctrl = MaskControl2;
							string mask2 = "";
							MaskControl2.Name = f.GetAttribute("DBColumnName").ToString();
							int i = 1;
							if (f.GetAttribute("Length").Length > 0)
							{
								for (i = 1; i <= System.Convert.ToInt32(f.GetAttribute("Length")); i++)
									mask = mask2 + "9";
							}
							else
							{
								if (f.GetAttribute("Mask").Length > 0)
									mask2 = f.GetAttribute("Mask");
								else
									mask2 = "";
							}

							MaskControl2.Mask = mask2;
							if (f.GetAttribute("Default").Length > 0)
								MaskControl2.Text = f.GetAttribute("Default");

							MaskControl2.Modified = false;
							m_curPanel.Controls.Add(MaskControl2);
							MaskControl2.TextChanged += new System.EventHandler(MaskedTextBox);
							break;

						case "Date":
							System.Windows.Forms.DateTimePicker dt = new System.Windows.Forms.DateTimePicker();
							ctrl = dt;
							dt.Name = f.GetAttribute("DBColumnName").ToString();
							dt.Value = DateTime.Now;
							dt.Format = System.Windows.Forms.DateTimePickerFormat.Short;
							m_curPanel.Controls.Add(dt);
							break;

						case "Relation":
							System.Windows.Forms.ComboBox cb2 = new System.Windows.Forms.ComboBox();
							ctrl = cb2;
							cb2.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
							cb2.Name = f.GetAttribute("DBColumnName").ToString();
							XmlNodeList vlist2 = m_dom.SelectNodes("descendant::Relation");
							XmlNodeList rellist = null;
							string str = null;
							foreach (XmlElement v in vlist2)
							{
								if (v.GetAttribute("LinkType").ToString() == elem.GetAttribute("FeatureClassName").ToString())
								{
									rellist = v.SelectNodes("descendant::Value");
									if (rellist.Count > 0)
										foreach (XmlElement r in rellist)
										{
											str = v.GetAttribute("FromType").ToString() + "-" + v.GetAttribute("ToType").ToString() + "-" + r.InnerText.ToString();
											cb2.Items.Add(str);
										}
									else //assume they are not using subtypes
										cb2.Items.Add(v.GetAttribute("FromType").ToString() + "-" + v.GetAttribute("ToType").ToString());
								}
							}
							//relations are always mandatory
							ctrl.BackColor = m_MandatoryColor;
							ctrl.Tag = "Mandatory";
							m_curPanel.Controls.Add(cb2);
							break;
					}

					if (f.GetAttribute("Mandatory").ToString() == "True")
					{
						ctrl.BackColor = m_MandatoryColor;
						ctrl.Tag = "Mandatory";
					}
				}
				ResizeForm();

				// set m_schEltClass
				XmlElement curElement = null;

				if (m_curPanel == Splitter.Panel1)
					curElement = m_curNode;
				else
					curElement = m_curLink;

				if (m_schEltClass == null)
					m_schEltClass = m_schEltClassCont.GetSchematicElementClass(curElement.GetAttribute("FeatureClassName"));
				else
					if (m_schEltClass.Name != curElement.GetAttribute("FeatureClassName"))
						m_schEltClass = m_schEltClassCont.GetSchematicElementClass(curElement.GetAttribute("FeatureClassName"));
			}

			catch (System.Exception e)
			{
				System.Windows.Forms.MessageBox.Show(e.Message);
			}
		}

Usage Example

        protected override void OnMouseUp(ESRI.ArcGIS.Desktop.AddIns.Tool.MouseEventArgs arg)
        {
            bool abortOperation = false;

            ESRI.ArcGIS.Schematic.ISchematicOperation schematicOperation = null;

            try
            {
                if (m_dockableDigit == null)
                {
                    return;
                }

                if (arg != null)
                {
                    m_x = arg.X;
                    m_y = arg.Y;
                }

                if (m_dockableWindow == null)
                {
                    return;
                }

                if (m_dockableWindow.IsVisible() == false)
                {
                    m_dockableWindow.Show(true);
                }

                ESRI.ArcGIS.SchematicControls.ISchematicTarget target = (ESRI.ArcGIS.SchematicControls.ISchematicTarget)m_schematicExtension;

                if (target != null)
                {
                    m_schematicLayer = target.SchematicTarget;
                }

                if (m_schematicLayer == null)
                {
                    System.Windows.Forms.MessageBox.Show("No target Layer");
                    return;
                }

                ISchematicInMemoryDiagram               inMemoryDiagram;
                ISchematicInMemoryFeatureClass          schematicInMemoryFeatureClass;
                ISchematicInMemoryFeatureClassContainer schematicInMemoryFeatureClassContainer;

                //Get the point
                ESRI.ArcGIS.Geometry.Point point = new ESRI.ArcGIS.Geometry.Point();

                ESRI.ArcGIS.ArcMapUI.IMxApplication mxApp;
                ESRI.ArcGIS.Display.IAppDisplay     appDisplay;
                IScreenDisplay         screenDisplay;
                IDisplay               display;
                IDisplayTransformation transform;
                ISpatialReference      spatialReference;

                inMemoryDiagram = m_schematicLayer.SchematicInMemoryDiagram;
                schematicInMemoryFeatureClassContainer = (ISchematicInMemoryFeatureClassContainer)inMemoryDiagram;

                if (schematicInMemoryFeatureClassContainer == null)
                {
                    return;
                }

                mxApp = (ESRI.ArcGIS.ArcMapUI.IMxApplication)m_app;

                if (mxApp == null)
                {
                    return;
                }

                appDisplay = mxApp.Display;

                if (appDisplay == null)
                {
                    return;
                }

                screenDisplay = appDisplay.FocusScreen;
                display       = screenDisplay;

                if (display == null)
                {
                    return;
                }

                transform = display.DisplayTransformation;

                if (transform == null)
                {
                    return;
                }

                spatialReference = transform.SpatialReference;

                WKSPoint mapPt = new WKSPoint();
                ESRI.ArcGIS.Display.tagPOINT devPoint;
                devPoint.x = m_x;
                devPoint.y = m_y;
                transform.TransformCoords(ref mapPt, ref devPoint, 1, 1); //'esriTransformToMap

                point.SpatialReference = spatialReference;
                point.Project(spatialReference);
                point.X = mapPt.X;
                point.Y = mapPt.Y;

                schematicInMemoryFeatureClass = schematicInMemoryFeatureClassContainer.GetSchematicInMemoryFeatureClass(m_dockableDigit.FeatureClass());

                if (schematicInMemoryFeatureClass == null)
                {
                    System.Windows.Forms.MessageBox.Show("Invalid Type.");
                    return;
                }

                if (m_dockableDigit.CreateNode())
                {
                    //TestMandatoryField
                    m_dockableDigit.btnOKPanel1.Visible = false;

                    if (m_dockableDigit.ValidateFields() == false)
                    {
                        m_dockableDigit.x(m_x);
                        m_dockableDigit.y(m_y);

                        System.Windows.Forms.MessageBox.Show(m_dockableDigit.ErrorProvider1.GetError(m_dockableDigit.btnOKPanel1) + m_messageFromOK);
                        return;
                    }

                    ESRI.ArcGIS.Geometry.IGeometry geometry;

                    ISchematicInMemoryFeature schematicInMemoryFeatureNode;

                    geometry = point;

                    schematicOperation = (ESRI.ArcGIS.Schematic.ISchematicOperation) new ESRI.ArcGIS.SchematicControls.SchematicOperation();

                    //digit operation is undo(redo)able we add it in the stack
                    IMxDocument doc = (IMxDocument)m_app.Document;
                    ESRI.ArcGIS.SystemUI.IOperationStack operationStack;
                    operationStack = doc.OperationStack;
                    operationStack.Do(schematicOperation);
                    schematicOperation.StartOperation("Digit", m_app, m_schematicLayer, true);

                    //do abort operation
                    abortOperation = true;

                    schematicInMemoryFeatureNode = schematicInMemoryFeatureClass.CreateSchematicInMemoryFeatureNode(geometry, "");
                    //schematicInMemoryFeatureNode.UpdateStatus = esriSchematicUpdateStatus.esriSchematicUpdateStatusNew; if we want the node deleted after update
                    schematicInMemoryFeatureNode.UpdateStatus = esriSchematicUpdateStatus.esriSchematicUpdateStatusLocked;

                    abortOperation = false;
                    schematicOperation.StopOperation();

                    ISchematicFeature schematicFeature = schematicInMemoryFeatureNode;
                    m_dockableDigit.FillValue(ref schematicFeature);

                    if (m_dockableDigit.AutoClear())
                    {
                        m_dockableDigit.SelectionChanged();
                    }
                }
                else
                {
                    m_dockableDigit.btnOKPanel2.Visible = false;

                    //Get the Tolerance of ArcMap
                    Double      tolerance;
                    IMxDocument mxDocument = (ESRI.ArcGIS.ArcMapUI.IMxDocument)m_app.Document;
                    ESRI.ArcGIS.esriSystem.WKSPoint point2 = new WKSPoint();
                    ESRI.ArcGIS.Display.tagPOINT    devPt;

                    tolerance = mxDocument.SearchTolerancePixels;
                    devPt.x   = (int)tolerance;
                    devPt.y   = (int)tolerance;

                    transform.TransformCoords(ref point2, ref devPt, 1, 2); //2 <-> esriTransformSize 4 <-> esriTransformToMap

                    tolerance = point2.X * 5;                               //increase the tolerance value

                    IEnumSchematicFeature schematicFeatures = m_schematicLayer.GetSchematicFeaturesAtPoint(point, tolerance, false, true);

                    ISchematicFeature schematicFeatureSelected = null;
                    double            distancetmp;
                    double            distance = 0;
                    schematicFeatures.Reset();

                    if (schematicFeatures.Count <= 0)
                    {
                        return;
                    }

                    //pSchematicFeatures may contain several features, we are choosing the closest node.
                    ISchematicFeature schematicFeature2 = schematicFeatures.Next();

                    double dX;
                    double dY;
                    ISchematicInMemoryFeatureNode schematicInMemoryFeatureNode = null;
                    if (schematicFeature2 != null)
                    {
                        if (schematicFeature2.SchematicElementClass.SchematicElementType == ESRI.ArcGIS.Schematic.esriSchematicElementType.esriSchematicNodeType)
                        {
                            schematicInMemoryFeatureNode = (ISchematicInMemoryFeatureNode)schematicFeature2;
                        }
                    }

                    ISchematicInMemoryFeatureNodeGeometry schematicInMemoryFeatureNodeGeometry = (ISchematicInMemoryFeatureNodeGeometry)schematicInMemoryFeatureNode;
                    dX = schematicInMemoryFeatureNodeGeometry.Position.X;
                    dY = schematicInMemoryFeatureNodeGeometry.Position.Y;
                    schematicFeatureSelected = schematicFeature2;
                    distance = SquareDistance(dX - point.X, dY - point.Y);

                    while (schematicFeature2 != null)
                    {
                        //find the closest featureNode...
                        if (schematicInMemoryFeatureNode != null)
                        {
                            schematicInMemoryFeatureNodeGeometry = (ISchematicInMemoryFeatureNodeGeometry)schematicInMemoryFeatureNode;

                            if (schematicInMemoryFeatureNodeGeometry == null)
                            {
                                continue;
                            }

                            dX          = schematicInMemoryFeatureNodeGeometry.Position.X;
                            dY          = schematicInMemoryFeatureNodeGeometry.Position.Y;
                            distancetmp = SquareDistance(dX - point.X, dY - point.Y);

                            if (distancetmp < distance)
                            {
                                distance = distancetmp;
                                schematicFeatureSelected = schematicFeature2;
                            }
                        }

                        schematicFeature2 = schematicFeatures.Next();

                        if (schematicFeature2 != null)
                        {
                            if (schematicFeature2.SchematicElementClass.SchematicElementType == ESRI.ArcGIS.Schematic.esriSchematicElementType.esriSchematicNodeType)
                            {
                                schematicInMemoryFeatureNode = (ISchematicInMemoryFeatureNode)schematicFeature2;
                            }
                        }
                    }

                    if (schematicFeatureSelected == null)
                    {
                        return;
                    }

                    if (schematicFeatureSelected.SchematicElementClass.SchematicElementType != esriSchematicElementType.esriSchematicNodeType)
                    {
                        return;
                    }

                    if (m_schematicFeature1 == null)
                    {
                        m_schematicFeature1 = schematicFeatureSelected;
                        m_dockableDigit.SchematicFeature1(m_schematicFeature1);

                        if (!m_dockableDigit.CheckValidFeature(true))
                        {
                            m_schematicFeature1 = null;
                            m_dockableDigit.SchematicFeature1(m_schematicFeature1);
                            throw new Exception("Invalid starting node for this link type");
                        }

                        //Begin Feedback
                        m_linkFbk         = new NewLineFeedback();
                        m_linkFbk.Display = screenDisplay;

                        //symbol
                        ISimpleLineSymbol sLnSym;
                        IRgbColor         rGB = new RgbColor();

                        sLnSym = (ESRI.ArcGIS.Display.ISimpleLineSymbol)m_linkFbk.Symbol;

                        //Make a color
                        rGB.Red   = 255;
                        rGB.Green = 0;
                        rGB.Blue  = 0;

                        // Setup the symbol with color and style
                        sLnSym.Color = rGB;

                        m_linkFbk.Start(point);
                        //End Feedback

                        //To know if we are in the same diagram.
                        m_schematicLayerForLink = m_schematicLayer;
                    }
                    else
                    {
                        if (m_schematicLayerForLink != m_schematicLayer)
                        {
                            System.Windows.Forms.MessageBox.Show("wrong Target layer");
                            m_schematicLayerForLink = null;
                            EndFeedBack();
                            return;
                        }
                        m_schematicFeature2 = schematicFeatureSelected;
                        m_dockableDigit.SchematicFeature2(m_schematicFeature2);

                        //TestMandatoryField
                        if (m_dockableDigit.ValidateFields() == false)
                        {
                            m_dockableDigit.x(m_x);
                            m_dockableDigit.y(m_y);

                            System.Windows.Forms.MessageBox.Show(m_dockableDigit.ErrorProvider1.GetError(m_dockableDigit.btnOKPanel2) + m_messageFromOK);
                            return;
                        }

                        if (!m_dockableDigit.CheckValidFeature(false))
                        {
                            m_schematicFeature2 = null;
                            m_dockableDigit.SchematicFeature2(m_schematicFeature2);
                            throw new Exception("Invalid End node for this link type");
                        }

                        //CreateLink
                        ISchematicInMemoryFeature schematicInMemoryFeatureLink;

                        schematicOperation = (ESRI.ArcGIS.Schematic.ISchematicOperation) new ESRI.ArcGIS.SchematicControls.SchematicOperation();

                        //digit operation is undo(redo)able we add it in the stack
                        IMxDocument doc = (IMxDocument)m_app.Document;
                        ESRI.ArcGIS.SystemUI.IOperationStack operationStack;
                        operationStack = doc.OperationStack;
                        operationStack.Do(schematicOperation);
                        schematicOperation.StartOperation("Digit", m_app, m_schematicLayer, true);

                        //do abort operation
                        abortOperation = true;

                        schematicInMemoryFeatureLink = schematicInMemoryFeatureClass.CreateSchematicInMemoryFeatureLink((ESRI.ArcGIS.Schematic.ISchematicInMemoryFeatureNode)m_schematicFeature1, (ESRI.ArcGIS.Schematic.ISchematicInMemoryFeatureNode)m_schematicFeature2, "");
                        //schematicInMemoryFeatureLink.UpdateStatus = esriSchematicUpdateStatus.esriSchematicUpdateStatusNew; if we want the node deleted after update
                        schematicInMemoryFeatureLink.UpdateStatus = esriSchematicUpdateStatus.esriSchematicUpdateStatusLocked;

                        abortOperation = false;
                        schematicOperation.StopOperation();

                        ISchematicFeature schematicFeature = schematicInMemoryFeatureLink;
                        m_dockableDigit.FillValue(ref schematicFeature);

                        //End Feedback
                        EndFeedBack();

                        m_schematicLayerForLink = null;

                        if (m_dockableDigit.AutoClear())
                        {
                            m_dockableDigit.SelectionChanged();
                        }
                    }
                }

                //Refresh the view and viewer windows
                RefreshView();
            }
            catch (System.Exception e)
            {
                if (abortOperation && (schematicOperation != null))
                {
                    schematicOperation.AbortOperation();
                }

                EndFeedBack();
                System.Windows.Forms.MessageBox.Show(e.Message);
            }

            return;
        }