#####################################################################
# Model

#
# toolbar
#

diagram.toolbar.button.ERDiagram.Entity =
\  node = createNode("erEntity");
\  node->setProperty("uniqueName","Entity")
diagram.toolbar.button.ERDiagram.Entity.icon = "ERDiagram/entity.gif"

diagram.toolbar.button.ERDiagram.Identifying Relationship =
\  link = createLink("erRelationship");
\  link->setProperty("rdbIdentifying",true);
\  if (!link->hasProperty("rdbIdentifying"), link->setProperty("rdbOptional", "true"));
\  link->setProperty("uniqueName","relationship")
diagram.toolbar.button.ERDiagram.Identifying Relationship.icon = "ERDiagram/identify-link.gif"

diagram.toolbar.button.ERDiagram.Non-Identifying Relationship =
\  link = createLink("erRelationship");
\  link->setProperty("uniqueName","relationship");
\  if (%src% == %dst%, link->setProperty("rdbOptional", "true"))
diagram.toolbar.button.ERDiagram.Non-Identifying Relationship.icon = "ERDiagram/non-identify-link.gif"

diagram.toolbar.button.ERDiagram.Many-to-Many Relationship =
\  link = createLink("erManyToManyRelationship");
\  link->setProperty("$name", "many-to-many");
\  uniqueName = link->getProperty("$uniqueName");
\  node=%diagram%->createNode("erEntity");
\  link->setProperty("crossReferencedEntity", node->getProperty("$uniqueName"));
\  node->setProperty("uniqueName", "Entity");
\  node->setProperty("manyToManyLink", uniqueName);
\  dstlink = createLink("erRelationship", node, %dst%);
\  dstlink->setProperty("uniqueName", "relationship");
\  dstlink->setProperty("manyToManyLink", uniqueName);
\  dstlink->setProperty("rdbIdentifying",true);
\  srclink = createLink("erRelationship", node, %src%);
\  srclink->setProperty("uniqueName", "relationship");
\  srclink->setProperty("manyToManyLink", uniqueName);
\  srclink->setProperty("rdbIdentifying", true)
diagram.toolbar.button.ERDiagram.Many-to-Many Relationship.icon = "ERDiagram/many-to-many-link.gif"


; set for all diagrams--need to change order

diagram.toolbar.button.ERDiagram.Note = createNode("Note")
diagram.toolbar.button.ERDiagram.Note.icon = "DiagramElementsToolbar/Note.gif"
diagram.toolbar.button.ERDiagram.Note link =
\  if (%src%->hasPropertyValue("$shapeType", "Note"),
\      createLink("NoteLink", %dst%, %src%),
\      createLink("NoteLink")
\  )
diagram.toolbar.button.ERDiagram.Note link.icon = "DiagramElementsToolbar/NoteLink.gif"

#
# popup menus
#
# Generic diagrams menu


# element menu

menu.item.element.erEntity.New Attribute = member =
\  createMember( "erAttribute" );
\  member->setProperty("uniqueName","attribute")
menu.item.element.erEntity.New Attribute.shortcut = CTRL+A
menu.item.element.erEntity.New Primary Key Attribute =
\  member = createMember( "erAttribute" );
\  member->setProperty("uniqueName","attribute");
\  member->setProperty("rdbPrimaryKey",true)
menu.item.element.erEntity.New Primary Key Attribute.shortcut = CTRL+R

menu.item.element.erEntity.Cut.separator.before = true
menu.item.element.erEntity.Cut
menu.item.element.erEntity.Copy
menu.item.element.erEntity.Clone
menu.item.element.erEntity.Paste
menu.item.element.erEntity.Delete from View
menu.item.element.erEntity.Delete from View with Shortcut
menu.item.element.erEntity.Delete
menu.item.element.erEntity.Delete with Shortcut
menu.item.element.erEntity.Delete with Shortcut.separator.after = true
menu.item.element.erEntity.Layout.separator.before = true
menu.item.element.erEntity.Layout = group
menu.item.element.erEntity.Hide/Show
menu.item.element.erEntity.Rename.separator.before = true
menu.item.element.erEntity.Rename
menu.item.element.erEntity.Version Control.separator.before = true
menu.item.element.erEntity.Version Control = group


