private void toolStripMenuItemInsertReference_Click(object sender, EventArgs e)
{
using(FormSelectEntity form = new FormSelectEntity(null, null, this.m_project, SelectDefinitionOptions.Entity | SelectDefinitionOptions.Type))
{
if(form.ShowDialog(this) == System.Windows.Forms.DialogResult.OK && form.SelectedEntity != null)
{
// determine the schema
DocSchema targetschema = null;
foreach(DocSection docSection in this.m_project.Sections)
{
foreach(DocSchema docSchema in docSection.Schemas)
{
if(form.SelectedEntity is DocEntity && docSchema.Entities.Contains((DocEntity)form.SelectedEntity) ||
form.SelectedEntity is DocType && docSchema.Types.Contains((DocType)form.SelectedEntity))
{
targetschema = docSchema;
break;
}
}
}
if (targetschema == null)
{
return;
}
DocSchema sourceschema = (DocSchema)this.treeView.SelectedNode.Tag;
if(sourceschema == targetschema)
{
MessageBox.Show(this, "The selected item is in the current schema; references may only be made to definitions from other schemas.", "Reference");
return;
}
// check for schema reference
DocSchemaRef targetschemaref = null;
TreeNode tnTargetSchema = null;
foreach (DocSchemaRef docSchemaRef in sourceschema.SchemaRefs)
{
if(docSchemaRef.Name != null && docSchemaRef.Name.Equals(targetschema.Name, StringComparison.OrdinalIgnoreCase))
{
// found it
targetschemaref = docSchemaRef;
int index = sourceschema.SchemaRefs.IndexOf(docSchemaRef);
tnTargetSchema = this.treeView.SelectedNode.Nodes[6].Nodes[index];
break;
}
}
if (targetschemaref == null)
{
targetschemaref = new DocSchemaRef();
targetschemaref.Name = targetschema.Name.ToUpper();
sourceschema.SchemaRefs.Add(targetschemaref);
tnTargetSchema = LoadNode(this.treeView.SelectedNode.Nodes[6], targetschemaref, targetschemaref.Name, false);
}
// add definition reference
DocDefinitionRef targetdef = null;
foreach(DocDefinitionRef docDefRef in targetschemaref.Definitions)
{
if(docDefRef.Name.Equals(form.SelectedEntity.Name))
{
targetdef = docDefRef;
break;
}
}
if(targetdef != null)
{
// select it...
MessageBox.Show(this, "The selected item has already been referenced in this schema.");
return;
}
targetdef = new DocDefinitionRef();
targetdef.Name = form.SelectedEntity.Name;
InitDefinition(targetdef);
targetschemaref.Definitions.Add(targetdef);
TreeNode tnTargetDef = LoadNode(tnTargetSchema, targetdef, targetdef.Name, false);
this.treeView.SelectedNode = tnTargetDef;
}
}
}