SIL.FieldWorks.XWorks.DTMenuHandler.OnDataTreeInsert C# (CSharp) Method

OnDataTreeInsert() public method

This method is called when a user selects an Insert operation in on a slice.
public OnDataTreeInsert ( object cmd ) : bool
cmd object
return bool
		public bool OnDataTreeInsert(object cmd)
		{
			Command command = (Command) cmd;
			string field = command.GetParameter("field");
			string className = command.GetParameter("className");
			string ownerClassName = command.GetParameter("ownerClass", "");
			Slice current = m_dataEntryForm.CurrentSlice;
			if (current != null)
			{
				current.Validate();
				if (current.Control is ContainerControl)
					((ContainerControl)current.Control).Validate();

			}
			if (current == null && m_dataEntryForm.Slices.Count > 0)
				current = m_dataEntryForm.FieldAt(0);
			string sliceName = command.GetParameter("slice", "");
			if (String.IsNullOrEmpty(ownerClassName) && current != null && current.Object != null)
			{
				var owner = current.Object.Owner;
				if (owner != null && owner.IsValidObject)
				{
					int clid = owner.ClassID;
					if (clid > 0)
						ownerClassName = owner.ClassName;
				}
			}
			if (sliceName != null && sliceName == "owner" && current != null)
			{
				// Find a slice corresponding to the current slice's owner's object.
				var cmo = current.Object;
				foreach (Slice slice in current.ContainingDataTree.Slices)
				{
					if (slice.Object == cmo.Owner)
					{
						current = slice;
						break;
					}
				}
			}
			if (current == null && m_dataEntryForm.Slices.Count > 0)
				current = m_dataEntryForm.Slices[0] as Slice;
			if (current == null || !current.Object.IsValidObject)
				return false;
			// If we're trying to replace a ghost slice, there could be trouble...
			if (current.IsGhostSlice && SliceConfiguredForField(current.ConfigurationNode, field))
			{
				// We can't let the current slice handle the insert because it blows up (see
				// LT-4725).  (This is because the ghost slice is deleted and disposed of in
				// the process of creating and inserting the real slice containing the newly
				// created real object.)  Try using an adjacent slice on the same object,
				// preferably a preceding one.
				Debug.Assert(current.Object != null);
				int hvoObject = current.Object.Hvo;
				int iNew = -1;
				for (int i = current.IndexInContainer - 1; i >= 0; --i)
				{
					if ((current.ContainingDataTree.Slices[i] as Slice).Object != null &&
						(current.ContainingDataTree.Slices[i] as Slice).Object.Hvo == hvoObject)
					{
						iNew = i;
						break;
					}
				}
				if (iNew == -1)
				{
					int cslice = current.ContainingDataTree.Slices.Count;
					for (int i = current.IndexInContainer + 1; i < cslice; ++i)
					{
						if ((current.ContainingDataTree.Slices[i] as Slice).Object != null &&
							(current.ContainingDataTree.Slices[i] as Slice).Object.Hvo == hvoObject)
						{
							iNew = i;
							break;
						}
					}
				}
				if (iNew == -1)
				{
					Logger.WriteEvent(String.Format(
						"Cannot insert class {1} into field {0} of a {2} over a ghost slice.",
						field, className, ownerClassName ?? "nullOwner"));
					return true;
				}
				current = current.ContainingDataTree.Slices[iNew] as Slice;
			}
			Logger.WriteEvent(String.Format("Inserting class {1} into field {0} of a {2}.",
				field, className, ownerClassName ?? "nullOwner"));
			current.HandleInsertCommand(field, className, ownerClassName,
				command.GetParameter("recomputeVirtual", null));

			Logger.WriteEvent("Done Inserting.");
			return true;	//we handled this.
		}