menu.item.element.erAttribute.New Attribute =
\      member = getParent()->createMember( "erAttribute" );
\      member->setProperty("uniqueName","attribute");
\      member->setProperty("rdbPrimaryKey",hasProperty("rdbPrimaryKey"));
\      cut( member );
\      member = paste(BEFORE);
\      member->editInplace(null, true)
menu.item.element.erAttribute.New Attribute.name = if(hasProperty("rdbPrimaryKey"), "New Primary Key Attribute", "New Attribute")
menu.item.element.erAttribute.New Attribute.shortcut = INSERT

menu.item.element.erAttribute.Primary Key.separator.before = true
menu.item.element.erAttribute.Primary Key.enabled = !hasProperty("$readOnly")
menu.item.element.erAttribute.Primary Key =
\      setProperty("rdbPrimaryKey", !hasProperty("rdbPrimaryKey"))
menu.item.element.erAttribute.Primary Key.checkbox = true
menu.item.element.erAttribute.Primary Key.checkbox.state = hasProperty("rdbPrimaryKey")

menu.item.element.erAttribute.Cut.separator.before = true
menu.item.element.erAttribute.Cut
menu.item.element.erAttribute.Copy
menu.item.element.erAttribute.Clone
menu.item.element.erAttribute.Paste
menu.item.element.erAttribute.Delete from View
menu.item.element.erAttribute.Delete from View with Shortcut
menu.item.element.erAttribute.Delete
menu.item.element.erAttribute.Delete with Shortcut
menu.item.element.erAttribute.Delete with Shortcut.separator.after = true
menu.item.element.erAttribute.Rename
menu.item.element.erAttribute.Version Control.separator.before = true
menu.item.element.erAttribute.Version Control = group

menu.item.element.erAttribute.ER New Attribute Shortcut =
\  member = getParent()->createMember( "erAttribute" );
\  member->setProperty("uniqueName","attribute")
mitem.element.erAttribute.ER New Attribute Shortcut.shortcut = CTRL+A
mitem.element.erAttribute.ER New Attribute Shortcut.hidden = true

mitem.element.erAttribute.ER New PK Attribute Shortcut =
\  member = getParent()->createMember( "erAttribute" );
\  member->setProperty("uniqueName","attribute");
\  member->setProperty("rdbPrimaryKey",true)
menu.item.element.erAttribute.ER New PK Attribute Shortcut.shortcut = CTRL+R
menu.item.element.erAttribute.ER New PK Attribute Shortcut.hidden = true

menu.item.element.erRelationship.Identifying =
\  setProperty("rdbIdentifying", !hasProperty("rdbIdentifying"))
menu.item.element.erRelationship.Identifying.checkbox = true
menu.item.element.erRelationship.Identifying.checkbox.state = hasProperty("rdbIdentifying")

menu.item.element.erRelationship.Optional =
\  setProperty("rdbOptional", !hasProperty("rdbOptional"))
menu.item.element.erRelationship.Optional.enabled = !hasProperty("rdbIdentifying")
menu.item.element.erRelationship.Optional.checkbox = true
menu.item.element.erRelationship.Optional.checkbox.state = !hasProperty("rdbIdentifying") && hasProperty("rdbOptional")

menu.item.element.erRelationship.to 0-* =
\  setProperty("erCardinality", null)
menu.item.element.erRelationship.to 0-*.name =
\  prefix = "One"; if( !hasProperty("rdbIdentifying") && hasProperty("rdbOptional"), prefix = "Zero or One");
\  prefix + " to Zero or More"
menu.item.element.erRelationship.to 0-*.checkbox = true
menu.item.element.erRelationship.to 0-*.checkbox.state = hasPropertyValue("erCardinality",null)
menu.item.element.erRelationship.to 0-*.separator.before = true

menu.item.element.erRelationship.to 1-* =
\      setProperty("erCardinality", "P")
menu.item.element.erRelationship.to 1-*.name =
\      prefix = "One"; if( !hasProperty("rdbIdentifying") && hasProperty("rdbOptional"), prefix = "Zero or One");
\      prefix + " to One or More"
menu.item.element.erRelationship.to 1-*.checkbox = true
menu.item.element.erRelationship.to 1-*.checkbox.state = hasPropertyValue("erCardinality","P")

menu.item.element.erRelationship.to 0-1 =
\      setProperty("erCardinality", "Z")
menu.item.element.erRelationship.to 0-1.name =
\      prefix = "One"; if( !hasProperty("rdbIdentifying") && hasProperty("rdbOptional"), prefix = "Zero or One");
\      prefix + " to Zero or One"
menu.item.element.erRelationship.to 0-1.checkbox = true
menu.item.element.erRelationship.to 0-1.checkbox.state = hasPropertyValue("erCardinality","Z")
menu.item.element.erRelationship.to 0-1.separator.after = true

menu.item.element.erRelationship.Delete from View
menu.item.element.erRelationship.Delete from View with Shortcut
menu.item.element.erRelationship.Delete
menu.item.element.erRelationship.Delete with Shortcut
menu.item.element.erRelationship.Delete with Shortcut.separator.after = true
menu.item.element.erRelationship.Rename
menu.item.element.erRelationship.Rename.separator.after = true

menu.item.element.erRelationship.Scroll to Source
menu.item.element.erRelationship.Scroll to Destination
menu.item.element.erRelationship.Scroll to Destination.separator.after = true
menu.item.element.erRelationship.Version Control = group


#
# Constraints
#

model.constraint.node.erEntity.container.1 = isPackage()
model.constraint.node.erEntity.container.1.message = "Entity can't be inserted in other elements"

model.constraint.link.erRelationship.source.1 = hasPropertyValue("$shapeType","erEntity")
model.constraint.link.erRelationship.source.1.message = "Relationship can link only Entities"
model.constraint.link.erRelationship.destination.1 = hasPropertyValue("$shapeType","erEntity")
model.constraint.link.erRelationship.destination.1.message = "Relationship can link only Entities"

model.constraint.link.erRelationship.both.1 = %src%->getContainingPackage() == %dst%->getContainingPackage()
model.constraint.link.erRelationship.both.1.message = "Relationship can not link entities from different packages"


model.constraint.link.erManyToManyRelationship.source.1 = hasPropertyValue("$shapeType","erEntity")
model.constraint.link.erManyToManyRelationship.source.1.message = "Relationship can link only Entities"
model.constraint.link.erManyToManyRelationship.destination.1 = hasPropertyValue("$shapeType","erEntity")
model.constraint.link.erManyToManyRelationship.destination.1.message = "Relationship can link only Entities"

model.constraint.link.erManyToManyRelationship.both.1 = %src%->getContainingPackage() == %dst%->getContainingPackage()
model.constraint.link.erManyToManyRelationship.both.1.message = "Relationship can not link entities from different packages"

#
# view map
#

view.map.*.erEntity.isTopLevel() =
\  if ((%diagram%->getProperty("erViewType") != "physicalView") &&
\      (hasProperty("manyToManyLink")),
\    hideElement()
\  );
\  setGraphicObject("RectangleInvisible");
\  backgroundColor = RGB(getViewProperty("backgroundColor"));
\  if(backgroundColor != null, setBackground(backgroundColor));
\  foregroundColor = RGB(getViewProperty("foregroundColor"));
\  if(foregroundColor != null, setForeground(foregroundColor));
\  setLayoutConstraints(minWidth(16),minHeight(16),
\                       preferredWidth(30),preferredHeight(50),
\                       canShiftX(true),canShiftY(true));
\  name = addCompartment("RectangleInvisible","Name");
\  name->setLayoutConstraints(horizontalAlign("left"),verticalAlign("top"),
\                             widthAlign("parentDefined"),fixedHeight(true));
\  dependent = hasProperty("dependent");
\  if( dependent,
\    (body = addCompartment("$RoundRectangle","Body")),
\    (body = addCompartment("RectangleObject","Body"))
\  );
\  body->setLayoutConstraints(horizontalAlign("left"),verticalAlign("top"),
\                             widthAlign("parentDefined"),heightAlign("parentDefined"));
\  primaryKey = addCompartment(body,"$ScrolledRectangleWithoutBorder","PrimaryKey");
\  primaryKey->setLayoutConstraints(minHeight(20));
\  primaryKey->setHorizontalMargin(5);
\  attributes = addCompartment(body,"$ScrolledRectangle","Attributes");
\  attributes->setLayoutConstraints(minHeight(20));
\  attributes->setHorizontalMargin(5);
\  if ((%diagram%->getProperty("erViewType") == "physicalView"),
\    nameLabel = addToCompartment(label(getProperty("rdbPhysicalName")),"Name");
\    nameLabel->setInplaceEditor({property:="rdbPhysicalName",default:=true});
\    nameLabel->setAlignment("Left");
\    nameLabel->setLayoutConstraints(preferredHeight(16),fixedHeight(true)),
\    nameLabel = addToCompartment(label(getProperty("$name")),"Name");
\    nameLabel->setInplaceEditor({property:="$name",default:=true});
\    nameLabel->setAlignment("Left");
\    nameLabel->setLayoutConstraints(preferredHeight(16),fixedHeight(true))
\  );
\  setCanHaveLinks();
\  if( !hasViewProperty("2DLook"),
\      body->setShadow(3, RGB(0,0,128))
\    );
\  markersCompartment = addCompartment( body, "RectangleInvisible", "MarkersCompartment" );
\  markersCompartment->setLayoutConstraints( minHeight(10), fixedHeight(true));
\  markers = markersCompartment->addCompartment( "RectangleInvisible", "Markers" );
\  markers->setLayoutConstraints( widthAlign("parentDefined"), heightAlign("parentDefined"), toDecreaseWidth(true) );
\  markers->setLayout( "Move" );
\  if( hasProperty("$readOnly"),
\    readonlyMark=markers->addCompartment( "LockMark", "lockMark" );
\    readonlyMark->setLayoutConstraints( minWidth(10), minHeight(10), horizontalAlign("right") )
\  );
\  if( hasViewProperty("$imported"), 
\    importedMark=markers->addCompartment("ShortcutMark", "shortcutMark");
\    importedMark->setLayoutConstraints(minWidth(12), minHeight(10), horizontalAlign("left"))
\  );
\  if( hasProperty("$hyperlink") || hasProperty("url"), nameLabel->setForeground(RGB(0,0,255)) )

view.map.*.erEntity.!isTopLevel() =
\  name = getProperty("$name");
\  setGraphicObject(label(name));
\  setInplaceEditor({property:="$name",default:=true});
\  if( hasProperty("$hyperlink") || hasProperty("url"), setForeground(RGB(0,0,255)) )

view.map.*.erAttribute =
\  if (setCompartment("Attributes") == null, hideElement());
\  if ((%diagram%->getProperty("erViewType") == "physicalView"),
\    physicalDeclaration = getProperty("fullPhysicalDeclaration");
\    setGraphicObject(label(physicalDeclaration));
#\    setInplaceEditor({property:="fullPhysicalDeclaration",default:=true}),
\    setInplaceEditor({property:="rdbPhysicalName",default:=true}),
\    name = getProperty("fullName");
\    if (hasProperty("referencedAttribute"), name = name + "(FK)");
\    setGraphicObject(label(name));
\    setInplaceEditor({property:="$name",default:=true})
\  );
\  if( hasProperty("rdbPrimaryKey"),
\    setCompartment("PrimaryKey"),
\    setCompartment("Attributes")
\  );
\  setLayoutConstraints(preferredHeight(16),fixedHeight(true));
\  if( hasProperty("$hyperlink") || hasProperty("url"), setForeground(RGB(0,0,255)) )

view.map.*.erRelationship =
\  if ((%diagram%->getProperty("erViewType") != "physicalView") &&
\      (hasProperty("manyToManyLink")),
\    hideElement()
\  );
\  if( !hasProperty("rdbIdentifying"), setLineStyle("dashed"));
\  if( !hasProperty("rdbIdentifying") && hasProperty("rdbOptional"), addIcon("Rhomb","OnDestination"));
\  if ((%diagram%->getProperty("erViewType") == "physicalView"),
\    label = addLabelIcon(getProperty("rdbPhysicalName"),"AboveLeftCenter");
\    label->setInplaceEditor({property:= "rdbPhysicalName", default:=true}),
\    label = addLabelIcon(getProperty("$name"),"AboveLeftCenter");
\    label->setInplaceEditor({property:= "$name", default:=true})
\  );
\  addLabelIcon(getProperty("erCardinality"),"UnderRightSource");
\  icon = addIcon("Circle","OnSource");
\  icon->setBackground(black)

view.map.*.erManyToManyRelationship =
\  if (%diagram%->getProperty("erViewType") == "physicalView",
\    hideElement()
\  );
\  label = addLabelIcon(getProperty("$name"),"AboveLeftCenter");
\  label->setInplaceEditor({property:= "$name", default:=true});
\  addLabelIcon(getProperty("erCardinality"),"UnderRightSource");
\  icon = addIcon("Circle","OnSource");
\  icon->setBackground(black);
\  icon = addIcon("Circle","OnDestination");
\  icon->setBackground(black)
